further changes to support fermentersteps - WIP

This commit is contained in:
avollkopf 2022-02-21 07:16:34 +01:00
parent 286198fcd1
commit 36329fb533
7 changed files with 419 additions and 86 deletions

View file

@ -1 +1 @@
__version__ = "4.0.1.18.a4"
__version__ = "4.0.1.18.a5"

View file

@ -162,6 +162,7 @@ class FermenterStep:
type: str = None
status: StepState = StepState.INITIAL
instance: str = None
step: dict = None
def __str__(self):
return "name={} props={}, type={}, instance={}".format(self.name, self.props, self.type, self.instance)

View file

@ -1,6 +1,7 @@
import asyncio
import logging
from abc import abstractmethod
import cbpi
from cbpi.api.base import CBPiBase
@ -77,7 +78,7 @@ class CBPiStep(CBPiBase):
self.task.cancel()
await self.task
except Exception as e:
self.logger.error(e)
logging.error(e)
async def reset(self):
pass
@ -116,8 +117,100 @@ class CBPiStep(CBPiBase):
def __str__(self):
return "name={} props={}, type={}".format(self.name, self.props, self.__class__.__name__)
class CBPiFermentationStep(CBPiStep):
#class CBPiFermentationStep(CBPiStep):
# def __init__(self, cbpi, fermenter, step, props, on_done) -> None:
# self.fermenter = fermenter
# id = step.get("id")
# name=step.get("name")
# self.step=step
# super().__init__(cbpi, id, name, props, on_done)
class CBPiFermentationStep(CBPiBase):
def __init__(self, cbpi, fermenter, step, props, on_done) -> None:
self.fermenter = fermenter
super().__init__(cbpi, step.id, step.name, props, on_done)
self.name = step.get("name")
self.cbpi = cbpi
self.id = step.get("id")
self.timer = None
self._done_callback = on_done
self.props = props
self.cancel_reason: StepResult = None
self.summary = ""
self.task = None
self.running: bool = False
self.logger = logging.getLogger(__name__)
self.step = step
self.update_key="fermenterstepupdate"
def _done(self, task):
if self._done_callback is not None:
try:
result = task.result()
logging.info(result)
logging.info(self.fermenter.id)
fermenter=self.fermenter.id
self._done_callback(self, result, fermenter)
except Exception as e:
self.logger.error(e)
async def start(self):
self.logger.info("Start {}".format(self.name))
self.running = True
self.task = asyncio.create_task(self._run())
self.task.add_done_callback(self._done)
async def next(self):
self.running = False
self.cancel_reason = StepResult.NEXT
self.task.cancel()
await self.task
async def stop(self):
try:
self.running = False
if self.task is not None and self.task.done() is False:
self.cancel_reason = StepResult.STOP
logging.info(self.cancel_reason)
self.task.cancel()
await self.task
except Exception as e:
logging.error(e)
async def reset(self):
pass
async def on_props_update(self, props):
self.props = {**self.props, **props}
async def save_props(self):
await self.cbpi.step.save()
async def push_update(self):
self.cbpi.fermenter.push_update(self.update_key)
async def on_start(self):
pass
async def on_stop(self):
pass
async def _run(self):
try:
await self.on_start()
await self.run()
self.cancel_reason = StepResult.DONE
except asyncio.CancelledError as e:
pass
finally:
await self.on_stop()
return self.cancel_reason
@abstractmethod
async def run(self):
pass
def __str__(self):
return "name={} props={}, type={}".format(self.name, self.props, self.__class__.__name__)

View file

@ -1,3 +1,4 @@
from abc import abstractmethod
import asyncio
import cbpi
import copy
@ -34,14 +35,19 @@ class FermentStep:
except Exception as e:
self.logger.error(e)
@abstractmethod
async def run(self):
while True:
while self.running:
logging.info(self.step)
await asyncio.sleep(1)
pass
async def _run(self):
try:
await self.on_start()
await self.run()
await self.step.instance.on_start()
await self.step.instance.run()
#await self.on_start()
#await self.run()
self.cancel_reason = StepResult.DONE
except asyncio.CancelledError as e:
pass
@ -109,18 +115,6 @@ class FermentationController:
destfile = os.path.join(".", 'config', "fermenter_data.json")
json.dump(data,open(destfile,'w'),indent=4, sort_keys=True)
def push_update(self, key="fermenterupdate", fermentersteps=None):
if key == self.update_key:
self.cbpi.ws.send(dict(topic=key, data=list(map(lambda item: item.to_dict(), self.data))))
#self.cbpi.push_update("cbpi/{}".format(self.update_key), list(map(lambda item: item.to_dict(), self.data)))
for item in self.data:
self.cbpi.push_update("cbpi/{}/{}".format(self.update_key,item.id), item.to_dict())
pass
else:
self.cbpi.ws.send(dict(topic=key, data=fermentersteps))
async def shutdown(self, app=None):
self.save()
for fermenter in self.data:
@ -149,21 +143,27 @@ class FermentationController:
status = StepState(item.get("status", "I"))
type = item.get("type")
type_cfg = self.types.get(type)
if type_cfg is not None:
inst = type_cfg.get("class")()
print(inst)
try:
type_cfg = self.steptypes.get(type)
clazz = type_cfg.get("class")
instance = clazz(self.cbpi, fermenter, item, props, self._done)
except Exception as e:
logging.warning("Failed to create step instance %s - %s" % (id, e))
instance = None
step = FermenterStep(id=id, name=name, fermenter=fermenter, props=props, type=type, status=status, instance=None)
step.instance = FermentStep( self.cbpi, step, self._done)
step = FermenterStep(id=id, name=name, fermenter=fermenter, props=props, type=type, status=status, instance=instance)
#logging.info(step)
#logging.info(step.status)
return step
def _done(self, step_instance, result):
step_instance.step.status = StepState.DONE
def _done(self, step_instance, result, fermenter):
step_instance.step["status"] = "D"
self.save()
logging.info(step_instance)
logging.info(step_instance.step)
logging.info(fermenter)
if result == StepResult.NEXT:
asyncio.create_task(self.start(step_instance.step.fermenter.id))
asyncio.create_task(self.start(fermenter))
def _create(self, data):
try:
@ -283,44 +283,80 @@ class FermentationController:
with open(self.path, "w") as file:
json.dump(data, file, indent=4, sort_keys=True)
async def create_step(self, id, step: Step):
async def create_step(self, id, item):
try:
step.id = shortuuid.uuid()
item = self._find_by_id(id)
stepid = shortuuid.uuid()
props = item.get("props")
status = StepState("I")
type = item.get("type")
name = item.get("name")
props = Props(item.get("props"))
fermenter = self._find_by_id(id)
step.instance = FermentStep( self.cbpi, step, self._done)
try:
type_cfg = self.steptypes.get(type)
clazz = type_cfg.get("class")
instance = clazz(self.cbpi, fermenter, item, props, self._done)
except Exception as e:
logging.warning("Failed to create step instance %s - %s" % (id, e))
instance = None
step = FermenterStep(id=stepid, name=name, fermenter=fermenter, props=props, type=type, status=status, instance=instance)
item.steps.append(step)
fermenter.steps.append(step)
self.save()
fermentersteps=self.get_fermenter_steps()
self.push_update("fermenterstepupdate", fermentersteps)
self.push_update("fermenterstepupdate")
return step
except Exception as e:
self.logger.error(e)
async def update_step(self, id, step: FermenterStep):
item = self._find_by_id(id)
item.steps = list(map(lambda old: step if old.id == step.id else old, item.steps))
async def update_step(self, id, item):
fermenter = self._find_by_id(id)
stepid = item.get("id")
props = item.get("props")
status = StepState("I")
type = item.get("type")
#logging.info(type)
name = item.get("name")
props = Props(item.get("props"))
logging.info("update step")
try:
type_cfg = self.steptypes.get(type)
#logging.info(type_cfg)
clazz = type_cfg.get("class")
#logging.info(clazz)
instance = clazz(self.cbpi, fermenter, item, props, self._done)
except Exception as e:
logging.warning("Failed to create step instance %s - %s " % (item.id, e))
instance = None
step = FermenterStep(id=stepid, name=name, fermenter=fermenter, props=props, type=type, status=status, instance=instance)
#logging.info(step)
#logging.info(fermenter.steps)
try:
fermenter.steps = list(map(lambda old: step if old.id == step.id else old, fermenter.steps))
except Exception as e:
logging.info(e)
#logging.info(fermenter.steps)
self.save()
fermentersteps=self.get_fermenter_steps()
self.push_update("fermenterstepupdate", fermentersteps)
#logging.info("SAVEUPDATE")
self.push_update("fermenterstepupdate")
async def delete_step(self, id, stepid):
item = self._find_by_id(id)
# might require later check if step is active
item.steps = list(filter(lambda item: item.id != stepid, item.steps))
self.save()
fermentersteps=self.get_fermenter_steps()
self.push_update("fermenterstepupdate", fermentersteps)
self.push_update("fermenterstepupdate")
async def clearsteps(self, id):
item = self._find_by_id(id)
# might require later check if step is active
item.steps = []
self.save()
fermentersteps=self.get_fermenter_steps()
self.push_update("fermenterstepupdate", fermentersteps)
self.push_update("fermenterstepupdate")
def _find_by_status(self, data, status):
@ -330,17 +366,34 @@ class FermentationController:
return next((item for item in data if item.id == id), None)
async def start(self, id):
self.logger.info("Start")
self.logger.info("Start {}".format(id))
try:
item = self._find_by_id(id)
step = self._find_by_status(item.steps, StepState.INITIAL)
step = self._find_by_status(item.steps, StepState.ACTIVE)
if step is not None:
logging.error("Steps already running")
return
step = self._find_by_status(item.steps, StepState.STOP)
if step is not None:
await step.instance.start()
logging.info("Restarting step {}".format(step.name))
step.status = StepState.ACTIVE
self.save()
self.push_update("fermenterstepupdate")
return
step = self._find_by_status(item.steps, StepState.INITIAL)
logging.info(step)
if step is None:
self.logger.info("No futher step to start")
else:
await step.instance.start()
logging.info("Starting step {}".format(step.name))
step.status = StepState.ACTIVE
self.save()
self.push_update("fermenterstepupdate")
except Exception as e:
self.logger.error(e)
@ -349,9 +402,19 @@ class FermentationController:
try:
item = self._find_by_id(id)
step = self._find_by_status(item.steps, StepState.ACTIVE)
await step.instance.stop()
step.status = StepState.STOP
self.save()
logging.info(step)
logging.info(step.status)
if step != None:
logging.info("CALLING STOP STEP")
try:
await step.instance.stop()
step.status = StepState.STOP
self.save()
except Exception as e:
logging.error("Failed to stop fermenterstep - Id: %s" % step.id)
self.push_update("fermenterstepupdate")
except Exception as e:
self.logger.error(e)
@ -396,8 +459,27 @@ class FermentationController:
self.logger.info("Next {} ".format(id))
try:
item = self._find_by_id(id)
logging.info(item)
step = self._find_by_status(item.steps, StepState.ACTIVE)
await step.instance.next()
logging.info(step)
if step is not None:
if step.instance is not None:
step.status = StepState.DONE
await step.instance.next()
step = self._find_by_status(item.steps, StepState.STOP)
logging.info(step)
if step is not None:
if step.instance is not None:
logging.info(step)
step.status = StepState.DONE
logging.info(step)
self.save()
await self.start(id)
else:
logging.info("No Step is running")
self.push_update("fermenterstepupdate")
except Exception as e:
self.logger.error(e)
@ -415,6 +497,8 @@ class FermentationController:
except Exception as e:
self.logger.error(e)
self.save()
self.push_update("fermenterstepupdate")
except Exception as e:
self.logger.error(e)
@ -435,4 +519,18 @@ class FermentationController:
except Exception as e:
self.logger.error(e)
def push_update(self, key="fermenterupdate"):
if key == self.update_key:
self.cbpi.ws.send(dict(topic=key, data=list(map(lambda item: item.to_dict(), self.data))))
#self.cbpi.push_update("cbpi/{}".format(self.update_key), list(map(lambda item: item.to_dict(), self.data)))
for item in self.data:
self.cbpi.push_update("cbpi/{}/{}".format(self.update_key,item.id), item.to_dict())
pass
else:
#logging.info("FERMENTERSTEPUPDATE {}".format(key))
fermentersteps=self.get_fermenter_steps()
self.cbpi.ws.send(dict(topic=key, data=fermentersteps))

View file

@ -46,8 +46,8 @@ class StepController:
except Exception as e:
logging.warning("Failed to create step instance %s - %s" % (id, e))
instance = None
return Step(id, name, type=type, status=status, instance=instance, props=props )
step=Step(id, name, type=type, status=status, instance=instance, props=props )
return step
def load(self, startActive=False):

View file

@ -62,13 +62,13 @@ class FermenterNotificationStep(CBPiFermentationStep):
@parameters([Property.Number(label="Temp", configurable=True),
Property.Sensor(label="Sensor"),
Property.Fermenter(label="Fermenter"),
Property.Text(label="Notification",configurable = True, description = "Text for notification when Temp is reached"),
Property.Select(label="AutoMode",options=["Yes","No"], description="Switch Fermenterlogic automatically on and off -> Yes")])
class FermenterTargetTempStep(CBPiFermentationStep):
async def NextStep(self, **kwargs):
await self.next()
return StepResult.DONE
#await self.next(self.fermenter)
async def on_timer_done(self,timer):
self.summary = ""
@ -76,20 +76,21 @@ class FermenterTargetTempStep(CBPiFermentationStep):
await self.push_update()
if self.AutoMode == True:
await self.setAutoMode(False)
self.cbpi.notify(self.name, self.props.get("Notification","Target Temp reached. Please add malt and klick next to move on."), action=[NotificationAction("Next Step", self.NextStep)])
self.cbpi.notify(self.name, self.props.get("Notification","Target Temp reached. Please add malt and klick next to move on."))
await self.next()
async def on_timer_update(self,timer, seconds):
await self.push_update()
async def on_start(self):
self.AutoMode = True if self.props.get("AutoMode","No") == "Yes" else False
self.fermenter=self.get_fermenter(self.props.get("Fermenter", None))
self.starttemp= self.get_sensor_value(self.props.get("Sensor", None)).get("value")
if self.fermenter is not None:
self.fermenter.target_temp = int(self.props.get("Temp", 0))
if self.AutoMode == True:
await self.setAutoMode(True)
self.summary = "Waiting for Target Temp"
if self.cbpi.fermenter is not None and self.timer is None:
if self.fermenter is not None and self.timer is None:
self.timer = Timer(1 ,on_update=self.on_timer_update, on_done=self.on_timer_done)
await self.push_update()
@ -101,12 +102,20 @@ class FermenterTargetTempStep(CBPiFermentationStep):
await self.push_update()
async def run(self):
while self.running == True:
await asyncio.sleep(1)
sensor_value = self.get_sensor_value(self.props.get("Sensor", None)).get("value")
if sensor_value >= int(self.props.get("Temp",0)) and self.timer.is_running is not True:
self.timer.start()
self.timer.is_running = True
if self.fermenter.target_temp >= self.starttemp:
while self.running == True:
sensor_value = self.get_sensor_value(self.props.get("Sensor", None)).get("value")
if sensor_value >= int(self.props.get("Temp",0)) and self.timer.is_running is not True:
self.timer.start()
self.timer.is_running = True
await asyncio.sleep(1)
else:
while self.running == True:
sensor_value = self.get_sensor_value(self.props.get("Sensor", None)).get("value")
if sensor_value <= int(self.props.get("Temp",0)) and self.timer.is_running is not True:
self.timer.start()
self.timer.is_running = True
await asyncio.sleep(1)
await self.push_update()
return StepResult.DONE
@ -116,9 +125,9 @@ class FermenterTargetTempStep(CBPiFermentationStep):
async def setAutoMode(self, auto_state):
try:
if (self.fermenter.instance is None or self.fermenter.instance.state == False) and (auto_state is True):
await self.cbpi.fermenter.toggle(self.fermenter.id)
await self.fermenter.instance.toggle(self.fermenter.id)
elif (self.fermenter.instance.state == True) and (auto_state is False):
await self.cbpi.fermenter.stop(self.fermenter.id)
await self.fermenter.instance.stop(self.fermenter.id)
await self.push_update()
except Exception as e:
@ -128,7 +137,6 @@ class FermenterTargetTempStep(CBPiFermentationStep):
@parameters([Property.Number(label="Timer", description="Time in Minutes", configurable=True),
Property.Number(label="Temp", configurable=True),
Property.Sensor(label="Sensor"),
Property.Fermenter(label="Fermenter"),
Property.Select(label="AutoMode",options=["Yes","No"], description="Switch Fermenterlogic automatically on and off -> Yes")])
class FermenterStep(CBPiFermentationStep):
@ -164,17 +172,18 @@ class FermenterStep(CBPiFermentationStep):
await self.push_update()
async def on_start(self):
logging.info(self.fermenter)
self.AutoMode = True if self.props.get("AutoMode", "No") == "Yes" else False
self.fermenter=self.get_fermenter(self.props.Fermenter)
self.starttemp= self.get_sensor_value(self.props.get("Sensor", None)).get("value")
if self.fermenter is not None:
self.fermenter.target_temp = int(self.props.get("Temp", 0))
if self.AutoMode == True:
await self.setAutoMode(True)
await self.push_update()
if self.cbpi.fermenter is not None and self.timer is None:
if self.fermenter is not None and self.timer is None:
self.timer = Timer(int(self.props.get("Timer",0)) *60 ,on_update=self.on_timer_update, on_done=self.on_timer_done)
elif self.cbpi.fermenter is not None:
elif self.fermenter is not None:
try:
if self.timer.is_running == True:
self.timer.start()
@ -195,22 +204,33 @@ class FermenterStep(CBPiFermentationStep):
self.timer = Timer(int(self.props.get("Timer",0)) *60 ,on_update=self.on_timer_update, on_done=self.on_timer_done)
async def run(self):
while self.running == True:
await asyncio.sleep(1)
sensor_value = self.get_sensor_value(self.props.get("Sensor", None)).get("value")
if sensor_value >= int(self.props.get("Temp",0)) and self.timer.is_running is not True:
self.timer.start()
self.timer.is_running = True
estimated_completion_time = datetime.fromtimestamp(time.time()+ (int(self.props.get("Timer",0)))*60)
self.cbpi.notify(self.name, 'Timer started. Estimated completion: {}'.format(estimated_completion_time.strftime("%H:%M")), NotificationType.INFO)
if self.fermenter.target_temp >= self.starttemp:
while self.running == True:
await asyncio.sleep(1)
sensor_value = self.get_sensor_value(self.props.get("Sensor", None)).get("value")
if sensor_value >= int(self.props.get("Temp",0)) and self.timer.is_running is not True:
self.timer.start()
self.timer.is_running = True
estimated_completion_time = datetime.fromtimestamp(time.time()+ (int(self.props.get("Timer",0)))*60)
self.cbpi.notify(self.name, 'Timer started. Estimated completion: {}'.format(estimated_completion_time.strftime("%H:%M")), NotificationType.INFO)
else:
while self.running == True:
await asyncio.sleep(1)
sensor_value = self.get_sensor_value(self.props.get("Sensor", None)).get("value")
if sensor_value <= int(self.props.get("Temp",0)) and self.timer.is_running is not True:
self.timer.start()
self.timer.is_running = True
estimated_completion_time = datetime.fromtimestamp(time.time()+ (int(self.props.get("Timer",0)))*60)
self.cbpi.notify(self.name, 'Timer started. Estimated completion: {}'.format(estimated_completion_time.strftime("%H:%M")), NotificationType.INFO)
return StepResult.DONE
async def setAutoMode(self, auto_state):
try:
if (self.fermenter.instance is None or self.fermenter.instance.state == False) and (auto_state is True):
await self.cbpi.fermenter.toggle(self.fermenter.id)
await self.fermenter.instance.toggle(self.fermenter.id)
elif (self.fermenter.instance.state == True) and (auto_state is False):
await self.cbpi.fermenter.stop(self.fermenter.id)
await self.fermenter.instance.stop(self.fermenter.id)
await self.push_update()
except Exception as e:

View file

@ -313,8 +313,8 @@ class FermentationHttpEndpoints():
data = await request.json()
fermenterid= request.match_info['id']
step = FermenterStep(name=data.get("name"), props=Props(data.get("props", {})), type=data.get("type"))
response_data = await self.controller.create_step(fermenterid,step)
newstep = {"name": data.get("name"), "props": data.get("props", {}), "type": data.get("type")}
response_data = await self.controller.create_step(fermenterid,newstep)
return web.json_response(data=response_data.to_dict())
@request_mapping(path="/{fermenterid}/{stepid}", method="PUT", auth_required=False)
@ -352,8 +352,9 @@ class FermentationHttpEndpoints():
data = await request.json()
stepid = request.match_info['stepid']
fermenterid = request.match_info['fermenterid']
step = FermenterStep(stepid, data.get("name"), None, Props(data.get("props", {})), data.get("type"))
await self.controller.update_step(fermenterid,step)
updatedstep = {"id": stepid, "name": data.get("name"), "props": data.get("props", {}), "type": data.get("type")}
#step = FermenterStep(stepid, data.get("name"), None, Props(data.get("props", {})), data.get("type"))
await self.controller.update_step(fermenterid,updatedstep)
return web.Response(status=200)
@request_mapping(path="/{fermenterid}/{stepid}", method="DELETE", auth_required=False)
@ -462,4 +463,124 @@ class FermentationHttpEndpoints():
fermenterid= request.match_info['id']
await self.controller.clearsteps(fermenterid)
return web.Response(status=200)
@request_mapping(path="/{id}/startstep", method="POST", auth_required=False)
async def http_start_steps(self, request):
"""
---
description: Start steps for Fermenter with fermenterid
tags:
- Fermenter
parameters:
- name: "id"
in: "path"
description: "Fermenter ID"
required: true
type: "integer"
format: "int64"
responses:
"200":
description: successful operation
"""
fermenterid= request.match_info['id']
await self.controller.start(fermenterid)
return web.Response(status=200)
@request_mapping(path="/{id}/stopstep", method="POST", auth_required=False)
async def http_stop_steps(self, request):
"""
---
description: Stop steps for Fermenter with fermenterid
tags:
- Fermenter
parameters:
- name: "id"
in: "path"
description: "Fermenter ID"
required: true
type: "integer"
format: "int64"
responses:
"200":
description: successful operation
"""
fermenterid= request.match_info['id']
await self.controller.stop(fermenterid)
return web.Response(status=200)
@request_mapping(path="/{id}/nextstep", method="POST", auth_required=False)
async def http_next_step(self, request):
"""
---
description: Stop steps for Fermenter with fermenterid
tags:
- Fermenter
parameters:
- name: "id"
in: "path"
description: "Fermenter ID"
required: true
type: "integer"
format: "int64"
responses:
"200":
description: successful operation
"""
fermenterid= request.match_info['id']
await self.controller.next(fermenterid)
return web.Response(status=200)
@request_mapping(path="/{id}/nextstep", method="POST", auth_required=False)
async def http_next_step(self, request):
"""
---
description: Triggers next step for Fermenter with fermenterid
tags:
- Fermenter
parameters:
- name: "id"
in: "path"
description: "Fermenter ID"
required: true
type: "integer"
format: "int64"
responses:
"200":
description: successful operation
"""
fermenterid= request.match_info['id']
await self.controller.next(fermenterid)
return web.Response(status=200)
@request_mapping(path="/{id}/reset", method="POST", auth_required=False)
async def http_reset(self, request):
"""
---
description: Resets step status for Fermenter with fermenterid
tags:
- Fermenter
parameters:
- name: "id"
in: "path"
description: "Fermenter ID"
required: true
type: "integer"
format: "int64"
responses:
"200":
description: successful operation
"""
fermenterid= request.match_info['id']
await self.controller.reset(fermenterid)
return web.Response(status=200)