diff options
Diffstat (limited to 'lights.go')
-rw-r--r-- | lights.go | 71 |
1 files changed, 43 insertions, 28 deletions
@@ -7,6 +7,7 @@ import ( "net/http" "io/ioutil" "encoding/json" + "strings" ) type Light struct { @@ -21,7 +22,7 @@ type Light struct { alert string `json:"alert"` colormode string `json:"colormode"` reachable bool `json:"reachable"` - } + } `json:"state"` Type string `json:"type"` Name string `json:"name"` ModelID string `json:"modelid"` @@ -30,35 +31,49 @@ type Light struct { SWVersion string `json:"swversion"` } + + //http://192.168.1.128/api/319b36233bd2328f3e40731b23479207/lights/ -// http://<bridge_ip>/api/<username>/lights/ // GetAllLights retreives the state of all lights that the bridge is aware of. func GetAllLights(bridge *Bridge) { - response, error := http.Get( - fmt.Sprintf("http://%s/api/%s/lights/", bridge.IPAddress, bridge.Username)) - if error != nil { - trace("", error) - os.Exit(1) - } else if response.StatusCode != 200 { - trace(fmt.Sprintf("Bridge status error %d", response.StatusCode), nil) - os.Exit(1) - } - - body, error := ioutil.ReadAll(response.Body) - defer response.Body.Close() - if error != nil { - trace("", error) - os.Exit(1) - } - - data := Light{} - error = json.Unmarshal(body, &data) - if error != nil { - trace("", error) - os.Exit(1) + // 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( + fmt.Sprintf("http://%s/api/%s/lights/%d", bridge.IPAddress, bridge.Username, index)) + if error != nil { + trace("", error) + os.Exit(1) + } else if response.StatusCode != 200 { + trace(fmt.Sprintf("Bridge status error %d", response.StatusCode), nil) + os.Exit(1) + } + + // Read the response + body, error := 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) + } + } + + // Parse and load the response into the light array + data := Light{} + error = json.Unmarshal(body, &data) + if error != nil { + trace("", error) + os.Exit(1) + } + lights = append(lights, data) } - - log.Println(data) - -}
\ No newline at end of file + return lights +} |