craftbeerpi4-pione/core/api/step.py

75 lines
1.8 KiB
Python
Raw Normal View History

2018-12-05 07:31:12 +01:00
import time
import asyncio
import logging
2018-12-03 22:16:03 +01:00
class Step(object):
2018-12-05 07:31:12 +01:00
__dirty = False
managed_fields = []
2018-12-07 23:57:32 +01:00
_interval = 1
_max_exceptions = 2
_exception_count = 0
2018-12-05 07:31:12 +01:00
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")
2018-12-07 23:57:32 +01:00
self.is_stopped = False
self.is_next = False
2018-12-05 07:31:12 +01:00
self.start = time.time()
def running(self):
2018-12-07 23:57:32 +01:00
if self.is_next is True:
return False
if self.is_stopped is True:
2018-12-05 07:31:12 +01:00
return False
return True
2018-12-07 23:57:32 +01:00
async def run(self):
while self.running():
2018-12-05 07:31:12 +01:00
try:
2018-12-07 23:57:32 +01:00
await self.run_cycle()
2018-12-05 07:31:12 +01:00
except Exception as e:
2018-12-07 23:57:32 +01:00
logging.exception("Step Error")
self._exception_count = self._exception_count + 1
if self._exception_count == self._max_exceptions:
self.stop()
2018-12-05 07:31:12 +01:00
print("INTER",self._interval)
await asyncio.sleep(self._interval)
2018-12-07 23:57:32 +01:00
2018-12-05 07:31:12 +01:00
if self.is_dirty():
# Now we have to store the managed props
self.reset_dirty()
2018-12-03 22:16:03 +01:00
2018-12-07 23:57:32 +01:00
async def run_cycle(self):
2018-12-05 07:31:12 +01:00
print("NOTING IMPLEMENTED")
2018-12-03 22:16:03 +01:00
pass
2018-12-07 23:57:32 +01:00
def next(self):
self.is_next = True
2018-12-03 22:16:03 +01:00
def stop(self):
2018-12-07 23:57:32 +01:00
self.is_stopped = True
2018-12-05 07:31:12 +01:00
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)