craftbeerpi4-pione/core/controller/kettle_controller.py

162 lines
5.2 KiB
Python
Raw Normal View History

2018-12-03 22:16:03 +01:00
import re
2019-01-02 21:20:44 +01:00
2018-12-29 00:27:19 +01:00
from cbpi_api import *
2019-01-02 00:48:36 +01:00
from cbpi_api.exceptions import KettleException, ActorException, SensorException
2018-11-18 15:40:10 +01:00
from core.controller.crud_controller import CRUDController
from core.database.model import KettleModel
2019-01-02 00:48:36 +01:00
2019-01-02 21:20:44 +01:00
from core.job.aiohttp import get_scheduler_from_app
2019-01-02 00:48:36 +01:00
2018-11-18 15:40:10 +01:00
2019-01-02 21:20:44 +01:00
class KettleController(CRUDController):
2018-11-18 15:40:10 +01:00
'''
2018-11-29 21:59:08 +01:00
The main kettle controller
2018-11-18 15:40:10 +01:00
'''
model = KettleModel
def __init__(self, cbpi):
super(KettleController, self).__init__(cbpi)
self.cbpi = cbpi
self.types = {}
2019-01-02 21:20:44 +01:00
self.cbpi.register(self)
2019-01-01 15:35:35 +01:00
2018-11-18 15:40:10 +01:00
async def init(self):
'''
This method initializes all actors during startup. It creates actor instances
:return:
'''
await super(KettleController, self).init()
2019-01-04 09:29:09 +01:00
def get_state(self):
return dict(items=self.cache,types=self.types)
2018-11-18 15:40:10 +01:00
async def toggle_automtic(self, id):
2018-11-18 23:09:17 +01:00
'''
2018-11-29 21:59:08 +01:00
Convenience Method to toggle automatic
:param id: kettle id as int
:return: (boolean, string)
2018-11-18 23:09:17 +01:00
'''
2018-11-18 15:40:10 +01:00
kettle = await self.get_one(id)
2018-11-18 23:09:17 +01:00
if kettle is None:
2019-01-02 00:48:36 +01:00
raise KettleException("Kettle not found")
2018-11-18 23:09:17 +01:00
if kettle.logic is None:
2019-01-02 00:48:36 +01:00
raise CBPiExtension("Logic not found for kettle id: %s" % id)
2018-12-13 21:45:33 +01:00
await self.cbpi.bus.fire(topic="kettle/%s/automatic" % id, id=id)
2018-11-18 23:09:17 +01:00
2018-11-30 23:27:11 +01:00
@on_event(topic="job/done")
2018-12-13 21:45:33 +01:00
async def job_stop(self, key, **kwargs) -> None:
2018-11-30 23:27:11 +01:00
2018-12-03 22:16:03 +01:00
match = re.match("kettle_logic_(\d+)", key)
if match is not None:
kid = match.group(1)
kettle = self.cache[int(kid)]
kettle.instance = None
2018-11-30 23:27:11 +01:00
2018-11-18 23:09:17 +01:00
@on_event(topic="kettle/+/automatic")
async def handle_automtic_event(self, id, **kwargs):
'''
Method to handle the event 'kettle/+/automatic'
:param id: The kettle id
:param kwargs:
:return: None
'''
id = int(id)
2018-12-29 00:27:19 +01:00
2018-11-18 23:09:17 +01:00
if id in self.cache:
2018-12-29 00:27:19 +01:00
2018-11-18 23:09:17 +01:00
kettle = self.cache[id]
if hasattr(kettle, "instance") is False:
kettle.instance = None
2018-11-30 23:27:11 +01:00
self._is_logic_running(id)
2018-11-18 23:09:17 +01:00
if kettle.instance is None:
if kettle.logic in self.types:
clazz = self.types.get("CustomKettleLogic")["class"]
cfg = kettle.config.copy()
cfg.update(dict(cbpi=self.cbpi))
kettle.instance = clazz(**cfg)
2018-12-29 00:27:19 +01:00
2019-01-02 00:48:36 +01:00
await self.cbpi.job.start_job(kettle.instance.run(), "Kettle_logic_%s" % kettle.id, "kettle_logic%s" % id)
2018-11-18 23:09:17 +01:00
else:
kettle.instance.running = False
kettle.instance = None
2018-11-18 15:40:10 +01:00
2018-11-30 23:27:11 +01:00
def _is_logic_running(self, kettle_id):
scheduler = get_scheduler_from_app(self.cbpi.app)
2018-12-29 00:27:19 +01:00
2018-11-18 15:40:10 +01:00
async def heater_on(self, id):
2018-11-18 23:09:17 +01:00
'''
Convenience Method to switch the heater of a kettle on
:param id: the kettle id
:return: (boolean, string)
'''
kettle = await self.get_one(id)
if kettle is None:
2019-01-02 00:48:36 +01:00
raise KettleException("Kettle not found")
if kettle.sensor is None:
raise ActorException("Actor not defined for kettle id %s" % id)
2019-01-02 21:20:44 +01:00
heater_id = kettle.heater
await self.cbpi.bus.fire(topic="actor/%s/switch/on" % heater_id, actor_id=heater_id, power=99)
2018-11-18 15:40:10 +01:00
async def heater_off(self, id):
2018-11-18 23:09:17 +01:00
'''
Convenience Method to switch the heater of a kettle off
:param id:
:return:
'''
kettle = await self.get_one(id)
if kettle is None:
2019-01-02 00:48:36 +01:00
raise KettleException("Kettle not found")
if kettle.sensor is None:
raise ActorException("Actor not defined for kettle id %s" % id)
2019-01-02 21:20:44 +01:00
heater_id = kettle.heater
await self.cbpi.bus.fire(topic="actor/%s/switch/off" % heater_id, actor_id=heater_id, power=99)
2018-11-18 15:40:10 +01:00
async def agitator_on(self, id):
2019-01-02 00:48:36 +01:00
kettle = await self.get_one(id)
if kettle is None:
raise KettleException("Kettle not found")
if kettle.sensor is None:
raise ActorException("Actor not defined for kettle id %s" % id)
agitator_id = kettle.agitator
2019-01-02 21:20:44 +01:00
await self.cbpi.bus.fire(topic="actor/%s/switch/on" % agitator_id, actor_id=agitator_id, power=99)
2018-11-18 15:40:10 +01:00
async def agitator_off(self, id):
2019-01-02 00:48:36 +01:00
kettle = await self.get_one(id)
if kettle is None:
raise KettleException("Kettle not found")
if kettle.sensor is None:
raise ActorException("Actor not defined for kettle id %s" % id)
agitator_id = kettle.agitator
2019-01-02 21:20:44 +01:00
await self.cbpi.bus.fire(topic="actor/%s/switch/off" % agitator_id, actor_id=agitator_id, power=99)
2018-11-18 15:40:10 +01:00
async def get_traget_temp(self, id):
2018-11-18 23:09:17 +01:00
kettle = await self.get_one(id)
2019-01-02 00:48:36 +01:00
if kettle is None:
raise KettleException("Kettle Not Found")
2018-11-18 23:09:17 +01:00
return kettle.target_temp
2018-11-18 15:40:10 +01:00
async def get_temp(self, id):
2018-11-18 23:09:17 +01:00
2019-01-02 00:48:36 +01:00
kettle = await self.get_one(id)
if kettle is None:
raise KettleException("Kettle Not Found")
if kettle.sensor is None:
raise SensorException("Sensor not defined for kettle id %s" % id)
sensor_id = kettle.sensor
return await self.cbpi.sensor.get_value(sensor_id)