aboutsummaryrefslogtreecommitdiff
path: root/bridge.go
diff options
context:
space:
mode:
authorCollin Guarino <collin.guarino@gmail.com>2016-01-22 21:03:08 -0500
committerCollin Guarino <collin.guarino@gmail.com>2016-01-22 21:03:08 -0500
commite5a495cf76d99bd840d395dc6369b45d67716e27 (patch)
tree9dbde0c3d09c5ad15b7acd05665edd67175250c7 /bridge.go
parent6c259dc14c8da39f600aebd74b4ec4aa14b4c462 (diff)
Added rough implementation of GetBridgeInfo that parses the XML file.
Diffstat (limited to 'bridge.go')
-rw-r--r--bridge.go36
1 files 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://<bridge_ip>/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
}