From cf77b8889763034d2f24e32fc8a9f4ad5adca2e9 Mon Sep 17 00:00:00 2001 From: Lutz Bender Date: Sat, 20 Jan 2024 10:51:04 +0100 Subject: [PATCH] fix daily log date --- packages/helpermodules/update_config.py | 32 ++++++++++++++++++++++--- 1 file changed, 29 insertions(+), 3 deletions(-) diff --git a/packages/helpermodules/update_config.py b/packages/helpermodules/update_config.py index e5168f39ca..8c0b3c5a87 100644 --- a/packages/helpermodules/update_config.py +++ b/packages/helpermodules/update_config.py @@ -33,7 +33,7 @@ class UpdateConfig: - DATASTORE_VERSION = 35 + DATASTORE_VERSION = 36 valid_topic = [ "^openWB/bat/config/configured$", "^openWB/bat/set/charging_power_left$", @@ -1135,7 +1135,9 @@ def convert_file(file): for e in content["entries"]: if type(e["date"]) is not str: old_date = datetime.datetime.fromtimestamp(e["date"]) - e["date"] = old_date.strftime('%H:$M') + # old version had a bug formatting "date" '$M' <-> '%M' + # e["date"] = old_date.strftime('%H:$M') + e["date"] = old_date.strftime('%H:%M') if type(e["timestamp"]) is float: e["timestamp"] = int(e["timestamp"]) jsonFile.seek(0) @@ -1149,4 +1151,28 @@ def convert_file(file): files = glob.glob(str(self.base_path / "data" / "daily_log") + "/*") for file in files: convert_file(file) - Pub().pub("openWB/system/datastore_version", 35) + # next upgrade only fixes a bug introduced in an earlier version of this method + # so we can skip upgrade_datastore_35() if this fixed version has run + Pub().pub("openWB/system/datastore_version", 36) + + def upgrade_datastore_35(self) -> None: + def convert_file(file): + try: + with open(file, "r+") as jsonFile: + content = json.load(jsonFile) + for e in content["entries"]: + if type(e["date"]) is str and '$M' in e["date"]: + old_timestamp = datetime.datetime.fromtimestamp(e["timestamp"]) + e["date"] = old_timestamp.strftime('%H:%M') + jsonFile.seek(0) + json.dump(content, jsonFile) + jsonFile.truncate() + log.debug(f"Format der Logdatei {file} aktualisiert.") + except FileNotFoundError: + pass + except Exception: + log.exception(f"Logfile {file} konnte nicht konvertiert werden.") + files = glob.glob(str(self.base_path / "data" / "daily_log") + "/*") + for file in files: + convert_file(file) + Pub().pub("openWB/system/datastore_version", 36)