How to check particular measurement Type is present or not in the device

How to check if the device has particular type of measurement like “type”: “c8y_weight” is present or not using java code.

Hi Vaibhavi,

It depends on what you are using to call that measurement API.
If you are using Java SDK provided by Cumulocity then you can just use the class MeasurementFilter that has a couple of methods. You just need to call the method .byType and provide the string value for which you want to fetch that measurement. For example:

        Code snippet:
        MeasurementFilter measurementFilter = new MeasurementFilter()
                .bySource(new GId(deviceId)) //device id will be a string
                .byType("c8y_weight");

then you can just call the method of measurement API interface to fetch the measurements based on this filter which returns a iterable of MeasurementRepresentation. For example:

        Code Snippet
        Iterable<MeasurementRepresentation> measColl = measurementApi.getMeasurementsByFilter(measurementFilter).get(2000).allPages(); //this will return all the records

Now if you are fetching the measurements based on the REST call then you can just add the type param in the Rest call itself. From Postman the URL would be like this →

tenantURL/measurement/measurements?type=c8y_weight.

For doing a REST call with Java I believe you can get some solution on the internet on how to send a get call. There are numerous ways, but I do not have the code snippet right now :stuck_out_tongue:

Hope you understand the solution :slight_smile:

For better understanding go through these Cumulocity apis:
https://cumulocity.com/api/10.13.0/#operation/getMeasurementCollectionResource

Thanks,
Samanyu

1 Like