-
Notifications
You must be signed in to change notification settings - Fork 0
/
BrokerConnection.py
41 lines (30 loc) · 1.3 KB
/
BrokerConnection.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
import logging
import paho.mqtt.client as mqtt
class BrokerConnection:
"""manages transiently-stored broker connections"""
client = None
handler_method = None
connection_name = ''
qos_level = 0
def __init__(self, connection_name, hostname, qos_level=0):
self.connection_name = connection_name
self.qos_level = qos_level
self.client = mqtt.Client()
self.client.on_connect = self.on_connect
self.client.on_message = self.on_message
self.client.connect(hostname, 1883, 60)
# The callback for when the client receives a CONNACK response from the server.
def on_connect(self, client, userdata, flags, rc):
logging.info("BrokerConnection connected ")
# Subscribing in on_connect() means that if we lose the connection and
# reconnect then subscriptions will be renewed.
client.subscribe("#", qos=self.qos_level)
def on_message(self, client, userdata, msg):
self.handler_method(self, msg.topic, msg)
def start_receiving(self, handler_method):
self.handler_method = handler_method
self.client.loop_start()
def stop_receiving(self):
self.client.loop_stop(False)
def publish(self, topic, payload, qos_level):
self.client.publish(topic, payload, qos=qos_level)