コード例 #1
0
ファイル: commands.py プロジェクト: hiveeyes/beradio
def beradio_cmd():
    """
    Usage:
      beradio forward --source=serial:///dev/ttyUSB0 --target=mqtt://localhost [--protocol=<version>] [--log=<log>] [--debug]
      beradio decode <payload> [--protocol=<version>] [--debug]
      beradio info
      beradio --version
      beradio (-h | --help)

    Options:
      --source=<source>         Data source, e.g. serial:///dev/ttyUSB0
      --target=<target>         Data sink, e.g. mqtt://localhost
      --protocol=<version>      Protocol version: 1 or 2        [default: 2]
      --log=<log>               Where to send log output        [default: -]
      --version                 Show version information
      --debug                   Enable debug messages
      -h --help                 Show this screen

    """
    options = docopt(beradio_cmd.__doc__, version=APP_NAME)
    #print 'options: {}'.format(options)

    source = options.get('--source')
    target = options.get('--target')
    protocol = options.get('--protocol')

    boot_logging(options)

    if options.get('forward'):
        if source.startswith('serial://') and target.startswith('mqtt://'):
            source = source.replace('serial://', '')
            SerialToMQTT(serial_device=source, mqtt_broker=target, protocol=protocol).setup().forward()

        elif source.startswith('data://') and target.startswith('mqtt://'):
            data = source.replace('data://', '')
            if data == 'stdin':
                data = sys.stdin.read().strip()

            DataToMQTT(mqtt_broker=target, protocol=protocol).setup().publish(data)

        else:
            raise DocoptExit('Unable to handle combination of {} and {} in forwarding mode'.format(options.get('--source'), options.get('--target')))

    elif options.get('decode'):
        p = protocol_factory(protocol)
        payload = options.get('<payload>')
        return json.dumps(p.decode_safe(payload), indent=4)

    elif options.get('info'):
        network_id = str(NetworkIdentifier())
        gateway_id = str(GatewayIdentifier())

        print >>sys.stderr, '-' * 50
        print >>sys.stderr, APP_NAME.center(50)
        print >>sys.stderr, '-' * 50
        print >>sys.stderr, 'config file: {}'.format(BERadioConfig().config_file)
        print >>sys.stderr, 'network_id:  {}'.format(network_id)
        print >>sys.stderr, 'gateway_id:  {}'.format(gateway_id)
コード例 #2
0
ファイル: forward.py プロジェクト: roman2git/beradio
 def __init__(self,
              serial_device,
              mqtt_broker,
              mqtt_topic=None,
              protocol=2):
     self.serial_device = serial_device
     self.mqtt_broker = mqtt_broker
     self.mqtt_topic = mqtt_topic
     self.protocol_class = protocol_factory(protocol)
コード例 #3
0
    def set_offline(self):

        # Get "ping" payload
        info = self.get_status_data('offline')

        # Define testament to be published to "ping.json" in the context of the designated channel
        message = protocol_factory().get_envelope(node='gateway')
        publisher = BERadioMQTTPublisher(self,
                                         self.topic,
                                         message,
                                         retain=True)
        publisher.json(self.STATUS_SUFFIX, info)
コード例 #4
0
    def publish_ping(self):

        logger.info('Publishing ping message')

        # Get "ping" payload
        info = self.get_status_data('online')

        # Publish to "ping.json" in the context of the designated channel
        message = protocol_factory().get_envelope(node='gateway')
        publisher = BERadioMQTTPublisher(self,
                                         self.topic,
                                         message,
                                         retain=True)
        publisher.json(self.STATUS_SUFFIX, info)
コード例 #5
0
ファイル: forward.py プロジェクト: hiveeyes/beradio
 def __init__(self, serial_device, mqtt_broker, mqtt_topic=None, protocol=2):
     self.serial_device = serial_device
     self.mqtt_broker = mqtt_broker
     self.mqtt_topic = mqtt_topic
     self.protocol_class = protocol_factory(protocol)
コード例 #6
0
ファイル: commands.py プロジェクト: roman2git/beradio
def beradio_cmd():
    """
    Usage:
      beradio forward --source=serial:///dev/ttyUSB0 --target=mqtt://localhost [--protocol=<version>] [--log=<log>] [--debug]
      beradio decode <payload> [--protocol=<version>] [--debug]
      beradio info
      beradio --version
      beradio (-h | --help)

    Options:
      --source=<source>         Data source, e.g. serial:///dev/ttyUSB0
      --target=<target>         Data sink, e.g. mqtt://localhost
      --protocol=<version>      Protocol version: 1 or 2        [default: 2]
      --log=<log>               Where to send log output        [default: -]
      --version                 Show version information
      --debug                   Enable debug messages
      -h --help                 Show this screen

    """
    options = docopt(beradio_cmd.__doc__, version=APP_NAME)
    #print 'options: {}'.format(options)

    source = options.get('--source')
    target = options.get('--target')
    protocol = options.get('--protocol')

    boot_logging(options)

    if options.get('forward'):
        if source.startswith('serial://') and target.startswith('mqtt://'):
            source = source.replace('serial://', '')
            SerialToMQTT(serial_device=source,
                         mqtt_broker=target,
                         protocol=protocol).setup().forward()

        elif source.startswith('data://') and target.startswith('mqtt://'):
            data = source.replace('data://', '')
            if data == 'stdin':
                data = sys.stdin.read().strip()

            DataToMQTT(mqtt_broker=target,
                       protocol=protocol).setup().publish(data)

        else:
            raise DocoptExit(
                'Unable to handle combination of {} and {} in forwarding mode'.
                format(options.get('--source'), options.get('--target')))

    elif options.get('decode'):
        p = protocol_factory(protocol)
        payload = options.get('<payload>')
        return json.dumps(p.decode_safe(payload), indent=4)

    elif options.get('info'):
        network_id = str(NetworkIdentifier())
        gateway_id = str(GatewayIdentifier())

        print >> sys.stderr, '-' * 50
        print >> sys.stderr, APP_NAME.center(50)
        print >> sys.stderr, '-' * 50
        print >> sys.stderr, 'config file: {}'.format(
            BERadioConfig().config_file)
        print >> sys.stderr, 'network_id:  {}'.format(network_id)
        print >> sys.stderr, 'gateway_id:  {}'.format(gateway_id)