-
Notifications
You must be signed in to change notification settings - Fork 4
/
cloud_api_mqtt.py
executable file
·78 lines (63 loc) · 2.52 KB
/
cloud_api_mqtt.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
#!/usr/bin/env python3
import os
import json
import pprint
from importlib.metadata import version
import paho
import paho.mqtt.client as mqtt
host_addr = os.environ["HOST_ADDR"]
# The callback for when the client receives a CONNACK response from the server.
def on_connect(client, userdata, flags, rc):
print("Connected with result code " + str(rc))
# Subscribing in on_connect() means that if we lose the connection and
# reconnect then subscriptions will be renewed.
client.subscribe("sys/#")
client.subscribe("thing/#")
# Print interesting bits from message
def handle_osd_message(message: dict):
data = message["data"]
lat = data.pop("latitude", None)
lon = data.get("longitude", None)
# drop some data we are not interested in
attitude_head = data.pop("attitude_head", None)
attitude_pitch = data.pop("attitude_pitch", None)
attitude_roll = data.pop("attitude_roll", None)
height = data.pop("height", None)
data.pop("wireless_link", None)
data.pop("wireless_link_state", None)
data.pop("battery", None)
data.pop("live_status", None)
print(
f"🌍Status: Lat: {lat} Lon: {lon} height: {height} att_head {attitude_head} att_pitch {attitude_pitch} att_roll {attitude_roll}"
)
pprint.pprint(data)
# The callback for when a PUBLISH message is received from the server.
def on_message(client: mqtt.Client, userdata, msg: mqtt.MQTTMessage):
print("📨Got msg: " + msg.topic)
message = json.loads(msg.payload.decode("utf-8"))
if msg.topic.endswith("status"):
if message["method"] != "update_topo":
return
response = {
"tid": message["tid"],
"bid": message["bid"],
"timestamp": message["timestamp"] + 2,
"data": {"result": 0},
}
client.publish(msg.topic + "_reply", payload=json.dumps(response))
print("✅published")
elif msg.topic.endswith("osd") and msg.topic.startswith("thing"):
handle_osd_message(message)
PAHO_MAIN_VER = int(version("paho-mqtt").split(".")[0])
if PAHO_MAIN_VER == 1:
client = mqtt.Client(transport="tcp")
if PAHO_MAIN_VER == 2:
client = mqtt.Client(paho.mqtt.enums.CallbackAPIVersion.VERSION2, transport="tcp")
client.on_connect = on_connect
client.on_message = on_message
client.connect(host_addr, 1883, 60)
# Blocking call that processes network traffic, dispatches callbacks and
# handles reconnecting.
# Other loop*() functions are available that give a threaded interface and a
# manual interface.
client.loop_forever()