From 89217de4c5384feec23342fdec4fe2b896cf559a Mon Sep 17 00:00:00 2001 From: Collin Guarino Date: Thu, 4 Feb 2016 13:50:28 -0500 Subject: Prepared SetLightState for bridge.Put --- bridge.go | 6 +++--- light.go | 28 ++-------------------------- light_test.go | 2 +- 3 files changed, 6 insertions(+), 30 deletions(-) diff --git a/bridge.go b/bridge.go index 7172289..7693599 100644 --- a/bridge.go +++ b/bridge.go @@ -2,7 +2,6 @@ package hue import ( "log" - "os" "encoding/xml" "encoding/json" "net/http" @@ -55,8 +54,8 @@ func (self *Bridge) Get(path string) ([]byte, io.Reader, error) { } // bridge.Post will send an http POST to the bridge with -// a body formatted with parameters. -func (self *Bridge) Post(path string, params map[string]string) ([]byte, io.Reader, error) { +// 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 { @@ -83,6 +82,7 @@ func handleResponse(resp *http.Response) ([]byte, io.Reader, error) { return []byte{}, nil, err } reader := bytes.NewReader(body) + log.Println("Handled request: ", string(body)) return body, reader, nil } diff --git a/light.go b/light.go index ceffeef..4180c37 100644 --- a/light.go +++ b/light.go @@ -4,12 +4,9 @@ package hue import ( "fmt" - "net/http" - "io/ioutil" "encoding/json" "strings" "errors" - "bytes" ) type Light struct { @@ -54,32 +51,11 @@ type LightState struct { // SetLightState will modify light attributes such as on/off, saturation, // brightness, and more. See `SetLightState` struct. func SetLightState(bridge *Bridge, lightID string, newState LightState) error { - // Construct the http POST - req, err := json.Marshal(newState) + uri := fmt.Sprintf("/api/%s/lights/%s/state", bridge.Username, lightID) + _, _, err := bridge.Post(uri, newState) // TODO: change to PUT if err != nil { - trace("", err) return err } - - // Send the request and read the response - uri := fmt.Sprintf("http://%s/api/%s/lights/%s/state", - bridge.IPAddress, bridge.Username, lightID) - resp, err := http.Post(uri, "text/json", bytes.NewReader(req)) - if err != nil { - trace("", err) - return err - } - defer resp.Body.Close() - body, err := ioutil.ReadAll(resp.Body) - if err != nil { - trace("", err) - return err - } - - _ = body - - // TODO: Parse the response and return any error - //fmt.Println("LightState: ", string(body)) return nil } diff --git a/light_test.go b/light_test.go index e3d5952..b1ea862 100644 --- a/light_test.go +++ b/light_test.go @@ -19,6 +19,6 @@ func TestSetLightState(t *testing.T) { bridge := NewBridge("192.168.1.128", "319b36233bd2328f3e40731b23479207") lights, _ := GetAllLights(bridge) fmt.Println(lights[0].Name) - newState := LightState{On: false} + newState := LightState{On: true} SetLightState(bridge, lights[0].UniqueID, newState) } -- cgit v1.2.3