Versions Compared

Key

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

...

Code Block
languagejava
titleSampleDeviceEventHandler
linenumberstrue
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 production 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();
            
            // 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 GAPI.
                // We should avoid writing blocking code in this section.
                for (Property p : collection) {
                    printDiscoveredProperty(pSystem.out.println("Discovered: " + p.toString());
                }
            });

            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())     break;
        System.out.println(sb.toString());}
    }
}


Once constructed, the GatewayClient will automatically connect to the device, configure it as a Combined Interface, and form a network. If it can't connect to or configure the device, it will throw a GatewayConnectionException.

...

Code Block
languagejava
titleGetting a Property
linenumberstrue
final Property property = device.getProperty("onOff").getFuture().get(5, TimeUnit.SECONDS);
System.out.println("name: " + p.getName() + ", value: " + p.getValuep.toString());

Updating Properties

You can update a Device Property by providing the name and value.

...

Code Block
languagejava
titleSamplePropertyUpdateHandler
linenumberstrue
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.toString());
    }
}

...