From d2c09e04e2699170f2b87cc3dc1037866285c340 Mon Sep 17 00:00:00 2001 From: Jochen Vothknecht <jochen3120@gmail.com> Date: Wed, 15 Dec 2021 08:25:47 +0100 Subject: [PATCH] Moar MQTT stuff --- SiliconTorch/Device.py | 38 ++++++++++++++++++++------------------ 1 file changed, 20 insertions(+), 18 deletions(-) diff --git a/SiliconTorch/Device.py b/SiliconTorch/Device.py index 14dfe8f..48c1579 100644 --- a/SiliconTorch/Device.py +++ b/SiliconTorch/Device.py @@ -1,4 +1,5 @@ +from MQTT import Topic from typing import Any, Callable @@ -6,6 +7,8 @@ from typing import Any, Callable # TODO: dev is stalled! # at first, we'll design easier things to not loose focus on the project # Just use simple properties and triggers to get the same (write-only) behavior +# +# TODO ^2: use functools.wraps to copy the annotations of decorated functions class MQTTProperty: def __init__(self, name: str = None, initialValue: Any = None, getConvert: Callable[[str], Any] = None, setConvert: Callable[[Any], str] = None, readOnly = False): @@ -55,48 +58,47 @@ class MQTTProperty: class MQTTTrigger: + # TODO: should we check message's type instead to decide if it is binary? evaluate! def __init__(self, topic: str = None, message: str = None, binaryMessage: bytes = None): if topic is not None: - self.topic = topic + self.__topic = Topic(topic) else: raise AttributeError('topic is mandatory') # TODO: correct exception…? if binaryMessage is not None: - self.message = binaryMessage + self.__message = binaryMessage elif message is not None: - self.message = bytes(message, 'UTF-8') + self.__message = bytes(message, 'UTF-8') else: - self.message = b'' - - - - - - - - - - - + self.__message = b'' + # ################### + # ### TODO: !!! ### + # ################### + def __call__(self, func) -> Callable[[Any], Any]: + self.__function = func + return self class Device: - def __init__(self, torch, name, online = True): + def __init__(self, torch, name: str, online: bool = True): self._torch = torch - self._name = name + self.__name = name self.__online = online @property def name(self): - return self._name + return self.__name @property def online(self): return self.__online + @MQTTTrigger(topic='{NameSpace}/{DeviceName}/command', message='restart') + def restart(self): pass + class Extension: -- GitLab