-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMyMQTT.py
55 lines (44 loc) · 1.86 KB
/
MyMQTT.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
import paho.mqtt.client as PahoMQTT
import json
class MyMQTT:
def __init__(self, clientID, broker, port, notifier=None):
self.broker = broker
self.port = port
self.notifier = notifier
self.clientID = clientID
self._topic = ""
self._isSubscriber = False
# create an instance of paho.mqtt.client
self._paho_mqtt = PahoMQTT.Client(clientID,True)
# register the callback
self._paho_mqtt.on_connect = self.myOnConnect
self._paho_mqtt.on_message = self.myOnMessageReceived
def myOnConnect(self, paho_mqtt, userdata, flags, rc):
print ("Connected to %s with result code: %d" % (self.broker, rc))
def myOnMessageReceived(self, paho_mqtt, userdata, msg):
# A new message is received
self.notifier.notify(msg.topic, msg.payload)
def myPublish(self, topic, msg):
# publish a message with a certain topic
self._paho_mqtt.publish(topic, json.dumps(msg), 2)
def mySubscribe(self, topic):
# subscribe for a topic
self._paho_mqtt.subscribe(topic, 2)
# just to remember that it works also as a subscriber
self._isSubscriber = True
self._topic = topic
print ("Subscribing to %s" % (topic))
def start(self):
#manage connection to broker
self._paho_mqtt.connect(self.broker , self.port)
self._paho_mqtt.loop_start()
def unsubscribe(self):
if (self._isSubscriber):
# remember to unsuscribe if it is working also as subscriber
self._paho_mqtt.unsubscribe(self._topic)
def stop(self):
if (self._isSubscriber):
# remember to unsuscribe if it is working also as subscriber
self._paho_mqtt.unsubscribe(self._topic)
self._paho_mqtt.loop_stop()
self._paho_mqtt.disconnect()