Skip to content
This repository has been archived by the owner on Mar 2, 2020. It is now read-only.

Commit

Permalink
add device busy state
Browse files Browse the repository at this point in the history
  • Loading branch information
linuxluigi committed Jun 26, 2019
1 parent afd9100 commit 82115ca
Showing 1 changed file with 16 additions and 2 deletions.
18 changes: 16 additions & 2 deletions pylorawebchat/chat/lora_daemon.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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(
Expand All @@ -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())
Expand All @@ -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:
Expand Down Expand Up @@ -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

0 comments on commit 82115ca

Please sign in to comment.