-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMqttClient.py
294 lines (241 loc) · 10.9 KB
/
MqttClient.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
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
# -*- coding: utf-8 -*-
# --------------------------------------------------------------------------
# _____ ______________
# | __ \ /\|__ ____ __|
# | |__) | / \ | | | |
# | _ / / /\ \ | | | |
# | | \ \/ ____ \| | | |
# |_| \_\/ \_\_| |_| ... RFID ALL THE THINGS!
#
# A resource access control and telemetry solution for Makerspaces
#
# Developed at MakeIt Labs - New Hampshire's First & Largest Makerspace
# http://www.makeitlabs.com/
#
# Copyright 2018 MakeIt Labs
#
# Permission is hereby granted, free of charge, to any person obtaining a
# copy of this software and associated documentation files (the "Software"),
# to deal in the Software without restriction, including without limitation
# the rights to use, copy, modify, merge, publish, distribute, sublicense,
# and/or sell copies of the Software, and to permit persons to whom the
# Software is furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
# WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
# CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#
# --------------------------------------------------------------------------
#
# Author: Steve Richardson (steve.richardson@makeitlabs.com)
#
from PyQt5.QtCore import Qt, QThread, QWaitCondition, QMutex, QTimer, pyqtSignal, pyqtSlot, pyqtProperty
import paho.mqtt.client as mqtt
from Logger import Logger
import traceback
class MqttClient(QThread):
TOPIC_BROADCAST_CONTROL = 'control/broadcast'
TOPIC_TARGETED_CONTROL = 'control/node'
TOPIC_TARGETED_STATUS = 'status/node'
Disconnected = 0
Connecting = 1
Reconnecting = 2
Connected = 3
MQTT_3_1 = mqtt.MQTTv31
MQTT_3_1_1 = mqtt.MQTTv311
connected = pyqtSignal()
disconnected = pyqtSignal()
stateChanged = pyqtSignal(int)
hostnameChanged = pyqtSignal(str)
portChanged = pyqtSignal(int)
keepAliveChanged = pyqtSignal(int)
cleanSessionChanged = pyqtSignal(bool)
protocolVersionChanged = pyqtSignal(int)
broadcastEvent = pyqtSignal(str,str)
targetedEvent = pyqtSignal(str,str)
brokerConnectionEvent = pyqtSignal(bool)
def __init__(self, loglevel='WARNING', baseTopic='ratt'):
QThread.__init__(self)
self.logger = Logger(name='ratt.mqtt')
self.logger.setLogLevelStr(loglevel)
self.debug = self.logger.isDebug()
self._quit = False
self.cond = QWaitCondition()
self.mutex = QMutex()
self._node_id = None
self._hostname = 'localhost'
self._port = 1883
self._keepalive = 60
self._clean_session = True
self._protocol_version = MqttClient.MQTT_3_1
self._reconnect_time = 10000
self._state = MqttClient.Disconnected
self._base_topic = baseTopic
def init_client(self, hostname='localhost', port=1883, nodeId='', reconnectTime=60,
sslEnabled = False, caCertFile = '', clientCertFile = '', clientKeyFile = ''):
self._node_id = nodeId
self._hostname = hostname
self._port = port
self._keepalive = 60
self._reconnect_time = reconnectTime
self._client = mqtt.Client(clean_session=self._clean_session, protocol=self.protocolVersion)
self._client.on_connect = self.on_connect
self._client.on_message = self.on_message
self._client.on_disconnect = self.on_disconnect
self._client.on_log = self.on_log
if sslEnabled:
try:
self.logger.info('SSL enabled, ca_cert=' + caCertFile + ' cert=' + clientCertFile + ' key=' + clientKeyFile)
self._client.tls_set(ca_certs=caCertFile,
certfile=clientCertFile,
keyfile=clientKeyFile)
except IOError:
self.logger.error('Error loading SSL certificates! Are the paths and filenames correct in the .ini file?')
except:
self.logger.error('Unknown error loading SSL certificates.')
self.logger.info('MQTT client initialized, broker host is ' + self._hostname + ':' + str(self._port))
self.connectToBroker()
@pyqtProperty(int, notify=stateChanged)
def state(self):
return self._state
@state.setter
def state(self, state):
if self._state == state: return
self._state = state
self.stateChanged.emit(state)
@pyqtProperty(str, notify=hostnameChanged)
def hostname(self):
return self._hostname
@hostname.setter
def hostname(self, hostname):
if self._hostname == hostname: return
self._hostname = hostname
self.hostnameChanged.emit(hostname)
@pyqtProperty(int, notify=portChanged)
def port(self):
return self._port
@port.setter
def port(self, port):
if self._port == port: return
self._port = port
self.portChanged.emit(port)
@pyqtProperty(int, notify=keepAliveChanged)
def keepAlive(self):
return self._keepalive
@keepAlive.setter
def keepAlive(self, keepAlive):
if self._keepalive == keepAlive: return
self._keepalive = keepAlive
self.keepAliveChanged.emit(keepAlive)
@pyqtProperty(bool, notify=cleanSessionChanged)
def cleanSession(self):
return self._clean_session
@cleanSession.setter
def cleanSession(self, cleanSession):
if self._clean_session == cleanSession: return
self._clean_session = cleanSession
self.cleanSessionChanged.emit(cleanSession)
@pyqtProperty(int, notify=protocolVersionChanged)
def protocolVersion(self):
return self._protocol_version
@protocolVersion.setter
def protocolVersion(self, protocolVersion):
if self._protocol_version == protocolVersion: return
if protocolVersion in (MqttClient.MQTT_3_1, MQTT_3_1_1):
self._protocol_version = protocolVersion
self.protocolVersionChanged.emit(protocolVersion)
def topic_or_subtopic(self, topic=None, subtopic=None):
# "subtopic" is a shortcut way of not having to supply the prefix part of the topic
# for RATT; generally all messages should be published with this prefix
if self._node_id is not None:
if topic is None and subtopic is not None:
t = self._base_topic + '/' + MqttClient.TOPIC_TARGETED_STATUS + '/' + self._node_id + '/' + subtopic
else:
t = topic
return t
def publish(self, topic=None, subtopic=None, msg=None, qos=2, retain=False):
t = self.topic_or_subtopic(topic, subtopic)
self._client.publish(t, payload=msg, qos=qos, retain=retain)
def will_set(self, topic=None, subtopic=None, msg=None, qos=2, retain=False):
t = self.topic_or_subtopic(topic, subtopic)
self._client.will_set(t, payload=msg, qos=qos, retain=retain)
@pyqtSlot(str, str)
def slotPublishSubtopic(self, subtopic, msg):
self.logger.debug("publish Subtopic " + subtopic + ": " + msg)
self.publish(subtopic=subtopic, msg=msg)
@pyqtSlot(str, str)
def slotPublishSubtopicRetain(self, subtopic, msg):
self.logger.debug("publish Subtopic [retain] " + subtopic + ": " + msg)
self.publish(subtopic=subtopic, msg=msg, retain=True)
#################################################################
@pyqtSlot()
def connectToBroker(self):
if not self.isRunning():
self.start();
else:
self.cond.wakeOne();
def run(self):
self.logger.info("thread run")
while not self._quit:
if self.state is MqttClient.Disconnected and self._hostname:
try:
self.logger.info('client connecting to ' + self._hostname + ':' + str(self._port))
self.state = MqttClient.Connecting
self._client.reconnect_delay_set(min_delay=1, max_delay=30)
self._client.connect(self._hostname, port=self._port, keepalive=self._keepalive)
except:
self.logger.info('could not connect, retrying in ' + str(self._reconnect_time / 1000) + ' seconds')
self.state = MqttClient.Disconnected
self.msleep(self._reconnect_time)
self.msleep(self._reconnect_time)
self._client.loop(timeout=1.0)
self.logger.info("thread done")
@pyqtSlot()
def disconnectFromBroker(self):
self._client.disconnect()
def subscribe(self, topic):
if self.state == MqttClient.Connected:
self.logger.info('subscribe topic=' + topic)
self._client.subscribe(topic)
def on_log(self, client, userdata, level, buf):
if level == mqtt.MQTT_LOG_ERR:
f = self.logger.error
elif level == mqtt.MQTT_LOG_WARNING:
f = self.logger.warning
elif level == mqtt.MQTT_LOG_NOTICE:
f = self.logger.info
elif level == mqtt.MQTT_LOG_INFO:
f = self.logger.info
else:
f = self.logger.debug
f(buf)
def on_connect(self, client, userdata, flags, rc):
if self._node_id is not None:
self.state = MqttClient.Connected
self.logger.info('on_connect')
self.subscribe(self._base_topic + '/' + MqttClient.TOPIC_BROADCAST_CONTROL + '/#')
self.subscribe(self._base_topic + '/' + MqttClient.TOPIC_TARGETED_CONTROL + '/' + self._node_id + '/#')
self.brokerConnectionEvent.emit(True)
def on_disconnect(self, client, userdata, rc):
self.state = MqttClient.Disconnected
self.logger.info('on_disconnect')
self.brokerConnectionEvent.emit(False)
def on_message(self, client, userdata, message):
if self._node_id is not None:
topic_broadcast = self._base_topic + '/' + MqttClient.TOPIC_BROADCAST_CONTROL + '/'
topic_targeted = self._base_topic + '/' + MqttClient.TOPIC_TARGETED_CONTROL + '/' + self._node_id + '/'
topic = str(message.topic)
payload = str(message.payload)
self.logger.info('on_message: ' + topic + ' -> ' + payload)
if message.topic.startswith(topic_broadcast):
subtopic = topic.replace(topic_broadcast, '')
self.broadcastEvent.emit(subtopic, payload)
elif message.topic.startswith(topic_targeted):
subtopic = topic.replace(topic_targeted, '')
self.targetedEvent.emit(subtopic, payload)