-
Notifications
You must be signed in to change notification settings - Fork 116
Feature: On demand device polling #48
base: master
Are you sure you want to change the base?
Changes from all commits
7245207
1f92dd4
6fc7f28
464caf3
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -54,6 +54,16 @@ def register_workers(self, config): | |||||
partial(self._queue_command, command), 'interval', | ||||||
seconds=worker_config['update_interval'], | ||||||
) | ||||||
|
||||||
_LOGGER.debug("Adding on-demand polling for %s" % (worker_name)) | ||||||
poll_topics = [] | ||||||
poll_topics.append(worker_obj.format_topic('+', 'poll')) | ||||||
poll_topics.append(worker_obj.format_topic('poll')) | ||||||
for topic in poll_topics: | ||||||
self._mqtt_callbacks.append(( | ||||||
topic, | ||||||
partial(self._poll_wrapper, worker_obj) | ||||||
)) | ||||||
elif hasattr(worker_obj, 'run'): | ||||||
_LOGGER.debug("Registered: %s as daemon" % (worker_name)) | ||||||
self._daemons.append(worker_obj) | ||||||
|
@@ -100,6 +110,33 @@ def _pip_install_helper(package_names): | |||||
for package in package_names: | ||||||
pip_main(['install', '-q', package]) | ||||||
|
||||||
def _poll_wrapper(self, worker_obj, client, userdata, c): | ||||||
if c.payload.decode('utf-8') != '': | ||||||
return | ||||||
poll_device_supported = 'poll_device' in worker_obj.status_update.__code__.co_varnames | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It could also benefit from some devices getter. When there is None / not defined / only one device ( ? ) return from worker_obj.devices then it does not support |
||||||
topic_prefixes = [] | ||||||
topic_prefixes.append(userdata.get('global_topic_prefix', '')) | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please use some topic helpers, to be sure it wont break after some refactoring. Line 81 in 54ae518
bt-mqtt-gateway/workers/base.py Line 10 in 54ae518
|
||||||
topic_prefixes.append(getattr(worker_obj, 'topic_prefix', '')) | ||||||
topic_prefixes.append('poll') | ||||||
device_name = str(c.topic) | ||||||
for suffix in topic_prefixes: | ||||||
if suffix: | ||||||
device_name = str(device_name).replace(suffix, '').replace('/', '') | ||||||
device_name = device_name if device_name else None | ||||||
if not poll_device_supported and device_name: | ||||||
_LOGGER.debug("This worker does not support polling specific devices." | ||||||
" Ignoring: %s", c.topic) | ||||||
return | ||||||
elif not poll_device_supported: | ||||||
poll_args = [] | ||||||
elif device_name not in getattr(worker_obj, 'devices', []): | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please add some getter for devices and use it, instead of accessing variable directly. |
||||||
_LOGGER.debug("Device not found. Ignoring: %s", c.topic) | ||||||
return | ||||||
else: | ||||||
poll_args = [device_name] | ||||||
_LOGGER.debug("Poll initiated for %s", c.topic) | ||||||
self._queue_command(self.Command(worker_obj.status_update, poll_args)) | ||||||
|
||||||
def _on_command_wrapper(self, worker_obj, client, userdata, c): | ||||||
_LOGGER.debug("on command wrapper for with %s: %s", c.topic, c.payload) | ||||||
global_topic_prefix = userdata['global_topic_prefix'] | ||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe it should be generalized to something like: device_filter=None , which will act as filter for self.devices, then that if and continue wont be needed inside each loop. When self.devices will be a getter method ( see other comment ) it could take array to filter devices and return only filtered subset.