Search in sources :

Example 1 with DiscoverCommandsReceivedResponse

use of com.zsmartsystems.zigbee.zcl.clusters.general.DiscoverCommandsReceivedResponse in project com.zsmartsystems.zigbee by zsmartsystems.

the class ZclGeneralCluster method discoverCommandsReceivedResponse.

/**
 * The Discover Commands Received Response
 * <p>
 * The Discover Commands Received Response is generated in response to a Discover Commands Received
 * command.
 *
 * @param discoveryComplete {@link Boolean} Discovery complete
 * @param commandIdentifiers {@link List<Integer>} Command identifiers
 * @return the {@link Future<CommandResult>} command result future
 */
public Future<CommandResult> discoverCommandsReceivedResponse(Boolean discoveryComplete, List<Integer> commandIdentifiers) {
    DiscoverCommandsReceivedResponse command = new DiscoverCommandsReceivedResponse();
    // Set the fields
    command.setDiscoveryComplete(discoveryComplete);
    command.setCommandIdentifiers(commandIdentifiers);
    return send(command);
}
Also used : DiscoverCommandsReceivedResponse(com.zsmartsystems.zigbee.zcl.clusters.general.DiscoverCommandsReceivedResponse)

Example 2 with DiscoverCommandsReceivedResponse

use of com.zsmartsystems.zigbee.zcl.clusters.general.DiscoverCommandsReceivedResponse in project com.zsmartsystems.zigbee by zsmartsystems.

the class ZclCluster method discoverCommandsReceived.

/**
 * Discovers the list of commands received by the cluster on the remote device. If the discovery is successful,
 * users should call {@link ZclCluster#getSupportedCommandsReceived()} to get the list of supported commands.
 * <p>
 * If the discovery has already been completed, and rediscover is false, then the future will complete immediately
 * and the user can use existing results. Normally there should not be a need to set rediscover to true.
 *
 * @param rediscover true to perform a discovery even if it was previously completed
 * @return Command future {@link Boolean} with the success of the discovery
 */
public Future<Boolean> discoverCommandsReceived(final boolean rediscover) {
    RunnableFuture<Boolean> future = new FutureTask<Boolean>(new Callable<Boolean>() {

        @Override
        public Boolean call() throws Exception {
            // cluster which would cause errors consolidating the responses
            synchronized (supportedCommandsReceived) {
                // If we don't want to rediscover, and we already have the list of attributes, then return
                if (!rediscover && !supportedCommandsReceived.isEmpty()) {
                    return true;
                }
                int index = 0;
                boolean complete = false;
                Set<Integer> commands = new HashSet<Integer>();
                do {
                    final DiscoverCommandsReceived command = new DiscoverCommandsReceived();
                    command.setClusterId(clusterId);
                    command.setDestinationAddress(zigbeeEndpoint.getEndpointAddress());
                    command.setStartCommandIdentifier(index);
                    command.setMaximumCommandIdentifiers(20);
                    CommandResult result = send(command).get();
                    if (result.isError()) {
                        return false;
                    }
                    DiscoverCommandsReceivedResponse response = (DiscoverCommandsReceivedResponse) result.getResponse();
                    complete = response.getDiscoveryComplete();
                    if (response.getCommandIdentifiers() != null) {
                        commands.addAll(response.getCommandIdentifiers());
                        index = Collections.max(commands) + 1;
                    }
                } while (!complete);
                supportedCommandsReceived.clear();
                supportedCommandsReceived.addAll(commands);
            }
            return true;
        }
    });
    // start the thread to execute it
    new Thread(future).start();
    return future;
}
Also used : TreeSet(java.util.TreeSet) HashSet(java.util.HashSet) Set(java.util.Set) CopyOnWriteArraySet(java.util.concurrent.CopyOnWriteArraySet) DiscoverCommandsReceived(com.zsmartsystems.zigbee.zcl.clusters.general.DiscoverCommandsReceived) ExecutionException(java.util.concurrent.ExecutionException) DiscoverCommandsReceivedResponse(com.zsmartsystems.zigbee.zcl.clusters.general.DiscoverCommandsReceivedResponse) CommandResult(com.zsmartsystems.zigbee.CommandResult) FutureTask(java.util.concurrent.FutureTask)

Aggregations

DiscoverCommandsReceivedResponse (com.zsmartsystems.zigbee.zcl.clusters.general.DiscoverCommandsReceivedResponse)2 CommandResult (com.zsmartsystems.zigbee.CommandResult)1 DiscoverCommandsReceived (com.zsmartsystems.zigbee.zcl.clusters.general.DiscoverCommandsReceived)1 HashSet (java.util.HashSet)1 Set (java.util.Set)1 TreeSet (java.util.TreeSet)1 CopyOnWriteArraySet (java.util.concurrent.CopyOnWriteArraySet)1 ExecutionException (java.util.concurrent.ExecutionException)1 FutureTask (java.util.concurrent.FutureTask)1