From 00e599d69895387752b00967b0099671d2036dc4 Mon Sep 17 00:00:00 2001 From: Lutz Bender Date: Tue, 10 Oct 2023 12:26:49 +0200 Subject: [PATCH] fix duration values of "0:60" --- .../data_migration/data_migration.py | 16 ++++---------- packages/helpermodules/timecheck.py | 13 +++++------ packages/helpermodules/update_config.py | 22 ++++++++++++++++++- 3 files changed, 31 insertions(+), 20 deletions(-) diff --git a/packages/helpermodules/data_migration/data_migration.py b/packages/helpermodules/data_migration/data_migration.py index 094cfd1af1..c86f404ee5 100644 --- a/packages/helpermodules/data_migration/data_migration.py +++ b/packages/helpermodules/data_migration/data_migration.py @@ -24,6 +24,7 @@ from helpermodules.hardware_configuration import update_hardware_configuration from helpermodules.measurement_logging.process_log import get_totals, string_to_float, string_to_int from helpermodules.measurement_logging.write_log import LegacySmartHomeLogData, get_names +from helpermodules.timecheck import convert_timedelta_to_time_string, get_difference from helpermodules.utils import thread_handler from helpermodules.pub import Pub @@ -196,18 +197,9 @@ def conv_1_9_datetimes(datetime_str): # Format Datum-Uhrzeit anpassen begin = conv_1_9_datetimes(row[0]) end = conv_1_9_datetimes(row[1]) - # Dauer formatieren - duration_list = row[5].split(" ") - if len(duration_list) == 2: - duration_list.pop(1) # "Min" - duration = f"00:{int(duration_list[0]):02d}" - elif len(duration_list) == 4: - duration_list.pop(1) # "H" - duration_list.pop(2) # "Min" - duration = f"{int(duration_list[0]):02d}:{int(duration_list[1]):02d}" - else: - raise ValueError(str(duration_list) + - " hat kein bekanntes Format.") + # Dauer neu berechnen, da die Dauer unter 1.9 falsch ausgegeben sein kann + duration = convert_timedelta_to_time_string( + datetime.timedelta(seconds=get_difference(begin, end))) old_cp = row[6].strip() # sometimes we have trailing spaces if data.data.cp_data.get(f"cp{self.map_to_new_ids(f'cp{old_cp}')}") is not None: cp_name = data.data.cp_data[f"cp{self.map_to_new_ids(f'cp{old_cp}')}"].data.config.name diff --git a/packages/helpermodules/timecheck.py b/packages/helpermodules/timecheck.py index ccf4737ae6..1f2565e80a 100644 --- a/packages/helpermodules/timecheck.py +++ b/packages/helpermodules/timecheck.py @@ -313,10 +313,9 @@ def get_difference_to_now(timestamp_begin: str) -> Tuple[str, int]: int: Differenz in Sekunden """ try: - begin = datetime.datetime.strptime(timestamp_begin[:-3], "%m/%d/%Y, %H:%M") - now = datetime.datetime.today() - diff = (now - begin) - return [__convert_timedelta_to_time_string(diff), int(diff.total_seconds())] + diff = datetime.timedelta( + seconds=get_difference(timestamp_begin, datetime.datetime.today().strftime("%m/%d/%Y, %H:%M"))) + return [convert_timedelta_to_time_string(diff), int(diff.total_seconds())] except Exception: log.exception("Fehler im System-Modul") return ["00:00", 0] @@ -330,7 +329,7 @@ def get_difference(timestamp_begin: str, timestamp_end: str) -> Optional[int]: timestamp_begin: str %m/%d/%Y, %H:%M:%S Anfangszeitpunkt timestamp_end: str %m/%d/%Y, %H:%M:%S - Anfangszeitpunkt + Endzeitpunkt Return ------ @@ -361,7 +360,7 @@ def duration_sum(first: str, second: str) -> str: """ try: sum = __get_timedelta_obj(first) + __get_timedelta_obj(second) - return __convert_timedelta_to_time_string(sum) + return convert_timedelta_to_time_string(sum) except Exception: log.exception("Fehler im System-Modul") return "00:00" @@ -388,7 +387,7 @@ def __get_timedelta_obj(time: str) -> datetime.timedelta: return delta -def __convert_timedelta_to_time_string(timedelta_obj: datetime.timedelta) -> str: +def convert_timedelta_to_time_string(timedelta_obj: datetime.timedelta) -> str: diff_hours = int(timedelta_obj.total_seconds() / 3600) diff_minutes = int((timedelta_obj.total_seconds() % 3600) / 60) return f"{diff_hours}:{diff_minutes:02d}" diff --git a/packages/helpermodules/update_config.py b/packages/helpermodules/update_config.py index 48e7a7f832..9cb46de409 100644 --- a/packages/helpermodules/update_config.py +++ b/packages/helpermodules/update_config.py @@ -28,7 +28,7 @@ class UpdateConfig: - DATASTORE_VERSION = 22 + DATASTORE_VERSION = 23 valid_topic = [ "^openWB/bat/config/configured$", "^openWB/bat/set/charging_power_left$", @@ -893,3 +893,23 @@ def upgrade_datastore_21(self) -> None: asdict(general_config)) Pub().pub(topic.replace("openWB/", "openWB/set/"), config_payload) Pub().pub("openWB/system/datastore_version", 22) + + def upgrade_datastore_22(self) -> None: + files = glob.glob("/var/www/html/openWB/data/charge_log/*") + for file in files: + modified = False + with open(file, "r+") as jsonFile: + try: + content = json.load(jsonFile) + for entry in content: + if entry.time.time_charged.endswith(":60"): + entry.time.time_charged = "1:00" + modified = True + if modified: + jsonFile.seek(0) + json.dump(content, jsonFile) + jsonFile.truncate() + log.debug(f"Format des Ladeprotokolls '{file}' aktualisiert.") + except Exception: + log.exception(f"Ladeprotokoll '{file}' konnte nicht aktualisiert werden.") + Pub().pub("openWB/system/datastore_version", 23)