Bug fix for first startup w/o fermenter

This commit is contained in:
avollkopf 2022-01-02 12:43:33 +01:00
parent d7c1b64493
commit 751b3dcad9
4 changed files with 56 additions and 44 deletions

View file

@ -1 +1 @@
__version__ = "4.0.1.a1" __version__ = "4.0.1.a2"

View file

@ -88,7 +88,7 @@ class FermentationController:
self.logger = logging.getLogger(__name__) self.logger = logging.getLogger(__name__)
self.path = os.path.join(".", 'config', "fermenter_data.json") self.path = os.path.join(".", 'config', "fermenter_data.json")
self._loop = asyncio.get_event_loop() self._loop = asyncio.get_event_loop()
self.data = {} self.data = []
self.types = {} self.types = {}
self.cbpi.app.on_cleanup.append(self.shutdown) self.cbpi.app.on_cleanup.append(self.shutdown)
@ -125,16 +125,14 @@ class FermentationController:
self.logger.error(e) self.logger.error(e)
async def load(self): async def load(self):
if os.path.exists(self.path) is False: # if os.path.exists(self.path) is False:
with open(self.path, "w") as file: # with open(self.path, "w") as file:
json.dump(dict(basic={}, steps=[]), file, indent=4, sort_keys=True) # json.dump(dict(basic={}, steps=[]), file, indent=4, sort_keys=True)
with open(self.path) as json_file: with open(self.path) as json_file:
d = json.load(json_file) data = json.load(json_file)
self.data = list(map(lambda item: self._create(item), d))
#for item in self.data: for i in data["data"]:
# logging.info("{} Starting ".format(item.name)) self.data.append(self._create(i))
# await self.start(item.id)
def _create_step(self, fermenter, item): def _create_step(self, fermenter, item):
id = item.get("id") id = item.get("id")
@ -159,22 +157,25 @@ class FermentationController:
asyncio.create_task(self.start(step_instance.step.fermenter.id)) asyncio.create_task(self.start(step_instance.step.fermenter.id))
def _create(self, data): def _create(self, data):
id = data.get("id") try:
name = data.get("name") id = data.get("id")
sensor = data.get("sensor") name = data.get("name")
heater = data.get("heater") sensor = data.get("sensor")
cooler = data.get("cooler") heater = data.get("heater")
logictype = data.get("type") cooler = data.get("cooler")
temp = data.get("target_temp") logictype = data.get("type")
brewname = data.get("brewname") temp = data.get("target_temp")
props = Props(data.get("props", {})) brewname = data.get("brewname")
fermenter = Fermenter(id, name, sensor, heater, cooler, brewname, props, temp, logictype) props = Props(data.get("props", {}))
fermenter.steps = list(map(lambda item: self._create_step(fermenter, item), data.get("steps", []))) fermenter = Fermenter(id, name, sensor, heater, cooler, brewname, props, temp, logictype)
self.push_update() fermenter.steps = list(map(lambda item: self._create_step(fermenter, item), data.get("steps", [])))
#self.cbpi.ws.send(dict(topic=self.update_key, data=list(map(lambda item: item.to_dict(), self.data)))) self.push_update()
#self.cbpi.push_update("cbpi/{}/update".format(self.update_key), list(map(lambda item: item.to_dict(), self.data))) #self.cbpi.ws.send(dict(topic=self.update_key, data=list(map(lambda item: item.to_dict(), self.data))))
#self.cbpi.push_update("cbpi/{}/update".format(self.update_key), list(map(lambda item: item.to_dict(), self.data)))
return fermenter
except:
return
return fermenter
def _find_by_id(self, id): def _find_by_id(self, id):
return next((item for item in self.data if item.id == id), None) return next((item for item in self.data if item.id == id), None)
@ -191,6 +192,9 @@ class FermentationController:
def get_state(self): def get_state(self):
# logging.info("{} Get State".format(self.name)) # logging.info("{} Get State".format(self.name))
if self.data == []:
logging.info(self.data)
return {"data": list(map(lambda x: x.to_dict(), self.data)), "types":self.get_types()} return {"data": list(map(lambda x: x.to_dict(), self.data)), "types":self.get_types()}
async def get(self, id: str ): async def get(self, id: str ):

View file

@ -18,26 +18,28 @@ class FermenterAutostart(CBPiExtension):
self._task = asyncio.create_task(self.run()) self._task = asyncio.create_task(self.run())
self.controller : FermentationController = cbpi.fermenter self.controller : FermentationController = cbpi.fermenter
async def run(self): async def run(self):
logging.info("Starting Fermenter Autorun") logging.info("Starting Fermenter Autorun")
#get all kettles #get all kettles
self.fermenter = self.controller.get_state() try:
for id in self.fermenter['data']: self.fermenter = self.controller.get_state()
try: for id in self.fermenter['data']:
self.autostart=(id['props']['AutoStart']) try:
if self.autostart == "Yes": self.autostart=(id['props']['AutoStart'])
fermenter_id=(id['id']) if self.autostart == "Yes":
logging.info("Enabling Autostart for Fermenter {}".format(fermenter_id)) fermenter_id=(id['id'])
self.fermenter=self.cbpi.fermenter._find_by_id(fermenter_id) logging.info("Enabling Autostart for Fermenter {}".format(fermenter_id))
try: self.fermenter=self.cbpi.fermenter._find_by_id(fermenter_id)
if (self.fermenter.instance is None or self.fermenter.instance.state == False): try:
await self.cbpi.fermenter.toggle(self.fermenter.id) if (self.fermenter.instance is None or self.fermenter.instance.state == False):
logging.info("Successfully switched on Ferenterlogic for Fermenter {}".format(self.fermenter.id)) await self.cbpi.fermenter.toggle(self.fermenter.id)
except Exception as e: logging.info("Successfully switched on Ferenterlogic for Fermenter {}".format(self.fermenter.id))
logging.error("Failed to switch on FermenterLogic {} {}".format(self.fermenter.id, e)) except Exception as e:
except: logging.error("Failed to switch on FermenterLogic {} {}".format(self.fermenter.id, e))
pass except:
pass
except:
pass
@parameters([Property.Number(label="HeaterOffsetOn", configurable=True, description="Offset as decimal number when the heater is switched on. Should be greater then 'HeaterOffsetOff'. For example a value of 2 switches on the heater if the current temperature is 2 degrees below the target temperature"), @parameters([Property.Number(label="HeaterOffsetOn", configurable=True, description="Offset as decimal number when the heater is switched on. Should be greater then 'HeaterOffsetOff'. For example a value of 2 switches on the heater if the current temperature is 2 degrees below the target temperature"),

View file

@ -27,6 +27,12 @@ class SystemHttpEndpoints:
"200": "200":
description: successful operation description: successful operation
""" """
try:
fermenter=self.cbpi.fermenter.get_state()
logging.info(fermenter)
except:
logging.info("!!!!!!!!!!!!!!!!!!!!!!!!!Error get fermenter state!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!")
return web.json_response(data=dict( return web.json_response(data=dict(
actor=self.cbpi.actor.get_state(), actor=self.cbpi.actor.get_state(),
fermenter=self.cbpi.fermenter.get_state(), fermenter=self.cbpi.fermenter.get_state(),