diff options
author | Collin Guarino <collin.guarino@gmail.com> | 2016-01-28 00:23:10 -0500 |
---|---|---|
committer | Collin Guarino <collin.guarino@gmail.com> | 2016-01-28 00:23:10 -0500 |
commit | 7afac327af365967b53051f7d26351e60334373e (patch) | |
tree | ebff91546163f621b91827a8c15c80be83943484 | |
parent | d48fc670d850562771011973a4094a5e1886c079 (diff) |
Refactored GetAllLights to work well with GetLight
-rw-r--r-- | bridge_test.go | 2 | ||||
-rw-r--r-- | lights.go | 47 | ||||
-rw-r--r-- | lights_test.go | 14 |
3 files changed, 41 insertions, 22 deletions
diff --git a/bridge_test.go b/bridge_test.go index 32e65df..d790c74 100644 --- a/bridge_test.go +++ b/bridge_test.go @@ -13,4 +13,4 @@ func TestNewBridge(t *testing.T) { func TestCreateUser(t *testing.T) { user, _ := CreateUser(NewBridge("192.168.1.128", "319b36233bd2328f3e40731b23479207"), "linux") log.Println(user) -}
\ No newline at end of file +} @@ -1,13 +1,13 @@ package hue import ( - "log" "fmt" "os" "net/http" "io/ioutil" "encoding/json" "strings" + "errors" ) type Light struct { @@ -36,15 +36,15 @@ type Light struct { //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) { +func GetAllLights(bridge *Bridge) []Light { // Loop through all light indicies to see if they exist // and parse their values. Supports 100 lights. var lights []Light for index := 1; index < 101; index++ { - response, error := http.Get( + response, err := http.Get( fmt.Sprintf("http://%s/api/%s/lights/%d", bridge.IPAddress, bridge.Username, index)) - if error != nil { - trace("", error) + if err != nil { + trace("", err) os.Exit(1) } else if response.StatusCode != 200 { trace(fmt.Sprintf("Bridge status error %d", response.StatusCode), nil) @@ -52,28 +52,37 @@ func GetAllLights(bridge *Bridge) { } // Read the response - body, error := ioutil.ReadAll(response.Body) + body, err := ioutil.ReadAll(response.Body) defer response.Body.Close() - if error != nil { - if strings.Contains(string(body), "not available") { - // Handle end of searchable lights - fmt.Printf("\n\n%d lights found.\n\n", index) - break - } else { - // Other error found while parsing - trace("", error) - os.Exit(1) - } + if err != nil { + trace("", err) + os.Exit(1) + } + if strings.Contains(string(body), "not available") { + // Handle end of searchable lights + fmt.Printf("\n\n%d lights found.\n\n", index) + break } // Parse and load the response into the light array data := Light{} - error = json.Unmarshal(body, &data) - if error != nil { - trace("", error) + err = json.Unmarshal(body, &data) + if err != nil { + trace("", err) os.Exit(1) } lights = append(lights, data) } return lights } + +// GetLight will return a light struct containing data on a given name. +func GetLight(bridge *Bridge, name string) (Light, error) { + lights := GetAllLights(bridge) + for index := 0; index < len(lights); index++ { + if lights[index].Name == name { + return lights[index], nil + } + } + return Light{}, errors.New("Light not found.") +} diff --git a/lights_test.go b/lights_test.go index 66f01c1..ac73d2e 100644 --- a/lights_test.go +++ b/lights_test.go @@ -2,9 +2,19 @@ package hue import ( "testing" + "log" ) - + func TestGetAllLights(t *testing.T) { bridge := NewBridge("192.168.1.128", "319b36233bd2328f3e40731b23479207") GetAllLights(bridge) -}
\ No newline at end of file +} + +func TestGetLight(t *testing.T) { + bridge := NewBridge("192.168.1.128", "319b36233bd2328f3e40731b23479207") + light , err := GetLight(bridge, "Bathroom Light") + if err != nil { + trace("", err) + } + log.Println("LIGHT: ", light) +} |