68 lines
2.2 KiB
Python
68 lines
2.2 KiB
Python
import time
|
|
import sys
|
|
import os
|
|
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
|
|
|
|
#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():
|
|
print("--------------------------------------------------")
|
|
print(" PyBusMonitor1553 - Active Controller")
|
|
print("--------------------------------------------------")
|
|
|
|
# 1. Initialize Components
|
|
dispatcher = MessageDispatcher()
|
|
network = UdpHandler(rx_ip=RX_IP, rx_port=RX_PORT)
|
|
|
|
# 2. Initialize Radar Logic Controller
|
|
# This sets up the default A1, A2, etc. messages
|
|
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:
|
|
# Filter for B-messages (RT -> BC, IS_TRANSMIT=True)
|
|
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()
|
|
|
|
# Send defaults immediately via scheduler loop
|
|
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:
|
|
# Here we could accept user input to call radar_ctrl.set_master_mode(...)
|
|
time.sleep(1)
|
|
except KeyboardInterrupt:
|
|
print("\nStopping...")
|
|
finally:
|
|
scheduler.stop()
|
|
network.stop()
|
|
sys.exit(0)
|
|
|
|
if __name__ == "__main__":
|
|
main() |