2019-07-27 21:08:19 +02:00
|
|
|
import json
|
2019-01-05 20:43:48 +01:00
|
|
|
import time
|
|
|
|
import asyncio
|
|
|
|
import logging
|
2019-08-16 21:36:55 +02:00
|
|
|
from abc import abstractmethod, ABCMeta
|
2021-01-17 22:49:18 +01:00
|
|
|
import logging
|
2021-02-07 13:08:13 +01:00
|
|
|
from cbpi.api.config import ConfigType
|
2021-02-10 07:38:55 +01:00
|
|
|
from cbpi.api.base import CBPiBase
|
|
|
|
from enum import Enum
|
|
|
|
__all__ = ["Stop_Reason", "CBPiStep"]
|
|
|
|
class Stop_Reason(Enum):
|
|
|
|
STOP = 1
|
|
|
|
NEXT = 2
|
2021-01-17 22:49:18 +01:00
|
|
|
|
2021-02-10 07:38:55 +01:00
|
|
|
|
|
|
|
class CBPiStep(CBPiBase, metaclass=ABCMeta):
|
2021-01-17 22:49:18 +01:00
|
|
|
def __init__(self, cbpi, id, name, props) :
|
|
|
|
self.cbpi = cbpi
|
2021-02-07 13:08:13 +01:00
|
|
|
self.props = {**props}
|
2021-01-17 22:49:18 +01:00
|
|
|
self.id = id
|
|
|
|
self.name = name
|
|
|
|
self.status = 0
|
|
|
|
self.running = False
|
|
|
|
self.stop_reason = None
|
|
|
|
self.pause = False
|
|
|
|
self.task = None
|
2021-02-10 07:38:55 +01:00
|
|
|
self._task = None
|
2021-01-17 22:49:18 +01:00
|
|
|
self._exception_count = 0
|
|
|
|
self._max_exceptions = 2
|
2021-02-10 07:38:55 +01:00
|
|
|
self.state_msg = ""
|
2021-01-17 22:49:18 +01:00
|
|
|
|
|
|
|
def get_state(self):
|
|
|
|
return self.state_msg
|
|
|
|
|
2021-02-10 07:38:55 +01:00
|
|
|
def push_update(self):
|
|
|
|
self.cbpi.step.push_udpate()
|
|
|
|
|
|
|
|
async def stop(self):
|
|
|
|
self.stop_reason = Stop_Reason.STOP
|
|
|
|
self._task.cancel()
|
|
|
|
await self._task
|
|
|
|
|
|
|
|
async def start(self):
|
2021-01-17 22:49:18 +01:00
|
|
|
self.stop_reason = None
|
2021-02-10 07:38:55 +01:00
|
|
|
self._task = asyncio.create_task(self.run())
|
|
|
|
self._task.add_done_callback(self.cbpi.step.done)
|
|
|
|
|
|
|
|
async def next(self):
|
|
|
|
self.stop_reason = Stop_Reason.NEXT
|
|
|
|
self._task.cancel()
|
2021-01-17 22:49:18 +01:00
|
|
|
|
|
|
|
async def reset(self):
|
|
|
|
pass
|
2021-02-10 07:38:55 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def on_props_update(self, props):
|
|
|
|
self.props = props
|
2021-01-17 22:49:18 +01:00
|
|
|
|
|
|
|
async def update(self, props):
|
2021-01-22 23:25:20 +01:00
|
|
|
await self.cbpi.step.update_props(self.id, props)
|
2021-01-17 22:49:18 +01:00
|
|
|
|
|
|
|
async def run(self):
|
2021-02-10 07:38:55 +01:00
|
|
|
try:
|
|
|
|
while True:
|
|
|
|
try:
|
|
|
|
await self.execute()
|
|
|
|
except asyncio.CancelledError as e:
|
|
|
|
raise e
|
|
|
|
except Exception as e:
|
|
|
|
self._exception_count += 1
|
|
|
|
logging.error("Step has thrown exception")
|
|
|
|
if self._exception_count >= self._max_exceptions:
|
|
|
|
self.stop_reason = "MAX_EXCEPTIONS"
|
|
|
|
return (self.id, self.stop_reason)
|
|
|
|
except asyncio.CancelledError as e:
|
|
|
|
return self.id, self.stop_reason
|
|
|
|
|
2021-02-07 13:08:13 +01:00
|
|
|
|
2021-01-17 22:49:18 +01:00
|
|
|
@abstractmethod
|
|
|
|
async def execute(self):
|
|
|
|
pass
|
2021-02-10 07:38:55 +01:00
|
|
|
|
2019-01-05 20:43:48 +01:00
|
|
|
|