Skip to content

Commit 8adf863

Browse files
committed
Revert pulses-change and improve
1 parent d7df15b commit 8adf863

File tree

1 file changed

+64
-61
lines changed

1 file changed

+64
-61
lines changed

plugwise_usb/nodes/helpers/pulses.py

Lines changed: 64 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -518,67 +518,8 @@ def _update_log_direction(
518518
or double slots containing consumption and production data.
519519
Single slots containing production data only is NOT supported/tested.
520520
"""
521-
if self._logs is None:
522-
return
523-
524-
def check_prev_production(
525-
self, address: int, slot: int, timestamp: datetime
526-
) -> datetime | None:
527-
"""Check the previous slot for production pulses."""
528-
prev_address, prev_slot = calc_log_address(address, slot, -1)
529-
if self._log_exists(prev_address, prev_slot):
530-
prev_timestamp = self._logs[prev_address][prev_slot].timestamp
531-
if not self._first_prev_log_processed:
532-
self._first_prev_log_processed = True
533-
if prev_timestamp == timestamp:
534-
# Given log is the second log with same timestamp,
535-
# mark direction as production
536-
self._logs[address][slot].is_consumption = False
537-
self._logs[prev_address][prev_slot].is_consumption = True
538-
self._log_production = True
539-
elif self._log_production:
540-
self._logs[address][slot].is_consumption = True
541-
if self._logs[prev_address][prev_slot].is_consumption:
542-
self._logs[prev_address][prev_slot].is_consumption = False
543-
self._reset_log_references()
544-
elif self._log_production is None:
545-
self._log_production = False
546-
return prev_timestamp
547-
548-
if self._first_prev_log_processed:
549-
self._first_prev_log_processed = False
550-
return None
551-
552-
def check_next_production(
553-
self, address: int, slot: int, timestamp: datetime
554-
) -> datetime | None:
555-
"""Check the next slot for production pulses."""
556-
next_address, next_slot = calc_log_address(address, slot, 1)
557-
if self._log_exists(next_address, next_slot):
558-
next_timestamp = self._logs[next_address][next_slot].timestamp
559-
if not self._first_next_log_processed:
560-
self._first_next_log_processed = True
561-
if next_timestamp == timestamp:
562-
# Given log is the first log with same timestamp,
563-
# mark direction as production of next log
564-
self._logs[address][slot].is_consumption = True
565-
if self._logs[next_address][next_slot].is_consumption:
566-
self._logs[next_address][next_slot].is_consumption = False
567-
self._reset_log_references()
568-
self._log_production = True
569-
elif self._log_production:
570-
self._logs[address][slot].is_consumption = False
571-
self._logs[next_address][next_slot].is_consumption = True
572-
elif self._log_production is None:
573-
self._log_production = False
574-
return next_timestamp
575-
576-
if self._first_next_log_processed:
577-
self._first_next_log_processed = False
578-
return None
579-
580-
prev_timestamp = check_prev_production(self, address, slot, timestamp)
581-
next_timestamp = check_next_production(self, address, slot, timestamp)
521+
prev_timestamp = self._check_prev_production(self, address, slot, timestamp)
522+
next_timestamp = self._check_next_production(self, address, slot, timestamp)
582523
if self._first_prev_log_processed and self._first_next_log_processed:
583524
# _log_production is True when 2 out of 3 consecutive slots have
584525
# the same timestamp
@@ -587,6 +528,68 @@ def check_next_production(
587528
^ (next_timestamp == timestamp)
588529
)
589530

531+
def _check_prev_production(
532+
self, address: int, slot: int, timestamp: datetime
533+
) -> datetime | None:
534+
"""Check the previous slot for production pulses."""
535+
if self._logs is None:
536+
return
537+
538+
prev_address, prev_slot = calc_log_address(address, slot, -1)
539+
if self._log_exists(prev_address, prev_slot):
540+
prev_timestamp = self._logs[prev_address][prev_slot].timestamp
541+
if not self._first_prev_log_processed:
542+
self._first_prev_log_processed = True
543+
if prev_timestamp == timestamp:
544+
# Given log is the second log with same timestamp,
545+
# mark direction as production
546+
self._logs[address][slot].is_consumption = False
547+
self._logs[prev_address][prev_slot].is_consumption = True
548+
self._log_production = True
549+
elif self._log_production:
550+
self._logs[address][slot].is_consumption = True
551+
if self._logs[prev_address][prev_slot].is_consumption:
552+
self._logs[prev_address][prev_slot].is_consumption = False
553+
self._reset_log_references()
554+
elif self._log_production is None:
555+
self._log_production = False
556+
return prev_timestamp
557+
558+
if self._first_prev_log_processed:
559+
self._first_prev_log_processed = False
560+
return None
561+
562+
def _check_next_production(
563+
self, address: int, slot: int, timestamp: datetime
564+
) -> datetime | None:
565+
"""Check the next slot for production pulses."""
566+
if self._logs is None:
567+
return
568+
569+
next_address, next_slot = calc_log_address(address, slot, 1)
570+
if self._log_exists(next_address, next_slot):
571+
next_timestamp = self._logs[next_address][next_slot].timestamp
572+
if not self._first_next_log_processed:
573+
self._first_next_log_processed = True
574+
if next_timestamp == timestamp:
575+
# Given log is the first log with same timestamp,
576+
# mark direction as production of next log
577+
self._logs[address][slot].is_consumption = True
578+
if self._logs[next_address][next_slot].is_consumption:
579+
self._logs[next_address][next_slot].is_consumption = False
580+
self._reset_log_references()
581+
self._log_production = True
582+
elif self._log_production:
583+
self._logs[address][slot].is_consumption = False
584+
self._logs[next_address][next_slot].is_consumption = True
585+
elif self._log_production is None:
586+
self._log_production = False
587+
return next_timestamp
588+
589+
if self._first_next_log_processed:
590+
self._first_next_log_processed = False
591+
return None
592+
590593
def _update_log_interval(self) -> None:
591594
"""Update the detected log interval based on the most recent two logs."""
592595
if self._logs is None or self._log_production is None:

0 commit comments

Comments
 (0)