-
Notifications
You must be signed in to change notification settings - Fork 6
/
gateway.go
400 lines (341 loc) · 8.01 KB
/
gateway.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
package miio
import (
"crypto/aes"
"crypto/cipher"
"encoding/json"
"fmt"
"image/color"
"net"
)
const (
gatewayPort = 9898
cmdAck = "_ack"
)
var (
iv = []byte{0x17, 0x99, 0x6d, 0x09, 0x3d, 0x28, 0xdd, 0xb3, 0xba, 0x69, 0x5a, 0x2e, 0x6f, 0x58, 0x56, 0x2e}
)
// DeviceUpdateMessage contains data about an update.
type DeviceUpdateMessage struct {
ID string
State interface{}
}
// GatewayState defines the gateway state.
type GatewayState struct {
On bool
RGB color.Color
Brightness uint8
internalRGB uint32
}
// Gateway represents a Xiaomi gateway.
type Gateway struct {
XiaomiDevice
multiCast *net.UDPConn
aesKey []byte
State *GatewayState
UpdateChan chan *DeviceUpdateMessage
Sensors map[string]*SensorHT
Magnets map[string]*Magnet
Motions map[string]*Motion
Switches map[string]*Switch
}
// NewGateway creates a new gateway.
func NewGateway(deviceIP, aesKey string) (*Gateway, error) {
g := &Gateway{
UpdateChan: make(chan *DeviceUpdateMessage, 50),
State: &GatewayState{RGB: color.RGBA{R: 0, G: 0, B: 0, A: 0}},
Sensors: make(map[string]*SensorHT),
Magnets: make(map[string]*Magnet),
Motions: make(map[string]*Motion),
Switches: make(map[string]*Switch),
aesKey: []byte(aesKey),
}
err := g.start(deviceIP, "", gatewayPort)
if err != nil {
g.stop()
return nil, err
}
err = g.getDevices()
if err != nil {
g.stop()
return nil, err
}
err = g.startMultiCast()
if err != nil {
g.stop()
return nil, err
}
go g.processMessages()
go g.processGatewayMessages()
return g, nil
}
// GetUpdateMessage returns a state update message.
func (g *Gateway) GetUpdateMessage() *DeviceUpdateMessage {
return &DeviceUpdateMessage{
State: g.State,
ID: g.deviceID,
}
}
// SetColor sets the LED light color.
func (g *Gateway) SetColor(c color.Color) error {
r, gC, b, _ := c.RGBA()
_, _, _, a := g.State.RGB.RGBA()
corrected := color.RGBA{
R: uint8(r/256),
G: uint8(gC/256),
B: uint8(b/256),
A: uint8(a/256),
}
data := map[string]interface{}{
fieldRGB.String(): uint32FromColor(corrected),
}
return g.stateCommand(data)
}
// SetBrightness sets the LED light brightness.
func (g *Gateway) SetBrightness(br uint8) error {
if br > 100 {
br = 100
}
if 0 == br {
return g.Off()
}
r, gC, b, _ := g.State.RGB.RGBA()
corrected := color.RGBA{
R: uint8(r/256),
G: uint8(gC/256),
B: uint8(b/256),
A: 100 -br,
}
data := map[string]interface{}{
fieldRGB.String(): uint32FromColor(corrected),
}
return g.stateCommand(data)
}
// On turns the gateway on.
func (g *Gateway) On() error {
if !g.State.On {
return g.SetColor(color.RGBA{R: 255, G: 255, B: 255, A: 0})
}
return nil
}
// Off turns the gateway off.
func (g *Gateway) Off() error {
if g.State.On {
return g.SetColor(color.RGBA{R: 0, G: 0, B: 0, A: 0})
}
return nil
}
// Stop stops the gateway.
func (g *Gateway) Stop() {
if nil != g.multiCast {
g.multiCast.Close()
}
g.stop()
close(g.UpdateChan)
}
// UpdateState updates the gateway state.
func (g *Gateway) UpdateState() {
g.State.internalRGB = g.GetFieldValueUint32(fieldRGB, g.State.internalRGB)
if g.State.internalRGB > 0 {
g.State.On = true
g.State.RGB = rgbFromUint32(g.State.internalRGB)
_, _, _, a := g.State.RGB.RGBA()
a = a / 256
if a > 100 {
a = 100
}
g.State.Brightness = uint8(100 - a)
} else {
g.State.On = false
}
}
// Requests a list of connected devices.
func (g *Gateway) getDevices() error {
return g.command(cmdGetDevices, map[string]interface{}{})
}
// Processes incoming messages.
func (g *Gateway) processMessages() {
for msg := range g.messages {
m := msg.(*command)
switch m.Cmd {
case cmdGetDevices:
g.processDiscovery(m.Data)
g.processHandShake(m)
g.command(cmdGetDeviceState, map[string]interface{}{})
case cmdGetDeviceState, cmdDeviceReport, cmdSetDeviceState:
go g.processDeviceState(m)
case cmdHandShake, cmdHeartBeat:
go g.processHandShake(m)
}
}
}
// Processes handshake response.
func (g *Gateway) processHandShake(cmd *command) {
if "" != cmd.Token {
g.token = cmd.Token
}
}
// Processes a discovery message.
func (g *Gateway) processDiscovery(data string) {
devIDs := make([]string, 0)
err := json.Unmarshal([]byte(data), &devIDs)
if err != nil {
LOGGER.Error("Failed to get device list: %s", err.Error())
return
}
for _, v := range devIDs {
g.commandWithSid(cmdGetDeviceState, map[string]interface{}{}, v)
}
}
// Processes a device state message.
func (g *Gateway) processDeviceState(cmd *command) {
g.Lock()
defer g.Unlock()
mod, err := gatewayDeviceModelString(cmd.Model)
if err != nil {
LOGGER.Error("Unknown device type: %s", cmd.Model)
return
}
data := make(map[string]interface{})
err = json.Unmarshal([]byte(cmd.Data), &data)
if err != nil {
LOGGER.Error("Failed to un-marshal device data: %s", err.Error())
return
}
var device IDevice
switch mod {
case devGateway:
device = g
case devSensorHT:
d, ok := g.Sensors[cmd.Sid]
if !ok {
d = &SensorHT{
State: &SensorHTState{},
Gateway: g,
ID: cmd.Sid,
}
g.Sensors[cmd.Sid] = d
}
device = d
case devMagnet:
d, ok := g.Magnets[cmd.Sid]
if !ok {
d = &Magnet{
State: &MagnetState{},
Gateway: g,
ID: cmd.Sid,
}
g.Magnets[cmd.Sid] = d
}
device = d
case devMotion:
d, ok := g.Motions[cmd.Sid]
if !ok {
d = &Motion{
State: &MotionState{},
Gateway: g,
ID: cmd.Sid,
}
g.Motions[cmd.Sid] = d
}
device = d
case devSwitch:
d, ok := g.Switches[cmd.Sid]
if !ok {
d = &Switch{
State: &SwitchState{},
Gateway: g,
ID: cmd.Sid,
}
g.Switches[cmd.Sid] = d
}
device = d
default:
LOGGER.Warn("Unsupported device type: %s", cmd.Model)
return
}
device.SetRawState(data)
device.UpdateState()
g.UpdateChan <- device.GetUpdateMessage()
}
// Starts multi-cast listener.
func (g *Gateway) startMultiCast() error {
l, err := net.ListenMulticastUDP("udp4", nil, &net.UDPAddr{
IP: net.IPv4(224, 0, 0, 50),
Port: gatewayPort,
})
if err != nil {
return err
}
g.multiCast = l
go g.listenMultiCast()
return nil
}
// Listens for a multi-cast messages.
func (g *Gateway) listenMultiCast() {
buf := make([]byte, 2048)
for {
size, _, err := g.multiCast.ReadFromUDP(buf)
if err != nil {
LOGGER.Error("Error reading from MultiCast: %s", err.Error())
return
}
if size > 0 {
LOGGER.Debug("Received device message: %s", string(buf[0:size]))
msg := make([]byte, size)
copy(msg, buf[0:size])
g.conn.DeviceMessages <- msg
}
}
}
// Requests a device state.
func (g *Gateway) stateCommand(data map[string]interface{}) error {
return g.commandWithSid(cmdSetDeviceState, data, g.deviceID)
}
// Performs a device command.
func (g *Gateway) command(cmd string, data map[string]interface{}) error {
return g.commandWithSid(cmd, data, g.deviceID)
}
// Performs a device command with specific SID.
func (g *Gateway) commandWithSid(cmd string, data map[string]interface{}, sid string) error {
block, err := aes.NewCipher(g.aesKey)
if err != nil {
LOGGER.Error("Failed to create CMD cipher: %s", err.Error())
return err
}
mode := cipher.NewCBCEncrypter(block, iv)
cipherText := make([]byte, len(g.token))
mode.CryptBlocks(cipherText, []byte(g.token))
command := &command{
deviceDTO: deviceDTO{
Sid: sid,
},
Cmd: cmd,
}
data["key"] = fmt.Sprintf("%X", cipherText)
bytes, _ := json.Marshal(data)
command.Data = string(bytes)
err = g.conn.Send(command)
if err != nil {
LOGGER.Error("Failed to send CMD: %s", err.Error())
return err
}
return nil
}
// Processes incoming gateway messages.
func (g *Gateway) processGatewayMessages() {
for msg := range g.conn.DeviceMessages {
cmd := &command{}
err := json.Unmarshal(msg, cmd)
if err != nil {
LOGGER.Error("Failed to un-marshal command: %s", err.Error())
continue
}
if len(cmd.Cmd) >= len(cmdAck) && cmd.Cmd[len(cmd.Cmd)-len(cmdAck):] == cmdAck {
cmd.Cmd = cmd.Cmd[:len(cmd.Cmd)-len(cmdAck)]
}
if "" == g.deviceID {
g.deviceID = cmd.Sid
}
g.messages <- cmd
}
}