craftbeerpi4-pione/cbpi/controller/step_controller.py

207 lines
5.9 KiB
Python
Raw Normal View History

2019-01-04 09:29:09 +01:00
import logging
2018-12-16 21:42:47 +01:00
import time
2019-01-02 00:48:36 +01:00
2019-01-05 20:43:48 +01:00
from cbpi.api import *
from cbpi.controller.crud_controller import CRUDController
from cbpi.database.model import StepModel
2018-12-03 22:16:03 +01:00
2018-12-08 14:21:00 +01:00
2019-01-02 21:20:44 +01:00
class StepController(CRUDController):
2018-12-09 22:20:33 +01:00
'''
The Step Controller. This controller is responsible to start and stop the brewing steps.
'''
2018-12-08 14:21:00 +01:00
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)
2019-01-04 09:29:09 +01:00
self.caching = False
self.is_stopping = False
2018-12-03 22:16:03 +01:00
self.cbpi = cbpi
2018-12-05 07:31:12 +01:00
self.current_task = None
2019-01-04 09:29:09 +01:00
self.is_next = False
2018-12-03 22:16:03 +01:00
self.types = {}
self.current_step = None
2019-01-02 00:48:36 +01:00
self.current_job = None
2019-01-02 21:20:44 +01:00
self.cbpi.register(self)
2019-01-04 09:29:09 +01:00
self.logger = logging.getLogger(__name__)
self.starttime = None
2018-12-15 00:01:37 +01:00
2018-12-03 22:16:03 +01:00
async def init(self):
2018-12-09 22:20:33 +01:00
'''
Initializer of the the Step Controller.
:return:
'''
2018-12-08 14:21:00 +01:00
await super(StepController, self).init()
2018-12-29 00:27:19 +01:00
2018-12-16 21:42:47 +01:00
step = await self.model.get_by_state('A')
# We have an active step
if step is not None:
2018-12-29 00:27:19 +01:00
2018-12-16 21:42:47 +01:00
# get the type
step_type = self.types.get(step.type)
if step_type is None:
# step type not found. cant restart step
return
if step.stepstate is not None:
cfg = step.stepstate.copy()
else:
cfg = {}
cfg.update(dict(cbpi=self.cbpi, id=step.id, managed_fields=self._get_manged_fields_as_array(step_type)))
self.current_step = step_type["class"](**cfg)
2019-01-01 15:35:35 +01:00
self.current_job = await self.cbpi.job.start_job(self.current_step.run(), step.name, "step")
2018-11-29 21:59:08 +01:00
2019-01-04 09:29:09 +01:00
def get_state(self):
return dict(items=self.get_all(),types=self.types,is_running=self.is_running(),current_step=self.current_step)
2018-12-07 23:57:32 +01:00
@on_event("step/action")
2018-12-13 21:45:33 +01:00
async def handle_action(self, action, **kwargs):
2018-12-09 22:20:33 +01:00
'''
Event Handler for "step/action".
It invokes the provided method name on the current step
:param action: the method name which will be invoked
:param kwargs:
:return: None
'''
if self.current_step is not None:
self.current_step.__getattribute__(action)()
2018-12-09 22:20:33 +01:00
2018-12-07 23:57:32 +01:00
@on_event("step/next")
2019-01-04 09:29:09 +01:00
async def next(self, **kwargs):
2018-12-09 22:20:33 +01:00
'''
Event Handler for "step/next".
It start the next step
:param kwargs:
:return: None
'''
2019-01-04 09:29:09 +01:00
print("REQUEST NEXT")
self.starttime = time.time()
if self.current_step is not None and self.is_next is False:
self.logger.info("Request Next Step to start. Stopping current step")
self.is_next = True
self.current_step.stop()
else:
self.logger.info("Can Start Next")
2018-12-07 23:57:32 +01:00
2018-12-03 22:16:03 +01:00
2018-12-15 00:01:37 +01:00
@on_event("job/step/done")
2019-01-04 09:29:09 +01:00
async def _step_done(self, topic, **kwargs):
2018-12-09 22:20:33 +01:00
'''
Event Handler for "step/+/done".
Starts the next step
:param topic:
:param kwargs:
:return:
'''
2018-12-15 00:01:37 +01:00
2019-01-04 09:29:09 +01:00
# SHUTDONW DO NOTHING
self.logger.info("HANDLE DONE IS SHUTDONW %s IS STOPPING %s IS NEXT %s" % ( self.cbpi.shutdown, self.is_stopping, self.is_next))
2018-12-16 21:42:47 +01:00
if self.cbpi.shutdown:
return
2018-12-29 00:27:19 +01:00
2019-01-04 09:29:09 +01:00
if self.is_stopping:
self.is_stopping = False
return
self.is_next = False
if self.current_step is not None:
await self.model.update_state(self.current_step.id, "D", int(time.time()))
self.current_step = None
await self.start()
2018-12-15 00:01:37 +01:00
2018-12-05 07:31:12 +01:00
2018-12-09 22:20:33 +01:00
def _get_manged_fields_as_array(self, type_cfg):
2018-12-29 00:27:19 +01:00
2018-12-05 07:31:12 +01:00
result = []
for f in type_cfg.get("properties"):
result.append(f.get("name"))
return result
2018-12-03 22:16:03 +01:00
2019-01-02 00:48:36 +01:00
def is_running(self):
if self.current_step is not None:
return True
else:
return False
@on_event("step/start")
2019-01-04 09:29:09 +01:00
async def start(self, **kwargs):
2018-12-03 22:16:03 +01:00
2018-12-09 22:20:33 +01:00
'''
Start the first step
:return:None
'''
2019-01-04 09:29:09 +01:00
if self.is_running() is False:
next_step = await self.model.get_by_state("I")
2018-12-15 00:01:37 +01:00
2019-01-04 09:29:09 +01:00
if next_step is not None:
step_type = self.types[next_step.type]
2018-12-16 21:42:47 +01:00
2019-01-04 09:29:09 +01:00
config = dict(cbpi=self.cbpi, id=next_step.id, name=next_step.name, managed_fields=self._get_manged_fields_as_array(step_type))
2018-12-16 21:42:47 +01:00
self.current_step = step_type["class"](**config)
2018-12-15 00:01:37 +01:00
2019-01-04 09:29:09 +01:00
next_step.state = 'A'
next_step.stepstate = next_step.config
next_step.start = int(time.time())
await self.model.update(**next_step.__dict__)
if self.starttime is not None:
end = time.time()
d = end - self.starttime
print("DURATION", d)
else:
print("NORMAL START")
self.current_job = await self.cbpi.job.start_job(self.current_step.run(), next_step.name, "step")
await self.cbpi.bus.fire("step/%s/started" % self.current_step.id)
2018-12-16 21:42:47 +01:00
else:
2019-01-04 09:29:09 +01:00
await self.cbpi.bus.fire("step/brewing/finished")
else:
self.logger.error("Process Already Running")
print("----------- END")
2019-01-02 00:48:36 +01:00
@on_event("step/stop")
async def stop(self, **kwargs):
2019-01-04 09:29:09 +01:00
if self.current_step is not None:
self.current_step.stop()
self.is_stopping = True
self.current_step = None
await self.model.reset_all_steps()
await self.cbpi.bus.fire("step/brewing/stopped")
@on_event("step/reset")
async def handle_reset(self, **kwargs):
'''
Event Handler for "step/reset".
Resets the current step
:param kwargs:
:return: None
'''
if self.current_step is not None:
await self.stop()
self.current_step = None
2019-01-02 00:48:36 +01:00
self.is_stopping = True
2019-01-04 09:29:09 +01:00
await self.model.reset_all_steps()