-
Notifications
You must be signed in to change notification settings - Fork 0
/
sub_client_test.py
74 lines (52 loc) · 1.75 KB
/
sub_client_test.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
from mqttsn_transport_udp import MQTTSNTransportUDP
from mqttsn_client import MQTTSNClient, MQTTSNState, MQTTSNGWInfo, MQTTSNPubTopic, MQTTSNSubTopic
from mqttsn_messages import MQTTSNFlags
import time
import logging
import sys
logging.basicConfig(stream=sys.stdout, format='[+]%(message)s', level=logging.DEBUG)
# list of gateways
gateways = [MQTTSNGWInfo(1, b'\x01')]
# setup transport info
port = 20000
transport = MQTTSNTransportUDP(port, b'\x02')
print("Starting client.")
# create client and connect
clnt = MQTTSNClient(b'SubClient', transport)
clnt.add_gateways(gateways)
clnt.connect(gwid=1)
# wait till we're connected
while not clnt.is_connected() or clnt.state == MQTTSNState.CONNECTING:
time.sleep(0.05)
clnt.loop()
# pub and sub topics
sub_topics = [MQTTSNSubTopic(b'led')]
pub_topics = [MQTTSNPubTopic(b'state')]
# just to hold state, must be a list because python
led_state = [b'\x00']
def message_callback(topic: bytes, data: bytes, flags: MQTTSNFlags):
out = 'Topic: {}, Data: {}, Flags: {}'.format(topic, data, flags.union)
print(out)
# set the led state and publish
led_state[0] = data
clnt.publish(b'state', led_state[0])
# register callback for incoming publish
clnt.on_message(message_callback)
def init_tasks():
if not clnt.register_topics(pub_topics):
return False
if not clnt.subscribe_topics(sub_topics):
return False
return True
print('Entering client loop.')
while True:
try:
time.sleep(0.05)
clnt.loop()
if clnt.state in (MQTTSNState.DISCONNECTED, MQTTSNState.LOST):
print("Gateway connection lost.")
# check if all the pubs and subs are done
if not init_tasks():
continue
except KeyboardInterrupt:
break