craftbeerpi4-pione/core/controller/step_controller.py

132 lines
4 KiB
Python
Raw Normal View History

2018-12-03 22:16:03 +01:00
import asyncio
from aiohttp import web
from core.api import on_event, request_mapping
2018-12-08 14:21:00 +01:00
from core.controller.crud_controller import CRUDController
from core.database.model import StepModel
from core.http_endpoints.http_api import HttpAPI
2018-12-03 22:16:03 +01:00
2018-12-08 14:21:00 +01:00
class StepController(HttpAPI, CRUDController):
model = StepModel
2018-11-29 21:59:08 +01:00
2018-12-03 22:16:03 +01:00
def __init__(self, cbpi):
2018-12-08 14:21:00 +01:00
super(StepController, self).__init__(cbpi)
2018-12-03 22:16:03 +01:00
self.cbpi = cbpi
2018-12-05 07:31:12 +01:00
self.current_task = None
2018-12-03 22:16:03 +01:00
self.types = {}
2018-12-08 14:21:00 +01:00
2018-12-03 22:16:03 +01:00
self.current_step = None
self.cbpi.register(self, "/step")
2018-11-29 21:59:08 +01:00
2018-12-03 22:16:03 +01:00
async def init(self):
#self.start()
2018-12-08 14:21:00 +01:00
await super(StepController, self).init()
2018-11-29 21:59:08 +01:00
pass
@request_mapping(path="/action", auth_required=False)
async def http_action(self, request):
self.cbpi.bus.fire("step/action", action="test")
return web.Response(text="OK")
2018-12-03 22:16:03 +01:00
@request_mapping(path="/start", auth_required=False)
async def http_start(self, request):
self.cbpi.bus.fire("step/start")
return web.Response(text="OK")
@request_mapping(path="/reset", auth_required=False)
async def http_reset(self, request):
self.cbpi.bus.fire("step/reset")
return web.Response(text="OK")
2018-12-07 23:57:32 +01:00
@request_mapping(path="/next", auth_required=False)
async def http_reset(self, request):
self.cbpi.bus.fire("step/next")
return web.Response(text="OK")
@on_event("step/action")
def handle_action(self, topic, action, **kwargs):
print("process action")
if self.current_step is not None:
self.current_step.__getattribute__(action)()
pass
2018-12-07 23:57:32 +01:00
@on_event("step/next")
def handle_next(self, **kwargs):
print("process action")
if self.current_step is not None:
self.current_step.next()
pass
2018-12-03 22:16:03 +01:00
@on_event("step/start")
def handle_start(self, topic, **kwargs):
self.start()
@on_event("step/reset")
2018-12-05 07:31:12 +01:00
def handle_reset(self, topic, **kwargs):
if self.current_step is not None:
self.current_task.cancel()
self.current_step.reset()
2018-12-07 23:57:32 +01:00
2018-12-05 07:31:12 +01:00
self.steps[self.current_step.id]["state"] = None
self.current_step = None
self.current_task = None
self.start()
@on_event("step/stop")
def handle_stop(self, topic, **kwargs):
if self.current_step is not None:
self.current_step.stop()
2018-12-08 14:21:00 +01:00
for key, step in self.cache.items():
step.state = None
2018-12-03 22:16:03 +01:00
2018-12-05 07:31:12 +01:00
self.current_step = None
2018-12-03 22:16:03 +01:00
@on_event("step/+/done")
def handle(self, topic, **kwargs):
self.start()
def _step_done(self, task):
2018-12-05 07:31:12 +01:00
if task.cancelled() == False:
2018-12-08 14:21:00 +01:00
self.cache[self.current_step.id].state = "D"
2018-12-05 07:31:12 +01:00
step_id = self.current_step.id
self.current_step = None
self.cbpi.bus.fire("step/%s/done" % step_id)
def get_manged_fields_as_array(self, type_cfg):
print("tYPE", type_cfg)
result = []
for f in type_cfg.get("properties"):
result.append(f.get("name"))
return result
2018-12-03 22:16:03 +01:00
def start(self):
if self.current_step is None:
loop = asyncio.get_event_loop()
open_step = False
2018-12-08 14:21:00 +01:00
for key, step in self.cache.items():
if step.state is None:
2018-12-05 07:31:12 +01:00
step_type = self.types["CustomStep"]
print("----------")
print(step_type)
print("----------")
2018-12-08 14:21:00 +01:00
config = dict(cbpi = self.cbpi, id=key, name=step.name, managed_fields=self.get_manged_fields_as_array(step_type))
2018-12-05 07:31:12 +01:00
self.current_step = step_type["class"](**config)
2018-12-07 23:57:32 +01:00
self.current_task = loop.create_task(self.current_step.run())
2018-12-05 07:31:12 +01:00
self.current_task.add_done_callback(self._step_done)
2018-12-03 22:16:03 +01:00
open_step = True
break
if open_step == False:
self.cbpi.bus.fire("step/berwing/finished")
2018-11-29 21:59:08 +01:00
async def stop(self):
pass