Table of Contents | ||
---|---|---|
|
Overview
This page describes how to develop against the Gateway API. It goes over the basics of initializing a GatewayClient object, and interacting with Device references.
Initializing GatewayClient
Initialize a GatewayClient by passing in a ConnectionInfo object, which contains details about what the connection type is (i.e. a zigbee uart port) and a string describing where to connect to it.
...
It will call the SampleDeviceEventHandler.onDeviceEvent(DeviceEvent event) method whenever a Device is detected on the network (i.e. added to the network), or whenever a device leaves the network.
Scanning for Devices
To join a device to the network, you first have to open the permit join window on the GatewayClient.
...
Note: You can close the permit join window by calling "scanForDevices" with 0 as the "duration" parameter.
Interacting with Devices
When a device joins the network, you should see a DeviceEvent. For example, the SampleDeviceEventHandler we passed into GatewayClient should print something like the following:
Code Block |
---|
Device Event: DEVICE_ADDED Device ID: 00244600000f1472_1 |
Getting Properties
getProperties()
You can use the Device API to request properties of the connected device.
...
Code Block | ||||||
---|---|---|---|---|---|---|
| ||||||
// Automatically discover the device. This is a non-blocking call. CompletableFuture<Collection<Property>> properties = device.getProperties(); |
getProperty()
You can also get specific properties using the getProperty() method. For example, to get the "OnOff" property from a LightDevice, you could do something like the following:
Code Block | ||||||
---|---|---|---|---|---|---|
| ||||||
final Property property = device.getProperty("onOff").getFuture().get(5, TimeUnit.SECONDS); System.out.println("name: " + p.getName() + ", value: " + p.getValue()); |
Updating Properties
You can update a Device Property by providing the name and value.
...
Code Block | ||||||
---|---|---|---|---|---|---|
| ||||||
Property property = new Property("onOff, "boolean", "true"); gateway.getDevice(id).updateProperty(property); |
LightDevice
The Gateway API will return a specific class for certain devices. Specific device classes, such as LightDevice, will have their own convenience methods that allow you to skip using the getProperty/updateProperty API's.
...
See the LightDevice API for more info.
ThermostatDevice
The Gateway API will return a specific class for certain devices. Specific device classes, such as ThermostatDevice, will have their own convenience methods that allow you to skip using the getProperty/updateProperty API's.
...
See the ThermostatDevice API for more info.
Listening for Device Property Updates
You can be notified when a Property on a Device changes by registering a "Property Update Handler". In Java, this can be any object that implements the "Consumer" interface, or a lambda.
...
The above example will simply print out the Device Id, as well as the Property name, value, and type, whenever the Property on that Device changes.
Enable Default Reporting
Certain Device types will have the enableDefaultReporting() method implemented, which will configure the Device to report changes to its properties.
...
Code Block | ||||||
---|---|---|---|---|---|---|
| ||||||
final Device device = gateway.getDevice(idStr); final String result = device.enableDefaultReporting().get(30, TimeUnit.SECONDS); System.out.println("Status for command: " + result); |
Shutdown
To perform a graceful shut down of the GatewayClient, allowing for all subsystems to save any critical data and release system resources (such as file handles, open ports, user threads), you must call the shutdown() method. Since the GatewayClient creates user threads, not performing a shutdown may keep the JVM running even after your application has exited.
...
See the GatewayClient API for more details.
Conclusion
This programming guide has shown how to initialize a GatewayClient, join devices, get their properties, and interact with them in a simple way.
...