Search in sources :

Example 1 with SynchronousCommandListener

use of com.zsmartsystems.zigbee.dongle.cc2531.network.SynchronousCommandListener in project com.zsmartsystems.zigbee by zsmartsystems.

the class CommandInterfaceImpl method sendSynchronousCommand.

/**
 * Sends synchronous command and adds listener.
 *
 * @param packet the command packet
 * @param listener the synchronous command response listener
 * @param timeoutMillis the timeout
 * @throws IOException if IO exception occurs in packet sending
 */
@Override
public void sendSynchronousCommand(final ZToolPacket packet, final SynchronousCommandListener listener, final long timeoutMillis) throws IOException {
    if (timeoutMillis == -1L) {
        synchronousCommandListenerTimeouts.put(listener, -1L);
    } else {
        final long expirationTime = System.currentTimeMillis() + timeoutMillis;
        synchronousCommandListenerTimeouts.put(listener, expirationTime);
    }
    final DoubleByte cmdId = packet.getCMD();
    final int value = (cmdId.getMsb() & 0xE0);
    if (value != 0x20) {
        throw new IllegalArgumentException("You are trying to send a non SREQ packet as synchronous command. " + "Evaluated " + value + " instead of " + 0x20 + "\nPacket " + packet.getClass().getName() + "\n" + packet);
    }
    cleanExpiredSynchronousCommandListeners();
    if (supportMultipleSynchrounsCommand) {
        synchronized (synchronousCommandListeners) {
            final short id = (short) (cmdId.get16BitValue() & 0x1FFF);
            while (synchronousCommandListeners.get(cmdId) != null) {
                try {
                    logger.trace("Waiting for other request {} to complete", id);
                    synchronousCommandListeners.wait(500);
                    cleanExpiredSynchronousCommandListeners();
                } catch (InterruptedException ignored) {
                }
            }
            synchronousCommandListeners.put(id, listener);
        }
    } else {
        synchronized (synchronousCommandListeners) {
            final short id = (short) (cmdId.get16BitValue() & 0x1FFF);
            while (!synchronousCommandListeners.isEmpty()) {
                try {
                    logger.trace("Waiting for other request to complete");
                    synchronousCommandListeners.wait(500);
                    cleanExpiredSynchronousCommandListeners();
                } catch (InterruptedException ignored) {
                }
            }
            logger.trace("Put synchronousCommandListeners listener for {} command", id);
            synchronousCommandListeners.put(id, listener);
        }
    }
    logger.trace("Sending SynchronousCommand {} ", packet);
    sendPacket(packet);
}
Also used : DoubleByte(com.zsmartsystems.zigbee.dongle.cc2531.zigbee.util.DoubleByte)

Example 2 with SynchronousCommandListener

use of com.zsmartsystems.zigbee.dongle.cc2531.network.SynchronousCommandListener in project com.zsmartsystems.zigbee by zsmartsystems.

the class CommandInterfaceImpl method notifySynchronousCommand.

/**
 * Notifies listeners about synchronous command response.
 *
 * @param packet the received packet
 */
private void notifySynchronousCommand(final ZToolPacket packet) {
    final DoubleByte cmdId = packet.getCMD();
    synchronized (synchronousCommandListeners) {
        final short id = (short) (cmdId.get16BitValue() & 0x1FFF);
        final SynchronousCommandListener listener = synchronousCommandListeners.get(id);
        if (listener != null) {
            listener.receivedCommandResponse(packet);
            synchronousCommandListeners.remove(id);
            synchronousCommandListeners.notifyAll();
        } else {
            // Notify asynchronous command listeners of unclaimed asynchronous command responses.
            final AsynchronousCommandListener[] listeners;
            synchronized (asynchrounsCommandListeners) {
                listeners = asynchrounsCommandListeners.toArray(new AsynchronousCommandListener[] {});
            }
            for (final AsynchronousCommandListener asynchronousCommandListener : listeners) {
                try {
                    asynchronousCommandListener.receivedUnclaimedSynchronousCommandResponse(packet);
                } catch (Throwable e) {
                    logger.error("Error in incoming asynchronous message processing: ", e);
                }
            }
        }
    }
}
Also used : SynchronousCommandListener(com.zsmartsystems.zigbee.dongle.cc2531.network.SynchronousCommandListener) AsynchronousCommandListener(com.zsmartsystems.zigbee.dongle.cc2531.network.AsynchronousCommandListener) DoubleByte(com.zsmartsystems.zigbee.dongle.cc2531.zigbee.util.DoubleByte)

Aggregations

DoubleByte (com.zsmartsystems.zigbee.dongle.cc2531.zigbee.util.DoubleByte)2 AsynchronousCommandListener (com.zsmartsystems.zigbee.dongle.cc2531.network.AsynchronousCommandListener)1 SynchronousCommandListener (com.zsmartsystems.zigbee.dongle.cc2531.network.SynchronousCommandListener)1