-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathyeelight.go
203 lines (171 loc) · 5.36 KB
/
yeelight.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
package yeelight
import (
"bytes"
"encoding/json"
"io"
"net"
"strings"
"sync"
"github.com/pkg/errors"
)
// YeeLight is a struct representing a YeeLight Smart LED Bulb.
type YeeLight struct {
CacheControl string `json:"cache_control,omitempty"`
Location string `json:"location,omitempty"`
ID string `json:"id,omitempty"`
Model string `json:"model,omitempty"`
FirmwareVersion string `json:"fw_ver,omitempty"`
Support SupportedFeatures `json:"support"`
Power PowerValue `json:"power,omitempty"`
Brightness int `json:"brightness,omitempty"`
ColorMode ColorModeValue `json:"color_mode,omitempty"`
ColorTemperature int `json:"color_temperature,omitempty"`
RGB RGBValue `json:"rgb,omitempty"`
Hue int `json:"hue,omitempty"`
Saturation int `json:"saturation,omitempty"`
Name string `json:"name"`
propMutex sync.RWMutex
tcpSocket net.Conn
connMutex sync.RWMutex
idMutex sync.RWMutex
// idCommand is the command ID used to identify correspondant Answer
idCommand int
// pendingCmds is the map where are stored chan for answers of sent commands.
// Once the "transaction" is done, the chan is closed and the map entry deleted.
pendingCmds map[int]chan Answer
errs chan error
events chan Notification
}
func (y *YeeLight) String() string {
b, _ := json.Marshal(y)
return string(b)
// return fmt.Sprintf("<id: %s, fw: %s, IP: %s> support :%s", y.ID, y.FirmwareVersion, y.Location, y.Support)
}
// GetErrors returns error chan where YeeLight device errors are sent.
func (y *YeeLight) GetErrors() <-chan error {
return y.errs
}
// GetNotification returns events chan where YeeLight device events are sent.
func (y *YeeLight) GetNotification() <-chan Notification {
return y.events
}
// newFromAdvertisement build a YeeLight struct from an advertisement
// message.
func newFromAdvertisement(msg []byte) (*YeeLight, error) {
buf := bytes.NewBuffer(msg)
chunk, err := buf.ReadBytes('\n')
if err != nil {
return nil, errors.WithStack(err)
}
if !(bytes.Compare(chunk, discoveryAnswerHeader) == 0 || bytes.Compare(chunk, advertisementHeader) == 0) {
return nil, errors.Wrapf(ErrWrongAdvertisement, "wrong advertisement header: %s", string(chunk))
}
lines := make(map[string]string)
for {
chunk, err = buf.ReadBytes('\n')
if err != nil {
if err == io.EOF {
break
}
return nil, errors.Wrap(err, "newFromAdventisement failed")
}
header := strings.SplitN(string(chunk), ": ", 2)
if len(header) >= 2 {
lines[strings.ToLower(header[0])] = strings.TrimSuffix(header[1], "\r\n")
}
}
return parseFromMap(lines)
}
// parseFromMap build a YeeLight struct from a map key-value.
func parseFromMap(lines map[string]string) (*YeeLight, error) {
y := &YeeLight{}
var err error
var found bool
y.CacheControl, found = lines["cache-control"]
if !found {
return nil, errors.Wrap(ErrWrongAdvertisement, "missing cache control header")
}
y.Location, found = lines["location"]
if !found {
return nil, errors.Wrap(ErrWrongAdvertisement, "missing location header")
}
y.Location = strings.TrimPrefix(y.Location, "yeelight://")
y.ID, found = lines["id"]
if !found {
return nil, errors.Wrap(ErrWrongAdvertisement, "missing id header")
}
y.Name, found = lines["name"]
if !found {
return nil, errors.Wrap(ErrWrongAdvertisement, "missing name header")
}
y.Model, found = lines["model"]
if !found {
return nil, errors.Wrap(ErrWrongAdvertisement, "missing model header")
}
y.FirmwareVersion, found = lines["fw_ver"]
if !found {
return nil, errors.Wrap(ErrWrongAdvertisement, "missing fw_ver header")
}
supportedFeatures, found := lines["support"]
if !found {
return nil, errors.Wrap(ErrWrongAdvertisement, "missing support header")
}
y.setSupport(supportedFeatures)
val, found := lines["power"]
if !found {
return nil, errors.Wrap(ErrWrongAdvertisement, "missing power header")
}
err = y.setPower(val)
if err != nil {
return nil, errors.Wrap(err, "wrong power value")
}
val, found = lines["bright"]
if !found {
return nil, errors.Wrap(ErrWrongAdvertisement, "missing bright header")
}
err = y.setBright(val)
if err != nil {
return nil, errors.Wrap(err, "wrong bright value")
}
val, found = lines["color_mode"]
if !found {
return nil, errors.Wrap(ErrWrongAdvertisement, "missing color_mode header")
}
err = y.setColorMode(val)
if err != nil {
return nil, errors.Wrap(err, "wrong color_mode value")
}
val, found = lines["ct"]
if !found {
return nil, errors.Wrap(ErrWrongAdvertisement, "missing ct header")
}
err = y.setColorTemperature(val)
if err != nil {
return nil, errors.Wrap(err, "wrong ct value")
}
val, found = lines["rgb"]
if !found {
return nil, errors.Wrap(ErrWrongAdvertisement, "missing rgb header")
}
err = y.setRGB(val)
if err != nil {
return nil, errors.Wrap(err, "wrong rgb value")
}
val, found = lines["hue"]
if !found {
return nil, errors.Wrap(ErrWrongAdvertisement, "missing hue header")
}
err = y.setHue(val)
if err != nil {
return nil, errors.Wrap(err, "wrong hue value")
}
val, found = lines["sat"]
if !found {
return nil, errors.Wrap(ErrWrongAdvertisement, "missing sat header")
}
err = y.setSaturation(val)
if err != nil {
return nil, errors.Wrap(err, "wrong sat value")
}
return y, nil
}