[neohub] check for connection refused (#12906)

Signed-off-by: Andrew Fiddian-Green <software@whitebear.ch>
This commit is contained in:
Andrew Fiddian-Green
2022-06-08 08:45:35 +02:00
committed by GitHub
parent 52bb4b3715
commit e24ca5708b
3 changed files with 30 additions and 2 deletions

View File

@@ -157,6 +157,7 @@ public class NeoHubBindingConstants {
public static final String CMD_CODE_TIMER = "{\"TIMER_%s\":\"%s\"}";
public static final String CMD_CODE_MANUAL = "{\"MANUAL_%s\":\"%s\"}";
public static final String CMD_CODE_READ_DCB = "{\"READ_DCB\":100}";
public static final String CMD_CODE_FIRMWARE = "{\"FIRMWARE\":0}";
/*
* note: from NeoHub rev2.6 onwards the INFO command is "deprecated" and it

View File

@@ -58,6 +58,8 @@ import com.google.gson.JsonSyntaxException;
@NonNullByDefault
public class NeoHubHandler extends BaseBridgeHandler {
private static final String SEE_README = "See documentation chapter \"Connection Refused Errors\"";
private final Logger logger = LoggerFactory.getLogger(NeoHubHandler.class);
private final Map<String, Boolean> connectionStates = new HashMap<>();
@@ -140,9 +142,26 @@ public class NeoHubHandler extends BaseBridgeHandler {
logger.debug("hub '{}' preferLegacyApi={}", getThing().getUID(), config.preferLegacyApi);
}
socket = new NeoHubSocket(config.hostName, config.portNumber, config.socketTimeout);
NeoHubSocket socket = this.socket = new NeoHubSocket(config.hostName, config.portNumber, config.socketTimeout);
this.config = config;
/*
* Try to 'ping' the hub, and if there is a 'connection refused', it is probably due to the mobile App |
* Settings | Legacy API Enable switch not being On, so go offline and log a warning message.
*/
try {
socket.sendMessage(CMD_CODE_FIRMWARE);
} catch (IOException e) {
String error = e.getMessage();
if (error != null && error.toLowerCase().startsWith("connection refused")) {
logger.warn("CONNECTION REFUSED!! (hub '{}') => {}", getThing().getUID(), SEE_README);
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR, SEE_README);
return;
}
} catch (NeoHubException e) {
// NeoHubException won't actually occur here
}
if (logger.isDebugEnabled()) {
logger.debug("hub '{}' start background polling..", getThing().getUID());
}