Ejemplo n.º 1
0
def to_cayenne_lpp(data):
    """Serialize plain data dictionary to binary CayenneLPP format.

    :param data: 

    """

    from cayennelpp import LppFrame
    frame = LppFrame()

    for key, value in data.items():

        # TODO: Maybe implement different naming conventions.
        name = key.split("_")[0]
        try:
            channel = int(key.split("_")[1])
        except IndexError:
            channel = 0

        if "temperature" in name:
            frame.add_temperature(channel, value)
        elif "digital-input" in name:
            frame.add_digital_input(channel, value)
        elif "digital_output" in name:
            frame.add_digital_output(channel, value)
        elif "analog-input" in name:
            frame.add_analog_input(channel, value)
        elif "analog-output" in name:
            frame.add_analog_output(channel, value)
        elif "illuminance" in name:
            frame.add_illuminance(channel, value)
        elif "presence" in name:
            frame.add_presence(channel, value)
        elif "humidity" in name:
            frame.add_humidity(channel, value)
        elif "accelerometer" in name:
            frame.add_accelerometer(channel, value)
        elif "barometer" in name:
            frame.add_barometer(channel, value)
        elif "gyrometer" in name:
            frame.add_gyrometer(channel, value)
        elif "gps" in name:
            frame.add_gps(channel, value)

        # TODO: Fork cayenneLPP and implement load cell telemetry.
        # TODO: Add load encoder as ID 122 (3322)
        # http://openmobilealliance.org/wp/OMNA/LwM2M/LwM2MRegistry.html#extlabel
        # http://www.openmobilealliance.org/tech/profiles/lwm2m/3322.xml
        elif False and "load" in name:
            frame.add_load(channel, value)

        # TODO: Map memfree and other baseline sensors appropriately.

        else:
            # TODO: raise Exception here?
            log.info(
                '[CayenneLPP] Sensor type "{}" not found in CayenneLPP'.format(
                    name))

    return frame.bytes()
Ejemplo n.º 2
0
def to_cayenne_lpp(dataframe: DataFrame):
    """
    Serialize dataframe to binary CayenneLPP format.

    :param dataframe:
    """

    from cayennelpp import LppFrame
    frame = LppFrame()

    channel = {}
    channel['temp'] = 0
    channel['ana_out'] = 0
    channel['hum'] = 0
    channel['press'] = 0
    channel['scale'] = 0

    # TODO: Iterate ``dataframe.readings`` to get more metadata from sensor configuration.
    # It is a list of ``SensorReading`` instances, each having a ``sensor`` and ``data`` attribute.

    for key, value in dataframe.data_out.items():

        #log.debug('-' * 42)

        # TODO: Maybe implement different naming conventions.
        name = key.split("_")[0]
        # try:
        #     channel = int(key.split(":")[1])
        # except IndexError:
        #     channel = 0

        if "temperature" in name:
            frame.add_temperature(channel['temp'], value)
            channel['temp'] += 1
        elif "voltage" in name:
            frame.add_analog_output(channel['ana_out'], value)
            channel['ana_out'] += 1
        elif "humidity" in name:
            frame.add_humidity(channel['hum'], value)
            channel['hum'] += 1
        elif "pressure" in name:
            frame.add_barometer(channel['press'], value)
            channel['press'] += 1
        elif "weight" in name:
            frame.add_analog_input(channel['scale'], value)
            channel['scale'] += 1
        elif "analog-output" in name:
            frame.add_analog_output(channel, value)
        elif "analog-input" in name:
            frame.add_analog_input(channel, value)
        elif "digital-input" in name:
            frame.add_digital_input(channel, value)
        elif "digital_output" in name:
            frame.add_digital_output(channel, value)
        elif "illuminance" in name:
            frame.add_luminosity(channel, value)
        elif "barometer" in name:
            frame.add_barometer(channel, value)
        elif "presence" in name:
            frame.add_presence(channel, value)
        elif "accelerometer" in name:
            frame.add_accelerometer(channel, value)
        elif "gyrometer" in name:
            frame.add_gyrometer(channel, value)
        elif "gps" in name:
            frame.add_gps(channel, value)

        # TODO: Fork cayenneLPP and implement load cell telemetry.
        # TODO: Add load encoder as ID 122 (3322)
        # http://openmobilealliance.org/wp/OMNA/LwM2M/LwM2MRegistry.html#extlabel
        # http://www.openmobilealliance.org/tech/profiles/lwm2m/3322.xml

        # elif False and "load" in name:
        #     frame.add_load(channel, value)

        # TODO: Map memfree and other baseline sensors appropriately.

        else:
            # TODO: raise Exception here?
            log.info(
                '[CayenneLPP] Sensor type "{}" not found in CayenneLPP'.format(
                    name))

    return frame.bytes()
Ejemplo n.º 3
0
def to_cayenne_lpp_hiveeyes(dataframe: DataFrame):
    """
    Serialize dataframe to binary CayenneLPP format.

    :param dataframe:
    """

    from cayennelpp import LppFrame
    frame = LppFrame()

    channel = {}
    channel['temp']   = 10
    channel['volt']   = 2
    channel['pres']   = 1
    channel['scal']   = 6

    # TODO: Iterate ``dataframe.readings`` to get more metadata from sensor configuration.
    # It is a list of ``SensorReading`` instances, each having a ``sensor`` and ``data`` attribute.

    # log.info('dataframe.data_out.items : %s', dataframe.data_out.items())

    for key, value in dataframe.data_out.items():

        #log.debug('-' * 42)

        # TODO: Maybe implement different naming conventions.
        name = key.split("_")[0]
        #     channel = 0

        if "system" in name:
            if "voltage" in name:
                # treat voltage as system sensors
                # put battery voltage to channel 0
                # put solar voltage to channel 1
                # put other voltages to channel 2++
                if "battery" in name:
                    chan = 0
                elif "solar" in name:
                    chan = 1
                else:
                    chan = channel['volt']
                    channel['volt'] += 1
                frame.add_voltage(chan, value)
            elif "temperature" in name:
                frame.add_temperature(0, value)
        elif "i2c" in name:
            # assume BME280 on I2C bus as outside sensor and assign the variables to channel 5
            if "temperature" in name:
                frame.add_temperature(5, value)
            elif "humidity" in name:
                frame.add_humidity(5, value)
            elif "pressure" in name:
                frame.add_barometer(5, value)
        elif "weight" in name:
            # treat weight as outside sensor and assign to channel 5-9
            # Channel 5 for single reading (weight.0))
            # Channel 6-9 for multiple readings (weight.[1-4])
            if name == "weight.0":
                chan = 5
            else:
                chan = channel['scal']
                channel['scal'] += 1
            frame.add_load(chan, value)
        elif "onewire" in name:
            # assume DS18B20 as inside sensors and assign to channel 10++
            if "temperature" in name:
                frame.add_temperature(channel['temp'], value)
                channel['temp'] += 1
        elif "analog-output" in name:
            frame.add_analog_output(channel, value)
        elif "analog-input" in name:
            frame.add_analog_input(channel, value)
        elif "digital-input" in name:
            frame.add_digital_input(channel, value)
        elif "digital_output" in name:
            frame.add_digital_output(channel, value)
        elif "illuminance" in name:
            frame.add_luminosity(channel, value)
        elif "barometer" in name:
            frame.add_barometer(channel, value)
        elif "presence" in name:
            frame.add_presence(channel, value)
        elif "accelerometer" in name:
            frame.add_accelerometer(channel, value)
        elif "gyrometer" in name:
            frame.add_gyrometer(channel, value)
        #elif "gps" in name:
        #    frame.add_gps(channel, value)


        # TODO: Fork cayenneLPP and implement load cell telemetry.
        # TODO: Add load encoder as ID 122 (3322)
        # http://openmobilealliance.org/wp/OMNA/LwM2M/LwM2MRegistry.html#extlabel
        # http://www.openmobilealliance.org/tech/profiles/lwm2m/3322.xml

        # elif False and "load" in name:
        #     frame.add_load(channel, value)

        # TODO: Map memfree and other baseline sensors appropriately.

        else:
            # TODO: raise Exception here?
            log.info('[CayenneLPP] Sensor type "{}" not found in CayenneLPP'.format(name))

    return frame.bytes()