Beispiel #1
0
    def post(self):
        """Method to create an application"""
        appeui = self.args['appeui']
        name = self.args['name']
        domain = self.args['domain']
        appnonce = self.args['appnonce']
        appkey = self.args['appkey']
        fport = self.args['fport']
        appinterface_id = self.args['appinterface_id']

        message = {}
        # Check for required args
        required = {'appeui', 'name', 'appnonce', 'appkey', 'fport'}
        for r in required:
            if self.args[r] is None:
                message[r] = "Missing the {} parameter.".format(r)
        if message:
            abort(400, message=message)

        # Check this application does not currently exist
        exists = yield Application.exists(where=['appeui = ?', appeui])
        if exists:
            message = {
                'error':
                "Application EUI {} currently exists".format(euiString(appeui))
            }
            abort(400, message=message)

        # Check the appkey doesn't exist
        exists = yield Application.exists(where=['appkey = ?', appkey])
        if exists:
            message = {
                'error':
                "Application key {} currently exists".format(
                    intHexString(appkey, 16))
            }
            abort(400, message=message)

        # Create and validate
        app = Application(appeui=appeui,
                          name=name,
                          domain=domain,
                          appnonce=appnonce,
                          appkey=appkey,
                          fport=fport,
                          appinterface_id=appinterface_id)
        (valid, message) = yield app.valid()
        if not valid:
            abort(400, message=message)

        try:
            a = yield app.save()
            if a is None:
                abort(500, message={'error': "Error saving the application."})
            location = self.restapi.api.prefix + '/app/' + str(appeui)
            returnValue(({}, 201, {'Location': location}))

        except TimeoutError:
            # Exception returns 500 to client
            log.error("REST API timeout for application POST request")
Beispiel #2
0
def show(ctx):
    """show the system configuration.
    
    Args:
        ctx (Context): Click context
    """

    # Form the url and payload
    server = ctx.obj['server']
    payload = {'token': ctx.obj['token']}
    url = 'http://{}/api/v{}/system'.format(server, str(version))

    # Make the request
    data = restRequest(server, url, 'get', payload, 200)
    if data is None:
        return

    # Single application
    c = data
    indent = ' ' * 10
    click.echo('System: {} at {}'.format(c['name'], server))
    click.echo('{}Network interface: {}'.format(indent, c['listen']))
    click.echo('{}LoraWAN port: {}'.format(indent, c['port']))
    click.echo('{}Web server port: {}'.format(indent, c['webport']))
    click.echo('{}Frequency band: {}'.format(indent, c['freqband']))
    click.echo('{}Network ID: 0x{}'.format(indent,
                                           intHexString(c['netid'], 3, sep=2)))
    click.echo('{}OTAA Address Range: 0x{} - 0x{}'.format(
        indent, devaddrString(c['otaastart']), devaddrString(c['otaaend'])))
    t = 'Yes' if c['adrenable'] else 'No'
    click.echo('{}ADR enabled: {}'.format(indent, t))
    if c['adrenable']:
        click.echo('{}ADR margin: {} dB'.format(indent, c['adrmargin']))
        click.echo('{}ADR cycle time: {} s'.format(indent, c['adrcycletime']))
        click.echo('{}ADR message time: {} s'.format(indent,
                                                     c['adrmessagetime']))
    t = 'Yes' if c['fcrelaxed'] else 'No'
    click.echo('{}Relaxed frame count: {}'.format(indent, t))
    t = 'Yes' if c['macqueueing'] else 'No'
    click.echo('{}MAC queueing: {}'.format(indent, t))
    if c['macqueueing']:
        click.echo('{}MAC queue limit: {} s'.format(indent,
                                                    c['macqueuelimit']))
    return
Beispiel #3
0
def show(ctx, deveui):
    """show a device, or all devices.
    
    Args:
        ctx (Context): Click context
        deveui (str): Device EUI string
    """
    if '.' in deveui:
        deveui = str(hexStringInt(str(deveui)))
    
    # Form the url and payload
    server = ctx.obj['server']
    payload = {'token': ctx.obj['token']}
    url = 'http://{}/api/v{}'.format(server, version)
    url += '/devices' if deveui == 'all' else '/device/{}'.format(deveui)
    
    # Make the request
    data = restRequest(server, url, 'get', payload, 200)
    if data is None:
        return
    
    # Single device
    if deveui != 'all':
        d = data
        indent = ' ' * 10
        enable = 'enabled' if d['enabled'] else 'disabled'
        drate = d['tx_datr'] if d['tx_datr'] else 'N/A'
        nwkid = hex(d['devaddr'] >> 25)
        snrav = '{0:.2f} dBm'.format(d['snr_average']) if d['snr_average'] else 'N/A'
        appname = d['appname'] if d['appname'] else 'N/A'
        lat = '{0:.4f}'.format(d['latitude']) if d['latitude'] else 'N/A'
        lon = '{0:.4f}'.format(d['longitude']) if d['longitude'] else 'N/A'
        activ = 'Over the air (OTAA)' if d['otaa'] else 'Personalization (ABP)'
        click.echo('Device EUI: ' + euiString(d['deveui']))
        click.echo(indent + 'device address ' + devaddrString(d['devaddr']) + \
                   ' nwkID ' + nwkid + ' ' + enable)
        click.echo(indent + 'name: ' + d['name'])
        click.echo(indent + 'class: ' + d['devclass'].upper())
        click.echo(indent + 'application EUI: ' + euiString(d['appeui']))
        click.echo(indent + 'activation: ' + activ)
        click.echo(indent + 'appname: ' + appname)
        click.echo(indent + 'latitude: ' + lat)
        click.echo(indent + 'longitude: ' + lon)
        if not d['otaa']:
            click.echo(indent + 'appskey: ' + intHexString(d['appskey'], 16))
            click.echo(indent + 'nwkskey: ' + intHexString(d['nwkskey'], 16))
        click.echo(indent + 'data rate: ' + drate)
        click.echo(indent + 'average SNR: ' + snrav)
        return
        
    # All devices
    click.echo('{:15}'.format('Device') + \
               '{:24}'.format('DeviceEUI') + \
               '{:12}'.format('DevAddr') + \
               '{:9}'.format('Enabled') + \
               '{:5}'.format('Act') + \
               '{:12}'.format('Average-SNR'))
    for i,d in data.iteritems():
        enable = 'Yes' if d['enabled'] else 'No'
        active = 'OTA' if d['otaa'] else 'ABP'
        snravg = '{0:.2f} dBm'.format(d['snr_average']) if d['snr_average'] else 'N/A'
        click.echo('{:14.14}'.format(d['name']) + ' ' + \
                   '{:23}'.format(euiString(d['deveui'])) +  ' ' + \
                   '{:12}'.format(devaddrString(d['devaddr'])) + \
                   '{:9}'.format(enable) + \
                   '{:5}'.format(active) + \
                   '{:12}'.format(snravg))