2021-02-16 20:37:51 +01:00
|
|
|
import asyncio
|
2019-08-16 21:36:55 +02:00
|
|
|
import logging
|
2021-01-22 23:25:20 +01:00
|
|
|
from abc import abstractmethod, ABCMeta
|
2019-01-05 20:43:48 +01:00
|
|
|
from cbpi.api.extension import CBPiExtension
|
2023-03-06 20:11:53 +01:00
|
|
|
from cbpi.api.dataclasses import DataType
|
2019-01-05 20:43:48 +01:00
|
|
|
|
2021-02-16 20:37:51 +01:00
|
|
|
|
2021-02-10 07:38:55 +01:00
|
|
|
from cbpi.api.base import CBPiBase
|
2019-01-05 20:43:48 +01:00
|
|
|
|
2021-02-10 07:38:55 +01:00
|
|
|
class CBPiSensor(CBPiBase, metaclass=ABCMeta):
|
2021-01-22 23:25:20 +01:00
|
|
|
|
|
|
|
def __init__(self, cbpi, id, props):
|
|
|
|
self.cbpi = cbpi
|
|
|
|
self.id = id
|
|
|
|
self.props = props
|
|
|
|
self.logger = logging.getLogger(__file__)
|
|
|
|
self.data_logger = None
|
|
|
|
self.state = False
|
|
|
|
self.running = False
|
2023-03-06 20:11:53 +01:00
|
|
|
self.datatype=DataType.VALUE
|
2024-05-28 21:51:17 +02:00
|
|
|
self.inrange = True
|
|
|
|
self.temprange = 0
|
|
|
|
self.kettle = None
|
|
|
|
self.fermenter = None
|
2021-01-22 23:25:20 +01:00
|
|
|
|
|
|
|
def init(self):
|
|
|
|
pass
|
|
|
|
|
|
|
|
def log_data(self, value):
|
|
|
|
self.cbpi.log.log_data(self.id, value)
|
|
|
|
|
|
|
|
def get_state(self):
|
|
|
|
pass
|
|
|
|
|
|
|
|
def get_value(self):
|
|
|
|
pass
|
|
|
|
|
|
|
|
def get_unit(self):
|
|
|
|
pass
|
|
|
|
|
2024-05-28 21:51:17 +02:00
|
|
|
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
|
2021-01-24 22:14:57 +01:00
|
|
|
try:
|
2024-05-28 21:51:17 +02:00
|
|
|
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))
|
2022-01-14 17:11:16 +01:00
|
|
|
if mqtt:
|
2024-05-28 21:51:17 +02:00
|
|
|
self.cbpi.push_update("cbpi/sensordata/{}".format(self.id), dict(id=self.id, value=value, datatype=self.datatype.value, inrange=self.inrange), retain=True)
|
2022-01-03 12:47:20 +01:00
|
|
|
# self.cbpi.push_update("cbpi/sensor/{}/udpate".format(self.id), dict(id=self.id, value=value), retain=True)
|
2021-01-24 22:14:57 +01:00
|
|
|
except:
|
2023-03-06 20:11:53 +01:00
|
|
|
logging.error("Failed to push sensor update for sensor {}".format(self.id))
|
2021-01-24 22:14:57 +01:00
|
|
|
|
2021-01-22 23:25:20 +01:00
|
|
|
async def start(self):
|
2021-02-16 20:37:51 +01:00
|
|
|
pass
|
2021-01-22 23:25:20 +01:00
|
|
|
|
|
|
|
async def stop(self):
|
2021-02-16 20:37:51 +01:00
|
|
|
pass
|
|
|
|
|
|
|
|
async def on_start(self):
|
|
|
|
pass
|
|
|
|
|
|
|
|
async def on_stop(self):
|
|
|
|
pass
|
|
|
|
|
|
|
|
async def run(self):
|
|
|
|
pass
|
|
|
|
|
|
|
|
async def _run(self):
|
|
|
|
|
|
|
|
try:
|
|
|
|
await self.on_start()
|
|
|
|
self.cancel_reason = await self.run()
|
|
|
|
except asyncio.CancelledError as e:
|
|
|
|
pass
|
|
|
|
finally:
|
|
|
|
await self.on_stop()
|