forked from Ankermgmt/ankermake-m5-protocol
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmqttapi.py
164 lines (128 loc) · 4.82 KB
/
mqttapi.py
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
import paho.mqtt.client as mqtt
import paho.mqtt
import logging as log
import ssl
import json
import uuid
from datetime import datetime, timedelta
from libflagship.util import enhex
from libflagship.mqtt import MqttMsg, MqttPktType
class AnkerMQTTBaseClient:
def __init__(self, printersn, mqtt, key, guid=None):
self._mqtt = mqtt
self._printersn = printersn
self._key = key
self._mqtt.on_connect = self._on_connect
self._mqtt.on_disconnect = self._on_disconnect
self._mqtt.on_message = self._on_message
self._mqtt.on_publish = self.on_publish
self._queue = []
self._guid = guid or str(uuid.uuid4())
self._connected = False
# internal function
def _on_connect(self, client, userdata, flags, rc):
if rc != 0:
raise IOError(f"could not connect: rc={rc} ({paho.mqtt.client.error_string(rc)})")
mqtt = self._mqtt
mqtt.subscribe(f"/phone/maker/{self.sn}/notice")
mqtt.subscribe(f"/phone/maker/{self.sn}/command/reply")
mqtt.subscribe(f"/phone/maker/{self.sn}/query/reply")
self._connected = True
self.on_connect(client, userdata, flags)
# internal function
def _on_disconnect(self, client, userdata, rc):
self._connected = False
# public api: override in subclass (if needed)
def on_connect(self, client, userdata, flags):
log.info("Connected to mqtt")
# public api: override in subclass (if needed)
def on_publish(self, client, userdata, result):
pass
# internal function
def _on_message(self, client, userdata, msg):
pkt, tail = MqttMsg.parse(msg.payload, key=self._key)
data = json.loads(pkt.data)
if isinstance(data, list):
self._queue.append((msg, data))
else:
self._queue.append((msg, [data]))
if tail:
log.warning(f"UNPARSED TAIL DATA: {tail}")
self.on_message(client, userdata, msg, pkt, tail)
# public api: override in subclass (if needed)
def on_message(self, client, userdata, msg, pkt, tail):
pass
@classmethod
def login(cls, printersn, username, password, key, ca_certs="ankermake-mqtt.crt", verify=True):
client = mqtt.Client()
if verify:
client.tls_set(ca_certs=ca_certs)
else:
client.tls_set(ca_certs=ca_certs, cert_reqs=ssl.CERT_NONE)
client.tls_insecure_set(True)
client.username_pw_set(username, password)
return cls(printersn, client, key)
def connect(self, server, port=8789, timeout=60):
self._mqtt.connect(server, port, timeout)
start = datetime.now()
end = start + timedelta(seconds=timeout)
while datetime.now() < end:
timeout = (end - datetime.now()).total_seconds()
self._mqtt.loop(timeout=timeout)
if self._connected:
return
raise IOError("Timeout while connecting to mqtt server")
@property
def sn(self):
return self._printersn
def send_raw(self, topic, msg):
payload = msg.pack(key=self._key)
self._mqtt.publish(topic, payload=payload)
@staticmethod
def make_mqtt_pkt(guid, data, packet_type=MqttPktType.Single, packet_num=0):
return MqttMsg(
size=0, # fixed by .pack()
m3=5,
m4=1,
m5=2,
m6=5,
m7=ord('F'),
packet_type=MqttPktType.Single,
packet_num=0,
time=0,
device_guid=guid,
data=data,
)
def send(self, topic, msg):
payload = self.make_mqtt_pkt(self._guid, json.dumps(msg).encode())
log.debug(f"Sending mqtt command on [{topic}]: {payload}")
return self.send_raw(topic, payload)
def query(self, msg):
return self.send(f"/device/maker/{self.sn}/query", msg)
def command(self, msg):
return self.send(f"/device/maker/{self.sn}/command", msg)
def loop(self):
self._mqtt.loop_forever()
def fetch(self, timeout=1.0):
self._mqtt.loop(timeout=timeout)
return self.clear_queue()
def fetchloop(self):
while True:
self._mqtt.loop(timeout=1.0)
yield from self.clear_queue()
def clear_queue(self):
res = self._queue[:]
self._queue.clear()
return res
def await_response(self, msgtype, timeout=10):
msgtype = int(msgtype)
start = datetime.now()
end = start + timedelta(seconds=timeout)
while datetime.now() < end:
timeout = (end - datetime.now()).total_seconds()
self._mqtt.loop(timeout=timeout)
for _, body in self.clear_queue():
for obj in body:
if obj["commandType"] == msgtype:
return obj
return None