diff options
author | Collin Guarino <collin.guarino@gmail.com> | 2016-02-18 23:30:40 -0500 |
---|---|---|
committer | Collin Guarino <collin.guarino@gmail.com> | 2016-02-18 23:30:40 -0500 |
commit | a95deb0690428f3c793ae7b4e2401ff56de72343 (patch) | |
tree | f16db5adb2f62fa034f962445ccb3179905d2bb7 | |
parent | 4715205f959490a55b8a2ad8aef56ce503709dbb (diff) |
Implemented Bridge.Login() func that will assign Bridge.Username and create the user if it does not exist. Also convered more functions to Bridge.func()
-rw-r--r-- | bridge.go | 14 | ||||
-rw-r--r-- | bridge_test.go | 2 | ||||
-rw-r--r-- | group_test.go | 3 | ||||
-rw-r--r-- | light.go | 6 | ||||
-rw-r--r-- | light_test.go | 5 | ||||
-rw-r--r-- | scene_test.go | 6 | ||||
-rw-r--r-- | schedule_test.go | 6 |
7 files changed, 29 insertions, 13 deletions
@@ -153,10 +153,9 @@ func (bridge *Bridge) Error(resp *http.Response, err error) (bool) { } // NewBridge defines hardware that is compatible with Hue. -func NewBridge(ip string, username string) (*Bridge, error) { +func NewBridge(ip string) (*Bridge, error) { bridge := Bridge { IPAddress: ip, - Username: username, } info, err := bridge.GetInfo() if err != nil { @@ -166,6 +165,17 @@ func NewBridge(ip string, username string) (*Bridge, error) { return &bridge, nil } +// Bridge.Login assigns a username to access the bridge with and +// will create the username key if it does not exist. +func (bridge *Bridge) Login(username string) error { + bridge.Username = username + err := bridge.CreateUser(username) + if err != nil { + return err + } + return nil +} + // GetBridgeInfo retreives the description.xml file from the bridge. func (bridge *Bridge) GetInfo() (BridgeInfo, error) { _, reader, err := bridge.Get("/description.xml") diff --git a/bridge_test.go b/bridge_test.go index 02f44bc..65c2c4a 100644 --- a/bridge_test.go +++ b/bridge_test.go @@ -12,7 +12,7 @@ import ( ) func TestCreateUser(t *testing.T) { - bridge, _ := NewBridge("192.168.1.128", "427de8bd6d49f149c8398e4fc08f") + bridge, _ := NewBridge("192.168.1.128") bridge.CreateUser("test") //bridge.DeleteUser(bridge.Username) } diff --git a/group_test.go b/group_test.go index 8fcbd09..ea13d64 100644 --- a/group_test.go +++ b/group_test.go @@ -13,7 +13,8 @@ import ( ) func TestGetGroups(t *testing.T) { - bridge, _ := NewBridge("192.168.1.128", "427de8bd6d49f149c8398e4fc08f") + bridge, _ := NewBridge("192.168.1.128") + bridge.Login("427de8bd6d49f149c8398e4fc08f") groups, _ := bridge.GetGroups() for group := range groups { fmt.Println(groups[group]) @@ -172,7 +172,7 @@ func (light *Light) SetState(newState LightState) error { } // GetAllLights retreives the state of all lights that the bridge is aware of. -func GetAllLights(bridge *Bridge) ([]Light, error) { +func (bridge *Bridge) GetAllLights() ([]Light, error) { // Loop through all light indicies to see if they exist // and parse their values. Supports 100 lights. var lights []Light @@ -212,8 +212,8 @@ func GetLightByIndex(bridge *Bridge, index int) (Light, error) { } // GetLight returns a light struct containing data on a given name. -func GetLightByName(bridge *Bridge, name string) (Light, error) { - lights, _ := GetAllLights(bridge) +func (bridge *Bridge) GetLightByName(name string) (Light, error) { + lights, _ := bridge.GetAllLights() for _, light := range lights { if light.Name == name { return light, nil diff --git a/light_test.go b/light_test.go index bdf589f..22b977e 100644 --- a/light_test.go +++ b/light_test.go @@ -15,8 +15,9 @@ import ( func TestSetLightState(t *testing.T) { fmt.Println("\nTESTING LIGHT STATE:\n\n") - bridge, _ := NewBridge("192.168.1.128", "427de8bd6d49f149c8398e4fc08f") - nameTest, _ := GetLightByName(bridge, "Desk Light") // Also tests GetAllLights + bridge, _ := NewBridge("192.168.1.128") + bridge.Login("427de8bd6d49f149c8398e4fc08f") + nameTest, _ := bridge.GetLightByName("Desk Light") // Also tests GetAllLights _ = nameTest selectedLight, _ := GetLightByIndex(bridge, 7) diff --git a/scene_test.go b/scene_test.go index 07d630c..8a74717 100644 --- a/scene_test.go +++ b/scene_test.go @@ -13,7 +13,8 @@ import ( ) func TestGetAllScenes(t *testing.T) { - bridge, _ := NewBridge("192.168.1.128", "427de8bd6d49f149c8398e4fc08f") + bridge, _ := NewBridge("192.168.1.128") + bridge.Login("427de8bd6d49f149c8398e4fc08f") scenes, _ := bridge.GetAllScenes() for scene := range scenes { fmt.Println("SCENE: ", scenes[scene]) @@ -24,7 +25,8 @@ func TestGetAllScenes(t *testing.T) { } func TestCreateScene(t *testing.T) { - bridge, _ := NewBridge("192.168.1.128", "427de8bd6d49f149c8398e4fc08f") + bridge, _ := NewBridge("192.168.1.128") + bridge.Login("427de8bd6d49f149c8398e4fc08f") scene := Scene{Lights: []string{"1", "2"}} _ = bridge.CreateScene(scene) } diff --git a/schedule_test.go b/schedule_test.go index 4aec8c6..3a4cc59 100644 --- a/schedule_test.go +++ b/schedule_test.go @@ -12,12 +12,14 @@ import ( ) func TestGetAllSchedules(t *testing.T) { - bridge, _ := NewBridge("192.168.1.128", "427de8bd6d49f149c8398e4fc08f") + bridge, _ := NewBridge("192.168.1.128") + bridge.Login("427de8bd6d49f149c8398e4fc08f") _, _ = bridge.GetAllSchedules() } func TestGetSchedule(t *testing.T) { - bridge, _ := NewBridge("192.168.1.128", "427de8bd6d49f149c8398e4fc08f") + bridge, _ := NewBridge("192.168.1.128") + bridge.Login("427de8bd6d49f149c8398e4fc08f") _, _ = bridge.GetSchedule("4673980164949558") } |