diff --git a/bundles/org.openhab.binding.intellifire/README.md b/bundles/org.openhab.binding.intellifire/README.md index 514a12dce63c9..609d41aa0bfe0 100644 --- a/bundles/org.openhab.binding.intellifire/README.md +++ b/bundles/org.openhab.binding.intellifire/README.md @@ -76,6 +76,7 @@ _*.sitemap examples are optional._ ```java Example thing configuration goes here. ``` + ### Item Configuration ```java diff --git a/bundles/org.openhab.binding.intellifire/ToDo.txt b/bundles/org.openhab.binding.intellifire/ToDo.txt index bab45c5d0e674..da8e649bed2d4 100644 --- a/bundles/org.openhab.binding.intellifire/ToDo.txt +++ b/bundles/org.openhab.binding.intellifire/ToDo.txt @@ -1,5 +1,5 @@ -Things online before succesful local poll? +Things online before successfull local poll? Combine light and fan into fireplace thing? (light:light, fan:fan) @@ -13,27 +13,11 @@ verify SSL certificate Send Command failure causes thing offline. Recovery? ****** -Test local IP address change. -IP Address successfully updates on initialization. -What to do if localPolling fails? - Try cloudPolling - Or retry out and re-initialize? - - - Initialize (including one cloud poll) - Local Poll - Retry local 2-3 times - Retry cloud 2-3 times (if this succeeds, go back to local polling?) - Initialize - - HttpResponse error messages need to differentiate between internet connection and local connection. Might need a config option to select local, cloud, or both? ****** -getusername used in python code? - Limit outgoing command frequency so we don't overload the server diff --git a/bundles/org.openhab.binding.intellifire/pom.xml b/bundles/org.openhab.binding.intellifire/pom.xml index 0deafef73b180..05e2d12b7a35e 100644 --- a/bundles/org.openhab.binding.intellifire/pom.xml +++ b/bundles/org.openhab.binding.intellifire/pom.xml @@ -1,13 +1,13 @@ - + 4.0.0 org.openhab.addons.bundles org.openhab.addons.reactor.bundles - 4.3.0-SNAPSHOT + 5.0.0-SNAPSHOT org.openhab.binding.intellifire diff --git a/bundles/org.openhab.binding.intellifire/src/main/java/org/openhab/binding/intellifire/internal/handlers/IntellifireBridgeHandler.java b/bundles/org.openhab.binding.intellifire/src/main/java/org/openhab/binding/intellifire/internal/handlers/IntellifireBridgeHandler.java index 51566d9dcc132..56a02e32dc6db 100644 --- a/bundles/org.openhab.binding.intellifire/src/main/java/org/openhab/binding/intellifire/internal/handlers/IntellifireBridgeHandler.java +++ b/bundles/org.openhab.binding.intellifire/src/main/java/org/openhab/binding/intellifire/internal/handlers/IntellifireBridgeHandler.java @@ -95,7 +95,7 @@ public void scheduledInitialize() { config = getConfigAs(IntellifireConfiguration.class); try { - if (login() && getUsername() && setupAccountData() && poll(IntellifireBindingConstants.CLOUD_POLLING)) { + if (login() && setupAccountData() && getUsername() && poll(IntellifireBindingConstants.CLOUD_POLLING)) { logger.debug("Succesfully opened connection to Intellifire's server: {} Username:{} ", IntellifireBindingConstants.URI_COOKIE, config.username); initPolling(5); @@ -200,10 +200,20 @@ public synchronized void initPolling(int initalDelay) { initialize(); return; } - - if (!(poll(IntellifireBindingConstants.LOCAL_POLLING))) { - commFailureCount++; - return; + if (commFailureCount < 2) { + if (!(poll(IntellifireBindingConstants.LOCAL_POLLING))) { + commFailureCount++; + return; + } else { + commFailureCount = 0; + } + } else { + if (!(poll(IntellifireBindingConstants.CLOUD_POLLING))) { + commFailureCount++; + return; + } else { + commFailureCount = 0; + } } if (this.thing.getStatus() != ThingStatus.ONLINE) { commFailureCount = 0; @@ -248,10 +258,15 @@ public boolean poll(boolean cloudPool) throws IntellifireException, InterruptedE } else { // Local Poll String ipAddress = account.getIPAddress(serialNumber); - IntellifirePollData localPollData = localPollFireplace(ipAddress); - if (localPollData != null) { - account.locations.get(i).fireplaces.fireplaces.get(j).pollData = localPollData; + if (!"".equals(ipAddress)) { + IntellifirePollData localPollData = localPollFireplace(ipAddress); + if (localPollData != null) { + account.locations.get(i).fireplaces.fireplaces.get(j).pollData = localPollData; + } else { + failureFlag = true; + } } else { + logger.error("Intellifire local poll failed. Invalid local IP Address received from cloud."); failureFlag = true; } } @@ -272,12 +287,7 @@ public boolean poll(boolean cloudPool) throws IntellifireException, InterruptedE } } } - - if (failureFlag) { - return false; - } else { - return true; - } + return !failureFlag; } public synchronized @Nullable IntellifirePollData cloudPollFireplace(String serialNumber) @@ -318,12 +328,11 @@ public String sendCommand(String serialNumber, String IPaddress, String apiKeyHe String cloudResponse = sendCloudCommand(serialNumber, cloudCommand, value); // Log cloud error - if (!("204").equals(localResponse)) { + if (!("204").equals(cloudResponse)) { logger.warn("Cloud command {} failed.", cloudCommand); } // Restart polling initPolling(5); - return cloudResponse; } } @@ -343,9 +352,12 @@ private String sendLocalCommand(String IPaddress, String apiKeyHexString, String // Get challenge string from local fireplace String challengeHexStr = getChallengeString(IPaddress); + logger.trace("Challenge string: {} received.", challengeHexStr); // Assemble command string String commandStr = "post:command=" + command + "&value=" + value; + logger.trace("Command string: {}", commandStr); + logger.trace("API key hex string: {}", apiKeyHexString); // Concatenate apiKey, challenge, command byte[] apiKeyBytes = decodeHexString(apiKeyHexString); @@ -375,6 +387,8 @@ private String sendLocalCommand(String IPaddress, String apiKeyHexString, String String responseHexString = encodeHexString(apiApiChallengePayloadHash); + logger.trace("Username: {}", account.userName); + // Hash the username and convert to hex string byte[] usernameHash = digest.digest(account.userName.getBytes()); String userNameHexString = encodeHexString(usernameHash); @@ -400,7 +414,7 @@ private String sendLocalCommand(String IPaddress, String apiKeyHexString, String private synchronized String httpResponseContent(String url, HttpMethod method, String contentType, String content, int timeout) throws InterruptedException { - for (int retry = 0; retry <= 5; retry++) { + for (int retry = 1; retry <= 2; retry++) { try { // Initialize request to load cookies into Request request = httpRequestBuilder(url, method, timeout, contentType); @@ -443,17 +457,10 @@ private synchronized String httpResponseContent(String url, HttpMethod method, S return httpResponse.getContentAsString(); } } catch (ExecutionException | TimeoutException e) { - logger.warn("Intellifire {} error: Try: {} ", getCallingMethod(), - (commFailureCount) * 2 + (retry + 1)); + logger.warn("Intellifire {} error: Try: {}", getCallingMethod(), (commFailureCount) * 2 + (retry)); if (retry >= 2) { - if (getCallingMethod().equals("localPollFireplace") - || getCallingMethod().equals("sendLocalCommand")) { - - String test = ""; - } return ""; - } else { } } } @@ -487,13 +494,21 @@ private byte[] decodeHexString(String hexString) { return DatatypeConverter.parseHexBinary(hexString); } - public String getApiKeyProperty(Map properties) { - String serialNumber = properties.get(IntellifireBindingConstants.PROPERTY_APIKEY); - if (serialNumber != null) { - return serialNumber; - } else { - return ""; + public String getApiKeyProperty(Map properties) throws InterruptedException, IntellifireException { + String apiKey = ""; + String locationID = properties.get(IntellifireBindingConstants.PROPERTY_LOCATIONID); + if (locationID != null) { + IntellifireLocation fireplaces = getFireplaces(locationID); + if (fireplaces != null) { + for (int j = 0; j < fireplaces.fireplaces.size(); j++) { + String serialNumber = fireplaces.fireplaces.get(j).serial; + if (serialNumber.equals(properties.get(IntellifireBindingConstants.PROPERTY_SERIALNUMBER))) { + apiKey = fireplaces.fireplaces.get(j).apiKey; + } + } + } } + return apiKey; } public String getSerialNumberProperty(Map properties) { diff --git a/bundles/org.openhab.binding.intellifire/src/main/java/org/openhab/binding/intellifire/internal/handlers/IntellifireFanHandler.java b/bundles/org.openhab.binding.intellifire/src/main/java/org/openhab/binding/intellifire/internal/handlers/IntellifireFanHandler.java index 06eb1243d012f..948d51714e757 100644 --- a/bundles/org.openhab.binding.intellifire/src/main/java/org/openhab/binding/intellifire/internal/handlers/IntellifireFanHandler.java +++ b/bundles/org.openhab.binding.intellifire/src/main/java/org/openhab/binding/intellifire/internal/handlers/IntellifireFanHandler.java @@ -56,7 +56,9 @@ public void handleCommand(ChannelUID channelUID, Command command) { Bridge bridge = getBridge(); if (bridge != null && bridge.getHandler() instanceof IntellifireBridgeHandler bridgehandler) { try { + // Retrieve API Key in case it has changed since discovery String apiKey = bridgehandler.getApiKeyProperty(thing.getProperties()); + updateProperty(IntellifireBindingConstants.PROPERTY_APIKEY, apiKey); String serialNumber = bridgehandler.getSerialNumberProperty(thing.getProperties()); String ipAddress = bridgehandler.getIPAddressProperty(thing.getProperties()); String httpResponse; @@ -81,6 +83,9 @@ public void handleCommand(ChannelUID channelUID, Command command) { this.updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR); return; } + } catch (IntellifireException e) { + logger.error("Intellifire handleCommand exception: {}", e.getMessage()); + return; } catch (InterruptedException e) { logger.error("Intellifire handleCommand exception: {}", e.getMessage()); return; diff --git a/bundles/org.openhab.binding.intellifire/src/main/java/org/openhab/binding/intellifire/internal/handlers/IntellifireFireplaceHandler.java b/bundles/org.openhab.binding.intellifire/src/main/java/org/openhab/binding/intellifire/internal/handlers/IntellifireFireplaceHandler.java index 89316963fbd4d..6a95ff552312a 100644 --- a/bundles/org.openhab.binding.intellifire/src/main/java/org/openhab/binding/intellifire/internal/handlers/IntellifireFireplaceHandler.java +++ b/bundles/org.openhab.binding.intellifire/src/main/java/org/openhab/binding/intellifire/internal/handlers/IntellifireFireplaceHandler.java @@ -76,7 +76,9 @@ public void handleCommand(ChannelUID channelUID, Command command) { Bridge bridge = getBridge(); if (bridge != null && bridge.getHandler() instanceof IntellifireBridgeHandler bridgehandler) { try { + // Retrieve API Key in case it has changed since discovery String apiKey = bridgehandler.getApiKeyProperty(thing.getProperties()); + updateProperty(IntellifireBindingConstants.PROPERTY_APIKEY, apiKey); String serialNumber = bridgehandler.getSerialNumberProperty(thing.getProperties()); String ipAddress = bridgehandler.getIPAddressProperty(thing.getProperties()); String httpResponse; @@ -139,6 +141,9 @@ public void handleCommand(ChannelUID channelUID, Command command) { return; } + } catch (IntellifireException e) { + logger.error("Intellifire handleCommand exception: {}", e.getMessage()); + return; } catch (InterruptedException e) { logger.error("Intellifire handleCommand exception: {}", e.getMessage()); return; diff --git a/bundles/org.openhab.binding.intellifire/src/main/java/org/openhab/binding/intellifire/internal/handlers/IntellifireLightHandler.java b/bundles/org.openhab.binding.intellifire/src/main/java/org/openhab/binding/intellifire/internal/handlers/IntellifireLightHandler.java index 145a488317e4b..621c6a1dfad16 100644 --- a/bundles/org.openhab.binding.intellifire/src/main/java/org/openhab/binding/intellifire/internal/handlers/IntellifireLightHandler.java +++ b/bundles/org.openhab.binding.intellifire/src/main/java/org/openhab/binding/intellifire/internal/handlers/IntellifireLightHandler.java @@ -56,7 +56,9 @@ public void handleCommand(ChannelUID channelUID, Command command) { Bridge bridge = getBridge(); if (bridge != null && bridge.getHandler() instanceof IntellifireBridgeHandler bridgehandler) { try { + // Retrieve API Key in case it has changed since discovery String apiKey = bridgehandler.getApiKeyProperty(thing.getProperties()); + updateProperty(IntellifireBindingConstants.PROPERTY_APIKEY, apiKey); String serialNumber = bridgehandler.getSerialNumberProperty(thing.getProperties()); String ipAddress = bridgehandler.getIPAddressProperty(thing.getProperties()); String httpResponse; @@ -82,6 +84,9 @@ public void handleCommand(ChannelUID channelUID, Command command) { return; } + } catch (IntellifireException e) { + logger.error("Intellifire handleCommand exception: {}", e.getMessage()); + return; } catch (InterruptedException e) { logger.error("Intellifire handleCommand exception: {}", e.getMessage()); return; diff --git a/bundles/org.openhab.binding.intellifire/src/main/java/org/openhab/binding/intellifire/internal/handlers/IntellifireRemoteHandler.java b/bundles/org.openhab.binding.intellifire/src/main/java/org/openhab/binding/intellifire/internal/handlers/IntellifireRemoteHandler.java index a38666a261578..cff3afec9ad31 100644 --- a/bundles/org.openhab.binding.intellifire/src/main/java/org/openhab/binding/intellifire/internal/handlers/IntellifireRemoteHandler.java +++ b/bundles/org.openhab.binding.intellifire/src/main/java/org/openhab/binding/intellifire/internal/handlers/IntellifireRemoteHandler.java @@ -71,7 +71,9 @@ public void handleCommand(ChannelUID channelUID, Command command) { Bridge bridge = getBridge(); if (bridge != null && bridge.getHandler() instanceof IntellifireBridgeHandler bridgehandler) { try { + // Retrieve API Key in case it has changed since discovery String apiKey = bridgehandler.getApiKeyProperty(thing.getProperties()); + updateProperty(IntellifireBindingConstants.PROPERTY_APIKEY, apiKey); String serialNumber = bridgehandler.getSerialNumberProperty(thing.getProperties()); String ipAddress = bridgehandler.getIPAddressProperty(thing.getProperties()); String httpResponse; @@ -140,6 +142,9 @@ public void handleCommand(ChannelUID channelUID, Command command) { return; } + } catch (IntellifireException e) { + logger.error("Intellifire handleCommand exception: {}", e.getMessage()); + return; } catch (InterruptedException e) { logger.error("Intellifire handleCommand exception: {}", e.getMessage()); return;