-
Notifications
You must be signed in to change notification settings - Fork 2
/
gateway.go
225 lines (188 loc) · 4.26 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
package aqara
import (
"crypto/aes"
"crypto/cipher"
"encoding/hex"
"encoding/json"
"net"
"sync"
)
// http://docs.opencloud.aqara.cn/en/development/gateway-LAN-communication/
// Gateway represent a Xiaomi Aqara Gateway
type Gateway struct {
IP net.IP
Port int
Sid string
Key string
Iface string
token string
socket *net.UDPConn
lock *sync.Mutex
}
// NewGateway create a Xiaomi Gateway instance
func NewGateway(ip net.IP, sid string, key string, iface string) Gateway {
return Gateway{
IP: ip,
Port: 9898,
Sid: sid,
Key: key,
Iface: iface,
lock: &sync.Mutex{},
}
}
type deviceMessage struct {
Token string `json:"token"`
Data string `json:"data"`
}
// GetDevices return a list of SID registered
func (g *Gateway) GetDevices() ([]string, error) {
resp, err := g.communicate([]byte("{\"cmd\":\"get_id_list\"}"))
if err != nil {
return nil, err
}
var message deviceMessage
if err = json.Unmarshal(resp, &message); err != nil {
return nil, err
}
g.token = message.Token
var devices []string
if err = json.Unmarshal([]byte(message.Data), &devices); err != nil {
return nil, err
}
return devices, nil
}
type deviceStatusMessage struct {
Cmd string `json:"cmd"`
Sid string `json:"sid"`
}
// GetDeviceStatus query a device's status from gateway
func (g *Gateway) GetDeviceStatus(sid string) (*ReportMessage, error) {
message, err := json.Marshal(deviceStatusMessage{
Cmd: "read",
Sid: sid,
})
if err != nil {
return nil, err
}
resp, err := g.communicate(message)
if err != nil {
return nil, err
}
return parseBuffer(resp, g)
}
type setMesage struct {
Cmd string `json:"cmd"`
Sid string `json:"sid"`
Data map[string]interface{} `json:"data"`
}
func (m *setMesage) Marshall(key string) ([]byte, error) {
m.Data["key"] = key
return json.Marshal(m)
}
// SetRGB set the gateway's light
func (g *Gateway) SetRGB(rgb uint64) (*ReportMessage, error) {
message, err := (&setMesage{
Cmd: "write",
Sid: g.Sid,
Data: map[string]interface{}{
"rgb": rgb,
},
}).Marshall(g.getKey())
if err != nil {
return nil, err
}
resp, err := g.communicate(message)
if err != nil {
return nil, err
}
return parseBuffer(resp, g)
}
// SetMID sound the gateway
func (g *Gateway) SetMID(mid uint, vol uint) (*ReportMessage, error) {
message, err := (&setMesage{
Cmd: "write",
Sid: g.Sid,
Data: map[string]interface{}{
"mid": mid,
"vol": vol,
},
}).Marshall(g.getKey())
if err != nil {
return nil, err
}
resp, err := g.communicate(message)
if err != nil {
return nil, err
}
return parseBuffer(resp, g)
}
// SetStatus set a device's status
func (g *Gateway) SetStatus(sid string, status string) (*ReportMessage, error) {
message, err := (&setMesage{
Cmd: "write",
Sid: sid,
Data: map[string]interface{}{
"status": status,
},
}).Marshall(g.getKey())
if err != nil {
return nil, err
}
resp, err := g.communicate(message)
if err != nil {
return nil, err
}
return parseBuffer(resp, g)
}
// send one message and receive one response
func (g *Gateway) communicate(message []byte) ([]byte, error) {
if err := g.checkConnection(); err != nil {
return nil, err
}
g.lock.Lock()
defer g.lock.Unlock()
if _, err := g.socket.Write(message); err != nil {
return nil, err
}
buffer := make([]byte, 1024)
size, err := g.socket.Read(buffer)
if err != nil {
return nil, err
}
return buffer[:size], nil
}
// check whether connection exists, or create them if it isn't
func (g *Gateway) checkConnection() error {
g.lock.Lock()
defer g.lock.Unlock()
if g.socket != nil {
return nil
}
addr, err := getInterfaceAddr(g.Iface)
if err != nil {
return err
}
con, err := net.DialUDP("udp", &net.UDPAddr{IP: addr, Port: 0}, &net.UDPAddr{
IP: g.IP,
Port: g.Port,
})
if err != nil {
return err
}
g.socket = con
return nil
}
var iv = []byte{0x17, 0x99, 0x6d, 0x09, 0x3d, 0x28, 0xdd, 0xb3, 0xba, 0x69, 0x5a, 0x2e, 0x6f, 0x58, 0x56, 0x2e}
func (g *Gateway) getKey() string {
if g.Key == "" || g.token == "" {
return ""
}
block, err := aes.NewCipher([]byte(g.Key))
if err != nil {
return ""
}
encrypter := cipher.NewCBCEncrypter(block, iv)
out := make([]byte, aes.BlockSize)
encrypter.CryptBlocks(out, []byte(g.token))
return hex.EncodeToString(out)
}