From 0ee80dd12241a6c884491a2282b0922d7f0dbe00 Mon Sep 17 00:00:00 2001 From: root <42302779+krocans@users.noreply.github.com> Date: Sat, 11 Aug 2018 23:32:08 +0300 Subject: [PATCH 1/6] Fix monitor data printed twice on missing 'value' --- example.py | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/example.py b/example.py index a4c27b4..44457ea 100644 --- a/example.py +++ b/example.py @@ -49,17 +49,16 @@ def mon(client, device_id): for key, value in res.items(): try: desc = model.value(key) + if isinstance(desc, wideq.EnumValue): + print('- {}: {}'.format( + key, desc.options.get(value, value) + )) + elif isinstance(desc, wideq.RangeValue): + print('- {0}: {1} ({2.min}-{2.max})'.format( + key, value, desc, + )) except KeyError: print('- {}: {}'.format(key, value)) - if isinstance(desc, wideq.EnumValue): - print('- {}: {}'.format( - key, desc.options.get(value, value) - )) - elif isinstance(desc, wideq.RangeValue): - print('- {0}: {1} ({2.min}-{2.max})'.format( - key, value, desc, - - )) except KeyboardInterrupt: pass From 910e55d0180e195f0d9eeca252e3734b1200da3e Mon Sep 17 00:00:00 2001 From: krocans <42302779+krocans@users.noreply.github.com> Date: Sun, 12 Aug 2018 10:16:23 +0300 Subject: [PATCH 2/6] Decode binary monitoring status data --- example.py | 5 ++++- wideq.py | 22 ++++++++++++++++++++++ 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/example.py b/example.py index 44457ea..c5bf10f 100644 --- a/example.py +++ b/example.py @@ -42,7 +42,10 @@ def mon(client, device_id): data = mon.poll() if data: try: - res = mon.decode_json(data) + if model.monitor_type == 1: + res = model.decode_monitor_binary(data) + else: + res = mon.decode_json(data) except ValueError: print('status data: {!r}'.format(data)) else: diff --git a/wideq.py b/wideq.py index e3bf450..c998247 100644 --- a/wideq.py +++ b/wideq.py @@ -679,6 +679,28 @@ class ModelInfo(object): options = self.value(key).options return options[value] + @property + def monitor_type(self): + """Get type of monitoring return data. + """ + + if self.data['Monitoring']['type'] == 'BINARY(BYTE)': + return 1 + else: + return 0 + + def decode_monitor_binary(self, data): + """Decode binary encoded status data. + """ + + decoded = {} + for item in self.data['Monitoring']['protocol']: + key = item['value'] + value = 0 + for i in range(item['startByte'], item['startByte'] + item['length']): + value = value * 256 + data[i - 1] + decoded[key] = str(value) + return decoded class Device(object): """A higher-level interface to a specific device. From 5a5e8bf47d11b387929f1f7913eedbf53f6c0967 Mon Sep 17 00:00:00 2001 From: krocans <42302779+krocans@users.noreply.github.com> Date: Sun, 12 Aug 2018 10:33:27 +0300 Subject: [PATCH 3/6] DeviceInfo decodes monitor status data --- example.py | 5 +---- wideq.py | 13 +++++++++++++ 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/example.py b/example.py index c5bf10f..faaed22 100644 --- a/example.py +++ b/example.py @@ -42,10 +42,7 @@ def mon(client, device_id): data = mon.poll() if data: try: - if model.monitor_type == 1: - res = model.decode_monitor_binary(data) - else: - res = mon.decode_json(data) + res = model.decode_monitor(data) except ValueError: print('status data: {!r}'.format(data)) else: diff --git a/wideq.py b/wideq.py index c998247..32eacbb 100644 --- a/wideq.py +++ b/wideq.py @@ -702,6 +702,19 @@ class ModelInfo(object): decoded[key] = str(value) return decoded + def decode_monitor_json(self, data): + """Decode a bytestring that encodes JSON status data.""" + + return json.loads(data.decode('utf8')) + + def decode_monitor(self, data): + """Decode status data.""" + + if self.monitor_type == 1: + return self.decode_monitor_binary(data) + else: + return self.decode_monitor_json(data) + class Device(object): """A higher-level interface to a specific device. From 93151962e915fb943b8ddd9ddf83b2ee312c1235 Mon Sep 17 00:00:00 2001 From: krocans <42302779+krocans@users.noreply.github.com> Date: Tue, 4 Sep 2018 00:24:14 +0300 Subject: [PATCH 4/6] decode_monitor_binary bytestring index fix (by NorDroN) --- wideq.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/wideq.py b/wideq.py index 32eacbb..284a6f8 100644 --- a/wideq.py +++ b/wideq.py @@ -695,11 +695,10 @@ class ModelInfo(object): decoded = {} for item in self.data['Monitoring']['protocol']: - key = item['value'] value = 0 for i in range(item['startByte'], item['startByte'] + item['length']): - value = value * 256 + data[i - 1] - decoded[key] = str(value) + value = value * 256 + data[i] + decoded[item['value']] = str(value) return decoded def decode_monitor_json(self, data): From 4ca839c2a916d59fef27793e86dad07bbd35e1c8 Mon Sep 17 00:00:00 2001 From: nordron Date: Tue, 11 Sep 2018 23:53:32 +0300 Subject: [PATCH 5/6] Refactoring --- wideq.py | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) diff --git a/wideq.py b/wideq.py index 284a6f8..f5d2e13 100644 --- a/wideq.py +++ b/wideq.py @@ -680,14 +680,11 @@ class ModelInfo(object): return options[value] @property - def monitor_type(self): - """Get type of monitoring return data. + def binary_monitor_data(self): + """Check that type of monitoring is BINARY(BYTE). """ - if self.data['Monitoring']['type'] == 'BINARY(BYTE)': - return 1 - else: - return 0 + return self.data['Monitoring']['type'] == 'BINARY(BYTE)' def decode_monitor_binary(self, data): """Decode binary encoded status data. @@ -695,10 +692,11 @@ class ModelInfo(object): decoded = {} for item in self.data['Monitoring']['protocol']: + key = item['value'] value = 0 - for i in range(item['startByte'], item['startByte'] + item['length']): - value = value * 256 + data[i] - decoded[item['value']] = str(value) + for v in data[item['startByte']:item['startByte'] + item['length']]: + value = (value << 8) + v + decoded[key] = str(value) return decoded def decode_monitor_json(self, data): @@ -709,11 +707,12 @@ class ModelInfo(object): def decode_monitor(self, data): """Decode status data.""" - if self.monitor_type == 1: + if self.binary_monitor_data: return self.decode_monitor_binary(data) else: return self.decode_monitor_json(data) + class Device(object): """A higher-level interface to a specific device. From 4ad49467190c77cc8cfece197845a00f069b7edb Mon Sep 17 00:00:00 2001 From: nordron Date: Wed, 12 Sep 2018 00:17:56 +0300 Subject: [PATCH 6/6] Revert changes --- example.py | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/example.py b/example.py index faaed22..db4c2fd 100644 --- a/example.py +++ b/example.py @@ -49,16 +49,16 @@ def mon(client, device_id): for key, value in res.items(): try: desc = model.value(key) - if isinstance(desc, wideq.EnumValue): - print('- {}: {}'.format( - key, desc.options.get(value, value) - )) - elif isinstance(desc, wideq.RangeValue): - print('- {0}: {1} ({2.min}-{2.max})'.format( - key, value, desc, - )) except KeyError: print('- {}: {}'.format(key, value)) + if isinstance(desc, wideq.EnumValue): + print('- {}: {}'.format( + key, desc.options.get(value, value) + )) + elif isinstance(desc, wideq.RangeValue): + print('- {0}: {1} ({2.min}-{2.max})'.format( + key, value, desc, + )) except KeyboardInterrupt: pass