import time import sys import os import argparse import tkinter as tk from .core.network import UdpHandler from .core.dispatcher import MessageDispatcher from .core.scheduler import TrafficScheduler from .core.controller import RadarController from .utils.printer import dump_message from .gui.main_window import BusMonitorApp #Configuration RX_IP = os.getenv("PYBM_RX_IP", "127.0.0.1") RX_PORT = int(os.getenv("PYBM_RX_PORT", str(61553))) TARGET_IP = os.getenv("PYBM_TARGET_IP", "127.0.0.1") TARGET_PORT = int(os.getenv("PYBM_TARGET_PORT", "51553")) def main_cli(): """Command-line interface mode (original behavior)""" print("--------------------------------------------------") print(" PyBusMonitor1553 - Active Controller (CLI Mode)") print("--------------------------------------------------") # 1. Initialize Components dispatcher = MessageDispatcher() network = UdpHandler(rx_ip=RX_IP, rx_port=RX_PORT) # 2. Initialize Radar Logic Controller radar_ctrl = RadarController() # 3. Initialize Scheduler with the Controller scheduler = TrafficScheduler(network, radar_ctrl, TARGET_IP, TARGET_PORT) # 4. Define the callback for received messages def on_packet(data, addr): header, messages = dispatcher.parse_packet(data) if messages: for msg in messages: if msg.IS_TRANSMIT: print(f"\n[RX] {msg.__class__.__name__} from RT{header.ta if header else '?'}") dump_message(msg) elif header and header.errors != 0: print(f"[RX] Server Error Code: {header.errors}") # 5. Start everything network.register_callback(on_packet) network.start() scheduler.start() print(f"System Running.") print(f"RX: {RX_IP}:{RX_PORT} | TX: {TARGET_IP}:{TARGET_PORT}") print("Press Ctrl+C to stop.") try: while True: time.sleep(1) except KeyboardInterrupt: print("\nStopping...") finally: scheduler.stop() network.stop() sys.exit(0) def main_gui(): """Graphical user interface mode""" # Enable quiet mode for scheduler from .core import scheduler as sched_module sched_module.QUIET_MODE = True print("--------------------------------------------------") print(" PyBusMonitor1553 - Active Controller (GUI Mode)") print("--------------------------------------------------") # Initialize Tkinter root = tk.Tk() app = BusMonitorApp(root) # Initialize Components dispatcher = MessageDispatcher() network = UdpHandler(rx_ip=RX_IP, rx_port=RX_PORT) radar_ctrl = RadarController() scheduler = TrafficScheduler(network, radar_ctrl, TARGET_IP, TARGET_PORT) # Counters for status bar tx_count = [0] rx_count = [0] # Define the callback for received messages def on_packet(data, addr): header, messages = dispatcher.parse_packet(data) rx_count[0] += 1 if messages: for msg in messages: if msg.IS_TRANSMIT: msg_name = msg.__class__.__name__ # Convert MsgB6 -> B6, MsgA1 -> A1, etc. short_name = msg_name.replace("Msg", "") # Extract raw words if available raw_words = [] if hasattr(msg, '_raw_data') and msg._raw_data: import struct data_bytes = msg._raw_data for i in range(0, len(data_bytes), 2): if i + 1 < len(data_bytes): word = struct.unpack('