From d48fc670d850562771011973a4094a5e1886c079 Mon Sep 17 00:00:00 2001 From: Collin Guarino Date: Wed, 27 Jan 2016 23:08:56 -0500 Subject: Complete solution to GetAllLights, fixed previously broken mapping. --- bridge.go | 10 +++++---- lights.go | 71 ++++++++++++++++++++++++++++++++++++++------------------------- 2 files changed, 49 insertions(+), 32 deletions(-) diff --git a/bridge.go b/bridge.go index c2b23e3..ea257f9 100644 --- a/bridge.go +++ b/bridge.go @@ -76,6 +76,8 @@ func GetBridgeInfo(self *Bridge) { self.Info = *data } +// CreateUser posts to ./api on the bridge to create a new whitelisted user. +// If the button on the bridge was not pressed then _____todo_____ func CreateUser(bridge *Bridge, deviceType string) (string, error) { // Construct the http POST params := map[string]string{"devicetype": deviceType} @@ -92,12 +94,12 @@ func CreateUser(bridge *Bridge, deviceType string) (string, error) { } defer response.Body.Close() body, err := ioutil.ReadAll(response.Body) - fmt.Printf(string(body)) + fmt.Println(string(body)) - // TODO: handle description saying "link button not pressed" - // ^ handle "error":{"type":101} + // TODO: decode and return + // TODO: handle errors. http://www.developers.meethue.com/documentation/error-messages - return string(body), err + return "", err } // Log the date, time, file location, line number, and function. diff --git a/lights.go b/lights.go index 1138312..7f60a52 100644 --- a/lights.go +++ b/lights.go @@ -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:///api//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 +} -- cgit v1.2.3