Versions Compared

Key

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

...

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

...

final Device device = gateway.getDevice(id);
final CompletableFuture<Collection<Property>> properties = device.getProperties();
            
// do some extra (asynchronous) handling on the properties, once they've been discovered.
properties.thenAccept( collection -> {
    // This code will be called from a thread managed by gateway api.
    // We should avoid blocking code in this section.
    for (Property p : collection) {
        System.out.println("name: " + p.getName() + ", value: " + p.getValue() + ", type: " + p.getTypep.toString());
    }
});

...

// one could also block on the CompletableFuture until it returns
Collection<Property> c = properties.get(30, TimeUnit.SECONDS); // wait (block) for up to 30 seconds to get the Collect of Property objects back

...

Code Block
languagejava
GatewayClient gateway = new GatewayClient(new ConnectionInfo(ConnectionType.ZIGBEE_UART, "/dev/ttyUSB0"));
...
Device device = gateway.getDevice(id);
Collection<Property> collection = device.getCachedProperties();

for (Property p : collection) {
    System.out.println("name: " + p.getNametoString()) + ", value: " + p.getValue() + ", type: " + p.getType());
}

getProperty(String propertyName)

Usage

...

;
}

getProperty(String propertyName)

Usage

Gets the Property with the associated propertyName.

...

Code Block
languagejava
GatewayClient gateway = new GatewayClient(new ConnectionInfo(ConnectionType.ZIGBEE_UART, "/dev/ttyUSB0"));
...
Device device = gateway.getDevice(id);
CompletableFuture<Property> futureProperty = device.getProperty(propertyName);

// This is an example of blocking on the future and waiting for a result.
Property property = futureProperty.get(30, TimeUnit.SECONDS);
System.out.println("name: " + property.getName() + ", value: " + property.getValuep.toString());

// One could also provide code that will be called asynchronously once the CompletableFuture has finished
futureProperty.thenAccept( p -> System.out.println("name: " + p.getNametoString() + ", value: " + p.getValue()) );

getCachedProperty(String propertyName)

...

Code Block
languagejava
GatewayClient gateway = new GatewayClient(new ConnectionInfo(ConnectionType.ZIGBEE_UART, "/dev/ttyUSB0"));
...
Device device = gateway.getDevice(id);
Optional<Property> optional = device.getCachedProperty(propertyName);
if (optional.isPresent()) {
    Property p = optional.get();
    System.out.println("name: " + p.getNametoString() + ", value: " + p.getValue());
}

getProtocolProperties()

...

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

...

Device device = gateway.getDevice(id);
CompletableFuture<Collection<Property>> properties = device.getProtocolProperties();
            
// do some extra (asynchronous) handling on the properties, once they've been discovered.
properties.thenAccept( collection -> {
    // This code will be called from a thread managed by gateway api.
    // We should avoid blocking code in this section.
    for (Property p : collection) {
        System.out.println("name: " + p.getName() + ", value: " + p.getValue() + ", type: " + p.getType())p.toString());
    }
});

...

// one could also block on the CompletableFuture until it returns
Collection<Property> c = properties.get(30, TimeUnit.SECONDS); // wait (block) for up to 30 seconds to get the Collect of Property objects back

...

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(toString) + ", value: " + p.getValue() + ", type: " + p.getType());
;
}

discoverAllProperties()

Usage

...

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());    
    }
});

...

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.getTypep.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, 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.getTypep.toString());
});

removePropertyUpdateHandler(BiConsumer<Device, Property> handler)

...