-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patht1_receiver.py
76 lines (63 loc) · 2.35 KB
/
t1_receiver.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
from processModule.serverConnect import connect_mqtt
import cv2
import os
import sys
import datetime as dt
import yaml
import subprocess
from processModule.camera_process import process_frames
"""
Receiver client as subscriber of both device clients for logging and processing
received published data. Runs subprocesses in parallel automatically.
"""
with open("config.yaml", "r") as f:
config = yaml.safe_load(f)
LOG_FILE = sys.argv[1] if len(sys.argv) > 1 else "log.txt"
if os.path.exists(LOG_FILE):
# if the file already exists, create a separate file with a unique name - avoid overwriting
base, ext = os.path.splitext(LOG_FILE)
idx = 1
while os.path.exists(f"{base}_{idx}{ext}"):
idx += 1
log_file_path = f"{base}_{idx}{ext}"
def subscribe(client, topic):
def on_message(client, userdata, msg):
if msg.topic == "data/camera/frame":
print(f"Received {len(msg.payload)} bytes from topic {msg.topic}")
process_frames(msg.payload) # display frames in cv2 window
msg_decode = str(bytearray(msg.payload))
else:
print(f"Received {msg.payload.decode()} from topic {msg.topic}")
msg_decode = msg.payload.decode()
if config["write_log"]:
with open(LOG_FILE, "a") as f:
f.write(
f"{dt.datetime.now().isoformat()}: Received {msg_decode} from {msg.topic}\n"
)
client.subscribe(topic)
client.on_message = on_message
def main():
camera_process = subprocess.Popen(["python", "cam_client.py"])
radar_process = subprocess.Popen(["python", "radar_client.py"])
client = connect_mqtt("PC")
subscribe(client, topic="data/radar")
subscribe(client, topic="data/camera/frame")
# subscribe(client, topic = "data/camera/ts")
def exit_handler(client):
camera_process.kill()
print("Camera process killed.")
radar_process.kill()
print("Radar process killed.") # RIP my friends
client.disconnect()
cv2.destroyAllWindows()
try:
client.loop_forever()
except KeyboardInterrupt:
print("Process Terminated. Exiting Receiver...")
exit_handler(client)
except Exception as e:
print("Something went wrong. Exiting Receiver...")
print(e)
exit_handler(client)
if __name__ == "__main__":
main()