Skip to content

Commit

Permalink
fix duration values of "0:60"
Browse files Browse the repository at this point in the history
  • Loading branch information
benderl committed Oct 17, 2023
1 parent d4cb5b6 commit 00e599d
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 20 deletions.
16 changes: 4 additions & 12 deletions packages/helpermodules/data_migration/data_migration.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand Down
13 changes: 6 additions & 7 deletions packages/helpermodules/timecheck.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand All @@ -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
------
Expand Down Expand Up @@ -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"
Expand All @@ -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}"
22 changes: 21 additions & 1 deletion packages/helpermodules/update_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@


class UpdateConfig:
DATASTORE_VERSION = 22
DATASTORE_VERSION = 23
valid_topic = [
"^openWB/bat/config/configured$",
"^openWB/bat/set/charging_power_left$",
Expand Down Expand Up @@ -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)

0 comments on commit 00e599d

Please sign in to comment.