step logic extended

This commit is contained in:
manuel83 2018-12-05 07:31:12 +01:00
parent 521f800236
commit 77871010a1
7 changed files with 399 additions and 665 deletions

File diff suppressed because it is too large Load diff

View file

@ -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
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)

View file

@ -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

View file

@ -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:

View file

@ -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)

View file

@ -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

View file

@ -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):