From 9dacd2d3c9c9c6840767b0526001009ec82a0a50 Mon Sep 17 00:00:00 2001 From: Fabio Manganiello Date: Sat, 21 Nov 2020 01:36:37 +0100 Subject: [PATCH 1/2] Fixed croniter timezone offset logic mismatch --- platypush/backend/alarm.py | 12 +++++++----- setup.py | 7 ++++--- 2 files changed, 11 insertions(+), 8 deletions(-) diff --git a/platypush/backend/alarm.py b/platypush/backend/alarm.py index 8e50a14099..385b3dbeac 100644 --- a/platypush/backend/alarm.py +++ b/platypush/backend/alarm.py @@ -1,4 +1,5 @@ import datetime +import enum import os import time import threading @@ -6,7 +7,7 @@ import threading from typing import Optional, Union, Dict, Any, List import croniter -import enum +from dateutil.tz import gettz from platypush.backend import Backend from platypush.context import get_bus, get_plugin @@ -54,18 +55,19 @@ class Alarm: self._runtime_snooze_interval = snooze_interval def get_next(self) -> float: - now = time.time() + now = datetime.datetime.now().replace(tzinfo=gettz()) try: cron = croniter.croniter(self.when, now) return cron.get_next() except (AttributeError, croniter.CroniterBadCronError): try: - timestamp = datetime.datetime.fromisoformat(self.when).timestamp() + timestamp = datetime.datetime.fromisoformat(self.when).replace(tzinfo=gettz()) except (TypeError, ValueError): - timestamp = (datetime.datetime.now() + datetime.timedelta(seconds=int(self.when))).timestamp() + timestamp = (datetime.datetime.now().replace(tzinfo=gettz()) + + datetime.timedelta(seconds=int(self.when))) - return timestamp if timestamp >= now else None + return timestamp.timestamp() if timestamp >= now else None def is_enabled(self): return self._enabled diff --git a/setup.py b/setup.py index 1aecaa464c..35305e9b62 100755 --- a/setup.py +++ b/setup.py @@ -163,6 +163,7 @@ setup( 'wheel', 'zeroconf>=0.27.0', 'tz', + 'python-dateutil', ], extras_require={ @@ -173,9 +174,9 @@ setup( # Support for Pushbullet backend and plugin 'pushbullet': ['pushbullet.py @ https://github.com/rbrcsk/pushbullet.py/tarball/master'], # Support for HTTP backend - 'http': ['flask', 'python-dateutil', 'frozendict', 'bcrypt'], + 'http': ['flask', 'frozendict', 'bcrypt'], # Support for uWSGI HTTP backend - 'uwsgi': ['flask', 'python-dateutil', 'frozendict', 'uwsgi', 'bcrypt'], + 'uwsgi': ['flask', 'frozendict', 'uwsgi', 'bcrypt'], # Support for MQTT backends 'mqtt': ['paho-mqtt'], # Support for RSS feeds parser @@ -220,7 +221,7 @@ setup( # Support for serial port plugin 'serial': ['pyserial'], # Support for ICal calendars - 'ical': ['icalendar', 'python-dateutil'], + 'ical': ['icalendar'], # Support for joystick backend 'joystick': ['inputs'], # Support for Kodi plugin From 1777ebb0516118cdef20046a92caab496fa7c6cb Mon Sep 17 00:00:00 2001 From: Fabio Manganiello Date: Wed, 30 Dec 2020 19:44:30 +0100 Subject: [PATCH 2/2] Retry logic in case of Pushbullet listener error --- platypush/backend/pushbullet/__init__.py | 25 ++++++++++++++++-------- 1 file changed, 17 insertions(+), 8 deletions(-) diff --git a/platypush/backend/pushbullet/__init__.py b/platypush/backend/pushbullet/__init__.py index c3e905b25c..e1fc67e01d 100644 --- a/platypush/backend/pushbullet/__init__.py +++ b/platypush/backend/pushbullet/__init__.py @@ -124,14 +124,29 @@ class PushbulletBackend(Backend): def close(self): if self.listener: self.listener.close() + self.listener = None def on_stop(self): super().on_stop() return self.close() + def on_error(self, e): + self.logger.exception(e) + self.close() + self.run_listener() + + def run_listener(self): + from pushbullet import Listener + + self.logger.info('Initializing Pushbullet backend - device_id: {}'.format(self.device_name)) + self.listener = Listener(account=self.pb, on_push=self.on_push(), on_error=self.on_error, + http_proxy_host=self.proxy_host, + http_proxy_port=self.proxy_port) + + self.listener.run_forever() + def run(self): # noinspection PyPackageRequirements - from pushbullet import Listener super().run() initialized = False @@ -144,13 +159,7 @@ class PushbulletBackend(Backend): self.logger.error('Pushbullet initialization error: {}'.format(str(e))) time.sleep(30) - self.logger.info('Initialized Pushbullet backend - device_id: {}' - .format(self.device_name)) + self.run_listener() - self.listener = Listener(account=self.pb, on_push=self.on_push(), - http_proxy_host=self.proxy_host, - http_proxy_port=self.proxy_port) - - self.listener.run_forever() # vim:sw=4:ts=4:et: