From f7bb831091a6e3bb8bf7e7927bb8470e626ea58f Mon Sep 17 00:00:00 2001 From: Collin Guarino Date: Wed, 24 Feb 2016 20:28:14 -0500 Subject: Major bug fix for Bridge.GetAllLights not working in missing index cases. --- bridge.go | 30 +++++++++++++++++++----------- light_test.go | 4 ++-- 2 files changed, 21 insertions(+), 13 deletions(-) diff --git a/bridge.go b/bridge.go index a9b8181..61960fa 100644 --- a/bridge.go +++ b/bridge.go @@ -23,6 +23,7 @@ import ( "bytes" "io" "errors" + "strconv" ) // Bridge struct defines hardware that is used to communicate with the lights. @@ -218,21 +219,28 @@ func (bridge *Bridge) DeleteUser(username string) error { // GetAllLights retreives the state of all lights that the bridge is aware of. func (bridge *Bridge) GetAllLights() ([]Light, error) { - // 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++ { - light, err := bridge.GetLightByIndex(index) + uri := fmt.Sprintf("/api/%s/lights", bridge.Username) + body, _, err := bridge.Get(uri) + if err != nil { + return []Light{}, err + } + + // An index is at the top of every Light in the array + lightMap := map[string]Light{} + err = json.Unmarshal(body, &lightMap) + if err != nil { + return []Light{}, errors.New("Unable to marshal GetAllLights response.") + } + + // Parse the index, add the light to the list, and return the array + lights := []Light{} + for index, light := range lightMap { + light.Index, err = strconv.Atoi(index) if err != nil { - break // Final light index reached, index does not exist. + return []Light{}, errors.New("Unable to convert light index to integer. ") } lights = append(lights, light) } - if len(lights) == 0 { - err := errors.New("Error: No lights found by GetAllLights.") - log.Println(err) - return lights, err - } return lights, nil } diff --git a/light_test.go b/light_test.go index ecc5460..b07e8fe 100644 --- a/light_test.go +++ b/light_test.go @@ -9,8 +9,8 @@ package hue import ( "testing" - //"fmt" - "time" + "fmt" + //"time" ) func TestSetLightState(t *testing.T) { -- cgit v1.2.3