Search in sources :

Example 1 with SocketSession

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

the class RioControllerHandler method initialize.

/**
 * Initializes the bridge. Confirms the configuration is valid and that our parent bridge is a
 * {@link RioSystemHandler}. Once validated, a {@link RioControllerProtocol} is set via
 * {@link #setProtocolHandler(RioControllerProtocol)} and the bridge comes online.
 */
@Override
public void initialize() {
    final Bridge bridge = getBridge();
    if (bridge == null) {
        updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR, "Cannot be initialized without a bridge");
        return;
    }
    if (bridge.getStatus() != ThingStatus.ONLINE) {
        updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.BRIDGE_OFFLINE);
        return;
    }
    final ThingHandler handler = bridge.getHandler();
    if (handler == null) {
        updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR, "No handler specified (null) for the bridge!");
        return;
    }
    if (!(handler instanceof RioSystemHandler)) {
        updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR, "Controller must be attached to a system bridge: " + handler.getClass());
        return;
    }
    final RioControllerConfig config = getThing().getConfiguration().as(RioControllerConfig.class);
    if (config == null) {
        updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR, "Configuration file missing");
        return;
    }
    final int configController = config.getController();
    if (configController < 1 || configController > 8) {
        updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR, "Controller must be between 1 and 8: " + configController);
        return;
    }
    controller.set(configController);
    // Get the socket session from the
    final SocketSession socketSession = getSocketSession();
    if (socketSession == null) {
        updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.HANDLER_INITIALIZING_ERROR, "No socket session found");
        return;
    }
    if (getProtocolHandler() != null) {
        getProtocolHandler().dispose();
    }
    setProtocolHandler(new RioControllerProtocol(configController, socketSession, new StatefulHandlerCallback(new AbstractRioHandlerCallback() {

        @Override
        public void statusChanged(ThingStatus status, ThingStatusDetail detail, String msg) {
            updateStatus(status, detail, msg);
        }

        @Override
        public void stateChanged(String channelId, State state) {
            updateState(channelId, state);
            fireStateUpdated(channelId, state);
        }

        @Override
        public void setProperty(String propertyName, String property) {
            getThing().setProperty(propertyName, property);
        }
    })));
    updateStatus(ThingStatus.ONLINE);
    getProtocolHandler().postOnline();
    refreshNamedHandler(gson, RioZoneHandler.class, RioConstants.CHANNEL_CTLZONES);
}
Also used : AbstractRioHandlerCallback(org.openhab.binding.russound.internal.rio.AbstractRioHandlerCallback) ThingStatus(org.openhab.core.thing.ThingStatus) ThingStatusDetail(org.openhab.core.thing.ThingStatusDetail) ThingHandler(org.openhab.core.thing.binding.ThingHandler) StatefulHandlerCallback(org.openhab.binding.russound.internal.rio.StatefulHandlerCallback) SocketSession(org.openhab.binding.russound.internal.net.SocketSession) State(org.openhab.core.types.State) RioSystemHandler(org.openhab.binding.russound.internal.rio.system.RioSystemHandler) Bridge(org.openhab.core.thing.Bridge)

Example 2 with SocketSession

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

the class RioZoneHandler method initialize.

/**
 * Initializes the bridge. Confirms the configuration is valid and that our parent bridge is a
 * {@link RioControllerHandler}. Once validated, a {@link RioZoneProtocol} is set via
 * {@link #setProtocolHandler(RioZoneProtocol)} and the bridge comes online.
 */
@Override
public void initialize() {
    final Bridge bridge = getBridge();
    if (bridge == null) {
        updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR, "Cannot be initialized without a bridge");
        return;
    }
    if (bridge.getStatus() != ThingStatus.ONLINE) {
        updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.BRIDGE_OFFLINE);
        return;
    }
    final ThingHandler handler = bridge.getHandler();
    if (handler == null) {
        updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR, "No handler specified (null) for the bridge!");
        return;
    }
    if (!(handler instanceof RioControllerHandler)) {
        updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR, "Source must be attached to a controller bridge: " + handler.getClass());
        return;
    }
    final RioZoneConfig config = getThing().getConfiguration().as(RioZoneConfig.class);
    if (config == null) {
        updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR, "Configuration file missing");
        return;
    }
    final int configZone = config.getZone();
    if (configZone < 1 || configZone > 8) {
        updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR, "Source must be between 1 and 8: " + configZone);
        return;
    }
    zone.set(configZone);
    final int handlerController = ((RioControllerHandler) handler).getId();
    controller.set(handlerController);
    // Get the socket session from the
    final SocketSession socketSession = getSocketSession();
    if (socketSession == null) {
        updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.HANDLER_INITIALIZING_ERROR, "No socket session found");
        return;
    }
    setProtocolHandler(new RioZoneProtocol(configZone, handlerController, getSystemFavoritesHandler(), getPresetsProtocol(), socketSession, new StatefulHandlerCallback(new AbstractRioHandlerCallback() {

        @Override
        public void statusChanged(ThingStatus status, ThingStatusDetail detail, String msg) {
            updateStatus(status, detail, msg);
        }

        @Override
        public void stateChanged(String channelId, State state) {
            if (channelId.equals(RioConstants.CHANNEL_ZONENAME)) {
                zoneName.set(state.toString());
            }
            updateState(channelId, state);
            fireStateUpdated(channelId, state);
        }

        @Override
        public void setProperty(String propertyName, String propertyValue) {
            getThing().setProperty(propertyName, propertyValue);
        }
    })));
    updateStatus(ThingStatus.ONLINE);
    getProtocolHandler().postOnline();
}
Also used : RioControllerHandler(org.openhab.binding.russound.internal.rio.controller.RioControllerHandler) AbstractRioHandlerCallback(org.openhab.binding.russound.internal.rio.AbstractRioHandlerCallback) ThingStatus(org.openhab.core.thing.ThingStatus) ThingStatusDetail(org.openhab.core.thing.ThingStatusDetail) ThingHandler(org.openhab.core.thing.binding.ThingHandler) AbstractThingHandler(org.openhab.binding.russound.internal.rio.AbstractThingHandler) StatefulHandlerCallback(org.openhab.binding.russound.internal.rio.StatefulHandlerCallback) SocketSession(org.openhab.binding.russound.internal.net.SocketSession) State(org.openhab.core.types.State) Bridge(org.openhab.core.thing.Bridge)

Example 3 with SocketSession

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

the class RioSourceHandler method initialize.

/**
 * Initializes the bridge. Confirms the configuration is valid and that our parent bridge is a
 * {@link RioSystemHandler}. Once validated, a {@link RioSystemProtocol} is set via
 * {@link #setProtocolHandler(RioSystemProtocol)} and the bridge comes online.
 */
@Override
public void initialize() {
    logger.debug("Initializing");
    final Bridge bridge = getBridge();
    if (bridge == null) {
        updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR, "Cannot be initialized without a bridge");
        return;
    }
    if (bridge.getStatus() != ThingStatus.ONLINE) {
        updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.BRIDGE_OFFLINE);
        return;
    }
    final ThingHandler handler = bridge.getHandler();
    if (handler == null) {
        updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR, "No handler specified (null) for the bridge!");
        return;
    }
    if (!(handler instanceof RioSystemHandler)) {
        updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR, "Source must be attached to a System bridge: " + handler.getClass());
        return;
    }
    final RioSourceConfig config = getThing().getConfiguration().as(RioSourceConfig.class);
    if (config == null) {
        updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR, "Configuration file missing");
        return;
    }
    final int configSource = config.getSource();
    if (configSource < 1 || configSource > 12) {
        updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR, "Source must be between 1 and 12: " + configSource);
        return;
    }
    source.set(configSource);
    // Get the socket session from the
    final SocketSession socketSession = getSocketSession();
    if (socketSession == null) {
        updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.HANDLER_INITIALIZING_ERROR, "No socket session found");
        return;
    }
    try {
        setProtocolHandler(new RioSourceProtocol(configSource, socketSession, new StatefulHandlerCallback(new AbstractRioHandlerCallback() {

            @Override
            public void statusChanged(ThingStatus status, ThingStatusDetail detail, String msg) {
                updateStatus(status, detail, msg);
            }

            @Override
            public void stateChanged(String channelId, State state) {
                if (channelId.equals(RioConstants.CHANNEL_SOURCENAME)) {
                    sourceName.set(state.toString());
                }
                if (state != null) {
                    updateState(channelId, state);
                    fireStateUpdated(channelId, state);
                }
            }

            @Override
            public void setProperty(String propertyName, String propertyValue) {
                getThing().setProperty(propertyName, propertyValue);
            }
        })));
        updateStatus(ThingStatus.ONLINE);
        getProtocolHandler().postOnline();
    } catch (Exception e) {
        updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR, e.toString());
    }
}
Also used : AbstractRioHandlerCallback(org.openhab.binding.russound.internal.rio.AbstractRioHandlerCallback) ThingStatus(org.openhab.core.thing.ThingStatus) ThingStatusDetail(org.openhab.core.thing.ThingStatusDetail) ThingHandler(org.openhab.core.thing.binding.ThingHandler) AbstractThingHandler(org.openhab.binding.russound.internal.rio.AbstractThingHandler) StatefulHandlerCallback(org.openhab.binding.russound.internal.rio.StatefulHandlerCallback) SocketSession(org.openhab.binding.russound.internal.net.SocketSession) State(org.openhab.core.types.State) RioSystemHandler(org.openhab.binding.russound.internal.rio.system.RioSystemHandler) Bridge(org.openhab.core.thing.Bridge)

Example 4 with SocketSession

use of org.openhab.binding.russound.internal.net.SocketSession 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

SocketSession (org.openhab.binding.russound.internal.net.SocketSession)4 AbstractRioHandlerCallback (org.openhab.binding.russound.internal.rio.AbstractRioHandlerCallback)3 StatefulHandlerCallback (org.openhab.binding.russound.internal.rio.StatefulHandlerCallback)3 Bridge (org.openhab.core.thing.Bridge)3 ThingStatus (org.openhab.core.thing.ThingStatus)3 ThingStatusDetail (org.openhab.core.thing.ThingStatusDetail)3 ThingHandler (org.openhab.core.thing.binding.ThingHandler)3 State (org.openhab.core.types.State)3 AbstractThingHandler (org.openhab.binding.russound.internal.rio.AbstractThingHandler)2 RioSystemHandler (org.openhab.binding.russound.internal.rio.system.RioSystemHandler)2 IOException (java.io.IOException)1 SocketChannelSession (org.openhab.binding.russound.internal.net.SocketChannelSession)1 WaitingSessionListener (org.openhab.binding.russound.internal.net.WaitingSessionListener)1 RioControllerHandler (org.openhab.binding.russound.internal.rio.controller.RioControllerHandler)1