Made command handlers talk to the serial port properly

This commit is contained in:
Jono Targett 2026-03-15 19:13:38 +10:30
parent 81791d67a9
commit 0cb46fad9b
3 changed files with 48 additions and 4 deletions

View File

@ -16,12 +16,12 @@ class Command:
def validate(self, o) -> bool: def validate(self, o) -> bool:
return self._validator.is_valid(o) return self._validator.is_valid(o)
def __call__(self, args): def __call__(self, handler_instance, args):
if not self.validate(args): if not self.validate(args):
raise ValueError(f"Invalid arguments for command '{self.name}'") raise ValueError(f"Invalid arguments for command '{self.name}'")
if self.handler is None: if self.handler is None:
raise RuntimeError(f"No handler bound for command '{self.name}'") raise RuntimeError(f"No handler bound for command '{self.name}'")
return self.handler(args) return self.handler(handler_instance, args)
def command(schema, **kwargs): def command(schema, **kwargs):

View File

@ -76,7 +76,7 @@ class MQTTHandler:
try: try:
command = self.get_available_commands()[command_name] command = self.get_available_commands()[command_name]
argument = json.loads(payload) argument = json.loads(payload)
result = await command(argument) result = await command(self, argument)
await respond(True, result) await respond(True, result)
except json.decoder.JSONDecodeError as e: except json.decoder.JSONDecodeError as e:

View File

@ -78,5 +78,49 @@ class UBXHandler(MQTTHandler):
await self.mqtt_client.publish(topic, value, qos=1, retain=True) await self.mqtt_client.publish(topic, value, qos=1, retain=True)
@command({"type": "number"}, description="An example command") @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}") 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