From c5cf9803ffc62942214732d792b8ce80dcf3b80c Mon Sep 17 00:00:00 2001 From: Fabio Manganiello Date: Sat, 7 Jan 2023 23:09:42 +0100 Subject: [PATCH 1/2] Take into account the notify_only_if_changed parameter --- platypush/plugins/hid/__init__.py | 3 ++- platypush/schemas/hid.py | 6 +++--- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/platypush/plugins/hid/__init__.py b/platypush/plugins/hid/__init__.py index b3d9498d..9705c292 100644 --- a/platypush/plugins/hid/__init__.py +++ b/platypush/plugins/hid/__init__.py @@ -142,6 +142,7 @@ class HidPlugin(RunnablePlugin): path = dev_def['path'] data_size = rule['data_size'] poll_seconds = rule['poll_seconds'] + notify_only_if_changed = rule['notify_only_if_changed'] last_data = None self.logger.info(f'Starting monitor for device {path}') @@ -162,7 +163,7 @@ class HidPlugin(RunnablePlugin): wait() continue - if len(data) and data != last_data: + if not notify_only_if_changed or data != last_data: data_dump = ''.join(f'{x:02x}' for x in data) get_bus().post(HidDeviceDataEvent(data=data_dump, **dev_def)) last_data = data diff --git a/platypush/schemas/hid.py b/platypush/schemas/hid.py index e260fa82..6cb372ca 100644 --- a/platypush/schemas/hid.py +++ b/platypush/schemas/hid.py @@ -68,10 +68,10 @@ class HidMonitoredDeviceSchema(HidDeviceSchema): ) poll_seconds = fields.Float( - missing=0.1, + missing=0, metadata={ - 'description': 'How often we should check this device for new data ' - '(default: 0.1 seconds)' + 'description': 'How often we should wait before data reads ' + '(default: no wait)' }, ) From 40bdc3b7f3313105f3f5efe7c8a02a0040e630fe Mon Sep 17 00:00:00 2001 From: Fabio Manganiello Date: Sat, 7 Jan 2023 23:21:59 +0100 Subject: [PATCH 2/2] Always wait 5 seconds (regardless of the poll interval) in case of errors. Also, print the error only on the first occurrence, to prevent log spamming. --- platypush/plugins/hid/__init__.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/platypush/plugins/hid/__init__.py b/platypush/plugins/hid/__init__.py index 9705c292..6e33e8c3 100644 --- a/platypush/plugins/hid/__init__.py +++ b/platypush/plugins/hid/__init__.py @@ -158,9 +158,10 @@ class HidPlugin(RunnablePlugin): device = hid.Device(dev_def['vendor_id'], dev_def['product_id']) # type: ignore data = device.read(data_size) except Exception as e: - self.logger.warning(f'Read error from {path}: {e}') + if device: + self.logger.warning(f'Read error from {path}: {e}') device = None - wait() + sleep(5) continue if not notify_only_if_changed or data != last_data: