Skip to content

Commit

Permalink
Updated program flow. Added APIkey retreival on every command.
Browse files Browse the repository at this point in the history
  • Loading branch information
matchews committed Dec 23, 2024
1 parent 1291c90 commit 7ba41cc
Show file tree
Hide file tree
Showing 8 changed files with 71 additions and 51 deletions.
1 change: 1 addition & 0 deletions bundles/org.openhab.binding.intellifire/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ _*.sitemap examples are optional._
```java
Example thing configuration goes here.
```

### Item Configuration

```java
Expand Down
18 changes: 1 addition & 17 deletions bundles/org.openhab.binding.intellifire/ToDo.txt
Original file line number Diff line number Diff line change
@@ -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)

Expand All @@ -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

Expand Down
6 changes: 3 additions & 3 deletions bundles/org.openhab.binding.intellifire/pom.xml
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<project xmlns="https://maven.apache.org/POM/4.0.0" xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="https://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">

<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>org.openhab.addons.bundles</groupId>
<artifactId>org.openhab.addons.reactor.bundles</artifactId>
<version>4.3.0-SNAPSHOT</version>
<version>5.0.0-SNAPSHOT</version>
</parent>

<artifactId>org.openhab.binding.intellifire</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
}
Expand All @@ -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)
Expand Down Expand Up @@ -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;
}
}
Expand All @@ -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);
Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand Down Expand Up @@ -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 {
}
}
}
Expand Down Expand Up @@ -487,13 +494,21 @@ private byte[] decodeHexString(String hexString) {
return DatatypeConverter.parseHexBinary(hexString);
}

public String getApiKeyProperty(Map<String, String> properties) {
String serialNumber = properties.get(IntellifireBindingConstants.PROPERTY_APIKEY);
if (serialNumber != null) {
return serialNumber;
} else {
return "";
public String getApiKeyProperty(Map<String, String> 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<String, String> properties) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit 7ba41cc

Please sign in to comment.