...
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 DeviceEventListener callback:
...
Listening for Device Property Updates
You can be notified when a Property on a Device changes by registering a "Property Update Listener". In Java, this can be any object that implements the "Consumer" interface, or a lambda.
For example, to automatically register an update listener the first time the GatewayClient becomes aware of a device, you can run something like the following code from the DeviceEventListener callback:
Code Block | ||||||
---|---|---|---|---|---|---|
| ||||||
if (event.getStatus() == DeviceEventStatus.ADD) { final Device d = event.getDevice(); final String deviceId = d.getID(); d.addPropertyUpdateListener(p -> { System.out.println("Property update triggered for device: " + deviceId); System.out.println("Property: " + p.getName() + ", value: " + p.getValue() + ", type: " + p.getType()); }); } |
The above example will simply print out the Device ID, as well as the Property name, value, and type, whenever the Property changes.
Conclusion
This programming guide has shown how to initialize a GatewayClient, join devices, get their properties, and interact with them in a simple way.
...