From 0cb46fad9b165d19904e981153584f9d0936554d Mon Sep 17 00:00:00 2001 From: Jono Targett Date: Sun, 15 Mar 2026 19:13:38 +1030 Subject: [PATCH] Made command handlers talk to the serial port properly --- command.py | 4 ++-- handler.py | 2 +- ubxhandler.py | 46 +++++++++++++++++++++++++++++++++++++++++++++- 3 files changed, 48 insertions(+), 4 deletions(-) diff --git a/command.py b/command.py index fbb7ba8..55e21ca 100644 --- a/command.py +++ b/command.py @@ -16,12 +16,12 @@ class Command: def validate(self, o) -> bool: return self._validator.is_valid(o) - def __call__(self, args): + def __call__(self, handler_instance, args): if not self.validate(args): raise ValueError(f"Invalid arguments for command '{self.name}'") if self.handler is None: raise RuntimeError(f"No handler bound for command '{self.name}'") - return self.handler(args) + return self.handler(handler_instance, args) def command(schema, **kwargs): diff --git a/handler.py b/handler.py index 6fae5df..6a0b777 100644 --- a/handler.py +++ b/handler.py @@ -76,7 +76,7 @@ class MQTTHandler: try: command = self.get_available_commands()[command_name] argument = json.loads(payload) - result = await command(argument) + result = await command(self, argument) await respond(True, result) except json.decoder.JSONDecodeError as e: diff --git a/ubxhandler.py b/ubxhandler.py index dff8c90..a5e951e 100644 --- a/ubxhandler.py +++ b/ubxhandler.py @@ -78,5 +78,49 @@ class UBXHandler(MQTTHandler): await self.mqtt_client.publish(topic, value, qos=1, retain=True) @command({"type": "number"}, description="An example command") - async def example_cmd(args): + async def example_cmd(self, args): print(f"Executing command with args {args}") + + @command( + { + "type": "object", + "properties": { + "portID": { + "type": "integer", + }, + "baudRate": { + "type": "integer", + "enum": [ + 110, + 300, + 600, + 1200, + 2400, + 4800, + 9600, + 14400, + 19200, + 38400, + 57600, + 115200, + 230400, + 460800, + 921600, + ], + }, + }, + "required": ["portID", "baudRate"], + "additionalProperties": False, + }, + description="Reconfigure the serial port for the UBX simulator.", + ) + async def configure_port(self, args): + message = pyubx2.UBXMessage( + "CFG", + "CFG-PRT", + pyubx2.ubxtypes_core.SET, + portID=args["portID"], + baudRate=args["baudRate"], + ) + num_bytes = await self.serial_port.write_async(message.serialize()) + return num_bytes