-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlights.go
280 lines (230 loc) · 6.21 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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
package lifx
import (
//"crypto/tls"
"encoding/json"
"errors"
"net/http"
"time"
)
const (
OK Status = "ok"
TimedOut Status = "timed_out"
Offline Status = "offline"
)
type (
Status string
Selector struct {
Id string `json:"id"`
Name string `json:"name"`
}
Product struct {
Name string `json:"name"`
Identifier string `json:"identifier"`
Company string `json:"company"`
Capabilities Capabilities `json:"capabilities"`
}
Capabilities struct {
HasColor bool `json:"has_color"`
HasVariableColorTemp bool `json:"has_variable_color_temp"`
HasIR bool `json:"has_ir"`
HasChain bool `json:"has_chain"`
HasMultizone bool `json:"has_multizone"`
MinKelvin float64 `json:"min_kelvin"`
MaxKelvin float64 `json:"max_kelvin"`
}
Light struct {
Id string `json:"id"`
UUID string `json:"uuid"`
Label string `json:"label"`
Connected bool `json:"connected"`
Power string `json:"power"`
Color HSBKColor `json:"color"`
Brightness float64 `json:"brightness"`
Effect string `json:"effect"`
Group Selector `json:"group"`
Location Selector `json:"location"`
Product Product `json:"product"`
LastSeen time.Time `json:"last_seen"`
SecondsLastSeen float64 `json:"seconds_last_seen"`
}
State struct {
Power string `json:"power,omitempty"`
Color Color `json:"color,omitempty"`
Brightness float64 `json:"brightness,omitempty"`
Duration float64 `json:"duration,omitempty"`
Infrared float64 `json:"infrared,omitempty"`
Fast bool `json:"fast,omitempty"`
}
StateDelta struct {
Power *string `json:"power,omitempty"`
Duration *float64 `json:"duration,omitempty"`
Infrared *float64 `json:"infrared,omitempty"`
Hue *float64 `json:"hue,omitempty"`
Saturation *float64 `json:"saturation,omitempty"`
Brightness *float64 `json:"brightness,omitempty"`
Kelvin *int `json:"kelvin,omitempty"`
}
StateWithSelector struct {
State
Selector string `json:"selector"`
}
States struct {
States []StateWithSelector `json:"states,omitempty"`
Defaults State `json:"defaults,omitempty"`
}
Toggle struct {
Duration float64 `json:"duration,omitempty"`
}
Breathe struct {
Color Color `json:"color,omitempty"`
FromColor Color `json:"from_color,omitempty"`
Period float64 `json:"period,omitempty"`
Cycles float64 `json:"cycles,omitempty"`
Persist bool `json:"persist,omitempty"`
PowerOn bool `json:"power_on,omitempty"`
Peak float64 `json:"peak,omitempty"`
}
)
var (
DefaultBreatheCycles float64 = 1
DefaultBreathePeriod float64 = 1
DefaultBreathePersist bool = false
DefaultBreathePowerOn bool = true
DefaultBreathePeak float64 = 0.5
)
func NewBreathe() Breathe {
var b Breathe
b.Period = DefaultBreathePeriod
b.Cycles = DefaultBreatheCycles
b.Persist = DefaultBreathePersist
b.PowerOn = DefaultBreathePowerOn
b.Peak = DefaultBreathePeak
return b
}
func (b *Breathe) Valid() error {
if b.Peak < 0 || b.Peak > 1 {
return errors.New("peak must be between 0.0 and 1.0")
}
return nil
}
func (c *Client) SetState(selector string, state State) (*LifxResponse, error) {
var (
err error
s *LifxResponse
resp *Response
)
if resp, err = c.setState(selector, state); err != nil {
return nil, err
}
defer resp.Body.Close()
if resp.IsError() {
return nil, resp.GetLifxError()
}
if state.Fast && resp.StatusCode == http.StatusAccepted {
return nil, nil
}
if err = json.NewDecoder(resp.Body).Decode(&s); err != nil {
return nil, err
}
return s, nil
}
func (c *Client) FastSetState(selector string, state State) (*LifxResponse, error) {
state.Fast = true
return c.SetState(selector, state)
}
func (c *Client) SetStates(selector string, states States) (*LifxResponse, error) {
var (
err error
s *LifxResponse
resp *Response
)
if resp, err = c.setStates(selector, states); err != nil {
return nil, err
}
defer resp.Body.Close()
if err = json.NewDecoder(resp.Body).Decode(&s); err != nil {
return nil, err
}
return s, nil
}
func (c *Client) StateDelta(selector string, delta StateDelta) (*LifxResponse, error) {
var (
err error
s *LifxResponse
resp *Response
)
if resp, err = c.stateDelta(selector, delta); err != nil {
return nil, err
}
defer resp.Body.Close()
if err = json.NewDecoder(resp.Body).Decode(&s); err != nil {
return nil, err
}
return s, nil
}
func (c *Client) Toggle(selector string, duration float64) (*LifxResponse, error) {
var (
err error
s *LifxResponse
resp *Response
)
if resp, err = c.toggle(selector, duration); err != nil {
return nil, err
}
defer resp.Body.Close()
if resp.IsError() {
return nil, resp.GetLifxError()
}
if err = json.NewDecoder(resp.Body).Decode(&s); err != nil {
return nil, err
}
return s, nil
}
func (c *Client) ListLights(selector string) ([]Light, error) {
var (
err error
s []Light
resp *Response
)
if resp, err = c.listLights(selector); err != nil {
return nil, err
}
defer resp.Body.Close()
if resp.StatusCode > 299 {
return nil, resp.GetLifxError()
}
if err = json.NewDecoder(resp.Body).Decode(&s); err != nil {
return nil, err
}
return s, nil
}
func (c *Client) PowerOff(selector string) (*LifxResponse, error) {
return c.SetState(selector, State{Power: "off"})
}
func (c *Client) FastPowerOff(selector string) {
c.SetState(selector, State{Power: "off", Fast: true})
}
func (c *Client) PowerOn(selector string) (*LifxResponse, error) {
return c.SetState(selector, State{Power: "on"})
}
func (c *Client) FastPowerOn(selector string) {
c.SetState(selector, State{Power: "on", Fast: true})
}
func (c *Client) Breathe(selector string, breathe Breathe) (*LifxResponse, error) {
var (
err error
s *LifxResponse
resp *Response
)
if resp, err = c.breathe(selector, breathe); err != nil {
return nil, err
}
defer resp.Body.Close()
if resp.IsError() {
return nil, resp.GetLifxError()
}
if err = json.NewDecoder(resp.Body).Decode(&s); err != nil {
return nil, err
}
return s, nil
}