diff options
-rw-r--r-- | bridge.go | 44 | ||||
-rw-r--r-- | light.go | 4 | ||||
-rw-r--r-- | light_test.go | 3 |
3 files changed, 26 insertions, 25 deletions
@@ -60,26 +60,6 @@ func (self *Bridge) Get(path string) ([]byte, io.Reader, error) { return handleResponse(resp) } -// bridge.Post will send an http POST to the bridge with -// a body formatted with parameters (in a generic interface) -func (self *Bridge) Post(path string, params interface{}) ([]byte, io.Reader, error) { - // Add the params to the request - request, err := json.Marshal(params) - if err != nil { - trace("", err) - return []byte{}, nil, nil - } - log.Println("\nSending POST body: ", string(request)) - - // Send the request and handle the response - uri := fmt.Sprintf("http://" + self.IPAddress + path) - resp, err := http.Post(uri, "text/json", bytes.NewReader(request)) - if self.Error(resp, err) { - return []byte{}, nil, nil - } - return handleResponse(resp) -} - // Bridge.Put will send an http PUT to the bridge with // a body formatted with parameters (in a generic interface) func (self *Bridge) Put(path string, params interface{}) ([]byte, io.Reader, error) { @@ -101,6 +81,26 @@ func (self *Bridge) Put(path string, params interface{}) ([]byte, io.Reader, err return handleResponse(resp) } +// bridge.Post will send an http POST to the bridge with +// a body formatted with parameters (in a generic interface) +func (self *Bridge) Post(path string, params interface{}) ([]byte, io.Reader, error) { + // Add the params to the request + request, err := json.Marshal(params) + if err != nil { + trace("", err) + return []byte{}, nil, nil + } + log.Println("\nSending POST body: ", string(request)) + + // Send the request and handle the response + uri := fmt.Sprintf("http://" + self.IPAddress + path) + resp, err := http.Post(uri, "text/json", bytes.NewReader(request)) + if self.Error(resp, err) { + return []byte{}, nil, nil + } + return handleResponse(resp) +} + // Bridge.Delete will send an http DELETE to the bridge func (self *Bridge) Delete(path string) error { uri := fmt.Sprintf("http://" + self.IPAddress + path) @@ -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 } @@ -132,7 +132,7 @@ func handleResponse(resp *http.Response) ([]byte, io.Reader, error) { return body, reader, nil } -// bridge.Error handles all bridge response status errors +// Bridge.Error handles all bridge response status errors func (self *Bridge) Error(resp *http.Response, err error) (bool) { if err != nil { trace("", err) @@ -24,7 +24,7 @@ type Light struct { Hue int `json:"hue"` // Hue value 1-65535 Saturation int `json:"sat"` // Saturation value 0-254 Effect string `json:"effect"` // "None" or "Colorloop" - XY [2]float32 `json:"xy"` // Coordinates of color in CIE color space + XY [2]float32 `json:"xy"` // Coordinates of color in CIE color space CT int `json:"ct"` // Mired Color Temperature (google it) Alert string `json:"alert"` ColorMode string `json:"colormode"` @@ -59,6 +59,8 @@ type LightState struct { Name string `json:"name,omitempty"` } +// Light.SetName will assign a new name in the light's +// attributes as recognized by the bridge. func (self *Light) SetName(name string) error { uri := fmt.Sprintf("/api/%s/lights/%d", self.Bridge.Username, self.Index) body := make(map[string]string) diff --git a/light_test.go b/light_test.go index 9f8f140..8961666 100644 --- a/light_test.go +++ b/light_test.go @@ -26,8 +26,7 @@ func TestGetLightByName(t *testing.T) { func TestSetLightState(t *testing.T) { fmt.Println("\nTESTING LIGHT STATE:\n\n") bridge := NewBridge("192.168.1.128", "319b36233bd2328f3e40731b23479207") - lights, _ := GetAllLights(bridge) - selectedLight := lights[5] + selectedLight, _ := GetLightByIndex(bridge, 7) selectedLight.On() time.Sleep(time.Second) |