Skip to content

Commit c89456e

Browse files
committed
Add_pulse_logs from sorted and pruned restored cache data
1 parent 164bad6 commit c89456e

File tree

1 file changed

+32
-29
lines changed

1 file changed

+32
-29
lines changed

plugwise_usb/nodes/circle.py

Lines changed: 32 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
from asyncio import Task, create_task
66
from collections.abc import Awaitable, Callable
77
from dataclasses import replace
8-
from datetime import UTC, datetime
8+
from datetime import UTC, datetime, timedelta
99
from functools import wraps
1010
import logging
1111
from typing import Any, Final, TypeVar, cast
@@ -26,7 +26,6 @@
2626
from ..constants import (
2727
DAY_IN_HOURS,
2828
DEFAULT_CONS_INTERVAL,
29-
MAX_LOG_HOURS,
3029
MAX_TIME_DRIFT,
3130
MINIMAL_POWER_UPDATE,
3231
NO_PRODUCTION_INTERVAL,
@@ -604,7 +603,7 @@ async def _energy_log_records_load_from_cache(self) -> bool:
604603
"Failed to restore energy log records from cache for node %s", self.name
605604
)
606605
return False
607-
restored_logs: dict[int, list[int]] = {}
606+
restored_logs: dict[int, dict[int, tuple[int, datetime]]] = {}
608607
if cache_data == "":
609608
_LOGGER.debug("Cache-record is empty")
610609
return False
@@ -617,38 +616,42 @@ async def _energy_log_records_load_from_cache(self) -> bool:
617616
if len(timestamp_energy_log) == 6:
618617
address = int(log_fields[0])
619618
slot = int(log_fields[1])
620-
self._energy_counters.add_pulse_log(
621-
address=address,
622-
slot=slot,
623-
timestamp=datetime(
624-
year=int(timestamp_energy_log[0]),
625-
month=int(timestamp_energy_log[1]),
626-
day=int(timestamp_energy_log[2]),
627-
hour=int(timestamp_energy_log[3]),
628-
minute=int(timestamp_energy_log[4]),
629-
second=int(timestamp_energy_log[5]),
630-
tzinfo=UTC,
631-
),
632-
pulses=int(log_fields[3]),
633-
import_only=True,
619+
pulses=int(log_fields[3])
620+
timestamp=datetime(
621+
year=int(timestamp_energy_log[0]),
622+
month=int(timestamp_energy_log[1]),
623+
day=int(timestamp_energy_log[2]),
624+
hour=int(timestamp_energy_log[3]),
625+
minute=int(timestamp_energy_log[4]),
626+
second=int(timestamp_energy_log[5]),
627+
tzinfo=UTC,
634628
)
635629
if restored_logs.get(address) is None:
636-
restored_logs[address] = []
637-
restored_logs[address].append(slot)
630+
restored_logs[address] = {}
631+
restored_logs[address][slot] = (pulses, timestamp)
632+
_LOGGER.debug("HOI restored_logs=%s", restored_logs)
638633

639634
# Sort and prune the records loaded from cache
640-
sorted_logs: dict[int, dict[int, PulseLogRecord]] = {}
641-
skip_before = datetime.now(tz=UTC) - timedelta(hours=MAX_LOG_HOURS)
642-
sorted_address = sorted(restored_logs.keys(), reverse=True)
643-
for address in sorted_address:
635+
sorted_logs: dict[int, dict[int, tuple[int, datetime]]] = {}
636+
skip_before = datetime.now(tz=UTC) - timedelta(hours=DAY_IN_HOURS)
637+
sorted_addresses = sorted(restored_logs.keys(), reverse=True)
638+
for address in sorted_addresses:
644639
sorted_slots = sorted(restored_logs[address].keys(), reverse=True)
645640
for slot in sorted_slots:
646-
if restored_logs[address][slot].timestamp > skip_before:
647-
if sorted_log.get(address) is None:
648-
sorted_log[address] = {}
649-
sorted_log[address][slot] = restored_logs[address][slot]
650-
651-
restored_logs = sorted_logs
641+
if restored_logs[address][slot][1] > skip_before:
642+
if sorted_logs.get(address) is None:
643+
sorted_logs[address] = {}
644+
sorted_logs[address][slot] = restored_logs[address][slot]
645+
646+
for address, data in sorted_logs.items():
647+
for slot, pulse_data in data.items():
648+
self._energy_counters.add_pulse_log(
649+
address=address,
650+
slot=slot,
651+
pulses=pulse_data[0],
652+
timestamp=pulse_data[1],
653+
import_only=True,
654+
)
652655

653656
self._energy_counters.update()
654657

0 commit comments

Comments
 (0)