Working with devices

As described in more detail in Basic Concepts, the concept of device includes both edge devices and gateways, therefore when you list devices you will list both types. The concept of gateway on the other hand is a device with other devices connected to it. The examples bellow illustrate how to perform some general operations with devices and gateways such as listing devices, getting devices and gateways, getting device features, and listing the edge devices connected to a particular gateway.

List devices - all or filtered

This approach to listing devices allows you to list all devices or all devices matching a given filter, and the results will be shown in pages. Try it out with the example code below:

/* List devices matching the given filter grouped in pages */
 
deviceInventory.devices(
 
/* Construct a filter to be used to list devices */
 
Filter.newBuilder()
 
/* include only devices in the namespace com.example */
 
.namespace("com.example")
 
/* only devices having attribute myAttribute */
 
.query("exists(attributes/myAttribute)")
 
/* sort the result by id */
 
.sort("+id")
 
/* include only id and policyId in the Device objects returned as result */
 
.field("id")
 
.field("policyId")
 
.build(),
 
/* Page size */
 
10
 
);

Attributes mentioned in the example above are described in more detail in Working with attributes.

List devices - filtered, only the first 'n' devices

With this approach to listing devices, you can see the first 'n' number of devices (maximum 200 devices) matching a given filter. It is illustrated in the following code snippet:

/* List the first N devices matching the given filter */
 
deviceInventory.devices(
 
/* Construct a filter to be used to list devices */
 
Filter.newBuilder()
 
/* include only devices in the namespace com.example */
 
.namespace("com.example")
 
/* only devices having attribute myAttribute */
 
.query("exists(attributes/myAttribute)")
 
/* sort the result by id */
 
.sort("+id")
 
/* include only id and policyId in the Device objects returned as result */
 
.field("id")
 
.field("policyId")
 
/* Number of elements to return. Default size is 25, maximum is 200*/
 
.size(200)
 
.build()
 
);

Get a device

Get a device with a particular deviceId:

deviceInventory.device(deviceId);

Get a device providing only the fields id and policyId:

deviceInventory.device(deviceId , "id", "policyId");

Get a gateway

Get a gateway with a particular gatewayId:

deviceInventory.gateway(gatewayId);

Get a gateway providing only the fields id and policyId:

deviceInventory.gateway(gatewayId , "id", "policyId");

Get a device feature

Get a device feature:

deviceInventory.feature(deviceId, featureId);

Get a device feature providing only configuration properties:

deviceInventory.feature(deviceId, featureId, "properties/configuration");

Alternatively, you can get a device feature through the device directly, using the following approach:

device.feature(featureId)

List edge devices connected through a gateway

List all edge devices connected to a gateway:

gateway.devices(Filter.newBuilder().build());