Skip to content

Commit

Permalink
Changes from Review
Browse files Browse the repository at this point in the history
  • Loading branch information
tmrobert8 committed Apr 13, 2017
1 parent c22024b commit 7cea13e
Show file tree
Hide file tree
Showing 30 changed files with 759 additions and 803 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ Import-Package:
com.google.gson.reflect,
com.google.gson.stream,
org.apache.commons.lang,
org.apache.commons.net.util,
org.eclipse.jetty.client,
org.eclipse.jetty.client.api,
org.eclipse.jetty.util.component,
Expand Down
2 changes: 1 addition & 1 deletion addons/binding/org.openhab.binding.russound/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ This binding provides integration with any Russound system that support the RIO

## Device Discovery

The Russound binding does support devices discovery via the paperUI. When you start device discovery, the system will scan all network interfaces for a Russound system device. If found, the device will be added to the inbox. Adding the device will then start a scan of the device to discover all the controllers, sources, and zones attached defined on the device. As these are found, they will be added to the inbox.
The Russound binding does support devices discovery via the paperUI. When you start device discovery, the system will scan all network interfaces and **all IP Addresses in the subnet on each interface** looking for a Russound system device. If found, the device will be added to the inbox. Adding the device will then start a scan of the device to discover all the controllers, sources, and zones attached defined on the device. As these are found, they will be added to the inbox.

## HABPANEL or other UI

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
* @author Tim Roberts
*/
public class RussoundHandlerFactory extends BaseThingHandlerFactory {
private final static Logger logger = LoggerFactory.getLogger(RussoundHandlerFactory.class);
private final Logger logger = LoggerFactory.getLogger(RussoundHandlerFactory.class);

public final static Set<ThingTypeUID> SUPPORTED_THING_TYPES_UIDS = ImmutableSet.of(RioConstants.BRIDGE_TYPE_RIO,
RioConstants.BRIDGE_TYPE_CONTROLLER, RioConstants.THING_TYPE_SOURCE, RioConstants.THING_TYPE_ZONE);
Expand Down Expand Up @@ -75,7 +75,7 @@ private synchronized void registerThingDiscovery(RioSystemHandler bridgeHandler)
logger.trace("Try to register Discovery service on BundleID: {} Service: {}",
bundleContext.getBundle().getBundleId(), DiscoveryService.class.getName());

Hashtable<String, String> prop = new Hashtable<String, String>();
final Hashtable<String, String> prop = new Hashtable<String, String>();

bundleContext.registerService(DiscoveryService.class.getName(), discoveryService, prop);
discoveryService.activate();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,30 +39,31 @@
*/
public class RioSystemDeviceDiscoveryService extends AbstractDiscoveryService {
/** The logger */
private final static Logger logger = LoggerFactory.getLogger(RioSystemDeviceDiscoveryService.class);
private final Logger logger = LoggerFactory.getLogger(RioSystemDeviceDiscoveryService.class);

/** The system handler to scan */
private final RioSystemHandler sysHandler;

/** Pattern to identify controller notifications */
private final Pattern RSP_CONTROLLERNOTIFICATION = Pattern.compile("(?i)^[SN] C\\[(\\d+)\\]\\.(\\w+)=\"(.*)\"$");
private static final Pattern RSP_CONTROLLERNOTIFICATION = Pattern
.compile("(?i)^[SN] C\\[(\\d+)\\]\\.(\\w+)=\"(.*)\"$");

/** Pattern to identify source notifications */
private final Pattern RSP_SRCNOTIFICATION = Pattern.compile("(?i)^[SN] S\\[(\\d+)\\]\\.(\\w+)=\"(.*)\"$");
private static final Pattern RSP_SRCNOTIFICATION = Pattern.compile("(?i)^[SN] S\\[(\\d+)\\]\\.(\\w+)=\"(.*)\"$");

/** Pattern to identify zone notifications */
private final Pattern RSP_ZONENOTIFICATION = Pattern
private static final Pattern RSP_ZONENOTIFICATION = Pattern
.compile("(?i)^[SN] C\\[(\\d+)\\]\\.Z\\[(\\d+)\\]\\.(\\w+)=\"(.*)\"$");

/**
* The {@link SocketSession} that will be used to scan the device
*/
private SocketSession _session;
private SocketSession session;

/**
* The {@link WaitingSessionListener} to the {@link #_session} to receive/process responses
* The {@link WaitingSessionListener} to the {@link #session} to receive/process responses
*/
private WaitingSessionListener _listener;
private WaitingSessionListener listener;

/**
* Create the discovery service from the {@link RioSystemHandler}
Expand All @@ -88,22 +89,20 @@ public void activate() {
}

/**
* Deactivates the scan - will disconnect the session and remove the {@link #_listener}
* Deactivates the scan - will disconnect the session and remove the {@link #listener}
*/
@Override
public void deactivate() {
if (_session != null) {
if (session != null) {
try {
_session.disconnect();
session.disconnect();
} catch (IOException e) {
// ignore
}
_session.removeListener(_listener);
_session = null;
_listener = null;
session.removeListener(listener);
session = null;
listener = null;
}

// sysHandler.unregisterDiscoveryService();
}

/**
Expand All @@ -112,7 +111,6 @@ public void deactivate() {
@Override
protected void startScan() {
// do nothing - started by RioSystemHandler

}

/**
Expand All @@ -122,13 +120,13 @@ protected void startScan() {
public void scanDevice() {
try {
final String ipAddress = sysHandler.getRioConfig().getIpAddress();
_session = new SocketChannelSession(ipAddress, RioConstants.RioPort);
_listener = new WaitingSessionListener();
_session.addListener(_listener);
session = new SocketChannelSession(ipAddress, RioConstants.RioPort);
listener = new WaitingSessionListener();
session.addListener(listener);

try {
logger.debug("Starting scan of RIO device at " + ipAddress);
_session.connect();
logger.debug("Starting scan of RIO device at {}", ipAddress);
session.connect();
discoverControllers();
discoverSources();
} catch (IOException e) {
Expand All @@ -146,23 +144,19 @@ public void scanDevice() {
*/
private void discoverControllers() {
for (int c = 1; c < 7; c++) {
try {
final String type = sendAndGet("GET C[" + c + "].type", RSP_CONTROLLERNOTIFICATION, 3);
if (StringUtils.isNotEmpty(type)) {
logger.debug("Controller #{} found - {}", c, type);
final String type = sendAndGet("GET C[" + c + "].type", RSP_CONTROLLERNOTIFICATION, 3);
if (StringUtils.isNotEmpty(type)) {
logger.debug("Controller #{} found - {}", c, type);

final ThingUID thingUID = new ThingUID(RioConstants.BRIDGE_TYPE_CONTROLLER,
sysHandler.getThing().getUID(), String.valueOf(c));
final ThingUID thingUID = new ThingUID(RioConstants.BRIDGE_TYPE_CONTROLLER,
sysHandler.getThing().getUID(), String.valueOf(c));

final DiscoveryResult discoveryResult = DiscoveryResultBuilder.create(thingUID)
.withProperty(RioControllerConfig.Controller, c).withBridge(sysHandler.getThing().getUID())
.withLabel("Controller #" + c).build();
thingDiscovered(discoveryResult);
final DiscoveryResult discoveryResult = DiscoveryResultBuilder.create(thingUID)
.withProperty(RioControllerConfig.Controller, c).withBridge(sysHandler.getThing().getUID())
.withLabel("Controller #" + c).build();
thingDiscovered(discoveryResult);

discoverZones(thingUID, c);
}
} catch (Exception e) {
logger.debug("Exception occurred scanning controller #{} : {}", c, e.getMessage(), e);
discoverZones(thingUID, c);
}
}
}
Expand All @@ -174,24 +168,20 @@ private void discoverControllers() {
*/
private void discoverSources() {
for (int s = 1; s < 9; s++) {
try {
final String type = sendAndGet("GET S[" + s + "].type", RSP_SRCNOTIFICATION, 3);
if (StringUtils.isNotEmpty(type)) {
final String name = sendAndGet("GET S[" + s + "].name", RSP_SRCNOTIFICATION, 3);
logger.debug("Source #{} - {}/{}", s, type, name);
final String type = sendAndGet("GET S[" + s + "].type", RSP_SRCNOTIFICATION, 3);
if (StringUtils.isNotEmpty(type)) {
final String name = sendAndGet("GET S[" + s + "].name", RSP_SRCNOTIFICATION, 3);
logger.debug("Source #{} - {}/{}", s, type, name);

final ThingUID thingUID = new ThingUID(RioConstants.THING_TYPE_SOURCE,
sysHandler.getThing().getUID(), String.valueOf(s));
final ThingUID thingUID = new ThingUID(RioConstants.THING_TYPE_SOURCE, sysHandler.getThing().getUID(),
String.valueOf(s));

final DiscoveryResult discoveryResult = DiscoveryResultBuilder.create(thingUID)
.withProperty(RioSourceConfig.Source, s).withBridge(sysHandler.getThing().getUID())
.withLabel((StringUtils.isEmpty(name) || name.equalsIgnoreCase("null") ? "Source" : name)
+ " (" + s + ")")
.build();
thingDiscovered(discoveryResult);
}
} catch (Exception e) {
logger.debug("Exception occurred scanning source #{} : {}", s, e.getMessage(), e);
final DiscoveryResult discoveryResult = DiscoveryResultBuilder.create(thingUID)
.withProperty(RioSourceConfig.Source, s).withBridge(sysHandler.getThing().getUID())
.withLabel((StringUtils.isEmpty(name) || name.equalsIgnoreCase("null") ? "Source" : name) + " ("
+ s + ")")
.build();
thingDiscovered(discoveryResult);
}
}
}
Expand All @@ -213,23 +203,16 @@ private void discoverZones(ThingUID controllerUID, int c) {
throw new IllegalArgumentException("c must be between 1 and 8");
}
for (int z = 1; z < 9; z++) {
try {
final String name = sendAndGet("GET C[" + c + "].Z[" + z + "].name", RSP_ZONENOTIFICATION, 4);
if (StringUtils.isNotEmpty(name)) {
logger.debug("Controller #{}, Zone #{} found - {}", c, z, name);
final String name = sendAndGet("GET C[" + c + "].Z[" + z + "].name", RSP_ZONENOTIFICATION, 4);
if (StringUtils.isNotEmpty(name)) {
logger.debug("Controller #{}, Zone #{} found - {}", c, z, name);

final ThingUID thingUID = new ThingUID(RioConstants.THING_TYPE_ZONE, controllerUID,
String.valueOf(z));
final ThingUID thingUID = new ThingUID(RioConstants.THING_TYPE_ZONE, controllerUID, String.valueOf(z));

final DiscoveryResult discoveryResult = DiscoveryResultBuilder.create(thingUID)
.withProperty(RioZoneConfig.Zone, z).withBridge(controllerUID)
.withLabel((StringUtils.isEmpty(name) || name.equalsIgnoreCase("null") ? "Zone" : name)
+ " (" + z + ")")
.build();
thingDiscovered(discoveryResult);
}
} catch (Exception e) {
logger.debug("Exception occurred scanning zone #{}-{} : {}", c, z, e.getMessage(), e);
final DiscoveryResult discoveryResult = DiscoveryResultBuilder.create(thingUID)
.withProperty(RioZoneConfig.Zone, z).withBridge(controllerUID)
.withLabel((name.equalsIgnoreCase("null") ? "Zone" : name) + " (" + z + ")").build();
thingDiscovered(discoveryResult);
}
}
}
Expand Down Expand Up @@ -257,13 +240,21 @@ private String sendAndGet(String message, Pattern respPattern, int groupNum) {
throw new IllegalArgumentException("groupNum must be >= 0");
}
try {
_session.sendCommand(message);
final Matcher m = respPattern.matcher(_listener.getResponse());
session.sendCommand(message);
final String r = listener.getResponse();
final Matcher m = respPattern.matcher(r);
if (m.matches() && m.groupCount() >= groupNum) {
logger.debug("Message '{}' returned an valid response: {}", message, r);
return m.group(groupNum);
}
logger.debug("Message '{}' returned an invalid response: {}", message, r);
return null;
} catch (InterruptedException e) {
logger.debug("Sending message '{}' was interrupted and could not be completed", message);
return null;
} catch (Exception e) {
} catch (IOException e) {
logger.debug("Sending message '{}' resulted in an IOException and could not be completed: {}", message,
e.getMessage(), e);
return null;
}
}
Expand Down
Loading

0 comments on commit 7cea13e

Please sign in to comment.