Search in sources :

Example 1 with WaitingSessionListener

use of org.openhab.binding.russound.internal.net.WaitingSessionListener in project openhab-addons by openhab.

the class RioSystemDeviceDiscoveryService method scanDevice.

/**
 * Starts a device scan. This will connect to the device and discover the controllers/sources/zones attached to the
 * device and then disconnect via {@link #deactivate()}
 */
public void scanDevice() {
    try {
        final String ipAddress = sysHandler.getRioConfig().getIpAddress();
        session = new SocketChannelSession(ipAddress, RioConstants.RIO_PORT);
        listener = new WaitingSessionListener();
        session.addListener(listener);
        try {
            logger.debug("Starting scan of RIO device at {}", ipAddress);
            session.connect();
            discoverControllers();
            discoverSources();
        } catch (IOException e) {
            logger.debug("Trying to scan device but couldn't connect: {}", e.getMessage(), e);
        }
    } finally {
        deactivate();
    }
}
Also used : SocketChannelSession(org.openhab.binding.russound.internal.net.SocketChannelSession) IOException(java.io.IOException) WaitingSessionListener(org.openhab.binding.russound.internal.net.WaitingSessionListener)

Example 2 with WaitingSessionListener

use of org.openhab.binding.russound.internal.net.WaitingSessionListener in project openhab-addons by openhab.

the class RioSystemDiscovery method scanAddress.

/**
 * Helper method to scan a specific address. Will open up port 9621 on the address and if opened, query for any
 * controller type (all 6 controllers are tested). If a valid type is found, a discovery result will be created.
 *
 * @param ipAddress a possibly null, possibly empty ip address (null/empty addresses will be ignored)
 */
private void scanAddress(String ipAddress) {
    if (ipAddress == null || ipAddress.isEmpty()) {
        return;
    }
    final SocketSession session = new SocketChannelSession(ipAddress, RioConstants.RIO_PORT);
    try {
        final WaitingSessionListener listener = new WaitingSessionListener();
        session.addListener(listener);
        session.connect(CONN_TIMEOUT_IN_MS);
        logger.debug("Connected to port {}:{} - testing to see if RIO", ipAddress, RioConstants.RIO_PORT);
        // need to check if any controllers are defined
        for (int c = 1; c < 7; c++) {
            session.sendCommand("GET C[" + c + "].type");
            final String resp = listener.getResponse();
            if (resp == null) {
                continue;
            }
            if (!resp.startsWith("S C[" + c + "].type=\"")) {
                continue;
            }
            final String type = resp.substring(13, resp.length() - 1);
            if (!type.isBlank()) {
                logger.debug("Found a RIO type #{}", type);
                addResult(ipAddress, type);
                break;
            }
        }
    } catch (InterruptedException e) {
        logger.debug("Connection was interrupted to port {}:{}", ipAddress, RioConstants.RIO_PORT);
    } catch (IOException e) {
        logger.trace("Connection couldn't be established to port {}:{}", ipAddress, RioConstants.RIO_PORT);
    } finally {
        try {
            session.disconnect();
        } catch (IOException e) {
        // do nothing
        }
    }
}
Also used : SocketSession(org.openhab.binding.russound.internal.net.SocketSession) SocketChannelSession(org.openhab.binding.russound.internal.net.SocketChannelSession) IOException(java.io.IOException) WaitingSessionListener(org.openhab.binding.russound.internal.net.WaitingSessionListener)

Aggregations

IOException (java.io.IOException)2 SocketChannelSession (org.openhab.binding.russound.internal.net.SocketChannelSession)2 WaitingSessionListener (org.openhab.binding.russound.internal.net.WaitingSessionListener)2 SocketSession (org.openhab.binding.russound.internal.net.SocketSession)1