From 4ea6a6f722fe8eacf44b5372c20b8ea6dc2b1ccd Mon Sep 17 00:00:00 2001 From: Collin Guarino Date: Tue, 2 Feb 2016 23:30:16 -0500 Subject: Convered GetAllLights to use the bridge.Get function. --- bridge.go | 31 ++++--------------------------- light.go | 19 +++++-------------- 2 files changed, 9 insertions(+), 41 deletions(-) diff --git a/bridge.go b/bridge.go index 899b188..f8009f8 100644 --- a/bridge.go +++ b/bridge.go @@ -15,29 +15,6 @@ import ( "io" ) -// type Bridge struct { -// IPAddress string -// Username string -// Info BridgeInfo -// } -// -// type BridgeInfo struct { -// Root struct { -// Device struct { -// DeviceType string `xml:"deviceType"` -// FriendlyName string `xml:"friendlyName"` -// Manufacturer string `xml:"manufacturer"` -// ManufacturerURL string `xml:"manufacturerURL"` -// ModelDescription string `xml:"modelDescription"` -// ModelName string `xml:"modelName"` -// ModelNumber string `xml:"modelNumber"` -// ModelURL string `xml:"modelURL"` -// SerialNumber string `xml:"serialNumber"` -// UDN string `xml:"UDN"` -// } `xml:"device"` -// } `xml:"root"` -// } - type Bridge struct { IPAddress string Username string @@ -63,7 +40,7 @@ type Device struct { UDN string `xml:"UDN"` } -func (self *Bridge) Get(path string) (io.Reader, error) { +func (self *Bridge) Get(path string) ([]byte, io.Reader, error) { resp, err := http.Get("http://" + self.IPAddress + path) if err != nil { trace("", err) @@ -77,7 +54,7 @@ func (self *Bridge) Get(path string) (io.Reader, error) { } reader := bytes.NewReader(body) // TODO: handle individual error codes - return reader, nil + return body, reader, nil } // Error Struct @@ -158,7 +135,7 @@ func NewBridge(ip string, username string) *Bridge { // GetBridgeInfo retreives the description.xml file from the bridge. func GetBridgeInfo(self *Bridge) Error { - reader, err := self.Get("/description.xml") + _, reader, err := self.Get("/description.xml") if err != nil { return ErrResponse } @@ -169,7 +146,7 @@ func GetBridgeInfo(self *Bridge) Error { os.Exit(1) } self.Info = data - fmt.Println("Bridge Info:\n", self.Info) + //fmt.Println("Bridge Info:\n", self.Info) return NoErr } diff --git a/light.go b/light.go index 4d21924..ceffeef 100644 --- a/light.go +++ b/light.go @@ -76,8 +76,10 @@ func SetLightState(bridge *Bridge, lightID string, newState LightState) error { return err } + _ = body + // TODO: Parse the response and return any error - fmt.Println(string(body)) + //fmt.Println("LightState: ", string(body)) return nil } @@ -89,20 +91,9 @@ func GetAllLights(bridge *Bridge) ([]Light, error) { for index := 1; index < 101; index++ { // Send an http GET and inspect the response - resp, err := http.Get( - fmt.Sprintf("http://%s/api/%s/lights/%d", bridge.IPAddress, bridge.Username, index)) + uri := fmt.Sprintf("/api/%s/lights/%d", bridge.Username, index) + body, _, err := bridge.Get(uri) if err != nil { - trace("", err) - return lights, err - } else if resp.StatusCode != 200 { - trace(fmt.Sprintf("Bridge status error %d", resp.StatusCode), nil) - } - - // Read and inspect the response content - body, err := ioutil.ReadAll(resp.Body) - defer resp.Body.Close() - if err != nil { - trace("", err) return lights, err } if strings.Contains(string(body), "not available") { -- cgit v1.2.3