diff options
author | Collin Guarino <collin.guarino@gmail.com> | 2016-02-29 19:32:17 -0500 |
---|---|---|
committer | Collin Guarino <collin.guarino@gmail.com> | 2016-02-29 19:32:17 -0500 |
commit | d115895133ffd0f287eb0de096a897faaa7756e2 (patch) | |
tree | 4353df14299e8d57901ec0b2a7b44fef1b4717c6 | |
parent | 7367e699d3c4c9a3c728c95e42b8ffcb7163f1ec (diff) |
Changed FindBridge to FindBridges (plural) and refactored the function to return []Bridge instead of just the first one.
-rw-r--r-- | bridge.go | 20 | ||||
-rw-r--r-- | bridge_test.go | 10 |
2 files changed, 14 insertions, 16 deletions
@@ -28,7 +28,7 @@ import ( // Bridge struct defines hardware that is used to communicate with the lights. type Bridge struct { - IPAddress string + IPAddress string `json:"internalipaddress"` Username string Info BridgeInfo } @@ -155,22 +155,20 @@ func HandleResponse(resp *http.Response) ([]byte, io.Reader, error) { // FindBridge will visit www.meethue.com/api/nupnp to see if a bridge // is available on the local network. This feature currently only supports one // bridge on the network. -func FindBridge() (Bridge, error) { +func FindBridges() ([]Bridge, error) { bridge := Bridge{IPAddress: "www.meethue.com"} body, _, err := bridge.Get("/api/nupnp") if err != nil { err = errors.New("Error: Unable to locate bridge.") log.Fatal(err) - return Bridge{}, err + return []Bridge{}, err } - content := string(body) - ip := content[strings.LastIndex(content, ":\"")+2 : strings.LastIndex(content, "\"}]")] - bridge.IPAddress = ip - err = bridge.GetInfo() - if err != nil { - return Bridge{}, err - } - return bridge, nil + bridges := []Bridge{} + err = json.Unmarshal(body, &bridges) + if err != nil { + return []Bridge{}, errors.New("Unable to parse FindBridges response. ") + } + return bridges, nil } // NewBridge defines hardware that is compatible with Hue. diff --git a/bridge_test.go b/bridge_test.go index cc312fe..04aa0d0 100644 --- a/bridge_test.go +++ b/bridge_test.go @@ -19,16 +19,16 @@ func TestCreateUser(t *testing.T) { //bridge.DeleteUser(bridge.Username) } -func TestFindBridge(t *testing.T) { - bridge, _ := FindBridge() - fmt.Println(bridge.IPAddress) +func TestFindBridges(t *testing.T) { + bridges, _ := FindBridges() + fmt.Println(bridges[0].IPAddress) } func TestBridgeLogin(t *testing.T) { - bridge, err := FindBridge() + bridges, err := FindBridges() if err != nil { fmt.Println("Error on TestBridgeLogin") } - bridge.Login("427de8bd6d49f149c8398e4fc08f") + bridges[0].Login("427de8bd6d49f149c8398e4fc08f") } |