diff options
author | Evgeny Zinoviev <me@ch1p.io> | 2023-06-06 18:54:31 +0300 |
---|---|---|
committer | Evgeny Zinoviev <me@ch1p.io> | 2023-06-06 18:54:31 +0300 |
commit | 1835e0a7b0bf0d5f31985b3e6b1ca33139441e90 (patch) | |
tree | 4d0a268f0b6bc35f8f7eb85fc425afcc9edda2ef /src/home/mqtt | |
parent | 940d88d301b3f4fca51f5958b4c7562d977e1905 (diff) |
multiple fixes
Diffstat (limited to 'src/home/mqtt')
-rw-r--r-- | src/home/mqtt/_node.py | 7 | ||||
-rw-r--r-- | src/home/mqtt/module/ota.py | 44 |
2 files changed, 34 insertions, 17 deletions
diff --git a/src/home/mqtt/_node.py b/src/home/mqtt/_node.py index 9e39911..f34da0c 100644 --- a/src/home/mqtt/_node.py +++ b/src/home/mqtt/_node.py @@ -1,7 +1,7 @@ import paho.mqtt.client as mqtt from .mqtt import MqttBase -from typing import List +from typing import List, Optional from ._module import MqttModule from ._payload import MqttPayload @@ -92,7 +92,10 @@ class MqttNode(MqttBase): self._module_subscriptions[topic] = module self._client.subscribe(f'hk/{self._node_id}/{topic}', qos) - def publish(self, topic: str, payload: bytes, qos: int = 1): + def publish(self, + topic: str, + payload: bytes, + qos: int = 1): self._client.publish(f'hk/{self._node_id}/{topic}', payload, qos) self._client.loop_write() diff --git a/src/home/mqtt/module/ota.py b/src/home/mqtt/module/ota.py index 86d6839..5a1a309 100644 --- a/src/home/mqtt/module/ota.py +++ b/src/home/mqtt/module/ota.py @@ -41,27 +41,41 @@ class OtaPayload(MqttPayload): class MqttOtaModule(MqttModule): + _ota_request: Optional[tuple[str, str, int]] + _mqtt_ref: Optional[MqttNode] + + def __init__(self, *args, **kwargs): + super().__init__(*args, **kwargs) + self._ota_request = None + self._mqtt_ref = None + def init(self, mqtt: MqttNode): mqtt.subscribe_module("otares", self) + self._mqtt_ref = mqtt + + if self._ota_request is not None: + secret, filename, qos = self._ota_request + self._ota_request = None + self.do_push_ota(secret, filename, qos) + def handle_payload(self, mqtt: MqttNode, topic: str, payload: bytes) -> Optional[MqttPayload]: if topic == 'otares': message = OtaResultPayload.unpack(payload) self._logger.debug(message) return message - # def push_ota(self, - # node_id, - # filename: str, - # publish_callback: callable, - # qos: int): - # device = next(d for d in self._devices if d.id == device_id) - # assert device.secret is not None, 'device secret not specified' - # - # self._ota_publish_callback = publish_callback - # payload = OtaPayload(secret=device.secret, filename=filename) - # publish_result = self._client.publish(f'hk/{device.id}/{self.TOPIC_LEAF}/admin/ota', - # payload=payload.pack(), - # qos=qos) - # self._ota_mid = publish_result.mid - # self._client.loop_write()
\ No newline at end of file + def do_push_ota(self, secret: str, filename: str, qos: int): + payload = OtaPayload(secret=secret, filename=filename) + self._mqtt_ref.publish('ota', + payload=payload.pack(), + qos=qos) + + def push_ota(self, + secret: str, + filename: str, + qos: int): + if not self._initialized: + self._ota_request = (secret, filename, qos) + else: + self.do_push_ota(secret, filename, qos)
\ No newline at end of file |