Search in sources :

Example 11 with CommandResult

use of com.zsmartsystems.zigbee.CommandResult 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)

Example 12 with CommandResult

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

the class ZclCluster method readSync.

/**
 * Read an attribute
 *
 * @param attribute the {@link ZclAttribute} to read
 * @return
 */
protected Object readSync(final ZclAttribute attribute) {
    logger.debug("readSync request: {}", attribute);
    CommandResult result;
    try {
        result = read(attribute).get();
    } catch (InterruptedException e) {
        logger.debug("readSync interrupted");
        return null;
    } catch (ExecutionException e) {
        logger.debug("readSync exception ", e);
        return null;
    }
    if (!result.isSuccess()) {
        return null;
    }
    ReadAttributesResponse response = result.getResponse();
    if (response.getRecords().get(0).getStatus() == ZclStatus.SUCCESS) {
        ReadAttributeStatusRecord attributeRecord = response.getRecords().get(0);
        return normalizer.normalizeZclData(attribute.getDataType(), attributeRecord.getAttributeValue());
    }
    return null;
}
Also used : ReadAttributesResponse(com.zsmartsystems.zigbee.zcl.clusters.general.ReadAttributesResponse) ReadAttributeStatusRecord(com.zsmartsystems.zigbee.zcl.field.ReadAttributeStatusRecord) ExecutionException(java.util.concurrent.ExecutionException) CommandResult(com.zsmartsystems.zigbee.CommandResult)

Example 13 with CommandResult

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

the class ZigBeeConsoleReportingConfigCommand method process.

@Override
public void process(ZigBeeNetworkManager networkManager, String[] args, PrintStream out) throws IllegalArgumentException, InterruptedException, ExecutionException {
    if (args.length != 4) {
        throw new IllegalArgumentException("Invalid number of arguments");
    }
    final ZigBeeEndpoint endpoint = getEndpoint(networkManager, args[1]);
    final int clusterId = parseCluster(args[3]);
    final ZclCluster cluster;
    final String direction = args[2].toUpperCase();
    if ("IN".equals(direction)) {
        cluster = endpoint.getInputCluster(clusterId);
    } else if ("OUT".equals(direction)) {
        cluster = endpoint.getOutputCluster(clusterId);
    } else {
        throw new IllegalArgumentException("Cluster direction must be IN or OUT");
    }
    final int attributeId = parseAttribute(args[4]);
    final ZclAttribute attribute = cluster.getAttribute(attributeId);
    final CommandResult result = cluster.getReporting(attribute).get();
    if (result.isSuccess()) {
        final ReadAttributesResponse response = result.getResponse();
        final ZclStatus statusCode = response.getRecords().get(0).getStatus();
        if (statusCode == ZclStatus.SUCCESS) {
            out.println("Cluster " + response.getClusterId() + ", Attribute " + response.getRecords().get(0).getAttributeIdentifier() + ", type " + response.getRecords().get(0).getAttributeDataType() + ", value: " + response.getRecords().get(0).getAttributeValue());
        } else {
            out.println("Attribute value read error: " + statusCode);
        }
    } else {
        out.println("Error executing command: " + result);
    }
}
Also used : ZclStatus(com.zsmartsystems.zigbee.zcl.ZclStatus) ZclAttribute(com.zsmartsystems.zigbee.zcl.ZclAttribute) ReadAttributesResponse(com.zsmartsystems.zigbee.zcl.clusters.general.ReadAttributesResponse) ZclCluster(com.zsmartsystems.zigbee.zcl.ZclCluster) ZigBeeEndpoint(com.zsmartsystems.zigbee.ZigBeeEndpoint) ZigBeeEndpoint(com.zsmartsystems.zigbee.ZigBeeEndpoint) CommandResult(com.zsmartsystems.zigbee.CommandResult)

Example 14 with CommandResult

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

the class ZigBeeConsoleReportingSubscribeCommand method process.

@Override
public void process(ZigBeeNetworkManager networkManager, String[] args, PrintStream out) throws IllegalArgumentException, InterruptedException, ExecutionException {
    if (args.length < 7) {
        throw new IllegalArgumentException("Invalid number of arguments");
    }
    final ZigBeeEndpoint endpoint = getEndpoint(networkManager, args[1]);
    final int clusterId = parseCluster(args[3]);
    final ZclCluster cluster;
    final String direction = args[2].toUpperCase();
    if ("IN".equals(direction)) {
        cluster = endpoint.getInputCluster(clusterId);
    } else if ("OUT".equals(direction)) {
        cluster = endpoint.getOutputCluster(clusterId);
    } else {
        throw new IllegalArgumentException("Cluster direction must be IN or OUT");
    }
    final int minInterval;
    try {
        minInterval = Integer.parseInt(args[5]);
    } catch (final NumberFormatException e) {
        throw new IllegalArgumentException("Min Interval has invalid format");
    }
    final int maxInterval;
    try {
        maxInterval = Integer.parseInt(args[6]);
    } catch (final NumberFormatException e) {
        throw new IllegalArgumentException("Max Interval has invalid format");
    }
    final int attributeId = parseAttribute(args[4]);
    final ZclAttribute attribute = cluster.getAttribute(attributeId);
    final Object reportableChange;
    if (args.length > 6) {
        reportableChange = parseValue(args[7], attribute.getDataType());
    } else {
        reportableChange = null;
    }
    final CommandResult result = cluster.setReporting(attribute, minInterval, maxInterval, reportableChange).get();
    if (result.isSuccess()) {
        final ConfigureReportingResponse response = result.getResponse();
        final ZclStatus statusCode = response.getRecords().get(0).getStatus();
        if (statusCode == ZclStatus.SUCCESS) {
            out.println("Attribute value configure reporting success.");
        } else {
            out.println("Attribute value configure reporting error: " + statusCode);
        }
    } else {
        out.println("Error executing command: " + result);
    }
}
Also used : ZclStatus(com.zsmartsystems.zigbee.zcl.ZclStatus) ZclAttribute(com.zsmartsystems.zigbee.zcl.ZclAttribute) ZclCluster(com.zsmartsystems.zigbee.zcl.ZclCluster) ZigBeeEndpoint(com.zsmartsystems.zigbee.ZigBeeEndpoint) ZigBeeEndpoint(com.zsmartsystems.zigbee.ZigBeeEndpoint) CommandResult(com.zsmartsystems.zigbee.CommandResult) ConfigureReportingResponse(com.zsmartsystems.zigbee.zcl.clusters.general.ConfigureReportingResponse)

Example 15 with CommandResult

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

the class ZigBeeConsoleAttributeWriteCommand method process.

@Override
public void process(ZigBeeNetworkManager networkManager, String[] args, PrintStream out) throws IllegalArgumentException, InterruptedException, ExecutionException {
    if (args.length != 5) {
        throw new IllegalArgumentException("Invalid number of arguments");
    }
    final ZigBeeEndpoint endpoint = getEndpoint(networkManager, args[1]);
    final int clusterId = parseCluster(args[3]);
    final int attributeId = parseAttribute(args[4]);
    final ZclCluster cluster;
    final String direction = args[2].toUpperCase();
    if ("IN".equals(direction)) {
        cluster = endpoint.getInputCluster(clusterId);
    } else if ("OUT".equals(direction)) {
        cluster = endpoint.getOutputCluster(clusterId);
    } else {
        throw new IllegalArgumentException("Cluster direction must be IN or OUT");
    }
    final ZclAttribute attribute = cluster.getAttribute(attributeId);
    final Object value = parseValue(args[4], attribute.getDataType());
    final CommandResult result = cluster.write(attribute, value).get();
    if (result.isSuccess()) {
        final WriteAttributesResponse response = result.getResponse();
        final int statusCode = response.getRecords().get(0).getStatus();
        if (statusCode == 0) {
            out.println("Attribute value write success.");
        } else {
            final ZclStatus status = ZclStatus.getStatus((byte) statusCode);
            out.println("Attribute value write error: " + status);
        }
    } else {
        out.println("Error executing command: " + result);
    }
}
Also used : ZclStatus(com.zsmartsystems.zigbee.zcl.ZclStatus) ZclAttribute(com.zsmartsystems.zigbee.zcl.ZclAttribute) WriteAttributesResponse(com.zsmartsystems.zigbee.zcl.clusters.general.WriteAttributesResponse) ZclCluster(com.zsmartsystems.zigbee.zcl.ZclCluster) ZigBeeEndpoint(com.zsmartsystems.zigbee.ZigBeeEndpoint) ZigBeeEndpoint(com.zsmartsystems.zigbee.ZigBeeEndpoint) CommandResult(com.zsmartsystems.zigbee.CommandResult)

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