aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorCollin Guarino <collin.guarino@gmail.com>2016-02-24 20:28:14 -0500
committerCollin Guarino <collin.guarino@gmail.com>2016-02-24 20:28:14 -0500
commitf7bb831091a6e3bb8bf7e7927bb8470e626ea58f (patch)
treec389182857917b4c2f7db14c143a5d3cae6cb4b1
parent203389517a8c07771aa69c89b28a8c62b04f6b49 (diff)
Major bug fix for Bridge.GetAllLights not working in missing index cases.
-rw-r--r--bridge.go30
-rw-r--r--light_test.go4
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) {