diff options
author | Collin Guarino <collin.guarino@gmail.com> | 2016-02-12 17:44:52 -0500 |
---|---|---|
committer | Collin Guarino <collin.guarino@gmail.com> | 2016-02-12 17:44:52 -0500 |
commit | 9a494fc233ff3fc74b1a30a672a10e49b44de549 (patch) | |
tree | 06e84be5d279a51d4a13cc3e3c78f79a21b453b1 /light.go | |
parent | a92342f99028ab7dee550b4f77874e5fd4202123 (diff) |
Added Light.Blink and made SetLightState get the new light state after a change.
Diffstat (limited to 'light.go')
-rw-r--r-- | light.go | 40 |
1 files changed, 39 insertions, 1 deletions
@@ -13,6 +13,7 @@ import ( "encoding/json" "strings" "errors" + "time" ) // Light struct defines attributes of a light. @@ -82,10 +83,38 @@ func (self *Light) On() error { // Light.Toggle will toggle the light source on and off func (self *Light) Toggle() error { if self.State.On { + fmt.Println("toggling off") return self.Off() } else { + fmt.Println("toggling on") return self.On() } + return nil +} + +// Light.Blink will turn the light off and on repeatedly for a given seconds +// interval and return the light back to its off or on state afterwards. +// Note: time will vary based on connection speed and algorithm speed. +func (self *Light) Blink(seconds int) error { + originalState := self.State.On + + // Toggle the light on and off + for i := 0; i <= seconds; i++ { + err := self.Toggle() + if err != nil { + return err + } + time.Sleep(time.Second) + } + + // Return the light to its original on or off state + if self.State.On != originalState { + err := self.Toggle() + if err != nil { + return err + } + } + return nil } // Light.ColorLoop will set the light state to 'colorloop' if `active` @@ -103,7 +132,16 @@ func (self *Light) ColorLoop(activate bool) error { 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) - return err + if err != nil { + return err + } + + // Get the new light state and update the current Light struct + *light, err = GetLight(light.Bridge, light.Name) + if err != nil { + return err + } + return nil } // GetAllLights retreives the state of all lights that the bridge is aware of. |