From 4d95017842d20b62560d33b75880e36a5e2d62cb Mon Sep 17 00:00:00 2001 From: avollkopf <43980694+avollkopf@users.noreply.github.com> Date: Sun, 10 Apr 2022 17:48:36 +0200 Subject: [PATCH] Added dummy pressure sensor for testing of spunding functionality --- cbpi/__init__.py | 2 +- cbpi/controller/fermentation_controller.py | 2 +- cbpi/craftbeerpi.py | 5 ++- cbpi/extension/dummysensor/__init__.py | 46 ++++++++++++++++++++-- 4 files changed, 48 insertions(+), 7 deletions(-) diff --git a/cbpi/__init__.py b/cbpi/__init__.py index 50aca94..8e2c938 100644 --- a/cbpi/__init__.py +++ b/cbpi/__init__.py @@ -1,3 +1,3 @@ -__version__ = "4.0.5.a6" +__version__ = "4.0.5.a7" __codename__ = "Spring Break" diff --git a/cbpi/controller/fermentation_controller.py b/cbpi/controller/fermentation_controller.py index 5d19193..8fa5de2 100644 --- a/cbpi/controller/fermentation_controller.py +++ b/cbpi/controller/fermentation_controller.py @@ -133,7 +133,7 @@ class FermentationController: def _find_by_id(self, id): - return next((item for item in self.data if item.id == id), None) + return next((item for item in self.data if item.id == id), None) async def get_all(self): return list(map(lambda x: x.to_dict(), self.data)) diff --git a/cbpi/craftbeerpi.py b/cbpi/craftbeerpi.py index b5efa61..c4dad9d 100644 --- a/cbpi/craftbeerpi.py +++ b/cbpi/craftbeerpi.py @@ -289,6 +289,7 @@ class CraftBeerPi: self._setup_http_index() self.plugin.load_plugins() self.plugin.load_plugins_from_evn() + await self.fermenter.init() await self.sensor.init() await self.step.init() @@ -296,8 +297,8 @@ class CraftBeerPi: await self.kettle.init() await self.call_initializer(self.app) await self.dashboard.init() - await self.fermenter.init() - + + self._swagger_setup() level = logging.INFO diff --git a/cbpi/extension/dummysensor/__init__.py b/cbpi/extension/dummysensor/__init__.py index 34c9c25..c935df2 100644 --- a/cbpi/extension/dummysensor/__init__.py +++ b/cbpi/extension/dummysensor/__init__.py @@ -1,9 +1,10 @@ # -*- coding: utf-8 -*- import asyncio import random - -from cbpi.api import parameters, CBPiSensor - +import logging +from cbpi.api import * +from cbpi.api.base import CBPiBase +from cbpi.api.dataclasses import Kettle, Props, Fermenter @parameters([]) class CustomSensor(CBPiSensor): @@ -23,6 +24,44 @@ class CustomSensor(CBPiSensor): def get_state(self): return dict(value=self.value) +@parameters([Property.Number(label="Pressure", configurable=True, description="Start Pressure"), + Property.Number(label="PressureIncrease", configurable=True, description="Pressure increase per hour"), + Property.Number(label="PressureDecrease", configurable=True, description="Pressure decrease per second on openm valve"), + Property.Fermenter(label="Fermenter",description="Fermenter")]) +class DummyPressure(CBPiSensor): + + def __init__(self, cbpi, id, props): + super(DummyPressure, self).__init__(cbpi, id, props) + self.value = float(self.props.get("Pressure",0)) + fermenter=self.props.get("Fermenter",None) + self.fermenter=self.get_fermenter(fermenter) + self.valve=self.fermenter.valve + + async def run(self): + self.uprate=float(self.props.get("PressureIncrease",0))/3600 + self.decrease=float(self.props.get("PressureDecrease",0)) + logging.info(self.uprate) + logging.info(self.decrease) + + while self.running: + valve_state=self.get_actor_state(self.valve) + fermenter_instance=self.fermenter.instance + if fermenter_instance: + fermenter_state=fermenter_instance.state + else: + fermenter_state = False + if valve_state == False and fermenter_state: + self.value = self.value + self.uprate + elif valve_state and fermenter_state: + self.value=self.value-self.decrease + + self.log_data(self.value) + + self.push_update(round(self.value,2)) + await asyncio.sleep(1) + + def get_state(self): + return dict(value=self.value) def setup(cbpi): ''' @@ -33,3 +72,4 @@ def setup(cbpi): :return: ''' cbpi.plugin.register("CustomSensor", CustomSensor) + cbpi.plugin.register("DummyPressure", DummyPressure)