-
Notifications
You must be signed in to change notification settings - Fork 2
/
client.py
70 lines (59 loc) · 2.03 KB
/
client.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
import time
import json
import logging
from config_manager import ConfigManager
from lora_controller import LoRaController, HANDSHAKE_REPLY
logging.basicConfig(level=logging.INFO)
def get_user_payload():
payload = input("\nPlease enter the JSON payload: ")
try:
json.loads(payload)
return payload
except json.JSONDecodeError as e:
logging.error(f"Invalid JSON input {e}")
return None
def main():
lora_config = ConfigManager.get_lora_config()
lora_config["serial_port"] = "/dev/tty.usbserial-210"
lora_controller = LoRaController(**lora_config)
lora_controller.setup()
server_address = 0x00
waiting = False
# get server address
start_time = time.time()
lora_controller.send_ping()
while True:
msg = lora_controller.listen()
if msg:
print("Message received")
msg_type, addr = lora_controller.parse_message_type(msg)
print("Message parsed")
if addr and msg_type == HANDSHAKE_REPLY:
server_address = addr
logging.info("Handshake completed")
break
# send a ping every 10 seconds until connected
if time.time() - start_time > 10:
lora_controller.send_ping()
start_time = time.time()
# start main loop
logging.info("Starting main loop")
while True:
while waiting:
msg = lora_controller.listen()
if msg:
logging.info(f"Message: {msg}")
waiting = False
if time.time() - start_time > 10:
logging.warning("Timeout after waiting for 10 seconds.")
waiting = False
payload = get_user_payload()
if payload:
try:
lora_controller.send_message(payload, server_address)
waiting = True
start_time = time.time()
except Exception as e:
logging.error(f"An unexpected error occurred: {e}")
if __name__ == "__main__":
main()