craftbeerpi4-pione/cbpi/api/step.py

107 lines
2.9 KiB
Python
Raw Normal View History

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-01-17 22:49:18 +01:00
class CBPiStep(metaclass=ABCMeta):
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
self._exception_count = 0
self._max_exceptions = 2
self.state_msg = "No state"
def get_state(self):
return self.state_msg
def stop(self):
self.stop_reason = "STOP"
self.running = False
def start(self):
self.running = True
self.stop_reason = None
def next(self):
self.stop_reason = "NEXT"
self.running = False
async def reset(self):
pass
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):
while self.running:
try:
await self.execute()
2021-02-07 13:08:13 +01:00
except Exception as e:
2021-01-17 22:49:18 +01:00
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)
await asyncio.sleep(1)
2021-02-07 13:08:13 +01:00
2021-01-17 22:49:18 +01:00
return (self.id, self.stop_reason)
2021-02-07 13:08:13 +01:00
2019-01-05 20:43:48 +01:00
2021-01-17 22:49:18 +01:00
@abstractmethod
async def execute(self):
pass
2019-01-05 20:43:48 +01:00
2021-02-07 13:08:13 +01:00
def get_static_config_value(self,name,default):
return self.cbpi.static_config.get(name, default)
2019-07-27 21:08:19 +02:00
2021-02-07 13:08:13 +01:00
def get_config_value(self,name,default):
return self.cbpi.config.get(name, default=default)
2019-07-27 21:08:19 +02:00
2021-02-07 13:08:13 +01:00
async def set_config_value(self,name,value):
return await self.cbpi.config.set(name,value)
2019-01-05 20:43:48 +01:00
2021-02-07 13:08:13 +01:00
async def add_config_value(self, name, value, type: ConfigType, description, options=None):
await self.cbpi.add(name, value, type, description, options=None)
2019-07-27 21:08:19 +02:00
2021-02-07 13:08:13 +01:00
def get_kettle(self,id):
return self.cbpi.kettle.find_by_id(id)
2019-07-27 21:08:19 +02:00
2021-02-07 13:08:13 +01:00
async def set_target_temp(self,id, temp):
await self.cbpi.kettle.set_target_temp(id, temp)
2019-07-27 21:08:19 +02:00
2021-02-07 13:08:13 +01:00
def get_sensor(self,id):
return self.cbpi.sensor.find_by_id(id)
2019-01-05 20:43:48 +01:00
2021-02-07 13:08:13 +01:00
def get_actor(self,id):
return self.cbpi.actor.find_by_id(id)
2019-01-05 20:43:48 +01:00
2021-02-07 13:08:13 +01:00
def get_actor_state(self,id):
try:
actor = self.cbpi.actor.find_by_id(id)
return actor.get("instance").get_state()
except:
logging.error("Faild to read actor state in step - actor {}".format(id))
return None
2019-01-05 20:43:48 +01:00
2021-02-07 13:08:13 +01:00
async def actor_on(self,id):
2019-01-05 20:43:48 +01:00
2021-02-07 13:08:13 +01:00
try:
await self.cbpi.actor.on(id)
except:
pass
async def actor_off(self,id):
try:
await self.cbpi.actor.off(id)
except:
pass