Example #1
0
def measurements(deveui):
    '''
        Returns all measurements of a sensor node
    '''
    return jsonify(list_to_dict((Sensor.select(Sensor, Measurement)
        .join(Measurement)
        .where(Sensor.deveuid == deveui)
        .get())
    ))
Example #2
0
def weather_data():
    '''
        Handles data recieved from the weather sensors
        the payload has the folowing format:
        ['tt', 'xx', 'xx', 'xx', 'xx']

        'tt' is the measurement type
        mapped via the 'measurement_type_map'

        the 'xx' values represent a number in the format of
        'xxxx.xxxx' (four digits bevore the and four after the dot)
    '''
    # remove namespace for parsing
    xml = ElementTree.fromstring(re.sub(' xmlns="[^"]+"', '', request.data, count=1))

    measurement = {
        'deveui': None,
        'time': None,
        'payload_hex': None }

    measurement_type_map = {
        '00': 'temparature',
        '01': 'lumen',
        '02': 'light' }

    for child in xml:
        print(child.tag.lower())
        if child.tag.lower() in measurement:
            print('added {0}'.format(child.text))
            measurement[child.tag.lower()] = child.text

    sensor, created = Sensor.get_or_create(
        deveuid=measurement['deveui'],
        position='47.3846794:8.5329564')

    Measurement.create(
        timestamp=datetime.datetime.strptime(measurement['time'][:19], '%Y-%m-%dT%H:%M:%S'),
        type=measurement_type_map[measurement['payload_hex'][:2]],
        value=measurement['payload_hex'][2:],
        sensor=sensor).save()

    sensor.save()

    return jsonify({'message': str(xml)})
Example #3
0
def graph_data(deveui):
    '''
        Get simple csv data for a grap
    '''
    data = (Sensor.select(Sensor, Measurement)
        .join(Measurement)
        .where(
            Sensor.deveuid == deveui &
            Measurement.type == 'temparature')
        .get())

    measurements = []
    for messung in data.measurements:
        measurements.append({
            'timestamp': messung.timestamp,
            'type': messung.type,
            'value': dafuqnumber(messung.value)
        })

    return jsonify({'data': measurements})
Example #4
0
def sensors():
    '''
        Returns all weather nodes
    '''
    return jsonify(list_to_dict(Sensor.get()))