1111
1212
1313class OmniLogic :
14- auto_refresh_enabled : bool = True
15-
1614 mspconfig : MSPConfig
1715 telemetry : Telemetry
1816
@@ -21,6 +19,8 @@ class OmniLogic:
2119
2220 _mspconfig_last_updated : float = 0.0
2321 _telemetry_last_updated : float = 0.0
22+ _mspconfig_dirty : bool = True
23+ _telemetry_dirty : bool = True
2424 _refresh_lock : asyncio .Lock
2525
2626 def __init__ (self , host : str , port : int = 10444 ) -> None :
@@ -30,58 +30,60 @@ def __init__(self, host: str, port: int = 10444) -> None:
3030 self ._api = OmniLogicAPI (host , port )
3131 self ._refresh_lock = asyncio .Lock ()
3232
33- async def refresh (self , update_mspconfig : bool = True , update_telemetry : bool = True ) -> None :
34- """Refresh the data from the OmniLogic controller.
35-
36- Args:
37- update_mspconfig: Whether to fetch and update MSPConfig data
38- update_telemetry: Whether to fetch and update Telemetry data
39- """
40- if update_mspconfig :
41- self .mspconfig = await self ._api .async_get_mspconfig ()
42- self ._mspconfig_last_updated = time .time ()
43- if update_telemetry :
44- self .telemetry = await self ._api .async_get_telemetry ()
45- self ._telemetry_last_updated = time .time ()
46-
47- self ._update_equipment ()
48-
49- # async def refresh_mspconfig(self) -> None:
50- # """Refresh only the MSPConfig data from the OmniLogic controller."""
51- # self.mspconfig = await self._api.async_get_mspconfig()
52- # self._mspconfig_last_updated = time.time()
53- # self._update_equipment()
54-
55- # async def refresh_telemetry(self) -> None:
56- # """Refresh only the Telemetry data from the OmniLogic controller."""
57- # self.telemetry = await self._api.async_get_telemetry()
58- # self._telemetry_last_updated = time.time()
59- # self._update_equipment()
60-
61- async def update_if_older_than (
33+ async def refresh (
6234 self ,
63- telemetry_min_time : float | None = None ,
64- mspconfig_min_time : float | None = None ,
35+ * ,
36+ mspconfig : bool = True ,
37+ telemetry : bool = True ,
38+ if_dirty : bool = True ,
39+ if_older_than : float = 10.0 ,
40+ force : bool = False ,
6541 ) -> None :
66- """Update telemetry/mspconfig only if older than specified timestamp.
67-
68- This method uses a lock to ensure only one refresh happens at a time.
69- If another thread/task already updated the data to be newer than required,
70- this method will skip the update.
42+ """Refresh the data from the OmniLogic controller.
7143
7244 Args:
73- telemetry_min_time: Update telemetry if last updated before this timestamp
74- mspconfig_min_time: Update mspconfig if last updated before this timestamp
45+ mspconfig: Whether to refresh MSPConfig data (if conditions are met)
46+ telemetry: Whether to refresh Telemetry data (if conditions are met)
47+ if_dirty: Only refresh if the data has been marked dirty
48+ if_older_than: Only refresh if data is older than this many seconds
49+ force: Force refresh regardless of dirty flag or age
7550 """
7651 async with self ._refresh_lock :
77- needs_telemetry = telemetry_min_time and self ._telemetry_last_updated < telemetry_min_time
78- needs_mspconfig = mspconfig_min_time and self ._mspconfig_last_updated < mspconfig_min_time
79-
80- if needs_telemetry or needs_mspconfig :
81- await self .refresh (
82- update_mspconfig = bool (needs_mspconfig ),
83- update_telemetry = bool (needs_telemetry ),
84- )
52+ current_time = time .time ()
53+
54+ # Determine if mspconfig needs updating
55+ update_mspconfig = False
56+ if mspconfig :
57+ if force :
58+ update_mspconfig = True
59+ elif if_dirty and self ._mspconfig_dirty :
60+ update_mspconfig = True
61+ elif (current_time - self ._mspconfig_last_updated ) > if_older_than :
62+ update_mspconfig = True
63+
64+ # Determine if telemetry needs updating
65+ update_telemetry = False
66+ if telemetry :
67+ if force :
68+ update_telemetry = True
69+ elif if_dirty and self ._telemetry_dirty :
70+ update_telemetry = True
71+ elif (current_time - self ._telemetry_last_updated ) > if_older_than :
72+ update_telemetry = True
73+
74+ # Perform the updates
75+ if update_mspconfig :
76+ self .mspconfig = await self ._api .async_get_mspconfig ()
77+ self ._mspconfig_last_updated = time .time ()
78+ self ._mspconfig_dirty = False
79+
80+ if update_telemetry :
81+ self .telemetry = await self ._api .async_get_telemetry ()
82+ self ._telemetry_last_updated = time .time ()
83+ self ._telemetry_dirty = False
84+
85+ if update_mspconfig or update_telemetry :
86+ self ._update_equipment ()
8587
8688 def _update_equipment (self ) -> None :
8789 """Update equipment objects based on the latest MSPConfig and Telemetry data."""
0 commit comments