"Hysteresis added"

This commit is contained in:
Manuel Fritsch 2021-02-10 22:11:35 +01:00
parent bd311edccd
commit 5f18e7a7b8
13 changed files with 83 additions and 79 deletions

View file

@ -1 +1 @@
__version__ = "4.0.0.18" __version__ = "4.0.0.19"

View file

@ -23,6 +23,9 @@ class CBPiBase(metaclass=ABCMeta):
def get_kettle(self,id): def get_kettle(self,id):
return self.cbpi.kettle.find_by_id(id) return self.cbpi.kettle.find_by_id(id)
def get_kettle_target_temp(self,id):
return self.cbpi.kettle.find_by_id(id).get("target_temp")
async def set_target_temp(self,id, temp): async def set_target_temp(self,id, temp):
await self.cbpi.kettle.set_target_temp(id, temp) await self.cbpi.kettle.set_target_temp(id, temp)

View file

@ -1,3 +1,4 @@
from cbpi.api.base import CBPiBase
from cbpi.api.extension import CBPiExtension from cbpi.api.extension import CBPiExtension
from abc import ABCMeta from abc import ABCMeta
import logging import logging
@ -5,23 +6,18 @@ import asyncio
class CBPiKettleLogic(metaclass=ABCMeta): class CBPiKettleLogic(CBPiBase, metaclass=ABCMeta):
def __init__(self, cbpi, id, props): def __init__(self, cbpi, id, props):
self.cbpi = cbpi self.cbpi = cbpi
self.id = id self.id = id
self.props = props self.props = props
self.logger = logging.getLogger(__file__)
self.data_logger = None
self.state = False self.state = False
self.running = False self.running = False
def init(self): def init(self):
pass pass
def log_data(self, value):
self.cbpi.log.log_data(self.id, value)
async def run(self): async def run(self):
self.state = True self.state = True
while self.running: while self.running:
@ -35,22 +31,5 @@ class CBPiKettleLogic(metaclass=ABCMeta):
self.running = True self.running = True
async def stop(self): async def stop(self):
self.running = False self.task.cancel()
await self.task
async def on(self, power):
'''
Code to switch the actor on. Power is provided as integer value
:param power: power value between 0 and 100
:return: None
'''
pass
async def off(self):
'''
Code to switch the actor off
:return: None
'''
pass

View file

@ -74,7 +74,9 @@ class BasicController:
item = self.find_by_id(id) item = self.find_by_id(id)
instance = item.get("instance") instance = item.get("instance")
await instance.stop() await instance.stop()
print("STOP ACTION")
await instance.task await instance.task
print("STOP ACTION", instance)
await self.push_udpate() await self.push_udpate()
except Exception as e: except Exception as e:
logging.error("{} Cant stop {} - {}".format(self.name, id, e)) logging.error("{} Cant stop {} - {}".format(self.name, id, e))

View file

@ -16,7 +16,6 @@ class NotificationController(object):
self.cbpi.register(self) self.cbpi.register(self)
@on_event(topic="notification/#") async def notify(self, key, message, type=None):
async def _on_event(self, key, message, type=None, **kwargs): self.cbpi.ws.send(dict(topic="notifiaction", type=type, message=message))
self.cbpi.ws.send(dict(key=message))

View file

@ -1,26 +0,0 @@
import asyncio
from cbpi.api import *
@parameters([])
class CustomLogic(CBPiKettleLogic):
async def run(self):
self.state = True
while self.running:
await asyncio.sleep(1)
self.state = False
def setup(cbpi):
'''
This method is called by the server during startup
Here you need to register your plugins at the server
:param cbpi: the cbpi core
:return:
'''
cbpi.plugin.register("CustomKettleLogic", CustomLogic)

View file

@ -15,17 +15,7 @@ class CustomSensor(CBPiSensor):
self.value = 0 self.value = 0
@action(key="Test", parameters=[])
async def action1(self, **kwargs):
print("ACTION!", kwargs)
@action(key="Test1", parameters=[])
async def action2(self, **kwargs):
print("ACTION!", kwargs)
@action(key="Test2", parameters=[])
async def action3(self, **kwargs):
print("ACTION!", kwargs)
async def run(self): async def run(self):

View file

@ -0,0 +1,51 @@
import asyncio
from asyncio import tasks
import logging
from cbpi.api import *
@parameters([Property.Number(label="OffsetOn", configurable=True, description="Offset below target temp when heater should switched on"),
Property.Number(label="OffsetOff", configurable=True, description="Offset below target temp when heater should switched off")])
class Hysteresis(CBPiKettleLogic):
async def stop(self):
self.task.cancel()
await self.task
async def run(self):
try:
self.offset_on = float(self.props.get("OffsetOn", 0))
self.offset_off = float(self.props.get("OffsetOff", 0))
self.kettle = self.get_kettle(self.id)
self.heater = self.kettle.get("heater")
logging.info("CustomLogic {} {} {} {}".format(self.offset_on, self.offset_off, self.id, self.heater))
while True:
sensor_value = self.get_sensor_value(self.kettle.get("sensor")).get("value")
target_temp = self.get_kettle_target_temp("oHxKz3z5RjbsxfSz6KUgov")
if sensor_value < target_temp - self.offset_on:
await self.actor_on(self.heater)
elif sensor_value >= target_temp - self.offset_off:
await self.actor_off(self.heater)
await asyncio.sleep(1)
except asyncio.CancelledError as e:
pass
except Exception as e:
logging.error("CustomLogic Error {}".format(e))
finally:
self.running = False
await self.actor_off(self.heater)
def setup(cbpi):
'''
This method is called by the server during startup
Here you need to register your plugins at the server
:param cbpi: the cbpi core
:return:
'''
cbpi.plugin.register("Hysteresis", Hysteresis)

View file

@ -64,6 +64,17 @@
"type": "Steps", "type": "Steps",
"x": 595, "x": 595,
"y": 50 "y": 50
},
{
"id": "9dfc216e-21d7-44af-b10f-cf4158144134",
"name": "KettleControl",
"props": {
"kettle": "oHxKz3z5RjbsxfSz6KUgov",
"orientation": "vertical"
},
"type": "KettleControl",
"x": 70,
"y": 80
} }
], ],
"pathes": [ "pathes": [

View file

@ -1,15 +1,18 @@
{ {
"data": [ "data": [
{ {
"agitator": "EsmZwWi9Qp3bzmXqq7N3Ly", "agitator": "YwGzXvWMpmbLb6XobesL8n",
"heater": "EsmZwWi9Qp3bzmXqq7N3Ly", "heater": "EsmZwWi9Qp3bzmXqq7N3Ly",
"id": "oHxKz3z5RjbsxfSz6KUgov", "id": "oHxKz3z5RjbsxfSz6KUgov",
"name": "MashTun", "name": "MashTun",
"props": {}, "props": {
"sensor": "8ohkXvFA9UrkHLsxQL38wu", "OffsetOff": "23",
"OffsetOn": "22"
},
"sensor": "RedQfuxfy4mYe6PwioY95y",
"state": {}, "state": {},
"target_temp": 45, "target_temp": 73,
"type": "CustomKettleLogic" "type": "Hysteresis"
} }
] ]
} }

View file

@ -23,7 +23,7 @@
"Temp": "2", "Temp": "2",
"Timer": "5" "Timer": "5"
}, },
"status": "D", "status": "P",
"type": "ActorStep" "type": "ActorStep"
} }
] ]

View file

@ -1,8 +0,0 @@
import math
timerTime = 3661
print(format_time(timerTime))