Skip to content

Commit 07306e2

Browse files
committed
Adding socket connection diagnostics
Signed-off-by: Matt Myers <[email protected]>
1 parent 579b899 commit 07306e2

File tree

2 files changed

+29
-8
lines changed

2 files changed

+29
-8
lines changed

bundles/org.openhab.binding.elkm1/src/main/java/org/openhab/binding/elkm1/internal/elk/ElkAlarmConnection.java

+10-1
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
import javax.net.ssl.HandshakeCompletedEvent;
3636
import javax.net.ssl.HandshakeCompletedListener;
3737
import javax.net.ssl.SSLContext;
38+
import javax.net.ssl.SSLException;
3839
import javax.net.ssl.SSLSession;
3940
import javax.net.ssl.SSLSocket;
4041
import javax.net.ssl.X509TrustManager;
@@ -175,6 +176,10 @@ public boolean sslLogin() {
175176
} catch (UnknownHostException e) {
176177
logger.error("Unable to open connection to Elk alarm: {}:{}", config.host, config.port, e);
177178
return false;
179+
} catch (SSLException e) {
180+
logger.error("Unable to open connection to Elk alarm: {}:{}. Must use secure Elk port.", config.host,
181+
config.port, e);
182+
return false;
178183
} catch (IOException e) {
179184
logger.error("Unable to open connection to Elk alarm: {}:{}", config.host, config.port, e);
180185
return false;
@@ -276,8 +281,12 @@ public void run() {
276281
} catch (IOException e) {
277282
if (e.getMessage().equals("Socket closed")) {
278283
logger.error("Error reading from Elk alarm. Socket Closed. {}:{}", config.host, config.port);
284+
return;
279285
} else {
280-
logger.error("Error reading from Elk alarm: {}:{}", config.host, config.port, e);
286+
logger.error(
287+
"Error reading from Elk alarm: {}:{}. Check hostname/address and secure/non-secure port.",
288+
config.host, config.port, e);
289+
return;
281290
}
282291
}
283292
}

bundles/org.openhab.binding.elkm1/src/main/java/org/openhab/binding/elkm1/internal/handler/ElkM1BridgeHandler.java

+19-7
Original file line numberDiff line numberDiff line change
@@ -125,20 +125,20 @@ public void removeListener(ElkM1HandlerListener listener) {
125125
public void initialize() {
126126
updateStatus(ThingStatus.UNKNOWN);
127127
initializeFuture = scheduler.schedule(this::scheduledInitialize, 1, TimeUnit.SECONDS);
128-
commWatchdogFuture = scheduler.scheduleWithFixedDelay(commWatchdog, 30, 120, TimeUnit.SECONDS);
129128
return;
130129
}
131130

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

137137
if (config.useSSL) {
138-
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_PENDING,
138+
updateStatus(ThingStatus.UNKNOWN, ThingStatusDetail.CONFIGURATION_PENDING,
139139
"Opening SSL/TLS server connection");
140140
} else {
141-
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_PENDING, "Opening server connection");
141+
updateStatus(ThingStatus.UNKNOWN, ThingStatusDetail.CONFIGURATION_PENDING, "Opening server connection");
142142
}
143143

144144
connection = new ElkAlarmConnection(config, messageFactory);
@@ -150,7 +150,7 @@ public void scheduledInitialize() {
150150
connection.sendCommand(new ZonePartition());
151151
connection.sendCommand(new ZoneStatus());
152152
connection.sendCommand(new ArmingStatus());
153-
updateStatus(ThingStatus.ONLINE, ThingStatusDetail.CONFIGURATION_PENDING, "Requesting version from alarm");
153+
updateStatus(ThingStatus.UNKNOWN, ThingStatusDetail.CONFIGURATION_PENDING, "Requesting version from alarm");
154154
} else {
155155
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR, "Unable to open socket to alarm");
156156
}
@@ -360,7 +360,7 @@ public void onZoneAdded(ElkM1ZoneHandler elkM1ZoneHandler) {
360360
*/
361361
public void updateArmedState(int area, ElkAlarmArmedState armed) throws Exception {
362362
ElkAlarmConfig config = getConfigAs(ElkAlarmConfig.class);
363-
String pincode = String.format("%06d", config.userCode);
363+
String pincode = ("000000" + config.userCode).substring(config.userCode.length());
364364
switch (armed) {
365365
case ArmedAway:
366366
connection.sendCommand(new ArmAway(area, pincode));
@@ -426,14 +426,26 @@ Thing getThingForType(ElkTypeToRequest type, int num) {
426426
return null;
427427
}
428428

429+
/**
430+
* Cancel Future
431+
*/
432+
public void cancelFuture(@Nullable ScheduledFuture<?> future) {
433+
if (future != null) {
434+
future.cancel(false);
435+
}
436+
}
437+
429438
/**
430439
* Shutdown the bridge
431440
*/
432441
@Override
433442
public void dispose() {
434-
connection.shutdown();
443+
cancelFuture(commWatchdogFuture);
444+
if (connection != null) {
445+
connection.shutdown();
446+
connection = null;
447+
}
435448
areas = new boolean[0];
436-
connection = null;
437449
assert (listeners.isEmpty());
438450
super.dispose();
439451
}

0 commit comments

Comments
 (0)