platypush/platypush/backend/adafruit/io/__init__.py

98 lines
3.0 KiB
Python
Raw Normal View History

from typing import Optional
2019-01-12 01:07:38 +01:00
from platypush.backend import Backend
from platypush.context import get_plugin
from platypush.message.event.adafruit import ConnectedEvent, DisconnectedEvent, \
FeedUpdateEvent
class AdafruitIoBackend(Backend):
"""
Backend that listens to messages received over the Adafruit IO message queue
Triggers:
* :class:`platypush.message.event.adafruit.ConnectedEvent` when the
backend connects to the Adafruit queue
* :class:`platypush.message.event.adafruit.DisconnectedEvent` when the
backend disconnects from the Adafruit queue
* :class:`platypush.message.event.adafruit.FeedUpdateEvent` when an
update event is received on a monitored feed
Requires:
* The :class:`platypush.plugins.adafruit.io.AdafruitIoPlugin` plugin to
be active and configured.
"""
def __init__(self, feeds, *args, **kwargs):
"""
:param feeds: List of feed IDs to monitor
:type feeds: list[str]
"""
super().__init__(*args, **kwargs)
from Adafruit_IO import MQTTClient
2019-01-12 01:07:38 +01:00
self.feeds = feeds
self._client: Optional[MQTTClient] = None
2019-01-12 01:07:38 +01:00
def _init_client(self):
if self._client:
return
from Adafruit_IO import MQTTClient
plugin = get_plugin('adafruit.io')
if not plugin:
raise RuntimeError('Adafruit IO plugin not configured')
2020-08-22 14:31:00 +02:00
# noinspection PyProtectedMember
2019-01-12 01:07:38 +01:00
self._client = MQTTClient(plugin._username, plugin._key)
self._client.on_connect = self.on_connect()
self._client.on_disconnect = self.on_disconnect()
self._client.on_message = self.on_message()
def on_connect(self):
def _handler(client):
2019-01-12 01:38:17 +01:00
for feed in self.feeds:
client.subscribe(feed)
2019-01-12 01:07:38 +01:00
self.bus.post(ConnectedEvent())
2020-08-22 14:31:00 +02:00
2019-01-12 01:07:38 +01:00
return _handler
def on_disconnect(self):
def _handler(*_, **__):
2019-01-12 01:07:38 +01:00
self.bus.post(DisconnectedEvent())
2020-08-22 14:31:00 +02:00
2019-01-12 01:07:38 +01:00
return _handler
def on_message(self, *_, **__):
2020-08-22 14:31:00 +02:00
# noinspection PyUnusedLocal
2019-01-12 01:07:38 +01:00
def _handler(client, feed, data):
2020-08-22 14:31:00 +02:00
try:
data = float(data)
2021-04-05 00:58:44 +02:00
except Exception as e:
self.logger.debug('Not a number: {}: {}'.format(data, e))
2019-01-12 01:07:38 +01:00
self.bus.post(FeedUpdateEvent(feed=feed, data=data))
2020-08-22 14:31:00 +02:00
2019-01-12 01:07:38 +01:00
return _handler
def run(self):
super().run()
self.logger.info(('Initialized Adafruit IO backend, listening on ' +
'feeds {}').format(self.feeds))
while not self.should_stop():
try:
self._init_client()
# noinspection PyUnresolvedReferences
2019-01-12 01:07:38 +01:00
self._client.connect()
# noinspection PyUnresolvedReferences
2019-01-12 01:07:38 +01:00
self._client.loop_blocking()
except Exception as e:
self.logger.exception(e)
self._client = None
# vim:sw=4:ts=4:et: