From 366d6cbe91d5cb61a8341e1319cbace57bcdb609 Mon Sep 17 00:00:00 2001 From: avollkopf <43980694+avollkopf@users.noreply.github.com> Date: Thu, 26 Jan 2023 17:43:19 +0100 Subject: [PATCH] Improved handling of missing or corrupt step_data and fermenterstep_data files at startup --- cbpi/__init__.py | 2 +- cbpi/configFolder.py | 4 +-- cbpi/controller/fermentation_controller.py | 20 ++++++++--- cbpi/controller/step_controller.py | 42 ++++++++++++++++------ 4 files changed, 50 insertions(+), 18 deletions(-) diff --git a/cbpi/__init__.py b/cbpi/__init__.py index 6c0ca09..2f7ee36 100644 --- a/cbpi/__init__.py +++ b/cbpi/__init__.py @@ -1,3 +1,3 @@ -__version__ = "4.1.0.rc5" +__version__ = "4.1.0.rc6" __codename__ = "Groundhog Day" diff --git a/cbpi/configFolder.py b/cbpi/configFolder.py index 043ed2e..807550b 100644 --- a/cbpi/configFolder.py +++ b/cbpi/configFolder.py @@ -91,8 +91,8 @@ class ConfigFolder: ['actor.json', 'file'], ['sensor.json', 'file'], ['kettle.json', 'file'], - ['fermenter_data.json', 'file'], - ['step_data.json', 'file'], + #['fermenter_data.json', 'file'], created by fermentation_controller @ start if not available + #['step_data.json', 'file'], created by step_controller @ start if not available ['config.json', 'file'], ['craftbeerpi.service', 'file'], ['chromium.desktop', 'file'], diff --git a/cbpi/controller/fermentation_controller.py b/cbpi/controller/fermentation_controller.py index 8fa5de2..9dbc6ba 100644 --- a/cbpi/controller/fermentation_controller.py +++ b/cbpi/controller/fermentation_controller.py @@ -36,7 +36,7 @@ class FermentationController: def check_fermenter_file(self): if os.path.exists(self.cbpi.config_folder.get_file_path("fermenter_data.json")) is False: - logging.info("INIT fermenter_data.json file") + logging.warning("Missing fermenter_data.json file. INIT empty file") data = { "data": [ ] @@ -71,11 +71,23 @@ class FermentationController: async def load(self): - with open(self.path) as json_file: - data = json.load(json_file) + try: + with open(self.path) as json_file: + data = json.load(json_file) + for i in data["data"]: + self.data.append(self._create(i)) + except: + logging.warning("Invalid fermenter_data.json file - Creating empty file") + os.remove(self.path) + data = { + "data": [ + ] + } + destfile = self.cbpi.config_folder.get_file_path("fermenter_data.json") + json.dump(data,open(destfile,'w'),indent=4, sort_keys=True) for i in data["data"]: - self.data.append(self._create(i)) + self.data.append(self._create(i)) def _create_step(self, fermenter, item): id = item.get("id") diff --git a/cbpi/controller/step_controller.py b/cbpi/controller/step_controller.py index 8850fd9..651b3fb 100644 --- a/cbpi/controller/step_controller.py +++ b/cbpi/controller/step_controller.py @@ -6,6 +6,7 @@ import yaml import logging import os.path from os import listdir +import os from os.path import isfile, join import shortuuid from cbpi.api.dataclasses import NotificationAction, Props, Step @@ -54,23 +55,42 @@ class StepController: # create file if not exists if os.path.exists(self.path) is False: + logging.warning("Missing step_data.json file. INIT empty file") with open(self.path, "w") as file: json.dump(dict(basic={}, steps=[]), file, indent=4, sort_keys=True) #load from json file - with open(self.path) as json_file: - data = json.load(json_file) - self.basic_data = data["basic"] - self.profile = data["steps"] + try: + with open(self.path) as json_file: + data = json.load(json_file) + self.basic_data = data["basic"] + self.profile = data["steps"] + # Start step after start up + self.profile = list(map(lambda item: self.create(item), self.profile)) + if startActive is True: + active_step = self.find_by_status("A") + if active_step is not None: + asyncio.get_event_loop().create_task(self.start_step(active_step)) + #self._loop.create_task(self.start_step(active_step)) + except: + logging.warning("Invalid step_data.json file - Creating empty file") + os.remove(self.path) + with open(self.path, "w") as file: + json.dump(dict(basic={"name": ""}, steps=[]), file, indent=4, sort_keys=True) + + with open(self.path) as json_file: + data = json.load(json_file) + self.basic_data = data["basic"] + self.profile = data["steps"] - # Start step after start up - self.profile = list(map(lambda item: self.create(item), self.profile)) - if startActive is True: - active_step = self.find_by_status("A") - if active_step is not None: - asyncio.get_event_loop().create_task(self.start_step(active_step)) - #self._loop.create_task(self.start_step(active_step)) + # Start step after start up + self.profile = list(map(lambda item: self.create(item), self.profile)) + if startActive is True: + active_step = self.find_by_status("A") + if active_step is not None: + asyncio.get_event_loop().create_task(self.start_step(active_step)) + #self._loop.create_task(self.start_step(active_step)) async def add(self, item: Step): logging.debug("Add step")