Autostart active or stopped step with orignila endtime

This commit is contained in:
avollkopf 2022-02-27 18:45:57 +01:00
parent ff37779863
commit 580bdd899a
6 changed files with 70 additions and 24 deletions

View file

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

View file

@ -162,8 +162,7 @@ class FermenterStep:
props: Props = Props() props: Props = Props()
type: str = None type: str = None
status: StepState = StepState.INITIAL status: StepState = StepState.INITIAL
#Add end data as unixtime to setp when active endtime: int = 0 # endtime if step is active and timer is running
#step_end: float = 0
instance: str = None instance: str = None
step: dict = None step: dict = None
@ -171,7 +170,7 @@ class FermenterStep:
return "name={} props={}, type={}, instance={}".format(self.name, self.props, self.type, self.instance) return "name={} props={}, type={}, instance={}".format(self.name, self.props, self.type, self.instance)
def to_dict(self): def to_dict(self):
msg = self.instance.summary if self.instance is not None else "" msg = self.instance.summary if self.instance is not None else ""
return dict(id=self.id, name=self.name, state_text=msg, type=self.type, status=self.status.value, props=self.props.to_dict()) return dict(id=self.id, name=self.name, state_text=msg, type=self.type, status=self.status.value, endtime = self.endtime, props=self.props.to_dict())

View file

@ -136,6 +136,7 @@ class CBPiFermentationStep(CBPiBase):
self.timer = None self.timer = None
self._done_callback = on_done self._done_callback = on_done
self.props = props self.props = props
self.endtime = int(step.get("endtime"))
self.cancel_reason: StepResult = None self.cancel_reason: StepResult = None
self.summary = "" self.summary = ""
self.task = None self.task = None
@ -187,8 +188,11 @@ class CBPiFermentationStep(CBPiBase):
async def on_props_update(self, props): async def on_props_update(self, props):
self.props = {**self.props, **props} self.props = {**self.props, **props}
async def update_endtime(self):
await self.cbpi.fermenter.update_endtime(self.fermenter.id, self.id, self.endtime)
async def save_props(self): async def save_props(self):
await self.cbpi.step.save() self.cbpi.fermenter.save()
async def push_update(self): async def push_update(self):
self.cbpi.fermenter.push_update(self.update_key) self.cbpi.fermenter.push_update(self.update_key)

View file

@ -156,8 +156,11 @@ class FermentationController:
name = item.get("name") name = item.get("name")
props = Props(item.get("props")) props = Props(item.get("props"))
status = StepState(item.get("status", "I")) status = StepState(item.get("status", "I"))
endtime = int(item.get("endtime", 0))
if status == StepState.ACTIVE: if status == StepState.ACTIVE:
status = StepState("S") status = StepState("S")
if status != StepState.STOP:
endtime = 0
type = item.get("type") type = item.get("type")
try: try:
@ -168,10 +171,11 @@ class FermentationController:
logging.warning("Failed to create step instance %s - %s" % (id, e)) logging.warning("Failed to create step instance %s - %s" % (id, e))
instance = None instance = None
step = FermenterStep(id=id, name=name, fermenter=fermenter, props=props, type=type, status=status, instance=instance) step = FermenterStep(id=id, name=name, fermenter=fermenter, props=props, type=type, status=status, endtime=endtime, instance=instance)
return step return step
def _done(self, step_instance, result, fermenter): def _done(self, step_instance, result, fermenter):
logging.info(result)
step_instance.step["status"] = "D" step_instance.step["status"] = "D"
self.save() self.save()
if result == StepResult.NEXT: if result == StepResult.NEXT:
@ -385,6 +389,17 @@ class FermentationController:
def _find_step_by_id(self, data, id): def _find_step_by_id(self, data, id):
return next((item for item in data if item.id == id), None) return next((item for item in data if item.id == id), None)
async def update_endtime(self, id, stepid, endtime):
try:
item = self._find_by_id(id)
step = self._find_step_by_id(item.steps, stepid)
step.endtime = int(endtime)
self.save()
self.push_update("fermenterstepupdate")
except Exception as e:
self.logger.error(e)
async def start(self, id): async def start(self, id):
self.logger.info("Start {}".format(id)) self.logger.info("Start {}".format(id))
try: try:
@ -397,8 +412,11 @@ class FermentationController:
step = self._find_by_status(item.steps, StepState.STOP) step = self._find_by_status(item.steps, StepState.STOP)
if step is not None: if step is not None:
endtime = step.endtime
await step.instance.start() await step.instance.start()
logging.info("Restarting step {}".format(step.name)) logging.info("Restarting step {}".format(step.name))
if endtime != 0:
logging.info("Need to change timer")
step.status = StepState.ACTIVE step.status = StepState.ACTIVE
self.save() self.save()
self.push_update("fermenterstepupdate") self.push_update("fermenterstepupdate")
@ -422,8 +440,8 @@ class FermentationController:
try: try:
item = self._find_by_id(id) item = self._find_by_id(id)
step = self._find_by_status(item.steps, StepState.ACTIVE) step = self._find_by_status(item.steps, StepState.ACTIVE)
logging.info(step) #logging.info(step)
logging.info(step.status) #logging.info(step.status)
if step != None: if step != None:
logging.info("CALLING STOP STEP") logging.info("CALLING STOP STEP")
try: try:
@ -514,6 +532,7 @@ class FermentationController:
try: try:
await step.instance.stop() await step.instance.stop()
step.status = StepState.INITIAL step.status = StepState.INITIAL
step.endtime = 0
except Exception as e: except Exception as e:
self.logger.error(e) self.logger.error(e)
self.save() self.save()

View file

@ -134,7 +134,7 @@ class FermenterTargetTempStep(CBPiFermentationStep):
if (self.fermenter.instance is None or self.fermenter.instance.state == False) and (auto_state is True): 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.cbpi.fermenter.toggle(self.fermenter.id)
elif (self.fermenter.instance.state == True) and (auto_state is False): elif (self.fermenter.instance.state == True) and (auto_state is False):
await self.fermenter.instance.stop() await self.cbpi.fermenter.toggle(self.fermenter.id)
await self.push_update() await self.push_update()
except Exception as e: except Exception as e:
@ -155,18 +155,20 @@ class FermenterStep(CBPiFermentationStep):
self.cbpi.notify(self.name, 'Timer started', NotificationType.INFO) self.cbpi.notify(self.name, 'Timer started', NotificationType.INFO)
self.timer.start() self.timer.start()
self.timer.is_running = True self.timer.is_running = True
self.endtime = time.time() + self.fermentationtime
await self.update_endtime()
estimated_completion_time = datetime.fromtimestamp(time.time()+ self.fermentationtime) estimated_completion_time = datetime.fromtimestamp(time.time()+ self.fermentationtime)
self.cbpi.notify(self.name, 'Timer started. Estimated completion: {}'.format(estimated_completion_time.strftime("%d.%m, %H:%M")), NotificationType.INFO) self.cbpi.notify(self.name, 'Timer started. Estimated completion: {}'.format(estimated_completion_time.strftime("%d.%m, %H:%M")), NotificationType.INFO)
else: else:
self.cbpi.notify(self.name, 'Timer is already running', NotificationType.WARNING) self.cbpi.notify(self.name, 'Timer is already running', NotificationType.WARNING)
@action("Add 5 Minutes to Timer", []) #@action("Add 5 Minutes to Timer", [])
async def add_timer(self): #async def add_timer(self):
if self.timer.is_running == True: # if self.timer.is_running == True:
self.cbpi.notify(self.name, '5 Minutes added', NotificationType.INFO) # self.cbpi.notify(self.name, '5 Minutes added', NotificationType.INFO)
await self.timer.add(300) # await self.timer.add(300)
else: # else:
self.cbpi.notify(self.name, 'Timer must be running to add time', NotificationType.WARNING) # self.cbpi.notify(self.name, 'Timer must be running to add time', NotificationType.WARNING)
async def on_timer_done(self,timer): async def on_timer_done(self,timer):
@ -174,6 +176,8 @@ class FermenterStep(CBPiFermentationStep):
self.fermenter.target_temp = 0 self.fermenter.target_temp = 0
if self.AutoMode == True: if self.AutoMode == True:
await self.setAutoMode(False) await self.setAutoMode(False)
#self.endtime = 0
#await self.update_endtime()
self.cbpi.notify(self.name, 'Step finished', NotificationType.SUCCESS) self.cbpi.notify(self.name, 'Step finished', NotificationType.SUCCESS)
await self.next(self.fermenter.id) await self.next(self.fermenter.id)
return StepResult.DONE return StepResult.DONE
@ -184,10 +188,16 @@ class FermenterStep(CBPiFermentationStep):
await self.push_update() await self.push_update()
async def on_start(self): async def on_start(self):
if self.endtime == 0:
timeD=int(self.props.get("TimerD", 0)) timeD=int(self.props.get("TimerD", 0))
timeH=int(self.props.get("TimerH", 0)) timeH=int(self.props.get("TimerH", 0))
timeM=int(self.props.get("TimerM", 0)) timeM=int(self.props.get("TimerM", 0))
self.fermentationtime=(timeM+(60*timeH)+(1440*timeD)) *60 self.fermentationtime=(timeM+(60*timeH)+(1440*timeD)) *60
else:
self.fermentationtime = self.endtime - time.time()
self.AutoMode = True if self.props.get("AutoMode", "No") == "Yes" else False self.AutoMode = True if self.props.get("AutoMode", "No") == "Yes" else False
self.starttemp= self.get_sensor_value(self.props.get("Sensor", None)).get("value") self.starttemp= self.get_sensor_value(self.props.get("Sensor", None)).get("value")
@ -198,13 +208,19 @@ class FermenterStep(CBPiFermentationStep):
await self.push_update() await self.push_update()
if self.fermenter is not None and self.timer is None: if self.fermenter is not None and self.timer is None:
logging.info("Set Timer")
self.timer = Timer(self.fermentationtime ,on_update=self.on_timer_update, on_done=self.on_timer_done) self.timer = Timer(self.fermentationtime ,on_update=self.on_timer_update, on_done=self.on_timer_done)
elif self.fermenter is not None: elif self.fermenter is not None:
try: try:
if self.timer.is_running == True: if self.timer.is_running == True:
self.timer.start() self.timer.start()
self.endtime = time.time() + self.fermentationtime
await self.update_endtime()
except: except:
pass pass
if self.endtime != 0 and self.timer is not None:
self.timer.start()
self.timer.is_running = True
self.summary = "Waiting for Target Temp" self.summary = "Waiting for Target Temp"
await self.push_update() await self.push_update()
@ -218,6 +234,8 @@ class FermenterStep(CBPiFermentationStep):
async def reset(self): async def reset(self):
self.timer = Timer(self.fermentationtime ,on_update=self.on_timer_update, on_done=self.on_timer_done) self.timer = Timer(self.fermentationtime ,on_update=self.on_timer_update, on_done=self.on_timer_done)
self.endtime = 0
await self.update_endtime()
async def run(self): async def run(self):
if self.fermenter.target_temp >= self.starttemp: if self.fermenter.target_temp >= self.starttemp:
@ -228,6 +246,8 @@ class FermenterStep(CBPiFermentationStep):
if sensor_value >= self.fermenter.target_temp and self.timer.is_running is not True: if sensor_value >= self.fermenter.target_temp and self.timer.is_running is not True:
self.timer.start() self.timer.start()
self.timer.is_running = True self.timer.is_running = True
self.endtime = time.time() + self.fermentationtime
await self.update_endtime()
estimated_completion_time = datetime.fromtimestamp(time.time()+ self.fermentationtime) estimated_completion_time = datetime.fromtimestamp(time.time()+ self.fermentationtime)
self.cbpi.notify(self.name, 'Timer started. Estimated completion: {}'.format(estimated_completion_time.strftime("%d.%m, %H:%M")), NotificationType.INFO) self.cbpi.notify(self.name, 'Timer started. Estimated completion: {}'.format(estimated_completion_time.strftime("%d.%m, %H:%M")), NotificationType.INFO)
elif self.fermenter.target_temp <= self.starttemp: elif self.fermenter.target_temp <= self.starttemp:
@ -238,6 +258,8 @@ class FermenterStep(CBPiFermentationStep):
if sensor_value <= self.fermenter.target_temp and self.timer.is_running is not True: if sensor_value <= self.fermenter.target_temp and self.timer.is_running is not True:
self.timer.start() self.timer.start()
self.timer.is_running = True self.timer.is_running = True
self.endtime = time.time() + self.fermentationtime
await self.update_endtime()
estimated_completion_time = datetime.fromtimestamp(time.time()+ self.fermentationtime) estimated_completion_time = datetime.fromtimestamp(time.time()+ self.fermentationtime)
self.cbpi.notify(self.name, 'Timer started. Estimated completion: {}'.format(estimated_completion_time.strftime("%d.%m, %H:%M")), NotificationType.INFO) self.cbpi.notify(self.name, 'Timer started. Estimated completion: {}'.format(estimated_completion_time.strftime("%d.%m, %H:%M")), NotificationType.INFO)
@ -248,7 +270,7 @@ class FermenterStep(CBPiFermentationStep):
if (self.fermenter.instance is None or self.fermenter.instance.state == False) and (auto_state is True): 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.cbpi.fermenter.toggle(self.fermenter.id)
elif (self.fermenter.instance.state == True) and (auto_state is False): elif (self.fermenter.instance.state == True) and (auto_state is False):
await self.fermenter.instance.stop() await self.cbpi.fermenter.toggle(self.fermenter.id)
await self.push_update() await self.push_update()
except Exception as e: except Exception as e:
@ -304,4 +326,4 @@ def setup(cbpi):
cbpi.plugin.register("FermenterNotificationStep", FermenterNotificationStep) cbpi.plugin.register("FermenterNotificationStep", FermenterNotificationStep)
cbpi.plugin.register("FermenterTargetTempStep", FermenterTargetTempStep) cbpi.plugin.register("FermenterTargetTempStep", FermenterTargetTempStep)
cbpi.plugin.register("FermenterStep", FermenterStep) cbpi.plugin.register("FermenterStep", FermenterStep)
cbpi.plugin.register("FermenterWaitStep", FermenterWaitStep) #cbpi.plugin.register("FermenterWaitStep", FermenterWaitStep)

View file

@ -47,6 +47,8 @@ setup(name='cbpi',
'tabulate==0.8.7', 'tabulate==0.8.7',
'asyncio-mqtt', 'asyncio-mqtt',
'psutil==5.8.0', 'psutil==5.8.0',
'colorama==0.4.4',
'PyInquirer==1.0.3',
'cbpi4ui', 'cbpi4ui',
'importlib_metadata'] + ( 'importlib_metadata'] + (
['RPi.GPIO==0.7.1'] if raspberrypi else [] ) + ['RPi.GPIO==0.7.1'] if raspberrypi else [] ) +