aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorCollin Guarino <collin.guarino@gmail.com>2016-02-18 23:48:12 -0500
committerCollin Guarino <collin.guarino@gmail.com>2016-02-18 23:48:12 -0500
commitfa741e65856124cec0fba2c5a5c222f1469aaf8d (patch)
tree06fe7571d0111df9630d35993e1cc9f80a27d520
parent6a4c4aef9b6fecaf568fe255fe6a588aff396630 (diff)
Moved functions, rearranged Bridge.funcs into bridge.go file
-rw-r--r--bridge.go51
-rw-r--r--light.go56
-rw-r--r--light_test.go2
3 files changed, 53 insertions, 56 deletions
diff --git a/bridge.go b/bridge.go
index 4f5a585..d729633 100644
--- a/bridge.go
+++ b/bridge.go
@@ -217,6 +217,57 @@ func (bridge *Bridge) DeleteUser(username string) error {
return nil
}
+// 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)
+ if err != nil {
+ break
+ }
+ lights = append(lights, light)
+ }
+ return lights, nil
+}
+
+// GetLightByIndex returns a light struct containing data on
+// a light given its index stored on the bridge. This is used for
+// quickly updating an individual light.
+func (bridge *Bridge) GetLightByIndex(index int) (Light, error) {
+ // Send an http GET and inspect the response
+ uri := fmt.Sprintf("/api/%s/lights/%d", bridge.Username, index)
+ body, _, err := bridge.Get(uri)
+ if err != nil {
+ return Light{}, err
+ }
+ if strings.Contains(string(body), "not available") {
+ return Light{}, errors.New("Index Error")
+ }
+
+ // Parse and load the response into the light array
+ light := Light{}
+ err = json.Unmarshal(body, &light)
+ if err != nil {
+ trace("", err)
+ }
+ light.Index = index
+ light.Bridge = bridge
+ return light, nil
+}
+
+// GetLight returns a light struct containing data on a given name.
+func (bridge *Bridge) GetLightByName(name string) (Light, error) {
+ lights, _ := bridge.GetAllLights()
+ for _, light := range lights {
+ if light.Name == name {
+ return light, nil
+ }
+ }
+ return Light{}, errors.New("Light not found.")
+}
+
// Log the date, time, file location, line number, and function.
// Message can be "" or Err can be nil (not both)
func trace(message string, err error) {
diff --git a/light.go b/light.go
index 06d5682..978a3ec 100644
--- a/light.go
+++ b/light.go
@@ -10,9 +10,6 @@ package hue
import (
"fmt"
- "encoding/json"
- "strings"
- "errors"
"time"
)
@@ -164,60 +161,9 @@ func (light *Light) SetState(newState LightState) error {
}
// Get the new light state and update the current Light struct
- *light, err = GetLightByIndex(light.Bridge, light.Index)
+ *light, err = light.Bridge.GetLightByIndex(light.Index)
if err != nil {
return err
}
return nil
}
-
-// 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 := GetLightByIndex(bridge, index)
- if err != nil {
- break
- }
- lights = append(lights, light)
- }
- return lights, nil
-}
-
-// GetLightByIndex returns a light struct containing data on
-// a light given its index stored on the bridge. This is used for
-// quickly updating an individual light.
-func GetLightByIndex(bridge *Bridge, index int) (Light, error) {
- // Send an http GET and inspect the response
- uri := fmt.Sprintf("/api/%s/lights/%d", bridge.Username, index)
- body, _, err := bridge.Get(uri)
- if err != nil {
- return Light{}, err
- }
- if strings.Contains(string(body), "not available") {
- return Light{}, errors.New("Index Error")
- }
-
- // Parse and load the response into the light array
- light := Light{}
- err = json.Unmarshal(body, &light)
- if err != nil {
- trace("", err)
- }
- light.Index = index
- light.Bridge = bridge
- return light, nil
-}
-
-// GetLight returns a light struct containing data on a given name.
-func (bridge *Bridge) GetLightByName(name string) (Light, error) {
- lights, _ := bridge.GetAllLights()
- for _, light := range lights {
- if light.Name == name {
- return light, nil
- }
- }
- return Light{}, errors.New("Light not found.")
-}
diff --git a/light_test.go b/light_test.go
index 22b977e..16c4e94 100644
--- a/light_test.go
+++ b/light_test.go
@@ -19,7 +19,7 @@ func TestSetLightState(t *testing.T) {
bridge.Login("427de8bd6d49f149c8398e4fc08f")
nameTest, _ := bridge.GetLightByName("Desk Light") // Also tests GetAllLights
_ = nameTest
- selectedLight, _ := GetLightByIndex(bridge, 7)
+ selectedLight, _ := bridge.GetLightByIndex(7)
selectedLight.On()
time.Sleep(time.Second)