craftbeerpi4-pione/core/controller/kettle_controller.py
2018-11-18 23:09:17 +01:00

151 lines
No EOL
4.4 KiB
Python

from aiohttp import web
from core.api import request_mapping, on_event
from core.controller.crud_controller import CRUDController
from core.database.model import KettleModel
from core.http_endpoints.http_api import HttpAPI
from core.utils import json_dumps
class KettleHttp(HttpAPI):
@request_mapping(path="/types", auth_required=False)
async def get_types(self, request):
web.json_response(data=self.types, dumps=json_dumps)
@request_mapping(path="/{id:\d+}/automatic", auth_required=False)
async def start2(self, request):
id = int(request.match_info['id'])
result = await self.toggle_automtic(id)
if result[0] is True:
return web.Response(text="OK")
else:
return web.Response(status=404, text=result[1])
@request_mapping(path="/{id:\d+}/heater", auth_required=False)
async def start(self, request):
id = int(request.match_info['id'])
result = await self.heater_on(id)
if result[0] is True:
return web.Response(text="OK")
else:
return web.Response(status=404, text=result[1])
class KettleController(CRUDController, KettleHttp):
'''
The main actor controller
'''
model = KettleModel
def __init__(self, cbpi):
super(KettleController, self).__init__(cbpi)
self.cbpi = cbpi
self.types = {}
self.cbpi.register(self, "/kettle")
async def init(self):
'''
This method initializes all actors during startup. It creates actor instances
:return:
'''
await super(KettleController, self).init()
async def toggle_automtic(self, id):
'''
:param id:
:return:
'''
kettle = await self.get_one(id)
if kettle is None:
return (False, "Kettle Not Found")
if kettle.logic is None:
return (False, "No Logic defined")
id = kettle.heater
self.cbpi.bus.fire(topic="kettle/%s/automatic" % id, id=id)
return (True, "Logic switched on switched")
@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)
print("WOOOHO", type(id), self.cache)
if id in self.cache:
print("id", id)
kettle = self.cache[id]
if hasattr(kettle, "instance") is False:
kettle.instance = None
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)
print("START LOGIC")
await self.cbpi.start_job(kettle.instance.run(), "Kettle_logic_%s" % kettle.id, "kettle_logic")
else:
kettle.instance.running = False
kettle.instance = None
async def heater_on(self, id):
'''
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:
return (False, "Kettle Not Found")
if kettle.heater is None:
return (False, "No Heater defined")
id = kettle.heater
self.cbpi.bus.fire(topic="actor/%s/on" % id, id=id, power=99)
return (True,"Heater switched on")
async def heater_off(self, id):
'''
Convenience Method to switch the heater of a kettle off
:param id:
:return:
'''
kettle = await self.get_one(id)
if kettle is None:
return (False, "Kettle Not Found")
if kettle.heater is None:
return (False, "No Heater defined")
id = kettle.heater
self.cbpi.bus.fire(topic="actor/%s/off" % id, id=id, power=99)
return (True, "Heater switched off")
async def agitator_on(self, id):
pass
async def agitator_off(self, id):
pass
async def get_traget_temp(self, id):
kettle = await self.get_one(id)
return kettle.target_temp
async def get_temp(self, id):
pass