From 82115cae6370d926f310b401d91205938f37c860 Mon Sep 17 00:00:00 2001 From: Steffen Exler Date: Wed, 26 Jun 2019 16:38:48 +0200 Subject: [PATCH] add device busy state --- pylorawebchat/chat/lora_daemon.py | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/pylorawebchat/chat/lora_daemon.py b/pylorawebchat/chat/lora_daemon.py index 72ec9ef..d7fdde0 100644 --- a/pylorawebchat/chat/lora_daemon.py +++ b/pylorawebchat/chat/lora_daemon.py @@ -73,6 +73,9 @@ def __init__(self): self.serial_worker = serial.threaded.ReaderThread(self.ser, self) self.serial_worker.start() + # set the not busy + self.device_busy: bool = False + # serial buffer self.buffer = bytearray() self.transport = None @@ -136,6 +139,12 @@ def handle_packet(self, packet: bytes): print(incoming_packet) + # set device busy state + if incoming_packet == "AT,SENDING": + self.device_busy = True + if incoming_packet == "AT,SENDED": + self.device_busy = False + # check if packet is a message if incoming_packet[:2] == "LR": msg: Message = Message( @@ -162,18 +171,20 @@ def send_message(self, msg: str, dest_address: str = "FFFF") -> None: :param msg: messages :param dest_address: address """ + self.wait_till_device_is_ready() + print("{}: {}".format(dest_address, msg)) time.sleep(0.1) self.ser.write("AT+DEST={}\r\n".format(dest_address).encode()) time.sleep(0.1) self.ser.write("AT+SEND={}\r\n".format(len(msg)).encode()) time.sleep(0.1) self.ser.write("{}\r\n".format(msg).encode()) - time.sleep(2) def add_messages_to_queue(self, address: str, msg: str): self.message_queue.append(Message(address=address, msg=msg)) def send_rti(self): + self.wait_till_device_is_ready() print("send RTI: {}".format(datetime.datetime.now())) time.sleep(0.1) self.ser.write("AT+DEST=FFFF\r\n".encode()) @@ -185,6 +196,10 @@ def send_rti(self): # set time for next RTI broadcast self.next_rti_broadcast: float = time.time() + random.randint(30, 61) + def wait_till_device_is_ready(self): + while self.device_busy: + time.sleep(0.1) + def run(self): """ main threat to send RTI & messages """ while self.active: @@ -218,6 +233,5 @@ def run(self): message: Message = Message() message.from_string(message_queue[1].decode("utf-8")) self.lora_daemon.message_queue.append(message) - print(message.to_string()) except TypeError: continue