From 1162a9cc5393cf614654ba763ccfd0298df0765f Mon Sep 17 00:00:00 2001 From: Evgeny Zinoviev Date: Sun, 1 Jan 2023 18:47:56 +0300 Subject: mqtt relay: support OTAResult payload --- src/home/mqtt/payload/relay.py | 7 +++++++ src/home/mqtt/relay.py | 5 ++++- src/relay_mqtt_util.py | 23 +++++++++++++++++------ 3 files changed, 28 insertions(+), 7 deletions(-) diff --git a/src/home/mqtt/payload/relay.py b/src/home/mqtt/payload/relay.py index debc2c8..1a38201 100644 --- a/src/home/mqtt/payload/relay.py +++ b/src/home/mqtt/payload/relay.py @@ -62,6 +62,12 @@ class PowerPayload(MQTTPayload): state: bool +class OTAResultPayload(MQTTPayload): + FORMAT = '=BB' + result: int + error_code: int + + class OTAPayload(MQTTPayload): secret: str filename: str @@ -87,3 +93,4 @@ class OTAPayload(MQTTPayload): # secret = buf[:12].decode() # filename = buf[12:].decode() # return OTAPayload(secret=secret, filename=filename) + diff --git a/src/home/mqtt/relay.py b/src/home/mqtt/relay.py index 97a1689..2094b10 100644 --- a/src/home/mqtt/relay.py +++ b/src/home/mqtt/relay.py @@ -8,7 +8,8 @@ from .payload.relay import ( InitialStatPayload, StatPayload, PowerPayload, - OTAPayload + OTAPayload, + OTAResultPayload ) @@ -76,6 +77,8 @@ class MQTTRelay(MQTTBase): message = InitialStatPayload.unpack(msg.payload) elif subtopic == 'power': message = PowerPayload.unpack(msg.payload) + elif subtopic == 'otares': + message = OTAResultPayload.unpack(msg.payload) if message and self._message_callback: self._message_callback(device_id, message) diff --git a/src/relay_mqtt_util.py b/src/relay_mqtt_util.py index 430af2c..29a0532 100755 --- a/src/relay_mqtt_util.py +++ b/src/relay_mqtt_util.py @@ -5,16 +5,27 @@ from argparse import ArgumentParser from home.config import config from home.mqtt import MQTTRelay, MQTTRelayDevice from home.mqtt.payload import MQTTPayload -from home.mqtt.payload.relay import InitialStatPayload, StatPayload +from home.mqtt.payload.relay import ( + InitialStatPayload, StatPayload, OTAResultPayload +) mqtt_relay: Optional[MQTTRelay] = None -def on_mqtt_message(device_id, message: MQTTPayload): - if isinstance(message, InitialStatPayload) or isinstance(message, StatPayload): - message = f'[{device_id}] state={message.flags.state} rssi={message.rssi}' - if isinstance(message, InitialStatPayload): - message += f' fw={message.fw_version}' +def on_mqtt_message(device_id, p: MQTTPayload): + message = None + + if isinstance(p, InitialStatPayload) or isinstance(p, StatPayload): + message = f'[stat] state={"on" if p.flags.state else "off"}' + message += f' rssi={p.rssi}' + message += f' free_heap={p.free_heap}' + if isinstance(p, InitialStatPayload): + message += f' fw={p.fw_version}' + + elif isinstance(p, OTAResultPayload): + message = f'[otares] result={p.result} error_code={p.error_code}' + + if message: print(message) -- cgit v1.2.3