...
Code Block | ||||||
---|---|---|---|---|---|---|
| ||||||
public class SampleDeviceEventHandler implements DeviceEventHandler { private final SamplePropertyUpdateHandler propertyHandler; public SampleDeviceEventHandler() { this.propertyHandler = new SamplePropertyUpdateHandler(); } @Override public void onDeviceEvent(DeviceEvent event) { // This method gets called when a device is added to, or removed from, the network. // 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 PropertyUpdateHandlers, or ProtocolHandlers) may be blocked. final DeviceEventStatus eventStatus = event.getStatus(); final Device device = event.getDevice(); final String deviceId = device.getID(); final String deviceType = device.getDeviceType().getType(); System.out.println("Device Event: " + eventStatus); System.out.println("Device Id: " + deviceId); System.out.println("Device Type: " + deviceType); switch (eventStatus) { case DEVICE_ADDED: // Register a property handler, to be notified when a Property on the Device changes. device.addPropertyUpdateHandler(propertyHandler); // Automatically discover the device. This is a non-blocking call. // Note: this can be a long-running network operation, and in a CompletableFuture<Collection<Property>>production properties = device.getProperties(); // Do some extra (asynchronous) handling on the properties, once they've been discovered. application, // you would typically replace this with individual calls to "getProperty(String)" // for only those properties that you are interested in, given the deviceType of the Device. CompletableFuture<Collection<Property>> properties = device.discoverAllProperties(); properties.thenAccept( collection -> { // Do some extra // This code will be called from a thread managed by GAPI.(asynchronous) handling on the properties, once they've been discovered. properties.thenAccept( collection -> { // We should avoid writing blocking code in this section. // This code will be called from a thread managed by GAPI. // We should avoid writing blocking code in this section. for (Property p : collection) { printDiscoveredProperty(p); } }); break; case DEVICE_REMOVED: device.removePropertyUpdateHandler(propertyHandler); break; default: break; } } private static void printDiscoveredProperty(Property p) { StringBuilder sb = new StringBuilder(); sb.append("Discovered Property: ").append(p.getName()) .append(", with value: ").append(p.getValue()) .append(", of type: ").append(p.getType()); System.out.println(sb.toString()); } } |
...
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 deviceWhen a device first joins the network, the Device class will only know some minimal information about the device (such as its device type), but it will not know which Properties exist on it.
In order to discover which Properties exist on the device, you will have to either call 1) getProperty(String) with the property name(s) of interest, or 2) call discoverAllProperties().
Once a Property is read for the first time, it will be added to a list of known Properties for the Device, which can always be accessed by calling "getCachedProperties()". Any calls to "getProperties()" will refresh the values of all known properties on the remote device.
The API has been designed so that users can choose what level of device discovery they want to perform (for example, based on the device type), and to optimize calls going out over the network. For example, some device types are sleepy battery powered devices, and may have 50+ attributes. In these cases, users would not want to call discoverAllProperties(), because it would kick-off a very long-running network operation. The GatewayClient has a small command queue and can only perform a limited number of network operations in parallel; therefore, one would not want to fill the queue with long-running network operations. In these cases, users should selectively call getProperty(String) on a few properties they want to discover.
discoverAllProperties()
You can use this method to request all the properties, and their values, from the remote device. Note the caveats listed above.
For example, to automatically discover the properties of a device the first time the GatewayClient becomes aware of it, you can run something like the following code from the DeviceEventHandler.onDeviceEvent callback:
The first time When 'getPropertiesdiscoverAllProperties' is called, the system will perform a discovery of the remote device's properties over the network. This may take some time; however, once properties have been discovered, users have the option of calling 'getCachedProperties()' to get the last known (cached) values immediately. Cached values are updated on writes, reads, and attribute reports. For more details, see the API documentation for getProperties().
Code Block | ||||||
---|---|---|---|---|---|---|
| ||||||
// Automatically discover the device. This is a non-blocking call. CompletableFuture<Collection<Property>> properties = device.getPropertiesdiscoverAllProperties(); |
getProperty(String)
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 | ||||||
---|---|---|---|---|---|---|
| ||||||
public class SamplePropertyUpdateHandler 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 GAPI. // 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.getTypeproperty.toString()); } } |
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.
...