forked from matthijsvisser/kamstrup-402-mqtt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mqtt_handler.py
executable file
·81 lines (67 loc) · 2.58 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
72
73
74
75
76
77
78
79
80
81
#!/usr/bin/python
#
# Created by Matthijs Visser
import logging
import paho.mqtt.client as paho
import ssl
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="", tls_enabled=False,
tls_ca_cert="", tls_cert="", tls_key="",tls_insecure=True):
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
self.tls_enabled = tls_enabled
self.tls_ca_cert = tls_ca_cert
self.tls_cert = tls_cert
self.tls_key = tls_key
self.tls_insecure = tls_insecure
def connect(self):
settings_message = ""
self.mqtt_client = paho.Client(self.client_id, True)
if self.authentication:
self.mqtt_client.username_pw_set(self.user, self.password)
settings_message = 'with username {}, '.format(self.user)
if self.tls_enabled:
self.mqtt_client.tls_set(self.tls_ca_cert,tls_version=ssl.PROTOCOL_TLSv1_2)
self.mqtt_client.tls_insecure_set(self.tls_insecure)
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()