forked from ninjasphere/go-hue
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlights.go
127 lines (98 loc) · 2.77 KB
/
lights.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
package hue
import (
"fmt"
"time"
)
const timeFormatISO8601 = "2006-01-02T15:04:05"
func (u *User) GetLights() ([]Light, error) {
url := fmt.Sprintf("/api/%s/lights", u.Username)
var lightsMap map[string]interface{}
err := u.Bridge.client.Get(url, &lightsMap)
if err != nil {
return nil, err
}
lights, err := parseLights(lightsMap)
if err != nil {
return nil, err
}
return lights, nil
}
func (u *User) GetNewLights() ([]Light, time.Time, error) {
url := fmt.Sprintf("/api/%s/lights/new", u.Username)
var lightsMap map[string]interface{}
err := u.Bridge.client.Get(url, &lightsMap)
if err != nil {
return nil, time.Time{}, err
}
lastScanString, ok := lightsMap["lastscan"].(string)
if !ok {
return nil, time.Time{}, NewAPIParseError("string", lightsMap["lastscan"], "lastscan")
}
lastScan, err := time.Parse(timeFormatISO8601, lastScanString)
if err != nil {
return nil, time.Time{}, err
}
delete(lightsMap, "lastscan")
lights, err := parseLights(lightsMap)
if err != nil {
return nil, time.Time{}, err
}
return lights, lastScan, nil
}
func (u *User) SearchForNewLights() error {
url := fmt.Sprintf("/api/%s/lights", u.Username)
_, err := u.Bridge.client.Post(url, nil)
if err != nil {
return err
}
return nil
}
func (u *User) GetLightAttributes(lightId string) (*LightAttributes, error) {
url := fmt.Sprintf("/api/%s/lights/%s", u.Username, lightId)
var lightAttributes *LightAttributes
err := u.Bridge.client.Get(url, &lightAttributes)
if err != nil {
return nil, err
}
return lightAttributes, nil
}
func (u *User) SetLightName(lightId string, name string) error {
url := fmt.Sprintf("/api/%s/lights/%s", u.Username, lightId)
request := map[string]string{"name": name}
_, err := u.Bridge.client.Put(url, &request)
if err != nil {
return err
}
return nil
}
func (u *User) SetLightState(lightId string, state *LightState) error {
url := fmt.Sprintf("/api/%s/lights/%s/state", u.Username, lightId)
_, err := u.Bridge.client.Put(url, &state)
if err != nil {
return err
}
return nil
}
func (u *User) SetGroupState(groupId string, state *LightState) error {
url := fmt.Sprintf("/api/%s/groups/%s/action", u.Username, groupId)
_, err := u.Bridge.client.Put(url, &state)
if err != nil {
return err
}
return nil
}
func parseLights(lightsMap map[string]interface{}) ([]Light, error) {
lights := make([]Light, 0, 10)
for lightId, lightInterface := range lightsMap {
lightMap, ok := lightInterface.(map[string]interface{})
if !ok {
return nil, NewAPIParseError("map[string]interface{}", lightInterface, "lights map")
}
name, ok := lightMap["name"].(string)
if !ok {
return nil, NewAPIParseError("string", lightMap["name"], "lights name")
}
lights = append(lights, Light{Id: lightId, Name: name})
}
return lights, nil
}