Example #1
0
def main():
    app = mkapp()
    app.listen(config.http_port)

    log.info('Start raproto http server on %s', config.http_port)
    with suppress(KeyboardInterrupt):
        ioloop.IOLoop.current().start()
Example #2
0
    def _on_message(self, client, userdata, msg):
        '''
        The subscribe message callback

        We accept json format.

        The device application should send MQTT retained messages when the
        connection status changed::

            {
                'state': 'online|offline|broken',
                'rev': 'the rev field from registration',
            }

        * ``online``: should be sent right after the device application
          connects to the MQTT server

        * ``offline``: should be sent before the device application disconnects
          from the MQTT server

        * ``broken``: should be sent as the "Last Will Message" right after the
          device application connects to the MQTT server

        * ``rev``: this field is used for stripping out out-of-date message.

        For the response of other command messages, please checkout
        ``iot.csm.ctrl``
        '''
        id_ = msg.topic.split('/')[0]
        data = msg.payload

        # check revision
        if data['state'] in ('online', 'offline', 'broken'):
            rev = data.get('rev')
            if self.rev != rev:
                log.warning(
                    'Device application %s '
                    'state msg revision unknown or out-of-date, dropping',
                    id_)
                return

        if data['state'] == 'online':
            with self.da_ready_lock:
                self.da_ready = True

            log.info('Device application %s state: %s', id_, data['state'])

        elif data['state'] == 'offline':
            with self.da_ready_lock:
                self.da_ready = False
                # clean up
                self.res_callbacks.clear()

            log.info('Device application %s state: %s', id_, data['state'])

        elif data['state'] == 'broken':
            with self.da_ready_lock:
                self.da_ready = False
                # clean up
                self.res_callbacks.clear()

            log.info('Device application %s state: %s', id_, data['state'])

            try:
                http_client = HTTPClient()
                http_client.fetch(
                    'http://localhost:9992/{}'.format(id_),
                    method='DELETE',
                    headers={'Content-Type': 'application/json'},
                    body=json.dumps({'rev': data['rev']}),
                    allow_nonstandard_methods=True
                )
            except HTTPError as e:
                pass

            http_client.close()

        else:
            with suppress(ValueError):
                self._handle_cmd_response(data)
Example #3
0
 def loop_forever(self):
     with suppress(KeyboardInterrupt):
         self.client.loop_forever()