diff options
author | Collin Guarino <collin.guarino@gmail.com> | 2016-02-13 11:54:05 -0500 |
---|---|---|
committer | Collin Guarino <collin.guarino@gmail.com> | 2016-02-13 11:54:05 -0500 |
commit | b2ae1e0fc5c8e93fef1a9ae81d097f90435cb435 (patch) | |
tree | 323eda62a3a974626058e6403d2757a0897a6c8f | |
parent | 32df96e1dc02531c42435dd6a12a0ac6f8725ff0 (diff) |
Converted SetLightState to Light.SetState
-rw-r--r-- | bridge.go | 10 | ||||
-rw-r--r-- | light.go | 21 | ||||
-rw-r--r-- | light_test.go | 5 |
3 files changed, 16 insertions, 20 deletions
@@ -57,7 +57,7 @@ func (self *Bridge) Get(path string) ([]byte, io.Reader, error) { if self.Error(resp, err) { return []byte{}, nil, err } - return handleResponse(resp) + return HandleResponse(resp) } // Bridge.Put will send an http PUT to the bridge with @@ -78,7 +78,7 @@ func (self *Bridge) Put(path string, params interface{}) ([]byte, io.Reader, err if err != nil { return []byte{}, nil, err } - return handleResponse(resp) + return HandleResponse(resp) } // bridge.Post will send an http POST to the bridge with @@ -98,7 +98,7 @@ func (self *Bridge) Post(path string, params interface{}) ([]byte, io.Reader, er if self.Error(resp, err) { return []byte{}, nil, nil } - return handleResponse(resp) + return HandleResponse(resp) } // Bridge.Delete will send an http DELETE to the bridge @@ -110,7 +110,7 @@ func (self *Bridge) Delete(path string) error { if err != nil { return err } - _, _, err = handleResponse(resp) + _, _, err = HandleResponse(resp) if err != nil { return err } @@ -120,7 +120,7 @@ func (self *Bridge) Delete(path string) error { // HandleResponse manages the http.Response content from a // bridge Get/Put/Post/Delete by checking it for errors // and invalid return types. -func handleResponse(resp *http.Response) ([]byte, io.Reader, error) { +func HandleResponse(resp *http.Response) ([]byte, io.Reader, error) { body, err := ioutil.ReadAll(resp.Body) if err != nil { trace("Error parsing bridge description xml.", nil) @@ -40,7 +40,7 @@ type Light struct { Bridge *Bridge } -// LightState used in SetLightState to amend light attributes. +// LightState used in Light.SetState to amend light attributes. type LightState struct { On bool `json:"on"` Bri uint8 `json:"bri,omitempty"` @@ -74,12 +74,12 @@ func (self *Light) SetName(name string) error { // Light.Off will turn the light source off func (self *Light) Off() error { - return SetLightState(self, LightState{On: false}) + return self.SetState(LightState{On: false}) } // Light.Off will turn the light source on func (self *Light) On() error { - return SetLightState(self, LightState{On: true}) + return self.SetState(LightState{On: true}) } // Light.Toggle will toggle the light source on and off @@ -103,7 +103,6 @@ func (self *Light) Delete() error { 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. @@ -136,20 +135,20 @@ func (self *Light) ColorLoop(activate bool) error { if activate { state = "colorloop" } - return SetLightState(self, LightState{On: true, Effect: state}) + return self.SetState(LightState{On: true, Effect: state}) } -// SetLightState will modify light attributes such as on/off, saturation, -// brightness, and more. See `SetLightState` struct. -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) +// Light.SetState will modify light attributes such as on/off, saturation, +// brightness, and more. See `LightState` struct. +func (self *Light) SetState(newState LightState) error { + uri := fmt.Sprintf("/api/%s/lights/%d/state", self.Bridge.Username, self.Index) + _, _, err := self.Bridge.Put(uri, newState) if err != nil { return err } // Get the new light state and update the current Light struct - *light, err = GetLightByIndex(light.Bridge, light.Index) + *self, err = GetLightByIndex(self.Bridge, self.Index) if err != nil { return err } diff --git a/light_test.go b/light_test.go index a4fd777..7a46889 100644 --- a/light_test.go +++ b/light_test.go @@ -33,8 +33,5 @@ func TestSetLightState(t *testing.T) { selectedLight.Blink(2) - // err := selectedLight.Delete() - // if err != nil { - // fmt.Println("error on delete") - // } + // _ := selectedLight.Delete() } |