-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy pathlight.go
309 lines (271 loc) · 9.29 KB
/
light.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
package huego
import (
"context"
"image/color"
"math"
)
// Light represents a bridge light https://developers.meethue.com/documentation/lights-api
type Light struct {
State *State `json:"state,omitempty"`
Type string `json:"type,omitempty"`
Name string `json:"name,omitempty"`
ModelID string `json:"modelid,omitempty"`
ManufacturerName string `json:"manufacturername,omitempty"`
UniqueID string `json:"uniqueid,omitempty"`
SwVersion string `json:"swversion,omitempty"`
SwConfigID string `json:"swconfigid,omitempty"`
ProductName string `json:"productname,omitempty"`
ID int `json:"-"`
bridge *Bridge
}
// State defines the attributes and properties of a light
type State struct {
On bool `json:"on"`
Bri uint8 `json:"bri,omitempty"`
Hue uint16 `json:"hue,omitempty"`
Sat uint8 `json:"sat,omitempty"`
Xy []float32 `json:"xy,omitempty"`
Ct uint16 `json:"ct,omitempty"`
Alert string `json:"alert,omitempty"`
Effect string `json:"effect,omitempty"`
TransitionTime uint16 `json:"transitiontime,omitempty"`
BriInc int `json:"bri_inc,omitempty"`
SatInc int `json:"sat_inc,omitempty"`
HueInc int `json:"hue_inc,omitempty"`
CtInc int `json:"ct_inc,omitempty"`
XyInc int `json:"xy_inc,omitempty"`
ColorMode string `json:"colormode,omitempty"`
Reachable bool `json:"reachable,omitempty"`
Scene string `json:"scene,omitempty"`
}
// NewLight defines a list of lights discovered the last time the bridge performed a light discovery.
// Also stores the timestamp the last time a discovery was performed.
type NewLight struct {
Lights []string
LastScan string `json:"lastscan"`
}
// SetState sets the state of the light to s.
func (l *Light) SetState(s State) error {
return l.SetStateContext(context.Background(), s)
}
// SetStateContext sets the state of the light to s.
func (l *Light) SetStateContext(ctx context.Context, s State) error {
_, err := l.bridge.SetLightStateContext(ctx, l.ID, s)
if err != nil {
return err
}
l.State = &s
return nil
}
// Off sets the On state of one light to false, turning it off
func (l *Light) Off() error {
return l.OffContext(context.Background())
}
// OffContext sets the On state of one light to false, turning it off
func (l *Light) OffContext(ctx context.Context) error {
state := State{On: false}
_, err := l.bridge.SetLightStateContext(ctx, l.ID, state)
if err != nil {
return err
}
l.State.On = false
return nil
}
// On sets the On state of one light to true, turning it on
func (l *Light) On() error {
return l.OnContext(context.Background())
}
// OnContext sets the On state of one light to true, turning it on
func (l *Light) OnContext(ctx context.Context) error {
state := State{On: true}
_, err := l.bridge.SetLightStateContext(ctx, l.ID, state)
if err != nil {
return err
}
l.State.On = true
return nil
}
// IsOn returns true if light state On property is true
func (l *Light) IsOn() bool {
return l.State.On
}
// Rename sets the name property of the light
func (l *Light) Rename(new string) error {
return l.RenameContext(context.Background(), new)
}
// RenameContext sets the name property of the light
func (l *Light) RenameContext(ctx context.Context, new string) error {
update := Light{Name: new}
_, err := l.bridge.UpdateLightContext(ctx, l.ID, update)
if err != nil {
return err
}
l.Name = new
return nil
}
// Bri sets the light brightness state property
func (l *Light) Bri(new uint8) error {
return l.BriContext(context.Background(), new)
}
// BriContext sets the light brightness state property
func (l *Light) BriContext(ctx context.Context, new uint8) error {
update := State{On: true, Bri: new}
_, err := l.bridge.SetLightStateContext(ctx, l.ID, update)
if err != nil {
return err
}
l.State.Bri = new
l.State.On = true
return nil
}
// Hue sets the light hue state property (0-65535)
func (l *Light) Hue(new uint16) error {
return l.HueContext(context.Background(), new)
}
// HueContext sets the light hue state property (0-65535)
func (l *Light) HueContext(ctx context.Context, new uint16) error {
update := State{On: true, Hue: new}
_, err := l.bridge.SetLightStateContext(ctx, l.ID, update)
if err != nil {
return err
}
l.State.Hue = new
l.State.On = true
return nil
}
// Sat sets the light saturation state property (0-254)
func (l *Light) Sat(new uint8) error {
return l.SatContext(context.Background(), new)
}
// SatContext sets the light saturation state property (0-254)
func (l *Light) SatContext(ctx context.Context, new uint8) error {
update := State{On: true, Sat: new}
_, err := l.bridge.SetLightStateContext(ctx, l.ID, update)
if err != nil {
return err
}
l.State.Sat = new
l.State.On = true
return nil
}
// Xy sets the x and y coordinates of a color in CIE color space. (0-1 per value)
func (l *Light) Xy(new []float32) error {
return l.XyContext(context.Background(), new)
}
// XyContext sets the x and y coordinates of a color in CIE color space. (0-1 per value)
func (l *Light) XyContext(ctx context.Context, new []float32) error {
update := State{On: true, Xy: new}
_, err := l.bridge.SetLightStateContext(ctx, l.ID, update)
if err != nil {
return err
}
l.State.Xy = new
l.State.On = true
return nil
}
// Ct sets the light color temperature state property
func (l *Light) Ct(new uint16) error {
return l.CtContext(context.Background(), new)
}
// CtContext sets the light color temperature state property
func (l *Light) CtContext(ctx context.Context, new uint16) error {
update := State{On: true, Ct: new}
_, err := l.bridge.SetLightStateContext(ctx, l.ID, update)
if err != nil {
return err
}
l.State.Ct = new
l.State.On = true
return nil
}
// Col sets the light color as RGB (will be converted to xy)
func (l *Light) Col(new color.Color) error {
return l.ColContext(context.Background(), new)
}
// ColContext sets the light color as RGB (will be converted to xy)
func (l *Light) ColContext(ctx context.Context, new color.Color) error {
xy, bri := ConvertRGBToXy(new)
update := State{On: true, Xy: xy, Bri: bri}
_, err := l.bridge.SetLightStateContext(ctx, l.ID, update)
if err != nil {
return err
}
l.State.Xy = xy
l.State.Bri = bri
l.State.On = true
return nil
}
// TransitionTime sets the duration of the transition from the light’s current state to the new state
func (l *Light) TransitionTime(new uint16) error {
return l.TransitionTimeContext(context.Background(), new)
}
// TransitionTimeContext sets the duration of the transition from the light’s current state to the new state
func (l *Light) TransitionTimeContext(ctx context.Context, new uint16) error {
update := State{On: l.State.On, TransitionTime: new}
_, err := l.bridge.SetLightStateContext(ctx, l.ID, update)
if err != nil {
return err
}
l.State.TransitionTime = new
return nil
}
// Effect the dynamic effect of the light, currently “none” and “colorloop” are supported
func (l *Light) Effect(new string) error {
return l.EffectContext(context.Background(), new)
}
// EffectContext the dynamic effect of the light, currently “none” and “colorloop” are supported
func (l *Light) EffectContext(ctx context.Context, new string) error {
update := State{On: true, Effect: new}
_, err := l.bridge.SetLightStateContext(ctx, l.ID, update)
if err != nil {
return err
}
l.State.Effect = new
l.State.On = true
return nil
}
// Alert makes the light blink in its current color. Supported values are:
// “none” – The light is not performing an alert effect.
// “select” – The light is performing one breathe cycle.
// “lselect” – The light is performing breathe cycles for 15 seconds or until alert is set to "none".
func (l *Light) Alert(new string) error {
return l.AlertContext(context.Background(), new)
}
// AlertContext makes the light blink in its current color. Supported values are:
// “none” – The light is not performing an alert effect.
// “select” – The light is performing one breathe cycle.
// “lselect” – The light is performing breathe cycles for 15 seconds or until alert is set to "none".
func (l *Light) AlertContext(ctx context.Context, new string) error {
update := State{On: true, Alert: new}
_, err := l.bridge.SetLightStateContext(ctx, l.ID, update)
if err != nil {
return err
}
l.State.Effect = new
l.State.On = true
return nil
}
// ConvertRGBToXy converts a given RGB color to the xy color of the ligth.
// implemented as in https://developers.meethue.com/develop/application-design-guidance/color-conversion-formulas-rgb-to-xy-and-back/
func ConvertRGBToXy(newcolor color.Color) ([]float32, uint8) {
r, g, b, _ := newcolor.RGBA()
rf := float64(r) / 65536.0
gf := float64(g) / 65536.0
bf := float64(b) / 65536.0
rf = gammaCorrect(rf)
gf = gammaCorrect(gf)
bf = gammaCorrect(bf)
X := float32(rf*0.649926 + gf*0.103455 + bf*0.197109)
Y := float32(rf*0.234327 + gf*0.743075 + bf*0.022598)
Z := float32(rf*0.0000000 + gf*0.053077 + bf*1.035763)
x := X / (X + Y + Z)
y := Y / (X + Y + Z)
xy := []float32{x, y}
return xy, uint8(Y * 254)
}
func gammaCorrect(value float64) float64 {
if value > 0.04045 {
return math.Pow((value+0.055)/(1.0+0.055), 2.4)
}
return (value / 12.92)
}