forked from bitStream93/ProtoTracer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmqtt_app_bridge.py
40 lines (32 loc) · 1.08 KB
/
mqtt_app_bridge.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
import json
import paho.mqtt.client as paho
import signal
broker="192.168.0.32"
port=1883
received_data = []
class GracefulKiller:
kill_now = False
def __init__(self):
signal.signal(signal.SIGINT, self.exit_gracefully)
signal.signal(signal.SIGTERM, self.exit_gracefully)
def exit_gracefully(self, *args):
self.kill_now = True
def on_message(client, userdata, message):
data = json.loads(message.payload)
received_data.append(data)
if __name__ == '__main__':
killer = GracefulKiller()
mqClient = paho.Client("Prototracer_bridge") # create client object
mqClient.connect(broker, port) # establish connection
mqClient.on_message=on_message
mqClient.subscribe("dev/prototracer")
mqClient.loop_start()
while not killer.kill_now:
with open('./data_pipe_fifo', 'w') as f:
if len(received_data) > 0:
data = received_data.pop(0)
out = f"{data['cmd']}:" + ":".join(data['dat'])
print(f"Writing: {out}")
f.write(out)
f.flush()
f.close()