Search in sources :

Example 16 with CommandResult

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

the class ZigBeeNetworkDiscoverer method getIeeeAddress.

/**
 * Get Node IEEE address
 *
 * @param networkAddress the network address of the node
 * @return true if the message was processed ok
 */
private boolean getIeeeAddress(final int networkAddress) {
    try {
        Integer startIndex = 0;
        int totalAssociatedDevices = 0;
        Set<Integer> associatedDevices = new HashSet<Integer>();
        IeeeAddress ieeeAddress = null;
        do {
            // Request extended response, start index for associated list is 0
            final IeeeAddressRequest ieeeAddressRequest = new IeeeAddressRequest();
            ieeeAddressRequest.setDestinationAddress(new ZigBeeEndpointAddress(networkAddress));
            ieeeAddressRequest.setRequestType(1);
            ieeeAddressRequest.setStartIndex(startIndex);
            ieeeAddressRequest.setNwkAddrOfInterest(networkAddress);
            CommandResult response = networkManager.unicast(ieeeAddressRequest, ieeeAddressRequest).get();
            if (response.isError()) {
                return false;
            }
            final IeeeAddressResponse ieeeAddressResponse = response.getResponse();
            logger.debug("{}: NWK Discovery IeeeAddressRequest returned {}", networkAddress, ieeeAddressResponse);
            if (ieeeAddressResponse != null && ieeeAddressResponse.getStatus() == ZdoStatus.SUCCESS) {
                ieeeAddress = ieeeAddressResponse.getIeeeAddrRemoteDev();
                if (startIndex.equals(ieeeAddressResponse.getStartIndex())) {
                    associatedDevices.addAll(ieeeAddressResponse.getNwkAddrAssocDevList());
                    startIndex += ieeeAddressResponse.getNwkAddrAssocDevList().size();
                    totalAssociatedDevices = ieeeAddressResponse.getNwkAddrAssocDevList().size();
                }
            }
        } while (startIndex < totalAssociatedDevices);
        addNode(ieeeAddress, networkAddress);
        // Start discovery for any associated nodes
        for (final int deviceNetworkAddress : associatedDevices) {
            startNodeDiscovery(deviceNetworkAddress);
        }
    } catch (InterruptedException | ExecutionException e) {
        logger.debug("NWK Discovery Error in checkIeeeAddressResponse ", e);
    }
    return true;
}
Also used : IeeeAddressResponse(com.zsmartsystems.zigbee.zdo.command.IeeeAddressResponse) ZigBeeEndpointAddress(com.zsmartsystems.zigbee.ZigBeeEndpointAddress) IeeeAddressRequest(com.zsmartsystems.zigbee.zdo.command.IeeeAddressRequest) ExecutionException(java.util.concurrent.ExecutionException) HashSet(java.util.HashSet) IeeeAddress(com.zsmartsystems.zigbee.IeeeAddress) CommandResult(com.zsmartsystems.zigbee.CommandResult)

Example 17 with CommandResult

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

the class ZigBeeNetworkDiscoverer method rediscoverNode.

/**
 * Starts a discovery on a node. This will send a {@link NetworkAddressRequest} as a broadcast and will receive
 * the response to trigger a full discovery.
 *
 * @param ieeeAddress the {@link IeeeAddress} of the node to discover
 */
public void rediscoverNode(final IeeeAddress ieeeAddress) {
    Runnable runnable = new Runnable() {

        @Override
        public void run() {
            logger.debug("{}: NWK Discovery starting node rediscovery", ieeeAddress);
            int retries = 0;
            try {
                do {
                    if (Thread.currentThread().isInterrupted()) {
                        break;
                    }
                    NetworkAddressRequest request = new NetworkAddressRequest();
                    request.setIeeeAddr(ieeeAddress);
                    request.setRequestType(0);
                    request.setStartIndex(0);
                    request.setDestinationAddress(new ZigBeeEndpointAddress(ZigBeeBroadcastDestination.BROADCAST_RX_ON.getKey()));
                    CommandResult response;
                    response = networkManager.unicast(request, request).get();
                    final NetworkAddressResponse nwkAddressResponse = response.getResponse();
                    if (nwkAddressResponse != null && nwkAddressResponse.getStatus() == ZdoStatus.SUCCESS) {
                        startNodeDiscovery(nwkAddressResponse.getNwkAddrRemoteDev());
                        break;
                    }
                    // We failed with the last request. Wait a bit then retry
                    try {
                        logger.debug("{}: NWK Discovery node rediscovery request failed. Wait before retry.", ieeeAddress);
                        Thread.sleep(retryPeriod);
                    } catch (InterruptedException e) {
                        break;
                    }
                } while (retries++ < retryCount);
            } catch (InterruptedException | ExecutionException e) {
                logger.debug("NWK Discovery error in rediscoverNode ", e);
            }
            logger.debug("{}: NWK Discovery finishing node rediscovery", ieeeAddress);
        }
    };
    networkManager.executeTask(runnable);
}
Also used : NetworkAddressRequest(com.zsmartsystems.zigbee.zdo.command.NetworkAddressRequest) ZigBeeEndpointAddress(com.zsmartsystems.zigbee.ZigBeeEndpointAddress) ExecutionException(java.util.concurrent.ExecutionException) CommandResult(com.zsmartsystems.zigbee.CommandResult) NetworkAddressResponse(com.zsmartsystems.zigbee.zdo.command.NetworkAddressResponse)

Example 18 with CommandResult

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

the class ZigBeeNodeServiceDiscoverer method requestNeighborTable.

/**
 * Get node neighbor table by making a {@link ManagementLqiRequest} call.
 *
 * @return list of {@link NeighborTable} if the request was processed ok, null otherwise
 * @throws ExecutionException
 * @throws InterruptedException
 */
private boolean requestNeighborTable() throws InterruptedException, ExecutionException {
    // Start index for the list is 0
    int startIndex = 0;
    int totalNeighbors = 0;
    Set<NeighborTable> neighbors = new HashSet<NeighborTable>();
    do {
        final ManagementLqiRequest neighborRequest = new ManagementLqiRequest();
        neighborRequest.setDestinationAddress(new ZigBeeEndpointAddress(node.getNetworkAddress()));
        neighborRequest.setStartIndex(startIndex);
        CommandResult response = networkManager.unicast(neighborRequest, neighborRequest).get();
        final ManagementLqiResponse neighborResponse = response.getResponse();
        logger.debug("{}: Node SVC Discovery ManagementLqiRequest response {}", node.getIeeeAddress(), response);
        if (neighborResponse == null) {
            return false;
        }
        if (neighborResponse.getStatus() == ZdoStatus.NOT_SUPPORTED) {
            supportsManagementLqi = false;
            return true;
        } else if (neighborResponse.getStatus() != ZdoStatus.SUCCESS) {
            return false;
        }
        // To avoid a loop, we need to check if there's any response.
        if (neighborResponse.getNeighborTableList().size() == 0) {
            break;
        }
        // Save the neighbors
        neighbors.addAll(neighborResponse.getNeighborTableList());
        // Continue with next request
        startIndex += neighborResponse.getNeighborTableList().size();
        totalNeighbors = neighborResponse.getNeighborTableEntries();
    } while (startIndex < totalNeighbors);
    node.setNeighbors(neighbors);
    return true;
}
Also used : ManagementLqiResponse(com.zsmartsystems.zigbee.zdo.command.ManagementLqiResponse) ZigBeeEndpointAddress(com.zsmartsystems.zigbee.ZigBeeEndpointAddress) NeighborTable(com.zsmartsystems.zigbee.zdo.field.NeighborTable) ManagementLqiRequest(com.zsmartsystems.zigbee.zdo.command.ManagementLqiRequest) ZigBeeEndpoint(com.zsmartsystems.zigbee.ZigBeeEndpoint) HashSet(java.util.HashSet) CommandResult(com.zsmartsystems.zigbee.CommandResult)

Example 19 with CommandResult

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

the class ZigBeeNodeServiceDiscoverer method requestPowerDescriptor.

/**
 * Get node power descriptor
 *
 * @return true if the message was processed ok, or if the end device does not support the power descriptor
 * @throws ExecutionException
 * @throws InterruptedException
 */
private boolean requestPowerDescriptor() throws InterruptedException, ExecutionException {
    final PowerDescriptorRequest powerDescriptorRequest = new PowerDescriptorRequest();
    powerDescriptorRequest.setDestinationAddress(new ZigBeeEndpointAddress(node.getNetworkAddress()));
    powerDescriptorRequest.setNwkAddrOfInterest(node.getNetworkAddress());
    CommandResult response = networkManager.unicast(powerDescriptorRequest, powerDescriptorRequest).get();
    final PowerDescriptorResponse powerDescriptorResponse = (PowerDescriptorResponse) response.getResponse();
    logger.debug("{}: Node SVC Discovery PowerDescriptorResponse returned {}", node.getIeeeAddress(), powerDescriptorResponse);
    if (powerDescriptorResponse == null) {
        return false;
    }
    if (powerDescriptorResponse.getStatus() == ZdoStatus.SUCCESS) {
        node.setPowerDescriptor(powerDescriptorResponse.getPowerDescriptor());
        return true;
    } else if (powerDescriptorResponse.getStatus() == ZdoStatus.NOT_SUPPORTED) {
        return true;
    }
    return false;
}
Also used : PowerDescriptorRequest(com.zsmartsystems.zigbee.zdo.command.PowerDescriptorRequest) ZigBeeEndpointAddress(com.zsmartsystems.zigbee.ZigBeeEndpointAddress) PowerDescriptorResponse(com.zsmartsystems.zigbee.zdo.command.PowerDescriptorResponse) CommandResult(com.zsmartsystems.zigbee.CommandResult)

Example 20 with CommandResult

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

the class ZigBeeNodeServiceDiscoverer method getSimpleDescriptor.

/**
 * Get the simple descriptor for an endpoint and create the {@link ZigBeeEndpoint}
 *
 * @param endpointId the endpoint id to request
 * @return the newly created {@link ZigBeeEndpoint} for the endpoint, or null on error
 * @throws ExecutionException
 * @throws InterruptedException
 */
private ZigBeeEndpoint getSimpleDescriptor(final int endpointId) throws InterruptedException, ExecutionException {
    final SimpleDescriptorRequest simpleDescriptorRequest = new SimpleDescriptorRequest();
    simpleDescriptorRequest.setDestinationAddress(new ZigBeeEndpointAddress(node.getNetworkAddress()));
    simpleDescriptorRequest.setNwkAddrOfInterest(node.getNetworkAddress());
    simpleDescriptorRequest.setEndpoint(endpointId);
    CommandResult response = networkManager.unicast(simpleDescriptorRequest, simpleDescriptorRequest).get();
    final SimpleDescriptorResponse simpleDescriptorResponse = (SimpleDescriptorResponse) response.getResponse();
    logger.debug("{}: Node SVC Discovery SimpleDescriptorResponse returned {}", node.getIeeeAddress(), simpleDescriptorResponse);
    if (simpleDescriptorResponse == null) {
        return null;
    }
    if (simpleDescriptorResponse.getStatus() == ZdoStatus.SUCCESS) {
        ZigBeeEndpoint endpoint = new ZigBeeEndpoint(networkManager, node, endpointId);
        SimpleDescriptor simpleDescriptor = simpleDescriptorResponse.getSimpleDescriptor();
        endpoint.setProfileId(simpleDescriptor.getProfileId());
        endpoint.setDeviceId(simpleDescriptor.getDeviceId());
        endpoint.setDeviceVersion(simpleDescriptor.getDeviceVersion());
        endpoint.setInputClusterIds(simpleDescriptor.getInputClusterList());
        endpoint.setOutputClusterIds(simpleDescriptor.getOutputClusterList());
        return endpoint;
    }
    return null;
}
Also used : SimpleDescriptorResponse(com.zsmartsystems.zigbee.zdo.command.SimpleDescriptorResponse) ZigBeeEndpointAddress(com.zsmartsystems.zigbee.ZigBeeEndpointAddress) SimpleDescriptorRequest(com.zsmartsystems.zigbee.zdo.command.SimpleDescriptorRequest) ZigBeeEndpoint(com.zsmartsystems.zigbee.ZigBeeEndpoint) CommandResult(com.zsmartsystems.zigbee.CommandResult) SimpleDescriptor(com.zsmartsystems.zigbee.zdo.field.SimpleDescriptor)

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