def beradio_cmd():
"""
Usage:
beradio forward --source=serial:///dev/ttyUSB0 --target=mqtt://localhost [--protocol=<version>] [--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]
--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)
评论列表
文章目录