@@ -519,8 +519,64 @@ def _update_log_direction(
519519 if self ._logs is None :
520520 return
521521
522- prev_timestamp = self ._check_prev_production (address , slot , timestamp )
523- next_timestamp = self ._check_next_production (address , slot , timestamp )
522+ def check_prev_production (
523+ self , address : int , slot : int , timestamp : datetime
524+ ) -> datetime | None :
525+ """Check the previous slot for production pulses."""
526+ prev_address , prev_slot = calc_log_address (address , slot , - 1 )
527+ if self ._log_exists (prev_address , prev_slot ):
528+ prev_timestamp = self ._logs [prev_address ][prev_slot ].timestamp
529+ if not self ._first_prev_log_processed :
530+ self ._first_prev_log_processed = True
531+ if prev_timestamp == timestamp :
532+ # Given log is the second log with same timestamp,
533+ # mark direction as production
534+ self ._logs [address ][slot ].is_consumption = False
535+ self ._logs [prev_address ][prev_slot ].is_consumption = True
536+ self ._log_production = True
537+ elif self ._log_production :
538+ self ._logs [address ][slot ].is_consumption = True
539+ if self ._logs [prev_address ][prev_slot ].is_consumption :
540+ self ._logs [prev_address ][prev_slot ].is_consumption = False
541+ self ._reset_log_references ()
542+ elif self ._log_production is None :
543+ self ._log_production = False
544+ return prev_timestamp
545+
546+ if self ._first_prev_log_processed :
547+ self ._first_prev_log_processed = False
548+ return None
549+
550+ def check_next_production (
551+ self , address : int , slot : int , timestamp : datetime
552+ ) -> datetime | None :
553+ """Check the next slot for production pulses."""
554+ next_address , next_slot = calc_log_address (address , slot , 1 )
555+ if self ._log_exists (next_address , next_slot ):
556+ next_timestamp = self ._logs [next_address ][next_slot ].timestamp
557+ if not self ._first_next_log_processed :
558+ self ._first_next_log_processed = True
559+ if next_timestamp == timestamp :
560+ # Given log is the first log with same timestamp,
561+ # mark direction as production of next log
562+ self ._logs [address ][slot ].is_consumption = True
563+ if self ._logs [next_address ][next_slot ].is_consumption :
564+ self ._logs [next_address ][next_slot ].is_consumption = False
565+ self ._reset_log_references ()
566+ self ._log_production = True
567+ elif self ._log_production :
568+ self ._logs [address ][slot ].is_consumption = False
569+ self ._logs [next_address ][next_slot ].is_consumption = True
570+ elif self ._log_production is None :
571+ self ._log_production = False
572+ return next_timestamp
573+
574+ if self ._first_next_log_processed :
575+ self ._first_next_log_processed = False
576+ return None
577+
578+ prev_timestamp = check_prev_production (self , address , slot , timestamp )
579+ next_timestamp = check_next_production (self , address , slot , timestamp )
524580 if self ._first_prev_log_processed and self ._first_next_log_processed :
525581 # _log_production is True when 2 out of 3 consecutive slots have
526582 # the same timestamp
@@ -529,62 +585,6 @@ def _update_log_direction(
529585 ^ (next_timestamp == timestamp )
530586 )
531587
532- def _check_prev_production (
533- self , address : int , slot : int , timestamp : datetime
534- ) -> datetime | None :
535- """Check the previous slot for production pulses."""
536- prev_address , prev_slot = calc_log_address (address , slot , - 1 )
537- if self ._log_exists (prev_address , prev_slot ):
538- prev_timestamp = self ._logs [prev_address ][prev_slot ].timestamp
539- if not self ._first_prev_log_processed :
540- self ._first_prev_log_processed = True
541- if prev_timestamp == timestamp :
542- # Given log is the second log with same timestamp,
543- # mark direction as production
544- self ._logs [address ][slot ].is_consumption = False
545- self ._logs [prev_address ][prev_slot ].is_consumption = True
546- self ._log_production = True
547- elif self ._log_production :
548- self ._logs [address ][slot ].is_consumption = True
549- if self ._logs [prev_address ][prev_slot ].is_consumption :
550- self ._logs [prev_address ][prev_slot ].is_consumption = False
551- self ._reset_log_references ()
552- elif self ._log_production is None :
553- self ._log_production = False
554- return prev_timestamp
555-
556- if self ._first_prev_log_processed :
557- self ._first_prev_log_processed = False
558- return None
559-
560- def _check_next_production (
561- self , address : int , slot : int , timestamp : datetime
562- ) -> datetime | None :
563- """Check the next slot for production pulses."""
564- next_address , next_slot = calc_log_address (address , slot , 1 )
565- if self ._log_exists (next_address , next_slot ):
566- next_timestamp = self ._logs [next_address ][next_slot ].timestamp
567- if not self ._first_next_log_processed :
568- self ._first_next_log_processed = True
569- if next_timestamp == timestamp :
570- # Given log is the first log with same timestamp,
571- # mark direction as production of next log
572- self ._logs [address ][slot ].is_consumption = True
573- if self ._logs [next_address ][next_slot ].is_consumption :
574- self ._logs [next_address ][next_slot ].is_consumption = False
575- self ._reset_log_references ()
576- self ._log_production = True
577- elif self ._log_production :
578- self ._logs [address ][slot ].is_consumption = False
579- self ._logs [next_address ][next_slot ].is_consumption = True
580- elif self ._log_production is None :
581- self ._log_production = False
582- return next_timestamp
583-
584- if self ._first_next_log_processed :
585- self ._first_next_log_processed = False
586- return None
587-
588588 def _update_log_interval (self ) -> None :
589589 """Update the detected log interval based on the most recent two logs."""
590590 if self ._logs is None or self ._log_production is None :
@@ -935,6 +935,7 @@ def _logs_missing(self, from_timestamp: datetime) -> list[int] | None:
935935 # We have an suspected interval, so try to calculate missing log addresses prior to first collected log
936936 _LOGGER .debug (
937937 "_logs_missing | %s | checking before range with log_interval=%s" ,
938+ self ._mac ,
938939 log_interval ,
939940 )
940941 calculated_timestamp = self ._logs [first_address ][
0 commit comments