-
Notifications
You must be signed in to change notification settings - Fork 3
/
miio.go
160 lines (131 loc) · 3.31 KB
/
miio.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
package miio
import (
"github.com/AlexxIT/openmiio_agent/internal/app"
"github.com/AlexxIT/openmiio_agent/internal/mqtt"
"github.com/AlexxIT/openmiio_agent/pkg/rpc"
"github.com/rs/zerolog"
)
const (
AddrBasis = int(1) // basic_gw
AddrBluetooth = int(2) // silabs_ncp_bt
AddrZigbee = int(4) // zigbee_gw
AddrHomeKit = int(8) // homekitserver
AddrAutomation = int(16) // mijia_automation
AddrMaster = int(32) // basic_app
AddrCentral = int(128) // central_service_lite
AddrCloud = int(0) // miio_client
AddrMQQT = int(-1) // mosquitto
)
func Init() {
if !app.Enabled("miio") {
return
}
log = app.GetLogger("miio")
mqtt.Subscribe(func(topic string, payload []byte) {
if topic == "miio/command" {
miioRequestRaw(AddrMQQT, payload)
}
}, "miio/command")
go rpc.MarksWorker()
go cloudWorker()
go localWorker()
}
func Send(to int, b []byte) {
switch to {
case AddrCloud:
sendToCloud(b)
case AddrMQQT:
mqtt.Publish("miio/command_ack", b, false)
default:
sendToUnicast(to, b)
}
}
var log zerolog.Logger
func miioRequestRaw(from int, b []byte) {
msg, err := rpc.NewMessage(b)
if err != nil {
log.Panic().Err(err).Caller().Msg(string(b))
return
}
miioRequest(from, msg)
}
func miioRequest(from int, msg rpc.Message) {
v, hasTO := msg["_to"]
to := rpc.Atoi(v)
if to > 0 {
// 1. Request from local to multiple local (if to > 0)
log.Trace().Msgf("[miio] %s msg from=%d to=%d", msg, from, to)
if from == to {
return // skip basic_gw bug
}
msg.SetInt("_from", from)
} else if msg0, to0 := rpc.FindMessage(msg); msg0 != nil {
// 2. Response from any to any (msg with original ID)
log.Trace().Msgf("[miio] %s res from=%d to=%d", msg, from, to0)
if from == AddrCloud && to0 > 0 && !hasPrefix(msg0["method"], `"local.`) {
mqtt.Publish("miio/report_ack", msg, false)
}
miioResponse(to0, msg0, msg)
return
} else {
// 3. Request from local to cloud (if from > 0)
// 4. Request from cloud or mqtt to local (if from <= 0)
// 5. Request from mqtt to cloud (if from=-1 and to=0)
log.Trace().Msgf("[miio] %s req from=%d to=0", msg, from)
if from > 0 && !hasPrefix(msg["method"], `"local.`) {
mqtt.Publish("miio/report", msg, false)
}
}
if hasTO {
delete(msg, "_to")
}
for _, patch := range rpc.Requests {
if patch(from, &msg) {
log.Trace().Msgf("[miio] %s req patch", msg)
break
}
}
if msg == nil {
return
}
if to == AddrCloud {
// swap message ID, so we can catch response on this request
rpc.MarkMessage(msg, from)
}
b, err := msg.Marshal()
if err != nil {
log.Warn().Err(err).Caller().Send()
return
}
switch {
case to > 0:
// request from local to multiple local
sendToMulticast(to, b)
case from > 0 || hasTO:
// request from local to cloud or from mqtt to cloud
sendToCloud(b)
default:
// request from cloud or mqtt to local
sendToMethod(msg.Method(), b)
}
}
func miioResponse(to int, req, res rpc.Message) {
for _, patch := range rpc.Responses {
if patch(to, req, &res) {
log.Trace().Msgf("[miio] %s res patch", res)
break
}
}
if res == nil {
return
}
b, err := res.Marshal()
if err != nil {
log.Warn().Err(err).Caller().Send()
return
}
Send(to, b)
}
func hasPrefix(b []byte, prefix string) bool {
return len(b) >= len(prefix) && string(b[0:len(prefix)]) == prefix
}