Beispiel #1
0
    def _on_disconnect(self, client, userdata, rc):
        log.info('%s (%s) disconnected from  %s.',
                 DANColor.wrap(DANColor.data, self.context.name),
                 DANColor.wrap(DANColor.data, self.context.app_id),
                 DANColor.wrap(DANColor.data, self.context.url))
        if hasattr(self, '_disconn_lock'):  # we won't have it if reconnecting
            self._disconn_lock.release()

        if self.context.on_disconnect:
            self.context.on_disconnect(self)
Beispiel #2
0
    def _on_disconnect(self, client, userdata, rc):
        log.info('Disconnect to %s.',
                 DANColor.wrap(DANColor.data, self.context.url))
        if hasattr(self, '_disconn_lock'):  # we won't have it if reconnecting
            self._disconn_lock.release()

        if self.context.on_disconnect:
            self.context.on_disconnect()
Beispiel #3
0
    def _on_connect(self, client, userdata, flags, rc):

        if not self._is_reconnect:
            log.info('Successfully connect to %s.',
                     DANColor.wrap(DANColor.data, self.context.url))
            log.info('Device ID: %s.',
                     DANColor.wrap(DANColor.data, self.context.app_id))
            log.info('Device name: %s.',
                     DANColor.wrap(DANColor.data, self.context.name))

            res, _ = client.subscribe(self.context.o_chans['ctrl'], qos=2)
            if res != MQTT_ERR_SUCCESS:
                #FIXME: use proper exception type
                raise Exception('Subscribe to control channel failed')

        else:  # in case of reconnecting, we need to renew all subscriptions
            log.info('Reconnect: %s.',
                     DANColor.wrap(DANColor.data, self.context.name))
            client.publish(self.context.i_chans['ctrl'],
                           json.dumps({
                               'state': 'offline',
                               'rev': self.context.rev
                           }),
                           retain=True,
                           qos=2)
            for k, topic in self.context.o_chans.items():
                log.info('Renew subscriptions for %s -> %s',
                         DANColor.wrap(DANColor.data, k),
                         DANColor.wrap(DANColor.data, topic))
                client.subscribe(topic, qos=2)
            # FIXME: online msg may reach eariler then offline, race condition
            time.sleep(1)

        msg = client.publish(self.context.i_chans['ctrl'],
                             json.dumps({
                                 'state': 'online',
                                 'rev': self.context.rev
                             }),
                             retain=True,
                             qos=2)
        self.context._mqueue.put(msg)

        self._is_reconnect = True

        if self.context.on_connect:
            self.context.on_connect()
Beispiel #4
0
from paho.mqtt import client as mqtt
from paho.mqtt.client import MQTT_ERR_SUCCESS

from iottalkpy.color import DANColor

# python2 compatibility
try:
    import queue
except ImportError:
    import Queue as queue

__all__ = ('NoData', 'Client', 'push', 'register', 'deregister',
           'loop_forever')

logging.basicConfig(level=logging.INFO)  # root logger setting
log = logging.getLogger(DANColor.wrap(DANColor.logger, 'DAN'))
log.setLevel(level=logging.INFO)


class NoData():
    pass


class DeviceFeature(object):
    def __init__(self, df_name, df_type=None):
        self._df_name = df_name
        self._df_type = df_type if df_type is not None else [None]
        self._push_data = None
        self._on_data = None

    @property