Made command handlers talk to the serial port properly
This commit is contained in:
parent
81791d67a9
commit
0cb46fad9b
@ -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):
|
||||
|
||||
@ -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:
|
||||
|
||||
@ -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
|
||||
|
||||
Loading…
Reference in New Issue
Block a user