From e5a495cf76d99bd840d395dc6369b45d67716e27 Mon Sep 17 00:00:00 2001 From: Collin Guarino Date: Fri, 22 Jan 2016 21:03:08 -0500 Subject: Added rough implementation of GetBridgeInfo that parses the XML file. --- bridge.go | 36 ++++++++++++++++++++++++++---------- 1 file changed, 26 insertions(+), 10 deletions(-) diff --git a/bridge.go b/bridge.go index d8f28b3..172d370 100644 --- a/bridge.go +++ b/bridge.go @@ -2,7 +2,10 @@ package main import ( "log" + "os" "encoding/xml" + "net/http" + "io/ioutil" ) func main() { @@ -12,10 +15,10 @@ func main() { type Bridge struct { IPAddress string Username string - Info Info + Info BridgeInfo } -Info struct { +type BridgeInfo struct { DeviceType string FriendlyName string ManufacturerURL string @@ -30,22 +33,35 @@ Info struct { func NewBridge(ip string) *Bridge { // TODO: if yaml file exists then return bridge := Bridge { - IPAddress: ip + IPAddress: ip, // TODO: other defaults here } - response := GetBridgeInfo() bridge.Info = GetBridgeInfo("192.168.1.128") // TODO: IP var + return &bridge } // Go to http:///description.xml and parse the info -func GetBridgeInfo(ip string) Info { - data := Info{} +func GetBridgeInfo(ip string) BridgeInfo { + response, error := http.Get(ip + "/description.xml") + if response.StatusCode != 200 { + log.Println("Bridge status error: %d", response.StatusCode) + os.Exit(1) + } + + body, error := ioutil.ReadAll(response.Body) + if error != nil { + log.Println("Error parsing bridge description XML") + os.Exit(1) + } - err := xml.Unmarshal([]byte(data), &data) - if err != nil { - fmt.Println("GetBridgeInfo error. Cannot parse data from description.") + data := make(map[string]string) + error = xml.Unmarshal(body, data) + if error != nil { + log.Println("GetBridgeInfo error. Cannot parse data from description.") } - log.Println("FriendlyName") + log.Println(data["FriendlyName"]) + + return BridgeInfo{} // TODO: fill in } -- cgit v1.2.3