-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathmanager.go
256 lines (223 loc) · 5.63 KB
/
manager.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
package migateway
import (
"errors"
"time"
"github.com/bingbaba/util/logs"
)
var (
LOGGER ILogger = logs.GetBlogger()
EOF byte = 0
)
type DeviceDiscoveryMessage struct {
ID string
State interface{}
}
type AqaraManager struct {
GateWay *GateWay
Motions map[string]*Motion
Switchs map[string]*Switch
DualWiredSwitches map[string]*DualWiredWallSwitch
SensorHTs map[string]*SensorHT
Magnets map[string]*Magnet
Plugs map[string]*Plug
ReportAllMessages bool
StateMessages chan interface{}
DiscoveryMessages chan *DeviceDiscoveryMessage
DiscoveryTime int64
FreshDevListTime int64
}
func NewAqaraManager() (m *AqaraManager) {
//AqaraManager
m = &AqaraManager{
Motions: make(map[string]*Motion),
Switchs: make(map[string]*Switch),
DualWiredSwitches: make(map[string]*DualWiredWallSwitch),
SensorHTs: make(map[string]*SensorHT),
Magnets: make(map[string]*Magnet),
Plugs: make(map[string]*Plug),
StateMessages: make(chan interface{}, 1024),
DiscoveryMessages: make(chan *DeviceDiscoveryMessage, 50),
DiscoveryTime: time.Now().Unix(),
}
return
}
func (m *AqaraManager) Start(c *Configure) (err error) {
if c == nil {
c = DefaultConf
}
m.ReportAllMessages = c.ReportAllMessages
// Connection
conn := NewConn(c)
err = conn.initMultiCast()
if err != nil {
return
}
// Find gateway
m.whois(conn)
// Show device list
gw_ip := m.GateWay.IP
err = conn.initGateWay(gw_ip)
if err != nil {
return
}
err = m.discovery()
// Report or heartbeat message
go func() {
for msg := range conn.devMsgs {
m.putDevice(msg)
}
}()
return
}
func (m *AqaraManager) Stop() {
if nil != m.GateWay.GatewayConnection {
m.GateWay.GatewayConnection.stop()
m.GateWay.GatewayConnection.closeRead <- true
m.GateWay.GatewayConnection.closeWrite <- true
}
close(m.GateWay.GatewayConnection.devMsgs)
close(m.StateMessages)
close(m.DiscoveryMessages)
}
func (m *AqaraManager) putDevice(dev *Device) (added bool) {
if nil == dev {
return false
}
LOGGER.Info("DEVICESYNC:: %s(%s): %s", dev.Model, dev.Sid, dev.Data)
gateway := m.GateWay
var discovery *DeviceDiscoveryMessage
added = true
switch dev.Model {
case MODEL_GATEWAY:
gateway.Set(dev)
case MODEL_MOTION:
d, found := m.Motions[dev.Sid]
if found {
d.Set(dev)
} else {
dev.Aqara = m
dev.GatewayConnection = gateway.GatewayConnection
m.Motions[dev.Sid] = NewMotion(dev)
discovery = &DeviceDiscoveryMessage{
ID:dev.Sid,
State:m.Motions[dev.Sid].State,
}
}
case MODEL_SWITCH:
d, found := m.Switchs[dev.Sid]
if found {
d.Set(dev)
} else {
dev.Aqara = m
dev.GatewayConnection = gateway.GatewayConnection
m.Switchs[dev.Sid] = NewSwitch(dev)
discovery = &DeviceDiscoveryMessage{
ID:dev.Sid,
State:m.Switchs[dev.Sid].State,
}
}
case MODEL_DUALWIREDSWITCH:
d, found := m.DualWiredSwitches[dev.Sid]
if found {
d.Set(dev)
} else {
dev.Aqara = m
dev.GatewayConnection = gateway.GatewayConnection
m.DualWiredSwitches[dev.Sid] = NewDualWiredSwitch(dev)
discovery = &DeviceDiscoveryMessage{
ID:dev.Sid,
State:m.DualWiredSwitches[dev.Sid].State,
}
}
case MODEL_SENSORHT:
d, found := m.SensorHTs[dev.Sid]
if found {
d.Set(dev)
} else {
dev.Aqara = m
dev.GatewayConnection = gateway.GatewayConnection
m.SensorHTs[dev.Sid] = NewSensorHt(dev)
discovery = &DeviceDiscoveryMessage{
ID:dev.Sid,
State:m.SensorHTs[dev.Sid].State,
}
}
case MODEL_MAGNET:
d, found := m.Magnets[dev.Sid]
if found {
d.Set(dev)
} else {
dev.Aqara = m
dev.GatewayConnection = gateway.GatewayConnection
m.Magnets[dev.Sid] = NewMagnet(dev)
discovery = &DeviceDiscoveryMessage{
ID:dev.Sid,
State:m.Magnets[dev.Sid].State,
}
}
case MODEL_PLUG:
d, found := m.Plugs[dev.Sid]
if found {
d.Set(dev)
} else {
dev.Aqara = m
dev.GatewayConnection = gateway.GatewayConnection
m.Plugs[dev.Sid] = NewPlug(dev)
discovery = &DeviceDiscoveryMessage{
ID:dev.Sid,
State:m.Plugs[dev.Sid].State,
}
}
default:
added = false
LOGGER.Warn("DEVICESYNC:: unknown model is %s", dev.Model)
}
if nil != discovery {
LOGGER.Debug("sending to discovery chan...")
m.DiscoveryMessages <- discovery
LOGGER.Debug("sending to discovery chan over!")
}
return
}
func (m *AqaraManager) whois(conn *GateWayConn) {
//read msg
iamResp := &IamResp{}
conn.communicate(NewWhoisRequest(), iamResp)
//gateway infomation
dev := NewGateWay(iamResp.Device)
dev.IP = iamResp.IP
dev.Port = iamResp.Port
dev.Aqara = m
dev.GatewayConnection = conn
m.GateWay = dev
}
func (m *AqaraManager) discovery() (err error) {
gateway := m.GateWay
conn := gateway.GatewayConnection
//get devlist response
LOGGER.Info("start to discover the device...")
devListResp := &DeviceListResp{}
if !conn.communicate(NewDevListRequest(), devListResp) {
return errors.New("show device list error")
}
//gateway.setToken(devListResp.Token)
gateway.GatewayConnection.token = devListResp.Token
//every device
for index, sid := range devListResp.getSidArray() {
LOGGER.Info("DISCOVERY[%d] of device %s", index, sid)
dev := conn.waitDevice(sid)
dev.Aqara = m
dev.GatewayConnection = conn
dev.Token = devListResp.Token
if m.putDevice(dev) {
LOGGER.Warn("DISCOVERY[%d]: found the device %s(%s): %v", index, dev.Model, dev.Sid, dev.Data)
} else {
LOGGER.Warn("DISCOVERY[%d]: unknown model %s device: %v", index, dev.Model, dev)
}
}
m.DiscoveryTime = time.Now().Unix()
return
}
func (m *AqaraManager) SetAESKey(key string) {
m.GateWay.GatewayConnection.SetAESKey(key)
}