-
Notifications
You must be signed in to change notification settings - Fork 3
/
effects.go
189 lines (147 loc) · 4.07 KB
/
effects.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
package nanoleaf
import (
"encoding/json"
"fmt"
"net/http"
)
// NanoEffects represents nanoleafs effects
type NanoEffects struct {
nano *Nanoleaf
endpoint string
}
// EffectData effects data
type EffectData struct {
Loop bool `json:"loop"`
Name string `json:"animName"`
Type string `json:"animType"`
Version string `json:"version"`
Data string `json:"animData"`
}
// newNanoEffects returns a new NanoEffects instance
func newNanoEffects(nano *Nanoleaf) *NanoEffects {
return &NanoEffects{
nano: nano,
endpoint: fmt.Sprintf("%s/%s/effects", nano.url, nano.token),
}
}
// List lists all effects registered
func (e *NanoEffects) List() ([]string, error) {
url := fmt.Sprintf("%s/effectsList", e.endpoint)
resp, err := e.nano.client.R().Get(url)
if err != nil {
return nil, err
}
if resp.StatusCode() == http.StatusUnauthorized {
return nil, ErrUnauthorized
}
if resp.StatusCode() != http.StatusOK {
return nil, ErrUnexpectedResponse
}
var effects []string
if err := json.Unmarshal(resp.Body(), &effects); err != nil {
return nil, ErrParsingJSON
}
return effects, nil
}
// Set sets given effects as active
func (e *NanoEffects) Set(name string) error {
body := jsonPayload{"select": name}
resp, err := e.nano.client.R().SetHeader("Content-Type", "application/json").SetBody(body).Put(e.endpoint)
if err != nil {
return err
}
if resp.StatusCode() == http.StatusUnauthorized {
return ErrUnauthorized
}
if resp.StatusCode() == http.StatusNotFound {
return ErrEffectNotFound
}
if resp.StatusCode() != http.StatusNoContent {
return ErrUnexpectedResponse
}
return nil
}
// Get returns the currently active effect
func (e *NanoEffects) Get() (string, error) {
url := fmt.Sprintf("%s/select", e.endpoint)
resp, err := e.nano.client.R().Get(url)
if err != nil {
return "", err
}
if resp.StatusCode() == http.StatusUnauthorized {
return "", ErrUnauthorized
}
if resp.StatusCode() != http.StatusOK {
return "", ErrUnexpectedResponse
}
var effect string
if err := json.Unmarshal(resp.Body(), &effect); err != nil {
return "", ErrParsingJSON
}
return effect, nil
}
// GetEffectData returns data of the given effect
func (e *NanoEffects) GetEffectData(effect string) (EffectData, error) {
var data EffectData
body := jsonPayload{
"write": jsonPayload{
"command": "request",
"animName": effect,
},
}
resp, err := e.nano.client.R().SetHeader("Content-Type", "application/json").SetBody(body).Put(e.endpoint)
if err != nil {
return data, err
}
if resp.StatusCode() == http.StatusUnauthorized {
return data, ErrUnauthorized
}
if resp.StatusCode() == http.StatusNotFound {
return data, ErrEffectNotFound
}
if resp.StatusCode() != http.StatusOK {
return data, ErrUnexpectedResponse
}
if err := json.Unmarshal(resp.Body(), &data); err != nil {
fmt.Println(err)
return data, ErrParsingJSON
}
return data, nil
}
// WriteRaw writes the raw command (outcome will depend on your body because the nanoleaf api is not well designed)
func (e *NanoEffects) WriteRaw(body jsonPayload) error {
resp, err := e.nano.client.R().SetHeader("Content-Type", "application/json").SetBody(body).Put(e.endpoint)
if err != nil {
return err
}
if resp.StatusCode() == http.StatusUnauthorized {
return ErrUnauthorized
}
if resp.StatusCode() != http.StatusNoContent {
return ErrUnexpectedResponse
}
return nil
}
// Temp displays effect described given animData temporarily
func (e *NanoEffects) Temp(data string, loop bool) error {
body := jsonPayload{
"write": jsonPayload{
"command": "display",
"animType": "custom",
"animData": data,
"loop": loop,
},
}
return e.WriteRaw(body)
}
// ToString returns the effect as a string
func (e *NanoEffects) ToString(effect StreamEffect) string {
data := fmt.Sprintf("%d", len(effect.Panels))
for _, panel := range effect.Panels {
data = fmt.Sprintf("%s %d %d", data, panel.ID, len(panel.Frames))
for _, frame := range panel.Frames {
data = fmt.Sprintf("%s %d %d %d 0 %d", data, frame.Red, frame.Green, frame.Blue, frame.Transition)
}
}
return data
}