-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathclient.go
432 lines (364 loc) · 9.75 KB
/
client.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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
package main
import (
"bytes"
"encoding/binary"
"encoding/json"
"fmt"
"io"
"math"
"net"
"net/http"
)
// ExternalControlPort is the UDP port for Nanoleaf external control.
const ExternalControlPort = 60222
// Client is a Nanoleaf REST API client.
type Client struct {
Host string
Token string
Verbose bool
client http.Client
}
// Get performs a GET request.
func (c Client) Get(path string) (string, error) {
if c.Verbose {
fmt.Println("GET", path)
}
url := c.Endpoint(path)
req, err := http.NewRequest(http.MethodGet, url, nil)
if err != nil {
return "", err
}
req.Header.Set("Accept", "application/json")
res, err := c.client.Do(req)
if err != nil {
return "", err
}
if res.Body != nil {
defer res.Body.Close()
}
body, err := io.ReadAll(res.Body)
if err != nil {
return "", err
}
if c.Verbose {
fmt.Println("<===", string(body))
fmt.Println()
}
return string(body), nil
}
// Put performs a PUT request.
func (c Client) Put(path string, body []byte) (string, error) {
if c.Verbose {
fmt.Println("PUT", path)
fmt.Println("===>", string(body))
}
url := c.Endpoint(path)
req, err := http.NewRequest(http.MethodPut, url, nil)
if err != nil {
return "", err
}
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Accept", "application/json")
req.Body = io.NopCloser(bytes.NewReader(body))
res, err := c.client.Do(req)
if err != nil {
return "", err
}
if res.Body != nil {
defer res.Body.Close()
}
responseBody, err := io.ReadAll(res.Body)
if err != nil {
return "", err
}
if c.Verbose {
fmt.Println("<===", res.Status)
if len(responseBody) > 0 {
fmt.Println("<===", string(responseBody))
}
fmt.Println()
}
return string(responseBody), nil
}
// Endpoint returns the full URL for an API endpoint.
func (c Client) Endpoint(path string) string {
return fmt.Sprintf("http://%s/api/v1/%s/%s", c.Host, c.Token, path)
}
// Effects represents the Nanoleaf panel effects state.
type Effects struct {
Selected string `json:"select"`
List []string `json:"effectsList"`
}
// Rhythm represents the Nanoleaf rhythm state.
type Rhythm struct {
Connected bool `json:"rhythmConnected"`
Active bool `json:"rhythmActive"`
ID int `json:"rhythmId"`
HardwareVersion string `json:"hardwareVersion"`
FirmwareVersion string `json:"firmwareVersion"`
AuxAvailable bool `json:"auxAvailable"`
Mode int `json:"rhythmMode"`
Position struct {
X float64 `json:"x"`
Y float64 `json:"y"`
O float64 `json:"o"`
} `json:"rhythmPos"`
}
// PanelLayout represents the Nanoleaf panel layout.
type PanelLayout struct {
Layout struct {
NumPanels int `json:"numPanels"`
SideLength int `json:"sideLength"`
PositionData []struct {
PanelID int `json:"panelId"`
X int `json:"x"`
Y int `json:"y"`
O int `json:"o"`
ShapeType int `json:"shapeType"`
} `json:"positionData"`
} `json:"layout"`
GlobalOrientation struct {
Value int `json:"value"`
Max int `json:"max"`
Min int `json:"min"`
} `json:"globalOrientation"`
}
// PanelInfo represents the Nanoleaf panel info response.
type PanelInfo struct {
Name string `json:"name"`
SerialNo string `json:"serialNo"`
Manufacturer string `json:"manufacturer"`
FirmwareVersion string `json:"firmwareVersion"`
Model string `json:"model"`
State State `json:"state"`
Effects Effects `json:"effects"`
PanelLayout PanelLayout `json:"panelLayout"`
Rhythm Rhythm `json:"rhythm"`
}
// GetPanelInfo returns the Nanoleaf panel info.
func (c Client) GetPanelInfo() (*PanelInfo, error) {
body, err := c.Get("")
if err != nil {
return nil, err
}
var panelInfo PanelInfo
err = json.Unmarshal([]byte(body), &panelInfo)
return &panelInfo, err
}
// ListEffects returns an array of effect names.
func (c Client) ListEffects() ([]string, error) {
body, err := c.Get("effects/effectsList")
if err != nil {
return nil, err
}
var list []string
err = json.Unmarshal([]byte(body), &list)
return list, err
}
// Off turns off Nanoleaf.
func (c Client) Off() error {
state := State{
On: &OnProperty{false},
}
bytes, err := json.Marshal(state)
if err != nil {
return err
}
_, err = c.Put("state", bytes)
return err
}
// On turns on Nanoleaf.
func (c Client) On() error {
state := State{
On: &OnProperty{true},
}
bytes, err := json.Marshal(state)
if err != nil {
return err
}
_, err = c.Put("state", bytes)
return err
}
// SelectEffect activates the specified effect.
func (c Client) SelectEffect(name string) error {
req := effectsSelectRequest{
Select: name,
}
bytes, err := json.Marshal(req)
if err != nil {
return err
}
c.Put("effects/select", bytes)
return nil
}
// SetBrightness sets the Nanoleaf's brightness.
func (c Client) SetBrightness(brightness int) error {
state := State{
Brightness: &BrightnessProperty{Value: brightness},
}
bytes, err := json.Marshal(state)
if err != nil {
return err
}
c.Put("state", bytes)
return nil
}
// SetColorTemperature sets the Nanoleaf's color temperature.
func (c Client) SetColorTemperature(temperature int) error {
state := State{
ColorTemperature: &ColorTemperatureProperty{Value: temperature},
}
bytes, err := json.Marshal(state)
if err != nil {
return err
}
c.Put("state", bytes)
return nil
}
// SetHSL sets the Nanoleaf's hue, saturation, and lightness (brightness).
func (c Client) SetHSL(hue int, sat int, lightness int) error {
state := State{
Brightness: &BrightnessProperty{Value: lightness},
Hue: &HueProperty{Value: hue},
Saturation: &SaturationProperty{Value: sat},
}
bytes, err := json.Marshal(state)
if err != nil {
return err
}
c.Put("state", bytes)
return nil
}
// SetRGB sets the Nanoleaf's color by converting RGB to HSL.
func (c Client) SetRGB(red int, green int, blue int) error {
h, s, l := rgbToHSL(red, green, blue)
return c.SetHSL(h, s, l)
}
// startExternalControl sets Nanoleaf to accept UDP input.
func (c Client) startExternalControl() error {
_, err := c.Put("effects", []byte(`{"write":{"command":"display","animType":"extControl","extControlVersion":"v2"}}`))
return err
}
// SetPanelColor represents a frame of external color data.
type SetPanelColor struct {
PanelID uint16
Red uint8
Green uint8
Blue uint8
White uint8
TransitionTime uint16
}
// SetCustomColors sets individual Nanoleaf pane colors.
func (c Client) SetCustomColors(frames []SetPanelColor) error {
err := c.startExternalControl()
if err != nil {
return err
}
hostAddr, err := net.ResolveTCPAddr("tcp", c.Host)
if err != nil {
return err
}
laddr, err := net.ResolveUDPAddr("udp", ":0")
if err != nil {
return err
}
raddr, err := net.ResolveUDPAddr("udp", fmt.Sprintf("%s:%d", hostAddr.IP, ExternalControlPort))
if err != nil {
return err
}
conn, err := net.DialUDP("udp", laddr, raddr)
if err != nil {
return err
}
numPanels := len(frames)
if numPanels < 0 || numPanels > math.MaxUint16 {
return fmt.Errorf("Expected between 0-%d panels, got %d", math.MaxUint16, numPanels)
}
headerSize := 2
panelFrameSize := 8
controlFrameSize := headerSize + panelFrameSize*numPanels
buf := make([]byte, controlFrameSize)
binary.BigEndian.PutUint16(buf, uint16(numPanels))
for i, panel := range frames {
offset := headerSize + panelFrameSize*i
binary.BigEndian.PutUint16(buf[offset:], panel.PanelID)
buf[offset+2] = panel.Red
buf[offset+3] = panel.Green
buf[offset+4] = panel.Blue
buf[offset+5] = panel.White
binary.BigEndian.PutUint16(buf[offset+6:], panel.TransitionTime)
}
conn.Write(buf)
conn.Close()
return nil
}
// BrightnessProperty represents the brightness of the Nanoleaf.
type BrightnessProperty struct {
Min *int `json:"min,omitempty"`
Max *int `json:"max,omitempty"`
Value int `json:"value"`
Duration int `json:"duration,omitempty"`
}
// ColorTemperatureProperty represents the color temperature of the Nanoleaf.
type ColorTemperatureProperty struct {
Min *int `json:"min,omitempty"`
Max *int `json:"max,omitempty"`
Value int `json:"value"`
}
// HueProperty represents the hue of the Nanoleaf.
type HueProperty struct {
Min *int `json:"min,omitempty"`
Max *int `json:"max,omitempty"`
Value int `json:"value"`
}
// OnProperty represents the power state of the Nanoleaf.
type OnProperty struct {
Value bool `json:"value"`
}
// SaturationProperty represents the saturation of the Nanoleaf.
type SaturationProperty struct {
Min *int `json:"min,omitempty"`
Max *int `json:"max,omitempty"`
Value int `json:"value"`
}
// State represents a Nanoleaf state.
type State struct {
On *OnProperty `json:"on,omitempty"`
Brightness *BrightnessProperty `json:"brightness,omitempty"`
ColorTemperature *ColorTemperatureProperty `json:"ct,omitempty"`
Hue *HueProperty `json:"hue,omitempty"`
Saturation *SaturationProperty `json:"sat,omitempty"`
ColorMode string `json:"colorMode,omitempty"`
}
// effectsSelectRequest represents a JSON PUT body for `effects/select`.
type effectsSelectRequest struct {
Select string `json:"select"`
}
func rgbToHSL(red, green, blue int) (int, int, int) {
r := float64(red) / 255.0
g := float64(green) / 255.0
b := float64(blue) / 255.0
min := math.Min(math.Min(r, g), b)
max := math.Max(math.Max(r, g), b)
c := max - min
l := (max + min) / 2
if c == 0 { // achromatic
return 0, 0, int(math.Round(100 * l))
}
v := max
h := 0.0
switch v {
case r:
h = 0 + (g-b)/c
case g:
h = 2 + (b-r)/c
case b:
h = 4 + (r-g)/c
}
h *= 60
if h < 0 {
h += 360
}
s := (v - l) / math.Min(l, 1-l)
return int(math.Round(h)), int(math.Round(100 * s)), int(math.Round(100 * l))
}