Skip to content

Commit

Permalink
Adding socket connection diagnostics
Browse files Browse the repository at this point in the history
Signed-off-by: Matt Myers <[email protected]>
  • Loading branch information
matchews committed Mar 17, 2024
1 parent 579b899 commit 07306e2
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import javax.net.ssl.HandshakeCompletedEvent;
import javax.net.ssl.HandshakeCompletedListener;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLException;
import javax.net.ssl.SSLSession;
import javax.net.ssl.SSLSocket;
import javax.net.ssl.X509TrustManager;
Expand Down Expand Up @@ -175,6 +176,10 @@ public boolean sslLogin() {
} catch (UnknownHostException e) {
logger.error("Unable to open connection to Elk alarm: {}:{}", config.host, config.port, e);
return false;
} catch (SSLException e) {
logger.error("Unable to open connection to Elk alarm: {}:{}. Must use secure Elk port.", config.host,
config.port, e);
return false;
} catch (IOException e) {
logger.error("Unable to open connection to Elk alarm: {}:{}", config.host, config.port, e);
return false;
Expand Down Expand Up @@ -276,8 +281,12 @@ public void run() {
} catch (IOException e) {
if (e.getMessage().equals("Socket closed")) {
logger.error("Error reading from Elk alarm. Socket Closed. {}:{}", config.host, config.port);
return;
} else {
logger.error("Error reading from Elk alarm: {}:{}", config.host, config.port, e);
logger.error(
"Error reading from Elk alarm: {}:{}. Check hostname/address and secure/non-secure port.",
config.host, config.port, e);
return;
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,20 +125,20 @@ public void removeListener(ElkM1HandlerListener listener) {
public void initialize() {
updateStatus(ThingStatus.UNKNOWN);
initializeFuture = scheduler.schedule(this::scheduledInitialize, 1, TimeUnit.SECONDS);
commWatchdogFuture = scheduler.scheduleWithFixedDelay(commWatchdog, 30, 120, TimeUnit.SECONDS);
return;
}

public void scheduledInitialize() {
commWatchdogFuture = scheduler.scheduleWithFixedDelay(commWatchdog, 120, 120, TimeUnit.SECONDS);
ElkAlarmConfig config = getConfigAs(ElkAlarmConfig.class);
messageFactory = new ElkMessageFactory();
areas = new boolean[ElkMessageFactory.MAX_AREAS];

if (config.useSSL) {
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_PENDING,
updateStatus(ThingStatus.UNKNOWN, ThingStatusDetail.CONFIGURATION_PENDING,
"Opening SSL/TLS server connection");
} else {
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_PENDING, "Opening server connection");
updateStatus(ThingStatus.UNKNOWN, ThingStatusDetail.CONFIGURATION_PENDING, "Opening server connection");
}

connection = new ElkAlarmConnection(config, messageFactory);
Expand All @@ -150,7 +150,7 @@ public void scheduledInitialize() {
connection.sendCommand(new ZonePartition());
connection.sendCommand(new ZoneStatus());
connection.sendCommand(new ArmingStatus());
updateStatus(ThingStatus.ONLINE, ThingStatusDetail.CONFIGURATION_PENDING, "Requesting version from alarm");
updateStatus(ThingStatus.UNKNOWN, ThingStatusDetail.CONFIGURATION_PENDING, "Requesting version from alarm");
} else {
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR, "Unable to open socket to alarm");
}
Expand Down Expand Up @@ -360,7 +360,7 @@ public void onZoneAdded(ElkM1ZoneHandler elkM1ZoneHandler) {
*/
public void updateArmedState(int area, ElkAlarmArmedState armed) throws Exception {
ElkAlarmConfig config = getConfigAs(ElkAlarmConfig.class);
String pincode = String.format("%06d", config.userCode);
String pincode = ("000000" + config.userCode).substring(config.userCode.length());
switch (armed) {
case ArmedAway:
connection.sendCommand(new ArmAway(area, pincode));
Expand Down Expand Up @@ -426,14 +426,26 @@ Thing getThingForType(ElkTypeToRequest type, int num) {
return null;
}

/**
* Cancel Future
*/
public void cancelFuture(@Nullable ScheduledFuture<?> future) {
if (future != null) {
future.cancel(false);
}
}

/**
* Shutdown the bridge
*/
@Override
public void dispose() {
connection.shutdown();
cancelFuture(commWatchdogFuture);
if (connection != null) {
connection.shutdown();
connection = null;
}
areas = new boolean[0];
connection = null;
assert (listeners.isEmpty());
super.dispose();
}
Expand Down

0 comments on commit 07306e2

Please sign in to comment.