Skip to content
This repository was archived by the owner on Mar 22, 2025. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
159 changes: 146 additions & 13 deletions ZigBeeValve/ZigBeeValve.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func main() {
Certificate: "ABCD",
Details: map[string][]string{"Developer": {"Arrowhead"}},
ProtoPort: map[string]int{"https": 0, "http": 8870, "coap": 0},
InfoLink: "https://github.com/sdoque/systems/tree/master/ZigBeeValve",
InfoLink: "https://github.com/sdoque/systems/tree/master",
}

// instantiate a template unit asset
Expand All @@ -55,7 +55,9 @@ func main() {
log.Fatalf("Resource configuration error: %+v\n", err)
}
ua, startup := newResource(uac, &sys, servsTemp)
startup()
if err := startup(); err != nil {
log.Fatalf("Error during startup: %s\n", err)
}
sys.UAssets[ua.GetName()] = &ua
}

Expand All @@ -80,32 +82,163 @@ func (t *UnitAsset) Serving(w http.ResponseWriter, r *http.Request, servicePath
switch servicePath {
case "setpoint":
t.setpt(w, r)
case "consumption":
t.consumption(w, r)
case "current":
t.current(w, r)
case "power":
t.power(w, r)
case "voltage":
t.voltage(w, r)
case "state":
t.state(w, r)
default:
http.Error(w, "Invalid service request [Do not modify the services subpath in the configuration file]", http.StatusBadRequest)
}
}

// TODO: Add webhandler for power plug controller (sun up/down) and/or schedule later on.
// STRETCH GOAL: Instead of looking for specific models types, add a list of supported devices that we can check against

// Function used by webhandler to either get or set the setpoint of a specific device
func (rsc *UnitAsset) setpt(w http.ResponseWriter, r *http.Request) {
switch r.Method {
case "GET":
setPointForm := rsc.getSetPoint()
usecases.HTTPProcessGetRequest(w, r, &setPointForm)
case "PUT":
sig, err := usecases.HTTPProcessSetRequest(w, r)
if err != nil {
http.Error(w, "Request incorrectly formatted", http.StatusBadRequest)
// Make sure only devices with setpoints actually support the http get method
if rsc.Model == "ZHAThermostat" || rsc.Model == "Smart plug" {
setPointForm := rsc.getSetPoint()
usecases.HTTPProcessGetRequest(w, r, &setPointForm)
return
}

rsc.setSetPoint(sig)
if rsc.Model == "ZHAThermostat" {
err = rsc.sendSetPoint()
http.Error(w, "That device doesn't support that method.", http.StatusInternalServerError)
return
case "PUT":
// Make sure only devices with setpoints actually support the http put method
if rsc.Model == "ZHAThermostat" || rsc.Model == "Smart plug" {
sig, err := usecases.HTTPProcessSetRequest(w, r)
if err != nil {
http.Error(w, "Couldn't send setpoint.", http.StatusInternalServerError)
http.Error(w, "Request incorrectly formatted", http.StatusBadRequest)
return
}
rsc.setSetPoint(sig)
return
}
http.Error(w, "This device doesn't support that method.", http.StatusInternalServerError)
return
default:
http.Error(w, "Method is not supported.", http.StatusNotFound)
}
}

// Function used by the webhandler to get the consumption of a device
func (rsc *UnitAsset) consumption(w http.ResponseWriter, r *http.Request) {
switch r.Method {
case "GET":
// Make sure only devices with consumption sensors actually support the http get method
if rsc.Model != "Smart plug" {
http.Error(w, "That device doesn't support that method.", http.StatusInternalServerError)
return
}
consumptionForm, err := rsc.getConsumption()
if err != nil {
http.Error(w, "Failed getting data, or data not present", http.StatusInternalServerError)
return
}
usecases.HTTPProcessGetRequest(w, r, &consumptionForm)
default:
http.Error(w, "Method is not supported", http.StatusNotFound)
}
}

// Function used by the webhandler to get the power of a device
func (rsc *UnitAsset) power(w http.ResponseWriter, r *http.Request) {
switch r.Method {
case "GET":
// Make sure only devices with power sensors actually support the http get method
if rsc.Model != "Smart plug" {
http.Error(w, "That device doesn't support that method.", http.StatusInternalServerError)
return
}
powerForm, err := rsc.getPower()
if err != nil {
http.Error(w, "Failed getting data, or data not present", http.StatusInternalServerError)
return
}
usecases.HTTPProcessGetRequest(w, r, &powerForm)
default:
http.Error(w, "Method is not supported", http.StatusNotFound)
}
}

// Function used by the webhandler to get the current of a device
func (rsc *UnitAsset) current(w http.ResponseWriter, r *http.Request) {
switch r.Method {
case "GET":
// Make sure only devices with current sensors actually support the http get method
if rsc.Model != "Smart plug" {
http.Error(w, "That device doesn't support that method.", http.StatusInternalServerError)
return
}
currentForm, err := rsc.getCurrent()
if err != nil {
http.Error(w, "Failed getting data, or data not present", http.StatusInternalServerError)
return
}
usecases.HTTPProcessGetRequest(w, r, &currentForm)
default:
http.Error(w, "Method is not supported", http.StatusNotFound)
}
}

// Function used by the webhandler to get the voltage of a device
func (rsc *UnitAsset) voltage(w http.ResponseWriter, r *http.Request) {
switch r.Method {
case "GET":
// Make sure only devices with voltage sensors actually support the http get method
if rsc.Model != "Smart plug" {
http.Error(w, "That device doesn't support that method.", http.StatusInternalServerError)
return
}
voltageForm, err := rsc.getVoltage()
if err != nil {
http.Error(w, "Failed getting data, or data not present", http.StatusInternalServerError)
return
}
usecases.HTTPProcessGetRequest(w, r, &voltageForm)
default:
http.Error(w, "Method is not supported", http.StatusNotFound)
}
}

func (rsc *UnitAsset) state(w http.ResponseWriter, r *http.Request) {
switch r.Method {
case "GET":
if rsc.Model != "Smart plug" {
http.Error(w, "That device doesn't support that method.", http.StatusInternalServerError)
return
}
stateForm, err := rsc.getState()
if err != nil {
http.Error(w, "Failed getting data, or data not present", http.StatusInternalServerError)
return
}
usecases.HTTPProcessGetRequest(w, r, &stateForm)
case "PUT":
if rsc.Model != "Smart plug" {
http.Error(w, "That device doesn't support that method.", http.StatusInternalServerError)
return
}
sig, err := usecases.HTTPProcessSetRequest(w, r)
if err != nil {
http.Error(w, "Request incorrectly formatted", http.StatusBadRequest)
return
}
err = rsc.setState(sig)
if err != nil {
http.Error(w, "Something went wrong when setting state", http.StatusBadRequest)
return
}
default:
http.Error(w, "Method is not supported", http.StatusNotFound)
}
}
Loading