-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmessage.py
More file actions
33 lines (26 loc) · 1 KB
/
message.py
File metadata and controls
33 lines (26 loc) · 1 KB
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
import paho.mqtt.client as mqtt
class _Message():
def __init__(self, account_id, api_key, client_id):
self.account_id = account_id
self.client = mqtt.Client(client_id=client_id, clean_session=True)
self.client.username_pw_set(account_id, api_key)
self.client.connect("iot.labstack.com", 1883)
def connect_handler(self, handler):
def connect_handler(client, userdata, flags, rc):
handler()
self.client.on_connect = connect_handler
def data_handler(self, handler):
def data_handler(client, userdata, msg):
handler(msg.topic, msg.payload)
self.client.on_message = data_handler
def publish(self, topic, message):
self.client.publish('{}/{}'.format(self.account_id, topic), message)
def subscribe(self, topic, shared=False):
topic = '{}/{}'.format(self.account_id, topic)
if shared:
topic = '$queue/' + topic
self.client.subscribe(topic)
def disconnect(self):
self.client.disconnect()
def loop_forever(self):
self.client.loop_forever()