22
33import sys
44from pathlib import Path
5+
6+ from powersensor_local .EventBuffer import EventBuffer
7+
58project_root = str (Path (__file__ ).parents [1 ])
69if project_root not in sys .path :
710 sys .path .append (project_root )
@@ -48,7 +51,7 @@ def same_duration(ev1: dict, ev2: dict):
4851 d2 = round (ev2 [dur ], 0 )
4952 return d1 == d2
5053
51- def matching_instants (starttime_utc : int , solar_events : list , housenet_events : list ) -> Optional [InstantaneousValues ]:
54+ def matching_instants (starttime_utc : int , solar_events : EventBuffer , housenet_events : EventBuffer ) -> Optional [InstantaneousValues ]:
5255 """Attempts to match and merge solar+housenet average_power events."""
5356 solar = solar_events .find_by_key (KEY_START , starttime_utc )
5457 housenet = housenet_events .find_by_key (KEY_START , starttime_utc )
@@ -73,7 +76,7 @@ def make_instant_housenet(ev: dict) -> Optional[InstantaneousValues]:
7376 duration_s = round (ev [KEY_DUR_S ], 0 )
7477 )
7578
76- def matching_summations (starttime_utc : int , solar_events : list , housenet_events : list ) -> Optional [SummationValues ]:
79+ def matching_summations (starttime_utc : int , solar_events : EventBuffer , housenet_events : EventBuffer ) -> Optional [SummationValues ]:
7780 """Attempts to match and merge solar+housenet summation events."""
7881 solar = solar_events .find_by_key (KEY_START , starttime_utc )
7982 housenet = housenet_events .find_by_key (KEY_START , starttime_utc )
@@ -148,10 +151,10 @@ def __init__(self, with_solar: bool):
148151 self ._expect_solar = with_solar
149152 self ._summation = self .SummationInfo (0 , 0 , 0 , 0 )
150153 self ._counters = self .Counters (0 , 0 , 0 , 0 , 0 )
151- self ._solar_instants = self . EventBuffer (31 )
152- self ._housenet_instants = self . EventBuffer (31 )
153- self ._solar_summations = self . EventBuffer (5 )
154- self ._housenet_summations = self . EventBuffer (5 )
154+ self ._solar_instants = EventBuffer (31 )
155+ self ._housenet_instants = EventBuffer (31 )
156+ self ._solar_summations = EventBuffer (5 )
157+ self ._housenet_summations = EventBuffer (5 )
155158
156159 async def process_average_power_event (self , ev : dict ):
157160 """Ingests an event of type 'average_power'."""
@@ -186,7 +189,6 @@ async def process_summation_event(self, ev: dict):
186189 await self ._process_summations (starttime_utc )
187190
188191 async def _process_instants (self , starttime_utc : int ):
189- v = None
190192 if self ._expect_solar :
191193 v = matching_instants (starttime_utc , self ._solar_instants , self ._housenet_instants )
192194 else :
@@ -216,7 +218,6 @@ async def _process_instants(self, starttime_utc: int):
216218 })
217219
218220 async def _process_summations (self , starttime_utc : int ):
219- v = None
220221 if self ._expect_solar :
221222 v = matching_summations (starttime_utc , self ._solar_summations , self ._housenet_summations )
222223 else :
@@ -295,30 +296,6 @@ def _increment_counters(self, d: SummationDeltas):
295296 self ._counters .from_grid += d .from_grid
296297 self ._counters .home_use += d .home_use
297298
298- class EventBuffer :
299- def __init__ (self , keep : int ):
300- self ._keep = keep
301- self ._evs = []
302-
303- def find_by_key (self , key : str , value : any ):
304- for ev in self ._evs :
305- if key in ev and ev [key ] == value :
306- return ev
307- return None
308-
309- def append (self , ev : dict ):
310- self ._evs .append (ev )
311- if len (self ._evs ) > self ._keep :
312- del self ._evs [0 ]
313-
314- def evict_older (self , key : str , value : float ):
315- while len (self ._evs ) > 0 :
316- ev = self ._evs [0 ]
317- if key in ev and ev [key ] <= value :
318- del self ._evs [0 ]
319- else :
320- return
321-
322299 @dataclass
323300 class SummationInfo :
324301 solar_resettime : int
0 commit comments