Skip to content

Commit 3898f15

Browse files
committed
Refactor _missing_addresses_before()
1 parent 6f305dd commit 3898f15

File tree

1 file changed

+22
-50
lines changed

1 file changed

+22
-50
lines changed

plugwise_usb/nodes/helpers/pulses.py

Lines changed: 22 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -915,65 +915,37 @@ def _last_known_duration(self) -> timedelta:
915915
address, slot = calc_log_address(address, slot, -1)
916916
return self._logs[address][slot].timestamp - last_known_timestamp
917917

918-
def _missing_addresses_before(
919-
self, address: int, slot: int, target: datetime
920-
) -> list[int]:
918+
def _missing_addresses_before(self, address: int, slot: int, target: datetime) -> list[int]:
921919
"""Return list of missing address(es) prior to given log timestamp."""
922-
addresses: list[int] = []
923920
if self._logs is None or target >= self._logs[address][slot].timestamp:
924-
return addresses
921+
return []
925922

926-
# default interval
927-
calc_interval_cons = timedelta(hours=1)
928-
if (
929-
self._log_interval_consumption is not None
930-
and self._log_interval_consumption > 0
931-
):
932-
# Use consumption interval
933-
calc_interval_cons = timedelta(minutes=self._log_interval_consumption)
934-
if self._log_interval_consumption == 0:
935-
pass
936-
937-
if not self._log_production: #False
938-
expected_timestamp = (
939-
self._logs[address][slot].timestamp - calc_interval_cons
940-
)
941-
address, slot = calc_log_address(address, slot, -1)
942-
while expected_timestamp > target and address > 0:
943-
if address not in addresses:
944-
addresses.append(address)
945-
expected_timestamp -= calc_interval_cons
946-
address, slot = calc_log_address(address, slot, -1)
947-
else:
948-
# Production logging active
949-
calc_interval_prod = timedelta(hours=1)
950-
if (
951-
self._log_interval_production is not None
952-
and self._log_interval_production > 0
953-
):
954-
calc_interval_prod = timedelta(minutes=self._log_interval_production)
923+
cons_interval = timedelta(
924+
minutes=self._log_interval_consumption
925+
if self._log_interval_consumption and self._log_interval_consumption > 0
926+
else 60
927+
)
955928

956-
expected_timestamp_cons = (
957-
self._logs[address][slot].timestamp - calc_interval_cons
958-
)
959-
expected_timestamp_prod = (
960-
self._logs[address][slot].timestamp - calc_interval_prod
961-
)
929+
prod_interval = timedelta(
930+
minutes=self._log_interval_production
931+
if self._log_production and self._log_interval_production and self._log_interval_production > 0
932+
else 60
933+
)
934+
935+
addresses, ts_cons, ts_prod = [], self._logs[address][slot].timestamp, self._logs[address][slot].timestamp
936+
address, slot = calc_log_address(address, slot, -1)
962937

938+
while address > 0 and (ts_cons - cons_interval > target or (self._log_production and ts_prod - prod_interval > target)):
939+
addresses = update_addresses(addres, addresses)
940+
if self._log_production and ts_prod - prod_interval > ts_cons - cons_interval:
941+
ts_prod -= prod_interval
942+
else:
943+
ts_cons -= cons_interval
963944
address, slot = calc_log_address(address, slot, -1)
964-
while (
965-
expected_timestamp_cons > target or expected_timestamp_prod > target
966-
) and address > 0:
967-
if address not in addresses:
968-
addresses.append(address)
969-
if expected_timestamp_prod > expected_timestamp_cons:
970-
expected_timestamp_prod -= calc_interval_prod
971-
else:
972-
expected_timestamp_cons -= calc_interval_cons
973-
address, slot = calc_log_address(address, slot, -1)
974945

975946
return addresses
976947

948+
977949
def _missing_addresses_after(
978950
self, address: int, slot: int, target: datetime
979951
) -> list[int]:

0 commit comments

Comments
 (0)