From c05519e646091a1791aa278f468962407e691bc3 Mon Sep 17 00:00:00 2001 From: Collin Guarino Date: Sun, 24 Jan 2016 13:23:07 -0500 Subject: Rough implementation for GetAllLights. --- lights.go | 66 +++++++++++++++++++++++++++++++++++++++++++++++---------------- 1 file changed, 49 insertions(+), 17 deletions(-) diff --git a/lights.go b/lights.go index b64eaed..eea2daf 100644 --- a/lights.go +++ b/lights.go @@ -2,29 +2,61 @@ package main import ( "log" + "fmt" + "os" + "net/http" + "io/ioutil" + "encoding/json" ) type Light struct { State struct { - On bool - Bri int - hue int - sat int - effect string // TODO: what is this? - xy []string // TODO: what is this? - ct int // TODO: what is this? - alert string - colormode string - reachable bool + On bool `json:"on"` + Bri int `json:"bri"` + hue int `json:"hue"` + sat int `json:"sat"` + effect string `json:"effect"` + xy []string `json:"xy"` // TODO: what is this? + ct int `json:"ct"` // TODO: what is this? + alert string `json:"alert"` + colormode string `json:"colormode"` + reachable bool `json:"reachable"` } - Type string - Name string - ModelID string - ManufacturerName string - UniqueID string - SWVersion string // TODO: what is this? + Type string `json:"type"` + Name string `json:"name"` + ModelID string `json:"modelid"` + ManufacturerName string `json:"manufacturername"` + UniqueID string `json:"uniqueid"` + SWVersion string `json:"swversion"` } -func GetAllLights() { +//http://192.168.1.128/api/319b36233bd2328f3e40731b23479207/lights/ +// http:///api//lights/ +func GetAllLights(bridge *Bridge) { + response, error := http.Get( + fmt.Sprintf("http://%s/api/%s/lights/", bridge.IPAddress, bridge.Username)) + if error != nil { + trace("", error) + os.Exit(1) + } else if response.StatusCode != 200 { + trace(fmt.Sprintf("Bridge status error %d", response.StatusCode), nil) + os.Exit(1) + } + + body, error := ioutil.ReadAll(response.Body) + defer response.Body.Close() + if error != nil { + trace("", error) + os.Exit(1) + } + + data := []Light{} + error = json.Unmarshal(body, &data) + if error != nil { + trace("", error) + os.Exit(1) + } + + log.Println(data) } \ No newline at end of file -- cgit v1.2.3