Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

The Device class represents a Device on the network, in a protocol agnostic way.

Certain device types Specific devices will have their own specific device class implementations implementations of the methods defined in the Device class (such as ZigbeeDevice and its subclasses, LightDevice). All other devices will be an instance of the GenericDevice, which implements the methods , ThermostatDevice, etc); however, they will all share the same API defined in the Device class interface.

Sub-Classes

Page Tree
root@self

...

getProperties()

Usage

Gets all Property objects known Properties associated with this device. Not all devices will have Property objects associated with them; however, all Devices will have underlying protocol-level properties that can be accessed via the getProtocolProperties() and getCachedProtocolProperties() methods.The first call to this method will perform a discovery of all the attributes on the device If there are no known Properties on the Device (for example, because they haven't yet been read via a call to "getProperty", or discovered via a call to "discoverAllProperties", or otherwise reported to the Gateway via a reporting mechanism, etc), then this method will return an empty Collection of Property objects.

Every call to this function will go out to the underlying network to retrieve all the latest values for all known Properties on the remote device.

This is a non-blocking call which returns a CompletableFuture of a Collection of Property objects.

...

Gets all last known (cached) Property objects associated with this device. Cached values are updated on reads, writes, reads, and attribute reports.

Calling this function will not result in a request going out over the network.

Returns a Collection of Property objects. The returned collection will be empty if no device discovery has taken place there are no known Properties on the Device because they haven't yet been read or discovered (i.e. no previous calls to getPropertiesgetProperty(String) or discoverAllProperties() have has been made).

Parameters

None.

...

This call will request the latest value of the Property from the device on the network. If no properties has been discovered so far, it will perform a discovery on all properties.

This is a non-blocking call which returns a CompletableFuture of the requested Property.

...

This call will return the last known (cached) value of the Property. Cached values are updated on reads, writes, reads, and attribute reports.

Returns an Optional of the requested Property. The Optional will be empty if the cached Property doesn't exist.

...

getProtocolProperties()

Usage

Gets all underlying known protocol properties from the device on the network .Every call to this function will go out to the underlying network to retrieve all the values (i.e. if it's a zigbee device, it will return the known zigbee attributes on the Device). If there are no known protocol properties on the Device (for example, because they haven't yet been read or discovered), then this method will return an empty Collection of Property objects.

Every call to this function will go out to the underlying network to retrieve all known Properties for the remote device.

This is a non-blocking call which returns a CompletableFuture of a Collection of Property objects.

...

Code Block
languagejava
GatewayClient gateway = new GatewayClient(new ConnectionInfo(ConnectionType.ZIGBEE_UART, "/dev/ttyUSB0"));
...
Device device = gateway.getDevice(id);
Collection<Property> properties = d.getCachedProtocolProperties();
for (Property p : properties) {
    System.out.println("name: " + p.getName() + ", value: " + p.getValue() + ", type: " + p.getType());
}

discoverAllProperties()

Usage

Gets all Property objects, and their values, that exist on the remote device. Note that this is a potentially long-running call. See below for more information.

A request to obtain all the Property values that exist on the remote device will be sent out over the underlying network, and will result in a PropertyUpdate handler being called if the value is different than the last time the Property was read, and a handler exists for this device.

Returns a CompletableFuture of a Collection of Property objects. Once a call to this method has been made, the list of Properties and their values will be cached, and individual Properties can be accessed or refreshed using the getProperty, getCachedProperty, getProperties, getCachedProperties, and updateProperty methods.

The CompletableFuture will complete exceptionally on all errors.

Note that this is potentially a long-running call, because the remote device may have many properties, or may be a sleepy battery powered device which responds to requests over long intervals in order to preserve battery power. This call is typically not used in production applications, as system integrators typically know in advance which properties they want to interact with, and will simply use the "getProperty" and "updateProperty" methods to interact with them directly, without having to discover all the properties on the device.

Parameters

None.

Returns

Return TypeDescription
CompletableFuture<Collection<Property>>

A CompletableFuture of a Collection of Property objects.

Will compete exceptionally on any errors.

Examples

Code Block
languagejava
GatewayClient gateway = new GatewayClient(new ConnectionInfo(ConnectionType.ZIGBEE_UART, "/dev/ttyUSB0"));
...
Device device = gateway.getDevice(id);

CompletableFuture<Collection<Property>> properties = device.discoverAllProperties();

// asynchronously print out the values once discovery is complete.
properties.thenAccept( collection -> {
    for (Property p : collection) {
        System.out.println("Discovered: " + p.toString());
    }
});

updateProperty(Property property)

Usage

Updates the Property associated with this device.

Parameters

NameTypeDescription
propertyPropertyAn object containing the name and value of the Property to update.

Returns

Return TypeDescription
Device.PropertyCommandData

Represents a request to read or write a Property.

Contains a CompletableFuture<Property> and a cached Property.

The CompletableFuture<Property> represents a request over the network to read/write the Property. Will be null if Property doesn't exist, and will return exceptionally on any other errors.

The cached Property contains the last known value, or empty if none/doesn't exist.

Examples

Code Block
languagejava
GatewayClient gw = new GatewayClient(new ConnectionInfo(ConnectionType.ZIGBEE_UART, "/dev/ttyUSB0"));
...
// turn a light device on
final Device d = gw.getDevice(id);
Property property = new Property("OnOff, "boolean", "true");
d.updateProperty(property).getFuture().whenComplete( (result, throwable) -> {
    if (throwable != null) {
        System.out.println("error: " + throwable.getMessage());
        System.out.println("suggestion: ensure you are forming the correct JSON request, with property key:value pairs");
    } else if (result != null) {
        System.out.println("result: " + result.getValue());    
    }
});

...

bindDevice(String destinationDeviceId, String propertyToBind)

Usage

Creates a binding between this device (the source) and a destination device, on the specified Property.

...

Note that if the Properties of a Device haven't yet been discovered, the bind will fail. It is assumed that the properties of a Device have been discovered (using a call like getProperties for example) before being able to bind to a specific Property.

Parameters

NameTypeDescription
destinationDeviceIdStringThe ID of the destination device that we want to bind to.
propertyToBindStringThe name of the Property to bind on. For example, OnOff.

Returns

Return TypeDescription
Optional<CommandData>

An optional CommandData object, which contains a CompletableFuture<String> of the future result, as well as a cached version of the result since the last call.

The result can contain error codes or messages. Usually, it will return a simple "success" if successful.

...

InvalidInputException if the supplied parameters are incorrect, refer to objects that don't exist or properties that can't be bound or don't exist because a discovery has not taken place yet.

Examples

Code Block
languagejava
GatewayClient gw = new GatewayClient(new ConnectionInfo(ConnectionType.ZIGBEE_UART, "/dev/ttyUSB0"));
...
final Device sourceDevice = gw.getDevice(sourceId);
sourceDevice.bindDevice(destinationId, property)
			.get()
	        .getCommandResult()
	        .whenComplete((result, error) -> {
	            if (error != null) {
	                System.out.println("error calling bind: " + error.getMessage());
	            } else {
	            	System.out.println("bind device result: " + result);
	            }
			});

unbindDevice(String destinationDeviceId, String propertyToBind)

Usage

Removes the binding between this device (the source) and the destination device.

Parameters

NameTypeDescription
destinationDeviceIdStringThe ID of the destination device that we want to bind to.
propertyToBindStringThe name of the Property to bind on. For example, OnOff.

Returns

Return TypeDescription
Optional<CommandData>

An optional CommandData object, which contains a CompletableFuture<String> of the future result, as well as a cached version of the result since the last call.

The result can contain error codes or messages. Usually, it will return a simple "success" if successful.

...

InvalidInputException if the supplied parameters are incorrect, refer to objects that don't exist or properties that can't be bound or don't exist because a discovery has not taken place yet.

Examples

Code Block
languagejava
GatewayClient gw = new GatewayClient(new ConnectionInfo(ConnectionType.ZIGBEE_UART, "/dev/ttyUSB0"));
...
final Device sourceDevice = gw.getDevice(sourceId);
sourceDevice.unbindDevice(destinationId, property)
			.get()
	        .getCommandResult()
	        .whenComplete((result, error) -> {
	            if (error != null) {
	                System.out.println("error calling unbind: " + error.getMessage());
	            } else {
	            	System.out.println("unbind device result: " + result);
	            }
			});

configureReporting(String propertyToBind, List<Parameter> parameters)

Usage

Configures reporting a specified Property on this device. Reports are generally sent to the destination device that has a binding (see bind() method) on the same Property being configured using the configureReporting() method.

Note: for reporting to work for zigbee devices, you must creating a binding between the remote Device and the GatewayClient (by using the bind() API first).

Parameters

NameTypeDescription
propertyToBindStringThe name of the Property to bind on. For example, onOff.
parametersList<Parameter>

A list of Parameter objects representing various configuration options.

See below for a list of configuration parameters that are currently supported.

...

Configuration parameters vary depending on the "DIRECTION" parameter. Currently, only one value of the DIRECTION parameter is supported:

NameValueDescription
ConfigureReportingParameter.DIRECTION0x00The receiver of the command should send reports to each destination, as resolved by the bindings for the cluster hosting the properties to be reported.
ConfigureReportingParameter.MIN_REPORTING_INTERVAL0x0000 - 0xFFFFThe minimum interval, in seconds, between issuing reports of the specified property.
ConfigureReportingParameter.MAX_REPORTING_INTERVAL0x0000 - 0xFFFFThe maximum interval, in seconds, between issuing reports of the specified property.
ConfigureReportingParameter.REPORTABLE_CHANGEOptionalThe minimum change to the property that will result in a report being issued. This field is of variable length. For attributes with 'analog' data type (things that can be represented by integers, floats, etc.) the field has the same data type as the attribute. Discrete data types (such as boolean, arrays, strings, etc) can omit this field.

Returns

Return TypeDescription
Optional<CommandData>

An optional CommandData object, which contains a CompletableFuture<String> of the future result, as well as a cached version of the result since the last call.

The result can contain error codes or messages. Usually, it will return a simple "success" if successful.

...

InvalidInputException if the supplied parameters are incorrect, or refer to properties or configuration parameters that don't exist or are malformed, etc.

Examples

Code Block
languagejava
GatewayClient gw = new GatewayClient(new ConnectionInfo(ConnectionType.ZIGBEE_UART, "/dev/ttyUSB0"));
...
final Device device = gw.getDevice(id);
List<Parameter> onOffParams = new ArrayList<>();
onOffParams.add(new Parameter(ConfigureReportingParameter.DIRECTION, "0x00"));
onOffParams.add(new Parameter(ConfigureReportingParameter.MIN_REPORTING_INTERVAL, "0x0000"));
onOffParams.add(new Parameter(ConfigureReportingParameter.MAX_REPORTING_INTERVAL, "0x012C")); // 5 minutes
try {
    CompletableFuture<String> futureResult = device.configureReporting("onOff", onOffParams).get().getCommandResult();
    String result = futureResult.get(30, TimeUnit.SECONDS);
    System.out.println("result: " + result);
} catch (Exception e) {
    throw new CompletionException(e);
}

enableDefaultReporting(String propertyToBind, List<Parameter> parameters)

Usage

Configures the Device to report to the GatewayClient whenever there are changes to its properties.

Depending on the Device type (i.e. LightDevice, ThermostatDevice), this method will configure different attributes for reporting. Calling this method on the DefaultDevice will have no effect.

Parameters

None.

Returns

Return TypeDescription
CompletableFuture<String>

A CompletableFuture<String> that will contain the result of the call.

The result can contain error codes or messages. Usually, it will return a simple "success" if successful.

...

IllegalStateException if the function is called before the Device Properties have been discovered (i.e. through getProperties()).

Examples

Code Block
languagejava
GatewayClient gw = new GatewayClient(new ConnectionInfo(ConnectionType.ZIGBEE_UART, "/dev/ttyUSB0"));
...
final Device device = gw.getDevice(idStr);
try {
    final String result = device.enableDefaultReporting().get(30, TimeUnit.SECONDS);
    System.out.println("Status for command: " + result);
} catch (IllegalStateException | InterruptedException | ExecutionException | TimeoutException e) {
    System.out.println(e.getMessage());
}

...

addPropertyUpdateHandler(BiConsumer<Device, Property> handler)

Usage

Adds a Property update listener that is triggered when any Property on a Device changes.

Will reject duplicates handler instances.

Parameters

NameTypeDescription
handlerBiConsumer<Device, Property>

An instance or lambda that implements the BiConsumer<Device, Property> interface.

Returns

Return TypeDescription
booleanReturns true if the handler was added to the list; false otherwise.

Examples

Code Block
languagejava
public class SamplePropertyHandler implements BiConsumer<Device, Property> {
    @Override
    public void accept(Device device, Property property) {
        // This method gets called whenever a Property on a device changes.
        // By default, client callbacks are called from a single thread managed by gateway api.
        // Therefore, avoid writing blocking code in this method, or other client callbacks
        // (such as DeviceEventHandlers, or ProtocolHandlers) may be blocked.
        System.out.println("Property update triggered for device: " + device.getID());
        System.out.println("Property: " + property.getName() + ", value: " + property.getValue() + ", type: " + property.getType());
}

...

GatewayClient gateway = new GatewayClient(new ConnectionInfo(ConnectionType.ZIGBEE_UART, "/dev/ttyUSB0"));
Device device = gateway.getDevice(id);

// example of supplying a class that implements the BiConsumer<Device, Property> interface
device.addPropertyUpdateHandler(new SamplePropertyHandler());

// example of a supplying a lambda
d.addPropertyUpdateListener(p -> {
	System.out.println("lambda property update triggered for device: " + d.getID());
    System.out.println("Property: " + p.getName() + ", value: " + p.getValue() + ", type: " + p.getType());
});

removePropertyUpdateHandler(BiConsumer<Device, Property> handler)

Usage

Removes a previously added handler.

Parameters

NameTypeDescription
handlerBiConsumer<Device, Property>An instance that implements the BiConsumer<Device, Property> interface.

Returns

Return TypeDescription
booleanReturns true if the handler was removed; false otherwise.

Examples

Code Block
languagejava
GatewayClient gateway = new GatewayClient(new ConnectionInfo(ConnectionType.ZIGBEE_UART, "/dev/ttyUSB0"));
...

SamplePropertyUpdateHandler handler = new SamplePropertyUpdateHandler();
gateway.getDevice(id).addPropertyUpdateHandler(handler);
...
// do stuff
...
gateway.getDevice(id).removePropertyUpdateHandler(handler);

removeAllPropertyUpdateHandlers()

Usage

Removes all handlers that have been previously registered.

Parameters

None.

Returns

Return TypeDescription
booleanReturns true if any handlers were removed; false otherwise.

Examples

Code Block
languagejava
GatewayClient gw = new GatewayClient(new ConnectionInfo(ConnectionType.ZIGBEE_UART, "/dev/ttyUSB0"));
...
gw.getDevice(id).removeAllPropertyUpdateHandlers();

addProtocolHandler(BiConsumer<Device, String> handler)

Usage

Adds a Protocol Handler that is triggered when any protocol level message from the Device is being passed through to the application.

Will reject duplicate handler instances.

Parameters

NameTypeDescription
handlerBiConsumer<Device, String>

An instance or lambda that implements the BiConsumer<Device, String> interface.

Returns

Return TypeDescription
booleanReturns true if the handler was added to the list; false otherwise.

Examples

Code Block
languagejava
public class SampleProtocolHandler implements BiConsumer<Device, String> {
    @Override
    public void accept(Device device, String json) {
        // This method will be called whenever an underlying protocol message is being passed through.
        // By default, client callbacks are called from a single thread managed by GAPI.
        // Therefore, avoid writing blocking code in this method, or other client callbacks
        // (such as DeviceEventHandlers, or PropertyUpdateHandlers) may be blocked.

        // See the ProtocolPassthrough documentation for the format of the json message,
        // or inspect the ZigBeeMessageTypeAdapter class for possible key names.
        try {
            final JsonObject jsonObject = jsonParser.parse(json).getAsJsonObject();
            final JsonObject message = jsonObject.get(ZigBeeMessageTypeAdapter.MESSAGE_KEY).getAsJsonObject();
            
            System.out.println("protocol passthru msg: " + message.toString());

        } catch (Exception ex) {
            System.out.println("Exception handling protocol msg: " + json + ", Exception: " + ex.toString());
        }
    }
}

...

GatewayClient gateway = new GatewayClient(new ConnectionInfo(ConnectionType.ZIGBEE_UART, "/dev/ttyUSB0"));
Device device = gateway.getDevice(id);

// example of supplying a class that implements the BiConsumer<Device, String> interface
device.addProtocolHandler(new SampleProtocolHandler());

// example of a supplying a lambda
d.addPropertyUpdateListener( (d,json) -> {
	System.out.println("lambda property update triggered for device: " + d.getID());
    System.out.println("json: " + json);
});

removeProtocolHandler(BiConsumer<Device, String> handler)

Usage

Removes a previously added handler.

Parameters

NameTypeDescription
handlerBiConsumer<Device, String>An instance that implements the BiConsumer<Device, String> interface.

Returns

Return TypeDescription
booleanReturns true if the handler was removed; false otherwise.

Examples

Code Block
languagejava
GatewayClient gateway = new GatewayClient(new ConnectionInfo(ConnectionType.ZIGBEE_UART, "/dev/ttyUSB0"));
...

SamplePropertyUpdateHandler handler = new SampleProtocolHandler();
gateway.getDevice(id).addProtocolHandler(handler);
...
// do stuff
...
gateway.getDevice(id).removeProtocolHandler(handler);

removeAllProtocolHandlers()

Usage

Removes all handlers that have been previously registered.

Parameters

None.

Returns

Return TypeDescription
booleanReturns true if any handlers were removed; false otherwise.

Examples

Code Block
languagejava
GatewayClient gw = new GatewayClient(new ConnectionInfo(ConnectionType.ZIGBEE_UART, "/dev/ttyUSB0"));
...
gw.getDevice(id).removeAllProtocolHandlers();

Network API

leaveNetwork()

Usage

Request the device to leave the network.

Parameters

None.

Returns

None.

Examples

Code Block
languagejava
GatewayClient gw = new GatewayClient(new ConnectionInfo(ConnectionType.ZIGBEE_UART, "/dev/ttyUSB0"));
...
final Device d = gw.getDevice(id);                
d.leaveNetwork();

...