-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcomms.py
173 lines (140 loc) · 5.78 KB
/
comms.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
# -*- coding: utf-8 -*-
"""
Created on Mon Nov 23 15:55:26 2020
@author: zefyr
"""
import logging
from paho.mqtt import client as mqtt_client
import json
import settings
import datetime
import traceback
broker = 'broker.emqx.io'
port = 1883
qos_ = 0
### Topics ###
initial = settings.uniqueComms + "ece180d/team11/init"
assign = settings.uniqueComms + "ece180d/team11/assign"
move = settings.uniqueComms + "ece180d/team11/move"
tag = settings.uniqueComms + "ece180d/team11/tag"
axes = settings.uniqueComms + "ece180d/team11/axes"
direction = settings.uniqueComms + "ece180d/team11/direc"
ready = settings.uniqueComms + "ece180d/team11/ready"
command = settings.uniqueComms + "ece180d/team11/command"
powerUp = settings.uniqueComms + "ece180d/team11/powerUp"
power = settings.uniqueComms + "ece180d/team11/power"
launch = settings.uniqueComms + "ece180d/team11/launch"
start = settings.uniqueComms + "ece180d/team11/start"
stop = settings.uniqueComms + "ece180d/team11/stop"
rotation = settings.uniqueComms + "ece180d/team11/rot"
coolDown = settings.uniqueComms + "ece180d/team11/cool"
piConfirmation = settings.uniqueComms + "ece180d/team11/piconf"
pcConfirmation = settings.uniqueComms + "ece180d/team11/pcconf"
pickup = settings.uniqueComms + "ece180d/team11/pickup"
activePower = settings.uniqueComms + "ece180d/team11/activePower"
timerOver = settings.uniqueComms + "ece180d/team11/timer"
drop = settings.uniqueComms + "ece180d/team11/drop"
dropped = settings.uniqueComms + "ece180d/team11/dropped"
class Transmitter:
'''
Used for transmitting data. To use:
1. Create a transmitter using a topic following MQTT standards, ie:
transmitter = Transmitter("someTopic")
2. Send a single message which can be converted to JSON:
transmitter.transmit(someMessage)
'''
def __init__(self):
#self.topic = topic
self.connected = False
self.client = mqtt_client.Client()
self.client.on_connect = self.on_connect
self.client.connect(broker, port, keepalive=1800)
def on_connect(self, client, userdata, flags, rc):
if rc == 0:
self.connected = True
log = "Transmitter connected"
logging.info(log)
if settings.verbose: print(log, flush=True)
else:
print("Failed to connect, return code %d\n", rc, flush=True)
def transmit(self, topic, package):
# Add message ID for troubleshooting
package['MessageId'] = datetime.datetime.now().strftime("%M%S%f")
# Retry up to 5 times
unsent = 1
count = 5
while unsent and count:
unsent, _ = self.client.publish(topic, json.dumps(package), qos=qos_)
count = count - 1
if unsent:
log = f"Failed to published `{package}` from `{topic}`"
logging.info(log)
if settings.verbose: print(log, flush=True)
else:
log = f"Published `{package}` from `{topic}`"
logging.info(log)
if settings.verbose: print(log, flush=True)
class Receiver:
'''
Used for receiving data. To use:
1. Create a receiver using a topic following MQTT standards, ie:
receiver = Receiver("someTopic")
2. Start the receiver. This runs a thread in the background.
receiver.start()
3. Check for receipt and obtain incoming messages converted from JSON from
the queue, ie:
while True:
if len(receiver.packages):
newMessage = receiver.packages.pop(0)
4. When done receiving stuff, end the background thread:
receiver.stop()
'''
def __init__(self, topic, clientId):
# Allow for multiple topic subscriptions
if type(topic) == tuple:
topics = []
for t in topic:
topics.append((t, qos_))
self.topic = topics
else:
self.topic = (topic, qos_)
self.connected = False
self.clientId = clientId
self.client = mqtt_client.Client(client_id = clientId)
self.client.on_connect = self.on_connect
self.client.on_message = self.on_message
self.client.on_subscribe = self.on_subscribe
self.client.connect(broker, port)
# Queue for package handling. Received packages are added to queue by
# MQTT loop activities and then removed by game loop
self.packages = []
def on_connect(self, client, userdata, flags, rc):
if rc == 0:
self.client.subscribe(self.topic)
log = f"Receiver connected: `{self.topic}`"
logging.info(log)
if settings.verbose: print(log, flush=True)
else:
log = f"Failed to connect, return code `{rc}`"
logging.info(log)
if settings.verbose: print(log, flush=True)
def on_subscribe(self, client, userdata, mid, granted_qos):
log = "Subscribed"
logging.info(log)
if settings.verbose: print(log, flush=True)
def on_message(self, client, userdata, msg):
log = f"Received `{msg.payload.decode()}` from `{msg.topic}`"
logging.info(log)
if settings.verbose: print(log, flush=True)
# Store topic and decoded dictionary payload
try:
self.packages.append((msg.topic, json.loads(msg.payload.decode())))
except:
log = f"Failed to add message to queue: `{msg.payload.decode()}` from `{msg.topic}`"
logging.info(log)
if settings.verbose: print(log, flush=True)
traceback.print_exc()
def start(self):
self.client.loop_start()
def stop(self):
self.client.loop_stop()