@@ -91,6 +91,8 @@ def __init__(self, mac: str) -> None:
9191 self ._logs : dict [int , dict [int , PulseLogRecord ]] | None = None
9292 self ._log_addresses_missing : list [int ] | None = None
9393 self ._log_production : bool | None = None
94+ self ._next_log_exists = False
95+ self ._prev_log_exists = False
9496 self ._pulses_consumption : int | None = None
9597 self ._pulses_production : int | None = None
9698 self ._pulses_timestamp : datetime | None = None
@@ -511,61 +513,66 @@ def _update_log_direction(
511513 slot ,
512514 timestamp ,
513515 )
514- prev_exists = next_exists = False
515516 prev_address , prev_slot = calc_log_address (address , slot , - 1 )
516517 if self ._log_exists (prev_address , prev_slot ):
517- prev_exists = True
518518 prev_timestamp = self ._logs [prev_address ][prev_slot ].timestamp
519519 _LOGGER .debug (
520520 "_update_log_direction | pr_address=%s | pr_slot=%s | timestamp=%s" ,
521521 prev_address ,
522522 prev_slot ,
523523 prev_timestamp ,
524524 )
525- if prev_timestamp == timestamp :
526- # Given log is the second log with same timestamp,
527- # mark direction as production
528- self ._logs [address ][slot ].is_consumption = False
529- self ._logs [prev_address ][prev_slot ].is_consumption = True
530- self ._log_production = True
531- elif self ._log_production :
532- self ._logs [address ][slot ].is_consumption = True
533- if self ._logs [prev_address ][prev_slot ].is_consumption :
534- self ._logs [prev_address ][prev_slot ].is_consumption = False
535- self ._reset_log_references ()
536- elif self ._log_production is None :
537- self ._log_production = False
525+ if not self ._prev_log_exists :
526+ self ._prev_log_exists = True
527+ if prev_timestamp == timestamp :
528+ # Given log is the second log with same timestamp,
529+ # mark direction as production
530+ self ._logs [address ][slot ].is_consumption = False
531+ self ._logs [prev_address ][prev_slot ].is_consumption = True
532+ self ._log_production = True
533+ elif self ._log_production :
534+ self ._logs [address ][slot ].is_consumption = True
535+ if self ._logs [prev_address ][prev_slot ].is_consumption :
536+ self ._logs [prev_address ][prev_slot ].is_consumption = False
537+ self ._reset_log_references ()
538+ elif self ._log_production is None :
539+ self ._log_production = False
540+ elif self ._prev_log_exists :
541+ self ._prev_log_exists = False
538542
539543 next_address , next_slot = calc_log_address (address , slot , 1 )
540544 if self ._log_exists (next_address , next_slot ):
541- next_exists = True
542545 next_timestamp = self ._logs [next_address ][next_slot ].timestamp
543546 _LOGGER .debug (
544547 "_update_log_direction | nxt_address=%s | nxt_slot=%s | timestamp=%s" ,
545548 next_address ,
546549 next_slot ,
547550 next_timestamp ,
548551 )
549- if next_timestamp == timestamp :
550- # Given log is the first log with same timestamp,
551- # mark direction as production of next log
552- self ._logs [address ][slot ].is_consumption = True
553- if self ._logs [next_address ][next_slot ].is_consumption :
554- self ._logs [next_address ][next_slot ].is_consumption = False
555- self ._reset_log_references ()
556- self ._log_production = True
557- elif self ._log_production :
558- self ._logs [address ][slot ].is_consumption = False
559- self ._logs [next_address ][next_slot ].is_consumption = True
560- elif self ._log_production is None :
561- self ._log_production = False
562-
563- if prev_exists and next_exists :
564- # _log_production is True when 2 out of 3 consecutive slots have
565- # the same timestamp, otherwise it is False
566- self ._log_production = (
567- next_timestamp == timestamp and prev_timestamp != timestamp
568- ) or (next_timestamp == prev_timestamp and next_timestamp != timestamp )
552+ if not self ._next_log_exists :
553+ self ._next_log_exists = True
554+ if next_timestamp == timestamp :
555+ # Given log is the first log with same timestamp,
556+ # mark direction as production of next log
557+ self ._logs [address ][slot ].is_consumption = True
558+ if self ._logs [next_address ][next_slot ].is_consumption :
559+ self ._logs [next_address ][next_slot ].is_consumption = False
560+ self ._reset_log_references ()
561+ self ._log_production = True
562+ elif self ._log_production :
563+ self ._logs [address ][slot ].is_consumption = False
564+ self ._logs [next_address ][next_slot ].is_consumption = True
565+ elif self ._log_production is None :
566+ self ._log_production = False
567+ elif self ._next_log_exists :
568+ self ._next_log_exists = False
569+
570+ if self ._prev_log_exists and self ._next_log_exists :
571+ # _log_production is True when 2 out of 3 consecutive slots have
572+ # the same timestamp, otherwise it is False
573+ self ._log_production = (
574+ next_timestamp == timestamp and prev_timestamp != timestamp
575+ ) or (next_timestamp == prev_timestamp and next_timestamp != timestamp )
569576
570577 def _update_log_interval (self ) -> None :
571578 """Update the detected log interval based on the most recent two logs."""
0 commit comments