Search in sources :

Example 21 with CommandResult

use of com.zsmartsystems.zigbee.CommandResult in project com.zsmartsystems.zigbee by zsmartsystems.

the class ZclCluster method discoverAttributes.

/**
 * Discovers the list of attributes supported by the cluster on the remote device.
 * <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.
 * <p>
 * This method returns a future to a boolean. Upon success the caller should call {@link #getSupportedAttributes()}
 * to get the list of supported attributes.
 *
 * @param rediscover true to perform a discovery even if it was previously completed
 * @return {@link Future} returning a {@link Boolean}
 */
public Future<Boolean> discoverAttributes(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 (supportedAttributes) {
                // If we don't want to rediscover, and we already have the list of attributes, then return
                if (!rediscover && !supportedAttributes.isEmpty()) {
                    return true;
                }
                int index = 0;
                boolean complete = false;
                Set<AttributeInformation> attributes = new HashSet<AttributeInformation>();
                do {
                    final DiscoverAttributesCommand command = new DiscoverAttributesCommand();
                    command.setClusterId(clusterId);
                    command.setDestinationAddress(zigbeeEndpoint.getEndpointAddress());
                    command.setStartAttributeIdentifier(index);
                    command.setMaximumAttributeIdentifiers(10);
                    CommandResult result = send(command).get();
                    if (result.isError()) {
                        return false;
                    }
                    DiscoverAttributesResponse response = (DiscoverAttributesResponse) result.getResponse();
                    complete = response.getDiscoveryComplete();
                    if (response.getAttributeInformation() != null) {
                        attributes.addAll(response.getAttributeInformation());
                        index = Collections.max(attributes).getIdentifier() + 1;
                    }
                } while (!complete);
                supportedAttributes.clear();
                for (AttributeInformation attribute : attributes) {
                    supportedAttributes.add(attribute.getIdentifier());
                }
            }
            return true;
        }
    });
    // start the thread to execute it
    new Thread(future).start();
    return future;
}
Also used : AttributeInformation(com.zsmartsystems.zigbee.zcl.field.AttributeInformation) TreeSet(java.util.TreeSet) HashSet(java.util.HashSet) Set(java.util.Set) CopyOnWriteArraySet(java.util.concurrent.CopyOnWriteArraySet) DiscoverAttributesCommand(com.zsmartsystems.zigbee.zcl.clusters.general.DiscoverAttributesCommand) DiscoverAttributesResponse(com.zsmartsystems.zigbee.zcl.clusters.general.DiscoverAttributesResponse) ExecutionException(java.util.concurrent.ExecutionException) CommandResult(com.zsmartsystems.zigbee.CommandResult) FutureTask(java.util.concurrent.FutureTask)

Example 22 with CommandResult

use of com.zsmartsystems.zigbee.CommandResult in project com.zsmartsystems.zigbee by zsmartsystems.

the class ZclCluster method discoverCommandsGenerated.

/**
 * Discovers the list of commands generated by the cluster on the remote device If the discovery is successful,
 * users should call {@link ZclCluster#getSupportedCommandsGenerated()} 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> discoverCommandsGenerated(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 (supportedCommandsGenerated) {
                // If we don't want to rediscover, and we already have the list of attributes, then return
                if (!rediscover && !supportedCommandsGenerated.isEmpty()) {
                    return true;
                }
                int index = 0;
                boolean complete = false;
                Set<Integer> commands = new HashSet<Integer>();
                do {
                    final DiscoverCommandsGenerated command = new DiscoverCommandsGenerated();
                    command.setClusterId(clusterId);
                    command.setDestinationAddress(zigbeeEndpoint.getEndpointAddress());
                    command.setStartCommandIdentifier(index);
                    command.setMaximumCommandIdentifiers(20);
                    CommandResult result = send(command).get();
                    if (result.isError()) {
                        return false;
                    }
                    DiscoverCommandsGeneratedResponse response = (DiscoverCommandsGeneratedResponse) result.getResponse();
                    complete = response.getDiscoveryComplete();
                    if (response.getCommandIdentifiers() != null) {
                        commands.addAll(response.getCommandIdentifiers());
                        index = Collections.max(commands) + 1;
                    }
                } while (!complete);
                supportedCommandsGenerated.clear();
                supportedCommandsGenerated.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) DiscoverCommandsGenerated(com.zsmartsystems.zigbee.zcl.clusters.general.DiscoverCommandsGenerated) ExecutionException(java.util.concurrent.ExecutionException) CommandResult(com.zsmartsystems.zigbee.CommandResult) FutureTask(java.util.concurrent.FutureTask) DiscoverCommandsGeneratedResponse(com.zsmartsystems.zigbee.zcl.clusters.general.DiscoverCommandsGeneratedResponse)

Aggregations

CommandResult (com.zsmartsystems.zigbee.CommandResult)22 ZigBeeEndpoint (com.zsmartsystems.zigbee.ZigBeeEndpoint)12 ZigBeeEndpointAddress (com.zsmartsystems.zigbee.ZigBeeEndpointAddress)10 ZclCluster (com.zsmartsystems.zigbee.zcl.ZclCluster)7 HashSet (java.util.HashSet)7 ExecutionException (java.util.concurrent.ExecutionException)6 ZclAttribute (com.zsmartsystems.zigbee.zcl.ZclAttribute)5 ZclStatus (com.zsmartsystems.zigbee.zcl.ZclStatus)5 IeeeAddress (com.zsmartsystems.zigbee.IeeeAddress)3 ReadAttributesResponse (com.zsmartsystems.zigbee.zcl.clusters.general.ReadAttributesResponse)3 Set (java.util.Set)3 TreeSet (java.util.TreeSet)3 CopyOnWriteArraySet (java.util.concurrent.CopyOnWriteArraySet)3 FutureTask (java.util.concurrent.FutureTask)3 ConfigureReportingResponse (com.zsmartsystems.zigbee.zcl.clusters.general.ConfigureReportingResponse)2 IeeeAddressRequest (com.zsmartsystems.zigbee.zdo.command.IeeeAddressRequest)2 IeeeAddressResponse (com.zsmartsystems.zigbee.zdo.command.IeeeAddressResponse)2 NetworkAddressRequest (com.zsmartsystems.zigbee.zdo.command.NetworkAddressRequest)2 NetworkAddressResponse (com.zsmartsystems.zigbee.zdo.command.NetworkAddressResponse)2 CommandResultFuture (com.zsmartsystems.zigbee.CommandResultFuture)1