From 617e13a6015e862cc850bc36783a2cd8ca59a241 Mon Sep 17 00:00:00 2001 From: manuel83 Date: Fri, 7 Dec 2018 23:57:32 +0100 Subject: [PATCH] change logic of step controller --- .idea/workspace.xml | 351 +++++++++++++------------ core/api/step.py | 35 ++- core/controller/step_controller.py | 17 +- core/extension/comp/static/index2.html | 2 +- core/extension/dummystep/__init__.py | 7 +- 5 files changed, 222 insertions(+), 190 deletions(-) diff --git a/.idea/workspace.xml b/.idea/workspace.xml index 3851efa..73ebae6 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -2,10 +2,11 @@ - - - - + + + + + - 0x10d622b380x10d622b38 __init__ 0x1048b31d0 Content @@ -220,6 +210,7 @@ logging dummy /dumy + _run StepBase @@ -239,7 +230,6 @@ @@ -360,11 +351,11 @@ @@ -1145,13 +1148,13 @@ - + - + @@ -1206,14 +1209,6 @@ - - - - - - - - @@ -1507,17 +1502,6 @@ - - - - - - - - - - - @@ -1526,26 +1510,6 @@ - - - - - - - - - - - - - - - - - - - - @@ -1632,16 +1596,6 @@ - - - - - - - - - - @@ -1650,6 +1604,24 @@ + + + + + + + + + + + + + + + + + + @@ -1660,12 +1632,43 @@ - + - - + + - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/core/api/step.py b/core/api/step.py index 4650beb..7123550 100644 --- a/core/api/step.py +++ b/core/api/step.py @@ -9,43 +9,54 @@ class Step(object): __dirty = False managed_fields = [] - _interval = 0.1 + _interval = 1 + _max_exceptions = 2 + _exception_count = 0 def __init__(self, *args, **kwargs): self.logger = logging.getLogger(__name__) for a in kwargs: super(Step, self).__setattr__(a, kwargs.get(a)) self.id = kwargs.get("id") - self.stop = False + self.is_stopped = False + self.is_next = False self.start = time.time() def running(self): - if self.stop is False: + if self.is_next is True: + return False + + if self.is_stopped is True: return False return True - async def _run(self): - i = 0 - while i < 20: + async def run(self): + + while self.running(): try: - await self.run() + await self.run_cycle() except Exception as e: - pass - #logging.exception("Step Error") + logging.exception("Step Error") + self._exception_count = self._exception_count + 1 + if self._exception_count == self._max_exceptions: + self.stop() print("INTER",self._interval) await asyncio.sleep(self._interval) - i = i + 1 + if self.is_dirty(): # Now we have to store the managed props self.reset_dirty() - async def run(self): + async def run_cycle(self): print("NOTING IMPLEMENTED") pass + def next(self): + self.is_next = True + def stop(self): - pass + self.is_stopped = True def reset(self): pass diff --git a/core/controller/step_controller.py b/core/controller/step_controller.py index 7280551..d711f5d 100644 --- a/core/controller/step_controller.py +++ b/core/controller/step_controller.py @@ -36,6 +36,11 @@ class StepController(): self.cbpi.bus.fire("step/reset") return web.Response(text="OK") + @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") @@ -43,6 +48,14 @@ class StepController(): self.current_step.__getattribute__(action)() pass + @on_event("step/next") + def handle_next(self, **kwargs): + print("process action") + if self.current_step is not None: + self.current_step.next() + pass + + @on_event("step/start") def handle_start(self, topic, **kwargs): @@ -53,7 +66,7 @@ class StepController(): if self.current_step is not None: self.current_task.cancel() self.current_step.reset() - print("rESeT", self.current_step.id, self.steps) + self.steps[self.current_step.id]["state"] = None self.current_step = None self.current_task = None @@ -102,7 +115,7 @@ class StepController(): print("----------") config = dict(cbpi = self.cbpi, id=key, name="Manuel", managed_fields=self.get_manged_fields_as_array(step_type)) self.current_step = step_type["class"](**config) - self.current_task = loop.create_task(self.current_step._run()) + self.current_task = loop.create_task(self.current_step.run()) self.current_task.add_done_callback(self._step_done) open_step = True break diff --git a/core/extension/comp/static/index2.html b/core/extension/comp/static/index2.html index ab1afad..4f1175b 100644 --- a/core/extension/comp/static/index2.html +++ b/core/extension/comp/static/index2.html @@ -1 +1 @@ -HALLO WELT INDEX \ No newline at end of file +HALLO WELT INDEX2 \ No newline at end of file diff --git a/core/extension/dummystep/__init__.py b/core/extension/dummystep/__init__.py index 4b8cef8..bdc4d88 100644 --- a/core/extension/dummystep/__init__.py +++ b/core/extension/dummystep/__init__.py @@ -8,14 +8,19 @@ class CustomStep(Step): name = Property.Number(label="Test") _interval = 1 + i = 0 @action(key="name", parameters=None) def test(self, **kwargs): self.name="WOOHOO" - async def run(self): + async def run_cycle(self): + #await asyncio.sleep(1) + self.i = self.i + 1 + + print("RUN STEP", self.id, self.name, self.__dict__)