aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorCollin Guarino <collin.guarino@gmail.com>2016-02-04 14:38:39 -0500
committerCollin Guarino <collin.guarino@gmail.com>2016-02-04 14:38:39 -0500
commita0a8f16f81b36bd700a3d08780ac6e67a9dfc8a0 (patch)
treed989c38f6f8baf004d763413ae0a46e66ed53ed2
parentf57f0c6b98ce758756e6c5f2c4aaf8946f5e5cb5 (diff)
Added index to Light struct to easily reference it from []light.
-rw-r--r--bridge.go8
-rw-r--r--light.go4
-rw-r--r--light_test.go7
3 files changed, 14 insertions, 5 deletions
diff --git a/bridge.go b/bridge.go
index d0ded7e..803f419 100644
--- a/bridge.go
+++ b/bridge.go
@@ -75,7 +75,13 @@ func (self *Bridge) Post(path string, params interface{}) ([]byte, io.Reader, er
func (self *Bridge) Put(path string, params interface{}) ([]byte, io.Reader, error) {
uri := fmt.Sprintf("http://" + self.IPAddress + path)
client := &http.Client{}
- request, err := http.NewRequest("PUT", uri, strings.NewReader(params))
+
+ data, err := json.Marshal(params)
+ if err != nil {
+ return []byte{}, nil, err
+ }
+
+ request, err := http.NewRequest("PUT", uri, bytes.NewReader(data))
resp, err := client.Do(request)
if err != nil {
return []byte{}, nil, err
diff --git a/light.go b/light.go
index 4180c37..0ec8e80 100644
--- a/light.go
+++ b/light.go
@@ -28,6 +28,7 @@ type Light struct {
ManufacturerName string `json:"manufacturername"`
UniqueID string `json:"uniqueid"`
SWVersion string `json:"swversion"`
+ Index int // Set by index of light array response
}
// LightState used in SetLightState to ammend light attributes.
@@ -52,7 +53,7 @@ type LightState struct {
// brightness, and more. See `SetLightState` struct.
func SetLightState(bridge *Bridge, lightID string, newState LightState) error {
uri := fmt.Sprintf("/api/%s/lights/%s/state", bridge.Username, lightID)
- _, _, err := bridge.Post(uri, newState) // TODO: change to PUT
+ _, _, err := bridge.Put(uri, newState) // TODO: change to PUT
if err != nil {
return err
}
@@ -84,6 +85,7 @@ func GetAllLights(bridge *Bridge) ([]Light, error) {
if err != nil {
trace("", err)
}
+ data.Index = index
lights = append(lights, data)
}
return lights, nil
diff --git a/light_test.go b/light_test.go
index b1ea862..6da677d 100644
--- a/light_test.go
+++ b/light_test.go
@@ -16,9 +16,10 @@ func TestGetLight(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)
- fmt.Println(lights[0].Name)
- newState := LightState{On: true}
- SetLightState(bridge, lights[0].UniqueID, newState)
+ fmt.Println("\nUNIQUE ID: ", lights[0].UniqueID)
+ newState := LightState{On: false}
+ SetLightState(bridge, lights[1].Index, newState)
}