Cumulocity Microservice Tutorial Part 5 (Creation of custom measurements)

Introduction

This microservice will create every x seconds a new custom temperature measurement.

Precondition

The tutorial is based on Microservice Tutorial Part 1a, Part 1b, Part 2, Part 3 & Part 4.

Source code

https://github.com/netperformance/cumulocity-microservice-part5

Microservice preparation

  1. Create a new microservice by using postman
  2. Get the bootstrap credentials by using postman
  3. Configure your application.properties
  4. Configure your cumulocity.json
  5. Subscribe to your microservice

The result of the tutorial shall look like this example:

The JSON structure will look like this example:

 

Structure

Measurement type

c8y_CustomTemperatureMeasurement

Fragment

c8y_example_CustomTemperatureMeasurement

Series

temperatureOutside

Unit

C

Value

71

Custom Measurement creation

The Java SDK is using the svenson library to parse a java class to JSON. If you are using the SDK, you must build the Java class structure so that svenson can convert it to JSON.

Example for a definition of a custom temperature measurement:

package c8y.example;


import java.math.BigDecimal;

import org.svenson.AbstractDynamicProperties;

import com.cumulocity.model.measurement.MeasurementValue;


public class CustomTemperatureMeasurement extends AbstractDynamicProperties {


    private static final long serialVersionUID = 1L;



    public static final String UNIT = "C";



    private MeasurementValue temperatureOutside = new MeasurementValue(UNIT);



    public MeasurementValue getTemperatureOutside() {

        return temperatureOutside;

    }



    public void setTemperatureOutside(MeasurementValue temperatureOutside) {

        this.temperatureOutside = temperatureOutside;

    }



    // Get the temperature (value)

    public BigDecimal getTemperature() {

        return temperatureOutside.getValue();

    }


    // Set the temperature (value)

    public void setTemperature(BigDecimal temperature) {

        temperatureOutside.setValue(temperature);

    }    

}

Your measurement needs to extend the ‘AbstractDynamicProperties’ svenson class. 

Measurement is based on the Cumulocity MeasurementValue class.

The name of the MeasurementValue is representing the measurement attribute (series) e.g. temperatureOutside:

private MeasurementValue temperatureOutside = new MeasurementValue(UNIT);

The name is composed of the package and the name of the class.

/package c8y.example;

….

public class CustomTemperatureMeasurement extends AbstractDynamicProperties {....}

The following command will create a new custom measurement:

// Create a new custom temperature measurement

CustomTemperatureMeasurement customTemperatureMeasurement = new CustomTemperatureMeasurement();


// Set the temperature random value        customTemperatureMeasurement.setTemperature(BigDecimal.valueOf(RandomUtils.nextInt(100)));

Create measurements with more than one value

The structure of the measurement will look like the following example:

Structure

Measurement type

c8y_CustomMixedMeasurement

Fragment 1

measurements_HumidityMeasurement

Series of fragment 1

humidity

Fragment 2

measurements_TemperatureMeasurement

Series 1 of fragment 2

temperatureInside

Series 2 of fragment 2

temperatureOutside

Creation of the humidity measurement:

measurements.HumidityMeasurement humidityMeasurement = new measurements.HumidityMeasurement();        humidityMeasurement.setHumidityValue(BigDecimal.valueOf(RandomUtils.nextInt(100)));

Creation of the temperature measurement with two fragments:

measurements.TemperatureMeasurement temperatureMeasurement = new TemperatureMeasurement();


// fragment 1

temperatureMeasurement.setTemperatureInsideValue(BigDecimal.valueOf(RandomUtils.nextInt(100)));


// fragment 2

temperatureMeasurement.setTemperatureOutsideValue(BigDecimal.valueOf(RandomUtils.nextInt(100)));

Add measurements to measurement representation:

measurementRepresentation.set(humidityMeasurement);

measurementRepresentation.set(temperatureMeasurement);

List of all tutorials

Cumulocity Microservice Tutorial Part 1a (Hello World)
Cumulocity Microservice Tutorial Part 1b (local testing)
Cumulocity Microservice Tutorial Part 2 (Managed Object CRUD)
Cumulocity Microservice Tutorial Part 3 (Microservice/User-Context, external ID & measurement creation
Cumulocity Microservice Tutorial Part 4 (Read and display data from an external weather API & microservice settings/configuration)
Cumulocity Microservice Tutorial Part 5 (Creation of custom measurements)
Cumulocity Microservice Tutorial Part 6 (send a REST request via SDK to get the latest measurement)
Cumulocity Microservice Tutorial Part 7 (Alarm handling)
Cumulocity Microservice Tutorial Part 8 (Event handling)
Cumulocity Microservice Tutorial Part 9 (Operation handling)