Working with attributes

Attributes serve to describe a thing in more detail and can be defined for devices, directories, tags and filters, thus providing additional searching and monitoring capabilities.

Attributes consist of a path and a value, and can be of any type, with a flat or a nested structure.

The following examples show you how to create, get, set, list and delete attributes. We have shown how to perform these operations with devices, however, you can use the same approach for directories, tags and filters.

Create an attribute

The following code snippet illustrates how to create an attribute:

Attribute<?> attribute = Attribute.newBuilder().path("path/here").value("value-here").build();

Get an attribute

Get a single attribute's value, providing the attribute's path:

device.attribute("path/to/attribute/here")

Set attributes

The following example illustrates how to set a single attribute to a device. Please, consider that if this is an existing attribute, its value will be replaced with the new one you are setting.

If it doesn't exist, it will be added to the other existing attributes of the device, if any.

device.setAttribute(attribute);

The code snippet below illustrates how to set all attributes to a device. Please, consider that the newly set attributes - even if it is just one attribute - will replace all existing attributes of the device, therefore you may potentially delete some as a result.

List<Attribute<?>> attributes = ...;
device.setAttributes(attributes);

List attributes

List all attributes defined for a particular device:

device.attributes()

Delete attributes

Delete a single attribute defined for a particular device, providing the attribute's path:

device.deleteAttribute("by/path");

Delete all attributes defined for a particular device:

device.deleteAttributes();