Skip to content

Commit

Permalink
Merge pull request #197 from Luos-io/feat/refresh_freq
Browse files Browse the repository at this point in the history
Adapt pyluos to the updates rc3.1.0
  • Loading branch information
nicolas-rabault authored Jun 3, 2024
2 parents 4835292 + 937f7ff commit 9e64152
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 15 deletions.
8 changes: 4 additions & 4 deletions pyluos/device.py
Original file line number Diff line number Diff line change
Expand Up @@ -290,10 +290,10 @@ def _update(self, new_state):
break
if (self._freedomLink != None):
self._freedomLink._assert(alias)
if 'services' not in new_state.keys():
if 's' not in new_state.keys():
return

for alias, mod in new_state['services'].items():
for alias, mod in new_state['s'].items():
if hasattr(self, alias):
getattr(self, alias)._update(mod)
if (self._freedomLink != None):
Expand All @@ -313,11 +313,11 @@ def update_data(self, alias, key, val, data):
def _push_once(self):
with self._cmd_lock:
if self._cmd:
self._write(json.dumps({'services': self._cmd}).encode())
self._write(json.dumps({'s': self._cmd}).encode())
self._cmd = defaultdict(lambda: defaultdict(lambda: None))
for cmd, binary in zip(self._cmd_data, self._binary):
time.sleep(0.01)
self._write(json.dumps({'services': cmd}).encode() + '\n'.encode() + binary)
self._write(json.dumps({'s': cmd}).encode() + '\n'.encode() + binary)

self._cmd_data = []
self._binary = []
Expand Down
13 changes: 12 additions & 1 deletion pyluos/io/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import json
import logging
from mergedeep import merge, Strategy


class IOHandler(object):
Expand All @@ -16,7 +17,17 @@ def is_ready(self):
def read(self, trials=5):
try:
data = self.recv()
return self.loads(data)
if data is None:
return {}
table = data.splitlines()
if len(table) > 1:
# load the Json of each substring
jsn = [self.loads(sub_data) for sub_data in table]
# merge all the Json data
result = merge({}, *jsn, strategy=Strategy.ADDITIVE)
return result
else:
return self.loads(data)
except Exception as e:
logging.getLogger(__name__).debug('Msg read failed: {}'.format(str(e)))
if trials == 0:
Expand Down
2 changes: 1 addition & 1 deletion pyluos/io/serial_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ def is_host_compatible(cls, host):

def __init__(self, host, baudrate=None):
if baudrate is None:
baudrate = os.getenv('LUOS_BAUDRATE', 1000000)
baudrate = os.getenv('LUOS_BAUDRATE', 3000000)

self._serial = _serial.Serial(host, baudrate)
self._serial.flush()
Expand Down
25 changes: 17 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,21 @@ 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
self._last_update.append(time.time())
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
3 changes: 2 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@
'crc8',
'ipython',
'requests',
'simple_websocket_server==0.4.2'
'simple_websocket_server==0.4.2',
'mergedeep'
],
extras_require={
'tests': ['pytest', 'flake8'],
Expand Down

0 comments on commit 9e64152

Please sign in to comment.