Improved handling of missing or corrupt step_data and fermenterstep_data files at startup

This commit is contained in:
avollkopf 2023-01-26 17:43:19 +01:00
parent 9e50b790e0
commit 366d6cbe91
4 changed files with 50 additions and 18 deletions

View file

@ -1,3 +1,3 @@
__version__ = "4.1.0.rc5"
__version__ = "4.1.0.rc6"
__codename__ = "Groundhog Day"

View file

@ -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'],

View file

@ -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")

View file

@ -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")