-
Notifications
You must be signed in to change notification settings - Fork 0
/
mqtt_client.py
57 lines (49 loc) · 2.06 KB
/
mqtt_client.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
import logging
import paho.mqtt.client as paho
log = logging.getLogger("mqtt")
class MqttClient:
def __init__(
self,
name,
broker="localhost",
port=1883,
username=None,
password=None,
will=None,
will_topic=None,
):
"""Create an MQTT client instance, with the optional possibility to set a
last will.
:param name: client identifier
:param broker: address or name of MQTT server
:param port: broker port
:param username: str, optional, user
:param password: str, optional, password
:param will: the last will of the client
:param will_topic: the topic to post the last will to"""
self.client = paho.Client(name, False)
self.client.on_message = self.on_request
self.external_handler = None
if will and will_topic:
self.client.will_set(will_topic, will, 1, True)
if username and password:
self.client.username_pw_set(username, password)
log.debug("Connecting to broker %s:%s", broker, port)
self.client.connect(broker, port=port)
def set_external_handler(self, handler):
self.external_handler = handler
def on_request(self, client, userdata, msg):
"""Invoked when a message is received on t_requests"""
if self.external_handler:
try:
self.external_handler(client, userdata, msg)
except: # noqa: E722
# that's alright, we don't know what might go wrong ,
# so we really need a broad exception handler here
log.exception('An exception occured inside the handler')
# we'll log the problematic payload too, but only up to
# a certain length, to prevent log cluttering, in case
# someone managed to post an excessively long message.
log.debug('Problematic message: `%.500s`', msg.payload)
else:
log.debug("MQTT IN %s %s", msg.topic, msg.payload)