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);
}
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();
}
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());
}
}
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
}
}
}
Aggregations