From 51136aef887180d472f11b9be0227fb7257de9c3 Mon Sep 17 00:00:00 2001 From: avollkopf <43980694+avollkopf@users.noreply.github.com> Date: Mon, 3 Jan 2022 12:47:20 +0100 Subject: [PATCH] Unification of mqtt data -> sensor values are updated under sensordata/{sensorid} One topic for each sensor to keep data small (esp compatibility) actor, kettle, sensor, fermenter, steps are updated also for each id individually to keep packets small e.g. actorupdate/{actorid} This was proposed by Innuendo to ensure compatibility with the ESP based MQTTDevice --- cbpi/__init__.py | 2 +- cbpi/api/sensor.py | 3 ++- cbpi/controller/actor_controller.py | 8 +++---- cbpi/controller/basic_controller2.py | 3 ++- cbpi/controller/fermentation_controller.py | 3 ++- cbpi/controller/step_controller.py | 4 ++-- .../extension/FermenterHysteresis/__init__.py | 24 ++++++++++++++----- 7 files changed, 31 insertions(+), 16 deletions(-) diff --git a/cbpi/__init__.py b/cbpi/__init__.py index cae6342..632deb3 100644 --- a/cbpi/__init__.py +++ b/cbpi/__init__.py @@ -1 +1 @@ -__version__ = "4.0.1.a4" +__version__ = "4.0.1.a5" diff --git a/cbpi/api/sensor.py b/cbpi/api/sensor.py index a71b75a..76182d5 100644 --- a/cbpi/api/sensor.py +++ b/cbpi/api/sensor.py @@ -35,7 +35,8 @@ class CBPiSensor(CBPiBase, metaclass=ABCMeta): def push_update(self, value): try: self.cbpi.ws.send(dict(topic="sensorstate", id=self.id, value=value)) - self.cbpi.push_update("cbpi/sensor/{}/udpate".format(self.id), dict(id=self.id, value=value), retain=True) + self.cbpi.push_update("cbpi/sensordata/{}".format(self.id), dict(id=self.id, value=value), retain=True) +# self.cbpi.push_update("cbpi/sensor/{}/udpate".format(self.id), dict(id=self.id, value=value), retain=True) except: logging.error("Faild to push sensor update") diff --git a/cbpi/controller/actor_controller.py b/cbpi/controller/actor_controller.py index 75bd623..edb559d 100644 --- a/cbpi/controller/actor_controller.py +++ b/cbpi/controller/actor_controller.py @@ -21,7 +21,7 @@ class ActorController(BasicController): if item.instance.state is False: await item.instance.on(power) await self.push_udpate() - self.cbpi.push_update("cbpi/actor/"+id, item.to_dict(), True) + self.cbpi.push_update("cbpi/actorupdate/{}".format(id), item.to_dict(), True) else: await self.set_power(id, power) @@ -34,7 +34,7 @@ class ActorController(BasicController): if item.instance.state is True: await item.instance.off() await self.push_udpate() - self.cbpi.push_update("cbpi/actor/"+id, item.to_dict()) + self.cbpi.push_update("cbpi/actorupdate/{}".format(id), item.to_dict()) except Exception as e: logging.error("Failed to switch on Actor {} {}".format(id, e), True) @@ -43,7 +43,7 @@ class ActorController(BasicController): item = self.find_by_id(id) instance = item.get("instance") await instance.toggle() - self.cbpi.push_update("cbpi/actor/update", item.to_dict()) + self.cbpi.push_update("cbpi/actorupdate/{}".format(id), item.to_dict()) except Exception as e: logging.error("Failed to toggle Actor {} {}".format(id, e)) @@ -59,6 +59,6 @@ class ActorController(BasicController): item = self.find_by_id(id) item.power = round(power) await self.push_udpate() - self.cbpi.push_update("cbpi/actor/"+id, item.to_dict()) + self.cbpi.push_update("cbpi/actorupdate/{}".format(id), item.to_dict()) except Exception as e: logging.error("Failed to update Actor {} {}".format(id, e)) diff --git a/cbpi/controller/basic_controller2.py b/cbpi/controller/basic_controller2.py index 022b329..18c2914 100644 --- a/cbpi/controller/basic_controller2.py +++ b/cbpi/controller/basic_controller2.py @@ -55,7 +55,8 @@ class BasicController: async def push_udpate(self): self.cbpi.ws.send(dict(topic=self.update_key, data=list(map(lambda item: item.to_dict(), self.data)))) - self.cbpi.push_update("cbpi/{}/update".format(self.update_key), list(map(lambda item: item.to_dict(), self.data))) + for item in self.data: + self.cbpi.push_update("cbpi/{}/{}".format(self.update_key,item.id), item.to_dict()) def find_by_id(self, id): return next((item for item in self.data if item.id == id), None) diff --git a/cbpi/controller/fermentation_controller.py b/cbpi/controller/fermentation_controller.py index 06abd36..d6ffd04 100644 --- a/cbpi/controller/fermentation_controller.py +++ b/cbpi/controller/fermentation_controller.py @@ -110,7 +110,8 @@ class FermentationController: def push_update(self): self.cbpi.ws.send(dict(topic=self.update_key, data=list(map(lambda item: item.to_dict(), self.data)))) - self.cbpi.push_update("cbpi/{}/update".format(self.update_key), list(map(lambda item: item.to_dict(), self.data))) + for item in self.data: + self.cbpi.push_update("cbpi/{}/{}".format(self.update_key,item.id), item.to_dict()) pass async def shutdown(self, app=None): diff --git a/cbpi/controller/step_controller.py b/cbpi/controller/step_controller.py index c5caaf9..8f0537a 100644 --- a/cbpi/controller/step_controller.py +++ b/cbpi/controller/step_controller.py @@ -256,8 +256,8 @@ class StepController: self.cbpi.ws.send(dict(topic="mash_profile_update", data=self.get_state())) else: self.cbpi.ws.send(dict(topic="step_update", data=list(map(lambda item: item.to_dict(), self.profile)))) - - self.cbpi.push_update(topic="cbpi/stepupdate", data=list(map(lambda item: item.to_dict(), self.profile))) + for item in self.profile: + self.cbpi.push_update(topic="cbpi/stepupdate/{}".format(item.id), data=(item.to_dict())) async def start_step(self,step): try: diff --git a/cbpi/extension/FermenterHysteresis/__init__.py b/cbpi/extension/FermenterHysteresis/__init__.py index 7fd0f6e..4ed1561 100644 --- a/cbpi/extension/FermenterHysteresis/__init__.py +++ b/cbpi/extension/FermenterHysteresis/__init__.py @@ -46,7 +46,8 @@ class FermenterAutostart(CBPiExtension): Property.Number(label="HeaterOffsetOff", configurable=True, description="Offset as decimal number when the heater is switched off. Should be smaller then 'HeaterOffsetOn'. For example a value of 1 switches off the heater if the current temperature is 1 degree below the target temperature"), Property.Number(label="CoolerOffsetOn", configurable=True, description="Offset as decimal number when the cooler is switched on. Should be greater then 'CoolerOffsetOff'. For example a value of 2 switches on the cooler if the current temperature is 2 degrees below the target temperature"), Property.Number(label="CoolerOffsetOff", configurable=True, description="Offset as decimal number when the cooler is switched off. Should be smaller then 'CoolerOffsetOn'. For example a value of 1 switches off the cooler if the current temperature is 1 degree below the target temperature"), - Property.Select(label="AutoStart", options=["Yes","No"],description="Autostart Fermenter on cbpi start")]) + Property.Select(label="AutoStart", options=["Yes","No"],description="Autostart Fermenter on cbpi start"), + Property.Sensor(label="sensor2",description="Optional Sensor for LCDisplay(e.g. iSpindle)")]) class FermenterHysteresis(CBPiFermenterLogic): @@ -60,27 +61,38 @@ class FermenterHysteresis(CBPiFermenterLogic): self.fermenter = self.get_fermenter(self.id) self.heater = self.fermenter.heater self.cooler = self.fermenter.cooler - + + heater = self.cbpi.actor.find_by_id(self.heater) + cooler = self.cbpi.actor.find_by_id(self.cooler) while self.running == True: sensor_value = float(self.get_sensor_value(self.fermenter.sensor).get("value")) target_temp = float(self.get_fermenter_target_temp(self.id)) + try: + heater_state = heater.instance.state + except: + heater_state= False + try: + cooler_state = cooler.instance.state + except: + cooler_state= False + if sensor_value + self.heater_offset_min <= target_temp: - if self.heater: + if self.heater and (heater_state == False): await self.actor_on(self.heater) if sensor_value + self.heater_offset_max >= target_temp: - if self.heater: + if self.heater and (heater_state == True): await self.actor_off(self.heater) if sensor_value >= self.cooler_offset_min + target_temp: - if self.cooler: + if self.cooler and (cooler_state == False): await self.actor_on(self.cooler) if sensor_value <= self.cooler_offset_max + target_temp: - if self.cooler: + if self.cooler and (cooler_state == True): await self.actor_off(self.cooler) await asyncio.sleep(1)