Skip to content

Commit

Permalink
Improve refresh_freq calculation allowing multiple JSON per service p…
Browse files Browse the repository at this point in the history
…er cycle.
  • Loading branch information
nicolas-rabault committed Mar 13, 2024
1 parent 4835292 commit dfbe08c
Showing 1 changed file with 16 additions and 8 deletions.
24 changes: 16 additions & 8 deletions pyluos/services/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@ def __init__(self,
self._luos_revision = "Unknown"
self._robus_revision = "Unknown"
self._killed = False
self._last_update = []
self._last_update = [time.time()]
self._tracked_property = ""
self._luos_statistics = {}

def __repr__(self):
Expand All @@ -56,13 +57,20 @@ def _update(self, new_state):
if not isinstance(new_state, dict):
new_state = {new_state: ""}

self._last_update.append(time.time())
if (len(self._last_update) > 1):
self.max_refresh_time = max(self.max_refresh_time, self._last_update[-1] - self._last_update[-2])
if (self._last_update[0] < time.time() - 1.0):
while (self._last_update[0] < time.time() - 10.0):
self._last_update.pop(0)
self.refresh_freq = (len(self._last_update) / 10.0) * 0.05 + 0.95 * self.refresh_freq
# Check if we alredy have a property to track or if we didn't receive any property since 2 seconds
if (self._tracked_property == "") or (self._last_update[-1] < time.time() - 2.0):
# the property we track is void or not available anymore, we have to get one of the property received.
for key in new_state.keys():
self._tracked_property = key
break
elif (self._tracked_property in new_state.keys()):
self._last_update.append(time.time())
if (len(self._last_update) > 1):
self.max_refresh_time = max(self.max_refresh_time, self._last_update[-1] - self._last_update[-2])
if (self._last_update[0] < time.time() - 1.0):
while (self._last_update[0] < time.time() - 10.0):
self._last_update.pop(0)
self.refresh_freq = (len(self._last_update) / 10.0) * 0.05 + 0.95 * self.refresh_freq

if 'revision' in new_state.keys():
self._firmware_revision = new_state['revision']
Expand Down

0 comments on commit dfbe08c

Please sign in to comment.