Skip to content

Commit cfbb880

Browse files
Improve refresh_freq calculation allowing multiple JSON per service per cycle.
1 parent 4835292 commit cfbb880

File tree

1 file changed

+17
-8
lines changed

1 file changed

+17
-8
lines changed

pyluos/services/service.py

+17-8
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,8 @@ def __init__(self,
4444
self._luos_revision = "Unknown"
4545
self._robus_revision = "Unknown"
4646
self._killed = False
47-
self._last_update = []
47+
self._last_update = [time.time()]
48+
self._tracked_property = ""
4849
self._luos_statistics = {}
4950

5051
def __repr__(self):
@@ -56,13 +57,21 @@ def _update(self, new_state):
5657
if not isinstance(new_state, dict):
5758
new_state = {new_state: ""}
5859

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

6776
if 'revision' in new_state.keys():
6877
self._firmware_revision = new_state['revision']

0 commit comments

Comments
 (0)