diff --git a/base_station_main.py b/base_station_main.py index 1ede1ae..15525c9 100644 --- a/base_station_main.py +++ b/base_station_main.py @@ -38,7 +38,7 @@ # The system will recommend disconnecting after missing TIMEOUT PINGs. # Exact timing depends on CHECK_FOR_TIMEOUTS_INTERVAL variable in base_station_tick() """ -from constants import TIMEOUT, bUEs +from constants import TIMEOUT # Internal imports @@ -63,9 +63,14 @@ def __init__(self, yaml_str): self.stdout_history = deque() self.ota = Ota( - self.yaml_data["OTA_PORT"], self.yaml_data["OTA_BAUDRATE"], self.yaml_data["OTA_ID"], self.stdout_history + self.yaml_data["OTA_PORT"], self.yaml_data["OTA_BAUDRATE"], self.stdout_history ) - logger.info(f"[DEBUG] OTA ID is set to: {self.ota.id}") + + # Fetch the Reyax ID from the OTA module + time.sleep(0.1) + self.reyax_id = self.ota.fetch_id() + + logger.info(f"[DEBUG] OTA ID is set to: {self.reyax_id}") self.EXIT = False @@ -179,12 +184,17 @@ def message_listener(self): if message_body.startswith("REQ"): logger.debug("Sending a CON") current_timestamp = int(time.time()) - bue_name = message_body[4:] + _, payload = message_body.split(":", 1) # Split on first colon + bue_name, bue_id_check = payload.split(",", 1) # Split hostname and bUE_id + + if int(bue_id_check) != bue_id: + logger.error(f"message_listener: Mismatched bUE ID in REQ message. Expected {bue_id}, got {bue_id_check}") + continue # Skip to the next message - self.ota.send_ota_message(bue_id, f"CON:{self.ota.id}:{current_timestamp}") + self.ota.send_ota_message(bue_id, f"CON:{self.reyax_id}:{current_timestamp}") self.bue_timeout_tracker[bue_name] = TIMEOUT if not bue_id in self.connected_bues: - logger.bind(bue_id=bue_id).info(f"Received a request signal from {bue_id}") + logger.bind(bue_id=bue_id).info(f"Received a request signal from {bue_id}:{bue_name}") self.connected_bues[bue_id] = bue_name else: logger.error(f"Got a connection request from {bue_id} but it is already listed as connected") diff --git a/bue_main.py b/bue_main.py index 0f967c9..c2ac287 100644 --- a/bue_main.py +++ b/bue_main.py @@ -1,4 +1,5 @@ # Standard library imports +import os import queue import sys import select @@ -54,14 +55,21 @@ def __init__(self, yaml_str = "bue_config.yaml"): try: self.ota = Ota( self.yaml_data["OTA_PORT"], - self.yaml_data["OTA_BAUDRATE"], - self.yaml_data["OTA_ID"], + self.yaml_data["OTA_BAUDRATE"] ) break except Exception as e: logger.error(f"Failed to initialize OTA module: {e}") time.sleep(2) + # Fetch the Reyax ID from the OTA module + time.sleep(0.1) + self.reyax_id = self.ota.fetch_id() + logger.info(f"__init__: OTA module initialized with Reyax ID {self.reyax_id}") + + # Fetch the device hostname + self.hostname = os.uname().nodename + # Build the state machine - states self.cur_st, self.nxt_st = State.INIT, State.INIT logger.info(f"__init__: Initializing current state to {self.cur_st.name}") @@ -240,7 +248,7 @@ def ota_connect_req(self): return # If flag not set, send another REQ message - self.ota_outgoing_queue.put((BROADCAST_OTA_ID, "REQ")) + self.ota_outgoing_queue.put((BROADCAST_OTA_ID, f"REQ:{self.hostname},{self.reyax_id}")) def ota_idle_ping(self): diff --git a/ota.py b/ota.py index 382229b..ced0210 100644 --- a/ota.py +++ b/ota.py @@ -16,7 +16,7 @@ class Ota: - def __init__(self, port, baudrate, id, stdout_history=None): + def __init__(self, port, baudrate, stdout_history=None): # Serial port configuration while True: @@ -27,8 +27,6 @@ def __init__(self, port, baudrate, id, stdout_history=None): print(f"Failed to open serial port: {e}") time.sleep(2) - self.id = id - self.stdout_history = stdout_history # Initialize CRC8 calculator @@ -155,6 +153,24 @@ def get_new_messages(self): except queue.Empty: pass return messages + + def fetch_id(self): + """ + Fetch the device ID from the Reyax module. + """ + try: + self.ser.write(b'AT+ADDRESS=?\r\n') + time.sleep(0.1) # Wait for response + response = self.ser.readlines() + for line in response: + decoded_line = line.decode('utf-8').strip() + if decoded_line.startswith('+ADDRESS='): + addr = decoded_line.split('=')[1] + self.id = int(addr) + return self.id + except Exception as e: + print(f"Failed to fetch ID: {e}") + return None def __del__(self): try: diff --git a/setup/message_dict.txt b/setup/message_dict.txt index efc23e9..4215aae 100644 --- a/setup/message_dict.txt +++ b/setup/message_dict.txt @@ -14,9 +14,9 @@ In the example for each of the messages, assume the bUE has an OTA id of 5, and REQ: Direction: bUE -> base - Meaning: The bUE is requesting that it join the OTA network and shares its name. It is broadcast over the ota, so it sends to address 0 + Meaning: The bUE is requesting that it join the OTA network and shares its hostname and Reyax Id. It is broadcast over the ota, so it sends to address 0 Body: None - Example: (bue_main.py) self.ota.send_ota_message(0, "REQ:Perry") + Example: (bue_main.py) self.ota.send_ota_message(0, "REQ:,") Response: The base station will respond with a CON message CON: