From f594ed04a0e5e04275a8d55d738056f3f53f114d Mon Sep 17 00:00:00 2001 From: avollkopf <43980694+avollkopf@users.noreply.github.com> Date: Tue, 28 May 2024 21:51:17 +0200 Subject: [PATCH] added rangewarning for testing (different color in dashboard -> dev version of ui required) --- cbpi/__init__.py | 2 +- cbpi/api/dataclasses.py | 4 ++- cbpi/api/sensor.py | 34 +++++++++++++++++++++++--- cbpi/extension/httpsensor/__init__.py | 13 ++++++---- cbpi/extension/mqtt_sensor/__init__.py | 11 ++++++--- cbpi/extension/onewire/__init__.py | 14 +++++++---- cbpi/extension/onewire/config.yaml | 2 +- requirements.txt | 4 +-- setup.py | 4 +-- 9 files changed, 63 insertions(+), 25 deletions(-) diff --git a/cbpi/__init__.py b/cbpi/__init__.py index f4f5438..c772fb0 100644 --- a/cbpi/__init__.py +++ b/cbpi/__init__.py @@ -1,3 +1,3 @@ -__version__ = "4.4.1.a10" +__version__ = "4.4.1.a11" __codename__ = "Yeast Starter" diff --git a/cbpi/api/dataclasses.py b/cbpi/api/dataclasses.py index bd9f411..1c403be 100644 --- a/cbpi/api/dataclasses.py +++ b/cbpi/api/dataclasses.py @@ -79,7 +79,9 @@ class Sensor: type: str = None instance: str = None datatype: DataType = DataType.VALUE - + inrange: bool = True + temp_range: float = 0 + def __str__(self): return "name={} props={}, state={}".format(self.name, self.props, self.state) def to_dict(self): diff --git a/cbpi/api/sensor.py b/cbpi/api/sensor.py index 9874ee8..341c72e 100644 --- a/cbpi/api/sensor.py +++ b/cbpi/api/sensor.py @@ -18,6 +18,10 @@ class CBPiSensor(CBPiBase, metaclass=ABCMeta): self.state = False self.running = False self.datatype=DataType.VALUE + self.inrange = True + self.temprange = 0 + self.kettle = None + self.fermenter = None def init(self): pass @@ -34,12 +38,34 @@ class CBPiSensor(CBPiBase, metaclass=ABCMeta): def get_unit(self): pass - def push_update(self, value, mqtt = True): - + def checkrange(self, value): + # if Kettle and fermenter are selected, range check is deactivated + if self.kettle is not None and self.fermenter is not None: + return True try: - self.cbpi.ws.send(dict(topic="sensorstate", id=self.id, value=value, datatype=self.datatype.value)) + if self.kettle is not None: + target_temp=float(self.kettle.target_temp) + if self.fermenter is not None: + target_temp=float(self.fermenter.target_temp) + + diff=abs(target_temp-value) + if diff>self.temprange: + return False + else: + return True + except Exception as e: + return True + + + def push_update(self, value, mqtt = True): + if self.temprange !=0: + self.inrange = self.checkrange(value) + else: + self.inrange = True + try: + self.cbpi.ws.send(dict(topic="sensorstate", id=self.id, value=value, datatype=self.datatype.value, inrange=self.inrange)) if mqtt: - self.cbpi.push_update("cbpi/sensordata/{}".format(self.id), dict(id=self.id, value=value, datatype=self.datatype.value), retain=True) + self.cbpi.push_update("cbpi/sensordata/{}".format(self.id), dict(id=self.id, value=value, datatype=self.datatype.value, inrange=self.inrange), retain=True) # self.cbpi.push_update("cbpi/sensor/{}/udpate".format(self.id), dict(id=self.id, value=value), retain=True) except: logging.error("Failed to push sensor update for sensor {}".format(self.id)) diff --git a/cbpi/extension/httpsensor/__init__.py b/cbpi/extension/httpsensor/__init__.py index c37c11e..2f50f53 100644 --- a/cbpi/extension/httpsensor/__init__.py +++ b/cbpi/extension/httpsensor/__init__.py @@ -12,10 +12,12 @@ cache = {} @parameters([Property.Text(label="Key", configurable=True, description="Http Key"), Property.Number(label="Timeout", configurable="True",unit="sec",description="Timeout in seconds to send notification (default:60 | deactivated: 0)"), - Property.Kettle(label="Kettle", description="Reduced logging if Kettle is inactive (only Kettle or Fermenter to be selected)"), - Property.Fermenter(label="Fermenter", description="Reduced logging in seconds if Fermenter is inactive (only Kettle or Fermenter to be selected)"), - Property.Number(label="ReducedLogging", configurable=True, description="Reduced logging frequency in seconds if selected Kettle or Fermenter is inactive (default: 60 sec | disabled: 0)")]) - + Property.Kettle(label="Kettle", description="Reduced logging if Kettle is inactive / range warning in dashboard (only Kettle or Fermenter to be selected)"), + Property.Fermenter(label="Fermenter", description="Reduced logging in seconds if Fermenter is inactive / range warning in dashboard (only Kettle or Fermenter to be selected)"), + Property.Number(label="ReducedLogging", configurable=True, description="Reduced logging frequency in seconds if selected Kettle or Fermenter is inactive (default: 60 sec | disabled: 0)"), + Property.Number(label="TempRange", configurable=True, unit="degree", + description="Temp range in degree between reading and target temp of fermenter/kettle. Larger difference shows different color in dashboard (default:0 | deactivated: 0)") + ]) class HTTPSensor(CBPiSensor): def __init__(self, cbpi, id, props): super(HTTPSensor, self).__init__(cbpi, id, props) @@ -35,11 +37,12 @@ class HTTPSensor(CBPiSensor): self.kettleid=self.props.get("Kettle", None) self.fermenterid=self.props.get("Fermenter", None) + self.temprange=float(self.props.get("TempRange", 0)) self.reducedlogging = True if self.kettleid or self.fermenterid else False if self.kettleid is not None and self.fermenterid is not None: self.reducedlogging=False - self.cbpi.notify("HTTPSensor", "Sensor '" + str(self.sensor.name) + "' cant't have Fermenter and Kettle defined for reduced logging.", NotificationType.WARNING, action=[NotificationAction("OK", self.Confirm)]) + self.cbpi.notify("HTTPSensor", "Sensor '" + str(self.sensor.name) + "' cant't have Fermenter and Kettle defined for reduced logging / range warning", NotificationType.WARNING, action=[NotificationAction("OK", self.Confirm)]) async def Confirm(self, **kwargs): self.nextchecktime = time.time() + self.timeout diff --git a/cbpi/extension/mqtt_sensor/__init__.py b/cbpi/extension/mqtt_sensor/__init__.py index c5b0c0b..68db819 100644 --- a/cbpi/extension/mqtt_sensor/__init__.py +++ b/cbpi/extension/mqtt_sensor/__init__.py @@ -11,11 +11,13 @@ from datetime import datetime @parameters([Property.Text(label="Topic", configurable=True, description="MQTT Topic"), Property.Text(label="PayloadDictionary", configurable=True, default_value="", description="Where to find msg in payload, leave blank for raw payload"), - Property.Kettle(label="Kettle", description="Reduced logging if Kettle is inactive (only Kettle or Fermenter to be selected)"), - Property.Fermenter(label="Fermenter", description="Reduced logging in seconds if Fermenter is inactive (only Kettle or Fermenter to be selected)"), + Property.Kettle(label="Kettle", description="Reduced logging if Kettle is inactive / range warning in dashboard (only Kettle or Fermenter to be selected)"), + Property.Fermenter(label="Fermenter", description="Reduced logging if Fermenter is inactive / range warning in dashboard (only Kettle or Fermenter to be selected)"), Property.Number(label="ReducedLogging", configurable=True, description="Reduced logging frequency in seconds if selected Kettle or Fermenter is inactive (default:60 sec | 0 disabled)"), Property.Number(label="Timeout", configurable=True, unit="sec", - description="Timeout in seconds to send notification (default:60 | deactivated: 0)")]) + description="Timeout in seconds to send notification (default:60 | deactivated: 0)"), + Property.Number(label="TempRange", configurable=True, unit="degree", + description="Temp range in degree between reading and target temp of fermenter/kettle. Larger difference shows different color in dashboard (default:0 | deactivated: 0)")]) class MQTTSensor(CBPiSensor): def __init__(self, cbpi, id, props): @@ -27,6 +29,7 @@ class MQTTSensor(CBPiSensor): self.subscribed = self.cbpi.satellite.subscribe(self.Topic, self.on_message) self.value: float = 999 self.timeout=int(self.props.get("Timeout", 60)) + self.temprange=float(self.props.get("TempRange", 0)) self.starttime = time.time() self.notificationsend = False self.nextchecktime=self.starttime+self.timeout @@ -42,7 +45,7 @@ class MQTTSensor(CBPiSensor): if self.kettleid is not None and self.fermenterid is not None: self.reducedlogging=False - self.cbpi.notify("MQTTSensor", "Sensor '" + str(self.sensor.name) + "' cant't have Fermenter and Kettle defined for reduced logging.", NotificationType.WARNING, action=[NotificationAction("OK", self.Confirm)]) + self.cbpi.notify("MQTTSensor", "Sensor '" + str(self.sensor.name) + "' cant't have Fermenter and Kettle defined for reduced logging / range warning.", NotificationType.WARNING, action=[NotificationAction("OK", self.Confirm)]) async def Confirm(self, **kwargs): self.nextchecktime = time.time() + self.timeout diff --git a/cbpi/extension/onewire/__init__.py b/cbpi/extension/onewire/__init__.py index 56f336f..309e410 100644 --- a/cbpi/extension/onewire/__init__.py +++ b/cbpi/extension/onewire/__init__.py @@ -52,10 +52,13 @@ class ReadThread (threading.Thread): @parameters([Property.Select(label="Sensor", options=getSensors()), Property.Number(label="offset",configurable = True, default_value = 0, description="Sensor Offset (Default is 0)"), Property.Select(label="Interval", options=[1,5,10,30,60], description="Interval in Seconds"), - Property.Kettle(label="Kettle", description="Reduced logging if Kettle is inactive (only Kettle or Fermenter to be selected)"), - Property.Fermenter(label="Fermenter", description="Reduced logging in seconds if Fermenter is inactive (only Kettle or Fermenter to be selected)"), - Property.Number(label="ReducedLogging", configurable=True, description="Reduced logging frequency in seconds if selected Kettle or Fermenter is inactive (default: 60 sec | disabled: 0)") - ]) + Property.Kettle(label="Kettle", description="Reduced logging if Kettle is inactive / range warning in dashboard(only Kettle or Fermenter to be selected)"), + Property.Fermenter(label="Fermenter", description="Reduced logging in seconds if Fermenter is inactive / range warning in dashboard (only Kettle or Fermenter to be selected)"), + Property.Number(label="ReducedLogging", configurable=True, description="Reduced logging frequency in seconds if selected Kettle or Fermenter is inactive (default: 60 sec | disabled: 0)"), + Property.Number(label="TempRange", configurable=True, unit="degree", + description="Temp range in degree between reading and target temp of fermenter/kettle. Larger difference shows different color in dashboard (default:0 | deactivated: 0)") + ]) + class OneWire(CBPiSensor): def __init__(self, cbpi, id, props): @@ -72,6 +75,7 @@ class OneWire(CBPiSensor): if self.reducedfrequency < 0: self.reducedfrequency = 0 self.lastlog=0 + self.temprange=float(self.props.get("TempRange", 0)) self.sensor=self.get_sensor(self.id) self.kettleid=self.props.get("Kettle", None) self.fermenterid=self.props.get("Fermenter", None) @@ -79,7 +83,7 @@ class OneWire(CBPiSensor): if self.kettleid is not None and self.fermenterid is not None: self.reducedlogging=False - self.cbpi.notify("OneWire Sensor", "Sensor '" + str(self.sensor.name) + "' cant't have Fermenter and Kettle defined for reduced logging.", NotificationType.WARNING, action=[NotificationAction("OK", self.Confirm)]) + self.cbpi.notify("OneWire Sensor", "Sensor '" + str(self.sensor.name) + "' cant't have Fermenter and Kettle defined for reduced logging / range warning.", NotificationType.WARNING, action=[NotificationAction("OK", self.Confirm)]) if (self.reducedfrequency != 0) and (self.interval >= self.reducedfrequency): self.reducedlogging=False self.cbpi.notify("OneWire Sensor", "Sensor '" + str(self.sensor.name) + "' has shorter or equal 'reduced logging' compared to regular interval.", NotificationType.WARNING, action=[NotificationAction("OK", self.Confirm)]) diff --git a/cbpi/extension/onewire/config.yaml b/cbpi/extension/onewire/config.yaml index 7f0aa15..961686c 100644 --- a/cbpi/extension/onewire/config.yaml +++ b/cbpi/extension/onewire/config.yaml @@ -1,3 +1,3 @@ -name: DummySensor +name: OneWireSensor version: 4 active: true \ No newline at end of file diff --git a/requirements.txt b/requirements.txt index 46df2da..806ce15 100644 --- a/requirements.txt +++ b/requirements.txt @@ -10,7 +10,7 @@ aiojobs==1.2.1 aiosqlite==0.17.0 cryptography==42.0.5 pyopenssl==24.1.0 -requests==2.31.0 +requests==2.32.2 voluptuous==0.14.2 pyfiglet==1.0.2 pandas==2.2.2 @@ -20,7 +20,7 @@ numpy==1.26.4 cbpi4gui click==8.1.7 importlib_metadata==4.11.1 -aiomqtt==2.0.1 +aiomqtt==2.1.0 psutil==5.9.8 zipp>=0.5 colorama==0.4.6 diff --git a/setup.py b/setup.py index c6cafe3..266bf84 100644 --- a/setup.py +++ b/setup.py @@ -50,13 +50,13 @@ setup(name='cbpi4', "aiosqlite==0.17.0", "cryptography==42.0.5", "pyopenssl==24.1.0", - "requests==2.31.0", + "requests==2.32.2", "voluptuous==0.14.2", "pyfiglet==1.0.2", 'click==8.1.7', 'shortuuid==1.0.13', 'tabulate==0.9.0', - 'aiomqtt==2.0.1', + 'aiomqtt==2.1.0', 'inquirer==3.2.4', 'colorama==0.4.6', 'psutil==5.9.8',