From 80a4f4ab1a92c687241043ddedab291a67257150 Mon Sep 17 00:00:00 2001 From: Collin Guarino Date: Sun, 31 Jan 2016 16:36:18 -0500 Subject: Refactored error system to make sure all functions return an error if it occurs. --- bridge.go | 6 ++---- light.go | 19 ++++++++++++------- light_test.go | 6 +++--- 3 files changed, 17 insertions(+), 14 deletions(-) diff --git a/bridge.go b/bridge.go index 38eeb77..22c43d0 100644 --- a/bridge.go +++ b/bridge.go @@ -114,7 +114,6 @@ func NewBridge(ip string, username string) *Bridge { } // GetBridgeInfo retreives the description.xml file from the bridge. -// Go to http:///description.xml func GetBridgeInfo(self *Bridge) { response, err := http.Get("http://" + self.IPAddress + "/description.xml") if err != nil { @@ -171,7 +170,6 @@ func CreateUser(bridge *Bridge, deviceType string) (string, Error) { if errFound && noLink { return "", ErrLink } - // TODO: decode and return return "", NoErr } @@ -183,8 +181,8 @@ func trace(message string, err error) { f := runtime.FuncForPC(pc[0]) file, line := f.FileLine(pc[0]) if err != nil { - log.Fatalf("%s:%d %s: %s\n", file, line, f.Name(), err) + log.Printf("%s:%d %s: %s\n", file, line, f.Name(), err) } else { - log.Fatalf("%s:%d %s: %s\n", file, line, f.Name(), message) + log.Printf("%s:%d %s: %s\n", file, line, f.Name(), message) } } diff --git a/light.go b/light.go index c667bc6..70fc262 100644 --- a/light.go +++ b/light.go @@ -53,11 +53,12 @@ type LightState struct { // SetLightState will modify light attributes such as on/off, saturation, // brightness, and more. See `SetLightState` struct. -func SetLightState(bridge *Bridge, lightID string, newState LightState) { +func SetLightState(bridge *Bridge, lightID string, newState LightState) error { // Construct the http POST req, err := json.Marshal(newState) if err != nil { trace("", err) + return err } // Send the request and read the response @@ -66,22 +67,24 @@ func SetLightState(bridge *Bridge, lightID string, newState LightState) { resp, err := http.Post(uri, "text/json", bytes.NewReader(req)) if err != nil { trace("", err) + return err } defer resp.Body.Close() body, err := ioutil.ReadAll(resp.Body) if err != nil { trace("", err) + return err } - // TODO: parse the response - + // TODO: Parse the response and return any error fmt.Println(string(body)) + return nil } //http://192.168.1.128/api/319b36233bd2328f3e40731b23479207/lights/ // GetAllLights retreives the state of all lights that the bridge is aware of. -func GetAllLights(bridge *Bridge) []Light { +func GetAllLights(bridge *Bridge) ([]Light, error) { // Loop through all light indicies to see if they exist // and parse their values. Supports 100 lights. var lights []Light @@ -90,6 +93,7 @@ func GetAllLights(bridge *Bridge) []Light { fmt.Sprintf("http://%s/api/%s/lights/%d", bridge.IPAddress, bridge.Username, index)) if err != nil { trace("", err) + return lights, err } else if response.StatusCode != 200 { trace(fmt.Sprintf("Bridge status error %d", response.StatusCode), nil) } @@ -99,10 +103,11 @@ func GetAllLights(bridge *Bridge) []Light { defer response.Body.Close() if err != nil { trace("", err) + return lights, err } if strings.Contains(string(body), "not available") { // Handle end of searchable lights - fmt.Printf("\n\n%d lights found.\n\n", index) + //fmt.Printf("\n\n%d lights found.\n\n", index) break } @@ -114,12 +119,12 @@ func GetAllLights(bridge *Bridge) []Light { } lights = append(lights, data) } - return lights + return lights, nil } // GetLight will return a light struct containing data on a given name. func GetLight(bridge *Bridge, name string) (Light, error) { - lights := GetAllLights(bridge) + lights, _ := GetAllLights(bridge) for index := 0; index < len(lights); index++ { if lights[index].Name == name { return lights[index], nil diff --git a/light_test.go b/light_test.go index 2481c0f..b1ea862 100644 --- a/light_test.go +++ b/light_test.go @@ -17,8 +17,8 @@ func TestGetLight(t *testing.T) { func TestSetLightState(t *testing.T) { bridge := NewBridge("192.168.1.128", "319b36233bd2328f3e40731b23479207") - randomLight := GetAllLights(bridge)[0] - fmt.Println(randomLight.Name) + lights, _ := GetAllLights(bridge) + fmt.Println(lights[0].Name) newState := LightState{On: true} - SetLightState(bridge, randomLight.UniqueID, newState) + SetLightState(bridge, lights[0].UniqueID, newState) } -- cgit v1.2.3