-
Notifications
You must be signed in to change notification settings - Fork 12
/
mqtt_handler.py
executable file
·71 lines (57 loc) · 2.12 KB
/
mqtt_handler.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
#!/usr/bin/python
#
# Created by Matthijs Visser
import logging
import paho.mqtt.client as paho
from logging.handlers import TimedRotatingFileHandler
log = logging.getLogger("log")
log.setLevel(logging.INFO)
class MqqtHandler (object):
def __init__(self, broker_ip, broker_port, client_id, topic_prefix, retain=False, qos=0,
authentication=False, user="", password=""):
self.broker = broker_ip
self.port = broker_port
self.client_id = client_id
self.topic_prefix = topic_prefix
self.retain = retain
self.qos = qos
self.authentication = authentication
self.user = user
self.password = password
def connect(self):
settings_message = ""
self.mqtt_client = paho.Client(paho.CallbackAPIVersion.VERSION1, self.client_id, True)
if self.authentication:
self.mqtt_client.username_pw_set(self.user, self.password)
settings_message = 'with username {}, '.format(self.user)
self.mqtt_client.connect(self.broker, self.port, 60)
self.mqtt_client.loop_start()
log.info('Connected to MQTT at: {}:{}'.format(self.broker, self.port))
settings_message += 'QoS level = {} and retain = {}'.format(self.qos, self.retain)
log.info(settings_message)
def disconnect(self):
self.mqtt_client.disconnect()
def publish(self, topic, message):
full_topic = self.create_topic(topic.lower())
try:
log.info('Publishing \'{}\'\t\'{}\'\tto {}:{}'.format(full_topic, message,
self.broker,
self.port))
mqtt_info = self.mqtt_client.publish(full_topic, message, self.qos, self.retain)
mqtt_info.wait_for_publish()
except ValueError as e:
logging.error('Value error: {}'.format(e))
except TypeError as e:
logging.error('Type error: {}'.format(e))
def subscribe(self, topic):
if self.mqtt_client.subscribe(topic) == 0:
log.info('Subscribed to topic: {}'.format(self.topic_prefix))
return True
else:
return False
def create_topic(self, data):
return "{}/{}".format(self.topic_prefix, data)
def loop_start(self):
self.mqtt_client.loop_start()
def loop_stop(self):
self.mqtt_client.loop_stop()