Skip to content
Snippets Groups Projects
Commit 1e2027fb authored by Matthias Bodenbenner's avatar Matthias Bodenbenner
Browse files

small refactoring

parent da26e2c8
No related branches found
No related tags found
No related merge requests found
No preview for this file type
import logging
logger = logging.getLogger('MQTT')
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
from .Exceptions import MQTTException as MQTTException
from .Exceptions import ConnectionError as ConnectionError
from .Exceptions import SubscriptionError
from .Exceptions import CallbackError as CallbackError
from .Exceptions import PublishError as PublishError
from .Client import MQTTPublisher
from .Client import MQTTReceiver
import logging
logger = logging.getLogger('MQTT')
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
from .exceptions import MQTTException as MQTTException
from .exceptions import ConnectionError as ConnectionError
from .exceptions import SubscriptionError
from .exceptions import CallbackError as CallbackError
from .exceptions import PublishError as PublishError
from .client import MQTTPublisher
from .client import MQTTReceiver
from .specialized import ScheduledMQTTPublisher
import datetime
import functools
import json
import logging
from typing import Dict
logger = logging.getLogger('MQTT')
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
......@@ -229,43 +226,3 @@ class MQTTReceiver(MQTTClient):
self.__on_message_callbacks[key](message.topic, message.payload)
except Exception as exception:
self.logger.error("Exception while processing callback for topic {}: {}".format(message.topic, str(exception)))
class ScheduledMQTTPublisher(MQTTPublisher):
def __init__(self, loop, duid, schedule: Dict, *args, **kwargs):
MQTTPublisher.__init__(self, duid, *args, **kwargs)
self._loop = loop
self._schedule = schedule
if len(self._schedule.keys()) > 0:
self._update()
def _update(self):
now = datetime.datetime.now()
next = None
for luid in self._schedule:
data = self._schedule[luid]
if data['next'] is not None and data['next'] <= now:
try:
value = data['method']()
ret = {'uuid': luid, 'value': value}
self.publish(luid, json.dumps(ret), 1)
except Exception as e:
pass
try:
if callable(data['interval']):
data['next'] = now + datetime.timedelta(seconds=data['interval']())
else:
data['next'] = now + datetime.timedelta(seconds=data['interval'])
except Exception as e:
# if the next time for publishing can not be set, deactivate publising by setting 'next' to None
data['next'] = None
if next is None or (data['next'] is not None and data['next'] < next):
next = data['next']
if next is None:
next = now + datetime.timedelta(seconds=10)
self._loop.call_later((next - now).seconds + (next - now).microseconds / 1e6, self._update)
File moved
import datetime
import json
from typing import Dict
from wzl.mqtt.client import MQTTPublisher
class ScheduledMQTTPublisher(MQTTPublisher):
def __init__(self, loop, duid, schedule: Dict, *args, **kwargs):
MQTTPublisher.__init__(self, duid, *args, **kwargs)
self._loop = loop
self._schedule = schedule
if len(self._schedule.keys()) > 0:
self._update()
def _update(self):
now = datetime.datetime.now()
next = None
for luid in self._schedule:
data = self._schedule[luid]
if data['next'] is not None and data['next'] <= now:
try:
value = data['method']()
ret = {'uuid': luid, 'value': value}
self.publish(luid, json.dumps(ret), 1)
except Exception as e:
pass
try:
if callable(data['interval']):
data['next'] = now + datetime.timedelta(seconds=data['interval']())
else:
data['next'] = now + datetime.timedelta(seconds=data['interval'])
except Exception as e:
# if the next time for publishing can not be set, deactivate publising by setting 'next' to None
data['next'] = None
if next is None or (data['next'] is not None and data['next'] < next):
next = data['next']
if next is None:
next = now + datetime.timedelta(seconds=10)
self._loop.call_later((next - now).seconds + (next - now).microseconds / 1e6, self._update)
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment