-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
98 lines (81 loc) · 2.82 KB
/
app.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
import argparse
import asyncio
import os
import pickle
from concurrent.futures.thread import ThreadPoolExecutor
from contextlib import asynccontextmanager
from typing import List
from hbmqtt.client import QOS_2, MQTTClient
from hbmqtt.mqtt.publish import PublishPacket, PublishPayload
from hbmqtt.session import ApplicationMessage
label = ""
@asynccontextmanager
async def connect_to_broker(broker: str):
try:
client = MQTTClient(config=dict(keep_alive=240))
await client.connect(broker)
yield client
finally:
await client.disconnect()
async def subscribe_to_topics(client: MQTTClient, topics: List[str]):
try:
await client.subscribe([(topic, QOS_2) for topic in topics])
while True:
message = await client.deliver_message()
packet: PublishPacket = message.publish_packet
if packet:
yield packet.topic_name, pickle.loads(packet.data)
finally:
await client.unsubscribe(topics) # do not need the QOS for unsub
async def publish_to_topics(client: MQTTClient, topics: List[str], data):
await asyncio.gather(
*[
asyncio.create_task(client.publish(topic, pickle.dumps(data), qos=QOS_2))
for topic in topics
]
)
async def subscribe_to_topics_task(client: MQTTClient, topics: List[str]):
async for topic, message in subscribe_to_topics(client, topics):
global label
l = f"[{label}] " if label else ""
print(f"{l}Recieved the following message in '{topic}': {message}")
async def publish_to_topics_task(client: MQTTClient, topics: List[str]):
i = 0
while True:
await publish_to_topics(client, topics, f"i is {i}")
await asyncio.sleep(1)
i += 1
async def main():
parser = argparse.ArgumentParser(description="MQTT Client")
parser.add_argument("--label", help="The log label of this process.", type=str)
parser.add_argument(
"--broker", help="The MQTT broker to connect to.", type=str, required=True
)
parser.add_argument(
"--topics-publish",
nargs="*",
help="List of topics to subscribe to.",
type=str,
default=[],
)
parser.add_argument(
"--topics-subscribe",
nargs="*",
help="List of topics to subscribe to.",
type=str,
default=[],
)
args = parser.parse_args()
global label
label = args.label
async with connect_to_broker(args.broker) as client:
subscribe_task = asyncio.create_task(
subscribe_to_topics_task(client, args.topics_subscribe)
)
await asyncio.sleep(0.5)
await asyncio.gather(
subscribe_task,
asyncio.create_task(publish_to_topics_task(client, args.topics_publish)),
)
if __name__ == "__main__":
asyncio.run(main())