From 77871010a1e9692952f24bffecdd0b9b9f5a5039 Mon Sep 17 00:00:00 2001 From: manuel83 Date: Wed, 5 Dec 2018 07:31:12 +0100 Subject: [PATCH] step logic extended --- .idea/workspace.xml | 919 +++++++++------------------ core/api/step.py | 61 +- core/controller/plugin_controller.py | 12 +- core/controller/step_controller.py | 49 +- core/craftbeerpi.py | 2 +- core/eventbus.py | 8 +- core/extension/dummystep/__init__.py | 13 +- 7 files changed, 399 insertions(+), 665 deletions(-) diff --git a/.idea/workspace.xml b/.idea/workspace.xml index c3f9428..5b5b60a 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -2,22 +2,12 @@ - - - - - - + - - - - - - + - git - task - unregister - Node - 0x103db6588 0x10d622b38 0x10bde4548 0x10d622b380x10d622b38 @@ -215,11 +226,16 @@ ###### ON ###### ON ###### O - ###### wait emit register step + cbp + ###### + l + logger + logging + StepBase @@ -258,7 +274,6 @@ @@ -326,456 +342,8 @@ \ No newline at end of file diff --git a/core/api/step.py b/core/api/step.py index 8c29c35..46a8f72 100644 --- a/core/api/step.py +++ b/core/api/step.py @@ -1,11 +1,64 @@ +import time + +import asyncio + +import logging + + class Step(object): - def __init__(self, key=None, cbpi=None): - self.cbpi = cbpi - self.id = key + __dirty = False + managed_fields = [] + _interval = 0.1 + + 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.start = time.time() + + def running(self): + if self.stop is False: + return False + + return True + + async def _run(self): + i = 0 + while i < 5: + try: + await self.run() + except Exception as e: + pass + #logging.exception("Step Error") + 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): + print("NOTING IMPLEMENTED") pass def stop(self): - pass \ No newline at end of file + pass + + def reset(self): + pass + + def is_dirty(self): + return self.__dirty + + def reset_dirty(self): + self.__dirty = False + + def __setattr__(self, name, value): + if name != "_Step__dirty" and name in self.managed_fields: + self.__dirty = True + super(Step, self).__setattr__(name, value) + else: + super(Step, self).__setattr__(name, value) \ No newline at end of file diff --git a/core/controller/plugin_controller.py b/core/controller/plugin_controller.py index e5f3aa4..994bb56 100644 --- a/core/controller/plugin_controller.py +++ b/core/controller/plugin_controller.py @@ -103,11 +103,10 @@ class PluginController(): self.cbpi.kettle.types[name] = {"class": clazz, "config": self._parse_props(clazz)} if issubclass(clazz, Step): - print("NAME", name) - self.cbpi.step.types[name] = {"class": clazz, "config": self._parse_props(clazz)} - - if issubclass(clazz, CBPiExtension): - self.c = clazz(self.cbpi) + self.cbpi.step.types[name] = self._parse_props(clazz) + print(self.cbpi.step.types) + #if issubclass(clazz, CBPiExtension): + # self.c = clazz(self.cbpi) def _parse_props(self, cls): @@ -146,4 +145,5 @@ class PluginController(): key = method.__getattribute__("key") parameters = method.__getattribute__("parameters") result["actions"].append({"method": method_name, "label": key, "parameters": parameters}) - pprint(result, width=200) + pprint(result) + return result \ No newline at end of file diff --git a/core/controller/step_controller.py b/core/controller/step_controller.py index c71490d..1688105 100644 --- a/core/controller/step_controller.py +++ b/core/controller/step_controller.py @@ -6,6 +6,7 @@ class StepController(): def __init__(self, cbpi): self.cbpi = cbpi + self.current_task = None self.types = {} self.steps = { 1: dict(name="S1", config=dict(time=1), type="CustomStep", state=None), @@ -34,21 +35,45 @@ class StepController(): self.start() @on_event("step/reset") - def handle_rest(self, topic, **kwargs): + def handle_reset(self, topic, **kwargs): + 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 + self.start() + + @on_event("step/stop") + def handle_stop(self, topic, **kwargs): + if self.current_step is not None: + self.current_step.stop() + for key, step in self.steps.items(): step["state"] = None - self.current_step = Nonecd + self.current_step = None @on_event("step/+/done") def handle(self, topic, **kwargs): self.start() def _step_done(self, task): - self.steps[self.current_step.id]["state"] = "D" - step_id = self.current_step.id - self.current_step = None - self.cbpi.bus.fire("step/%s/done" % step_id) + + if task.cancelled() == False: + self.steps[self.current_step.id]["state"] = "D" + 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 def start(self): @@ -57,10 +82,14 @@ class StepController(): open_step = False for key, step in self.steps.items(): if step["state"] is None: - type = self.types.get(step["type"]) - self.current_step = type["class"](key, self.cbpi) - task = loop.create_task(self.current_step.run()) - task.add_done_callback(self._step_done) + step_type = self.types["CustomStep"] + print("----------") + print(step_type) + 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.add_done_callback(self._step_done) open_step = True break if open_step == False: diff --git a/core/craftbeerpi.py b/core/craftbeerpi.py index 1b01073..daf4e3e 100644 --- a/core/craftbeerpi.py +++ b/core/craftbeerpi.py @@ -51,7 +51,7 @@ class CraftBeerPi(): self.initializer = [] setup(self.app, self) - self.bus = EventBus(self.app.loop) + self.bus = EventBus(self.app.loop, self) self.ws = WebSocket(self) self.actor = ActorController(self) self.sensor = SensorController(self) diff --git a/core/eventbus.py b/core/eventbus.py index 09f360d..7f56057 100644 --- a/core/eventbus.py +++ b/core/eventbus.py @@ -1,7 +1,7 @@ import asyncio import inspect import logging - +import json class EventBus(object): class Node(object): @@ -58,8 +58,9 @@ class EventBus(object): if clean_idx is not None: del content.parent._content[clean_idx] - def __init__(self, loop): + def __init__(self, loop, cbpi): self.logger = logging.getLogger(__name__) + self.cbpi = cbpi self._root = self.Node() self.registry = {} self.docs = {} @@ -73,6 +74,7 @@ class EventBus(object): def fire(self, topic: str, **kwargs) -> None: self.logger.info("EMIT EVENT %s Data: %s", topic, kwargs) + #self.cbpi.ws.send(json.dumps(dict(topic=topic, data=dict(**kwargs)))) trx = dict(i=0) for e in self.iter_match(topic): content_array = e @@ -91,7 +93,7 @@ class EventBus(object): else: content_obj.method(**kwargs, topic = topic) - + if content_obj.once is False: keep_idx.append(idx) diff --git a/core/extension/dummystep/__init__.py b/core/extension/dummystep/__init__.py index 5849202..015c3c6 100644 --- a/core/extension/dummystep/__init__.py +++ b/core/extension/dummystep/__init__.py @@ -1,16 +1,19 @@ import asyncio +from core.api import Property from core.api.step import Step class CustomStep(Step): + name = Property.Number(label="Test") + + async def run(self): - i = 0 - while i < 3: - await asyncio.sleep(1) - print("RUN STEP") - i = i + 1 + self.name = "HELLO WORLD" + #await asyncio.sleep(1) + raise Exception("OH O") + print("RUN STEP", self.id, self.name, self.__dict__) def setup(cbpi):