diff options
-rw-r--r-- | bridge.go | 6 | ||||
-rw-r--r-- | light.go | 28 | ||||
-rw-r--r-- | light_test.go | 2 |
3 files changed, 6 insertions, 30 deletions
@@ -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 } @@ -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) } |