diff --git a/pyluos/device.py b/pyluos/device.py index 9a9a88d..1dc6c0c 100644 --- a/pyluos/device.py +++ b/pyluos/device.py @@ -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): @@ -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 = [] diff --git a/pyluos/io/__init__.py b/pyluos/io/__init__.py index 27e3525..aebbcbf 100644 --- a/pyluos/io/__init__.py +++ b/pyluos/io/__init__.py @@ -1,5 +1,6 @@ import json import logging +from mergedeep import merge, Strategy class IOHandler(object): @@ -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: diff --git a/pyluos/io/serial_io.py b/pyluos/io/serial_io.py index 19d361b..691f275 100644 --- a/pyluos/io/serial_io.py +++ b/pyluos/io/serial_io.py @@ -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() diff --git a/pyluos/services/service.py b/pyluos/services/service.py index 2aaa225..8e9a433 100644 --- a/pyluos/services/service.py +++ b/pyluos/services/service.py @@ -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): @@ -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'] diff --git a/setup.py b/setup.py index ed80d06..e7bc68b 100644 --- a/setup.py +++ b/setup.py @@ -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'],