I wanted to create custom measurement through a microservice like c8y_weight measurement through a microservice,can anyone help in
Please provide more details like used programming language, SDK used vs. no SDK Used etc.
What did you try so far? (code snippets etc.)
Programming language java
code snippet:
try {
final ManagedObjectRepresentation deviceRepresentation = inventoryApi.get(new GId());
MeasurementRepresentation measurementRepresentation = new MeasurementRepresentation();
measurementRepresentation.setSource(deviceRepresentation);
measurementRepresentation.setDateTime(new DateTime());
measurementRepresentation.setType("c8y_Humidity");
measurementRepresentation.setProperty("c8y_humidity",json.getJSONObject("current").get("humidity"));
MeasurementRepresentation response = measurementApi.create(measurementRepresentation);
return Optional.of(response);
} catch (SDKException ex) { // handling exception
System.out.println(ex);
}
I wanted to add unit as well in it,so how can it be done
Hi,
you can add one or multiple measurements with value and unit like this:
import com.cumulocity.model.measurement.MeasurementValue;
...
String measurementType= "myType";
String series = "temp";
MeasurementRepresentation mr = new MeasurementRepresentation();
mr.setType(measurementType);
Map<String, MeasurementValue> measurementSeries = new HashMap<>();
String unit = "C";
long value = 1;
MeasurementValue m = new MeasurementValue(BigDecimal.valueOf(value), unit);
measurementSeries.put(series, m);
mr.set(measurementSeries, measurementType);
measurementApi.create(mr);
2 Likes
This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.