From d3c0bdfa560e4200ed9c4ff4b80dec491afc0e5c Mon Sep 17 00:00:00 2001 From: Collin Guarino Date: Sun, 7 Feb 2016 17:37:40 -0500 Subject: Restructured light struct and all funcs to manage bridge association. --- light.go | 14 +++++++++----- light_test.go | 2 +- 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/light.go b/light.go index 2d72502..2672171 100644 --- a/light.go +++ b/light.go @@ -29,6 +29,7 @@ type Light struct { UniqueID string `json:"uniqueid"` SWVersion string `json:"swversion"` Index int // Set by index of light array response // TODO: change to smaller int + Bridge *Bridge } // LightState used in SetLightState to ammend light attributes. @@ -50,18 +51,20 @@ type LightState struct { } func (self *Light) TurnOff() { - SetLightState(self.Index, LightState{On: false}) + SetLightState(self, LightState{On: false}) } func (self *Light) TurnOn() { - SetLightState(self.Index, LightState{On: true}) + SetLightState(self, LightState{On: true}) } + + // SetLightState will modify light attributes such as on/off, saturation, // brightness, and more. See `SetLightState` struct. -func SetLightState(bridge *Bridge, index int, newState LightState) error { - uri := fmt.Sprintf("/api/%s/lights/%d/state", bridge.Username, index) - _, _, err := bridge.Put(uri, newState) // TODO: change to PUT +func SetLightState(light *Light, newState LightState) error { + uri := fmt.Sprintf("/api/%s/lights/%d/state", light.Bridge.Username, light.Index) + _, _, err := light.Bridge.Put(uri, newState) // TODO: change to PUT if err != nil { return err } @@ -94,6 +97,7 @@ func GetAllLights(bridge *Bridge) ([]Light, error) { trace("", err) } data.Index = index + data.Bridge = bridge lights = append(lights, data) } return lights, nil diff --git a/light_test.go b/light_test.go index 0a63bf8..c9a16a5 100644 --- a/light_test.go +++ b/light_test.go @@ -24,7 +24,7 @@ func TestSetLightState(t *testing.T) { selectedLight.TurnOn() time.Sleep(time.Second) - selectedLight.TurnOf() + selectedLight.TurnOff() time.Sleep(time.Second) selectedLight.TurnOn() } -- cgit v1.2.3