Skip to content

Commit 7ba41cc

Browse files
committed
Updated program flow. Added APIkey retreival on every command.
1 parent 1291c90 commit 7ba41cc

File tree

8 files changed

+71
-51
lines changed

8 files changed

+71
-51
lines changed

bundles/org.openhab.binding.intellifire/README.md

+1
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ _*.sitemap examples are optional._
7676
```java
7777
Example thing configuration goes here.
7878
```
79+
7980
### Item Configuration
8081

8182
```java

bundles/org.openhab.binding.intellifire/ToDo.txt

+1-17
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11

2-
Things online before succesful local poll?
2+
Things online before successfull local poll?
33

44
Combine light and fan into fireplace thing? (light:light, fan:fan)
55

@@ -13,27 +13,11 @@ verify SSL certificate
1313
Send Command failure causes thing offline. Recovery?
1414

1515
******
16-
Test local IP address change.
17-
IP Address successfully updates on initialization.
18-
What to do if localPolling fails?
19-
Try cloudPolling
20-
Or retry out and re-initialize?
21-
22-
23-
Initialize (including one cloud poll)
24-
Local Poll
25-
Retry local 2-3 times
26-
Retry cloud 2-3 times (if this succeeds, go back to local polling?)
27-
Initialize
28-
29-
3016

3117
HttpResponse error messages need to differentiate between internet connection and local connection.
3218
Might need a config option to select local, cloud, or both?
3319
******
3420

35-
getusername used in python code?
36-
3721

3822
Limit outgoing command frequency so we don't overload the server
3923

bundles/org.openhab.binding.intellifire/pom.xml

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
<?xml version="1.0" encoding="UTF-8"?>
2-
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0"
3-
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
2+
<project xmlns="https://maven.apache.org/POM/4.0.0" xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="https://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
44

55
<modelVersion>4.0.0</modelVersion>
66

77
<parent>
88
<groupId>org.openhab.addons.bundles</groupId>
99
<artifactId>org.openhab.addons.reactor.bundles</artifactId>
10-
<version>4.3.0-SNAPSHOT</version>
10+
<version>5.0.0-SNAPSHOT</version>
1111
</parent>
1212

1313
<artifactId>org.openhab.binding.intellifire</artifactId>

bundles/org.openhab.binding.intellifire/src/main/java/org/openhab/binding/intellifire/internal/handlers/IntellifireBridgeHandler.java

+46-31
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ public void scheduledInitialize() {
9595
config = getConfigAs(IntellifireConfiguration.class);
9696

9797
try {
98-
if (login() && getUsername() && setupAccountData() && poll(IntellifireBindingConstants.CLOUD_POLLING)) {
98+
if (login() && setupAccountData() && getUsername() && poll(IntellifireBindingConstants.CLOUD_POLLING)) {
9999
logger.debug("Succesfully opened connection to Intellifire's server: {} Username:{} ",
100100
IntellifireBindingConstants.URI_COOKIE, config.username);
101101
initPolling(5);
@@ -200,10 +200,20 @@ public synchronized void initPolling(int initalDelay) {
200200
initialize();
201201
return;
202202
}
203-
204-
if (!(poll(IntellifireBindingConstants.LOCAL_POLLING))) {
205-
commFailureCount++;
206-
return;
203+
if (commFailureCount < 2) {
204+
if (!(poll(IntellifireBindingConstants.LOCAL_POLLING))) {
205+
commFailureCount++;
206+
return;
207+
} else {
208+
commFailureCount = 0;
209+
}
210+
} else {
211+
if (!(poll(IntellifireBindingConstants.CLOUD_POLLING))) {
212+
commFailureCount++;
213+
return;
214+
} else {
215+
commFailureCount = 0;
216+
}
207217
}
208218
if (this.thing.getStatus() != ThingStatus.ONLINE) {
209219
commFailureCount = 0;
@@ -248,10 +258,15 @@ public boolean poll(boolean cloudPool) throws IntellifireException, InterruptedE
248258
} else {
249259
// Local Poll
250260
String ipAddress = account.getIPAddress(serialNumber);
251-
IntellifirePollData localPollData = localPollFireplace(ipAddress);
252-
if (localPollData != null) {
253-
account.locations.get(i).fireplaces.fireplaces.get(j).pollData = localPollData;
261+
if (!"".equals(ipAddress)) {
262+
IntellifirePollData localPollData = localPollFireplace(ipAddress);
263+
if (localPollData != null) {
264+
account.locations.get(i).fireplaces.fireplaces.get(j).pollData = localPollData;
265+
} else {
266+
failureFlag = true;
267+
}
254268
} else {
269+
logger.error("Intellifire local poll failed. Invalid local IP Address received from cloud.");
255270
failureFlag = true;
256271
}
257272
}
@@ -272,12 +287,7 @@ public boolean poll(boolean cloudPool) throws IntellifireException, InterruptedE
272287
}
273288
}
274289
}
275-
276-
if (failureFlag) {
277-
return false;
278-
} else {
279-
return true;
280-
}
290+
return !failureFlag;
281291
}
282292

283293
public synchronized @Nullable IntellifirePollData cloudPollFireplace(String serialNumber)
@@ -318,12 +328,11 @@ public String sendCommand(String serialNumber, String IPaddress, String apiKeyHe
318328
String cloudResponse = sendCloudCommand(serialNumber, cloudCommand, value);
319329

320330
// Log cloud error
321-
if (!("204").equals(localResponse)) {
331+
if (!("204").equals(cloudResponse)) {
322332
logger.warn("Cloud command {} failed.", cloudCommand);
323333
}
324334
// Restart polling
325335
initPolling(5);
326-
327336
return cloudResponse;
328337
}
329338
}
@@ -343,9 +352,12 @@ private String sendLocalCommand(String IPaddress, String apiKeyHexString, String
343352

344353
// Get challenge string from local fireplace
345354
String challengeHexStr = getChallengeString(IPaddress);
355+
logger.trace("Challenge string: {} received.", challengeHexStr);
346356

347357
// Assemble command string
348358
String commandStr = "post:command=" + command + "&value=" + value;
359+
logger.trace("Command string: {}", commandStr);
360+
logger.trace("API key hex string: {}", apiKeyHexString);
349361

350362
// Concatenate apiKey, challenge, command
351363
byte[] apiKeyBytes = decodeHexString(apiKeyHexString);
@@ -375,6 +387,8 @@ private String sendLocalCommand(String IPaddress, String apiKeyHexString, String
375387

376388
String responseHexString = encodeHexString(apiApiChallengePayloadHash);
377389

390+
logger.trace("Username: {}", account.userName);
391+
378392
// Hash the username and convert to hex string
379393
byte[] usernameHash = digest.digest(account.userName.getBytes());
380394
String userNameHexString = encodeHexString(usernameHash);
@@ -400,7 +414,7 @@ private String sendLocalCommand(String IPaddress, String apiKeyHexString, String
400414

401415
private synchronized String httpResponseContent(String url, HttpMethod method, String contentType, String content,
402416
int timeout) throws InterruptedException {
403-
for (int retry = 0; retry <= 5; retry++) {
417+
for (int retry = 1; retry <= 2; retry++) {
404418
try {
405419
// Initialize request to load cookies into
406420
Request request = httpRequestBuilder(url, method, timeout, contentType);
@@ -443,17 +457,10 @@ private synchronized String httpResponseContent(String url, HttpMethod method, S
443457
return httpResponse.getContentAsString();
444458
}
445459
} catch (ExecutionException | TimeoutException e) {
446-
logger.warn("Intellifire {} error: Try: {} ", getCallingMethod(),
447-
(commFailureCount) * 2 + (retry + 1));
460+
logger.warn("Intellifire {} error: Try: {}", getCallingMethod(), (commFailureCount) * 2 + (retry));
448461

449462
if (retry >= 2) {
450-
if (getCallingMethod().equals("localPollFireplace")
451-
|| getCallingMethod().equals("sendLocalCommand")) {
452-
453-
String test = "";
454-
}
455463
return "";
456-
} else {
457464
}
458465
}
459466
}
@@ -487,13 +494,21 @@ private byte[] decodeHexString(String hexString) {
487494
return DatatypeConverter.parseHexBinary(hexString);
488495
}
489496

490-
public String getApiKeyProperty(Map<String, String> properties) {
491-
String serialNumber = properties.get(IntellifireBindingConstants.PROPERTY_APIKEY);
492-
if (serialNumber != null) {
493-
return serialNumber;
494-
} else {
495-
return "";
497+
public String getApiKeyProperty(Map<String, String> properties) throws InterruptedException, IntellifireException {
498+
String apiKey = "";
499+
String locationID = properties.get(IntellifireBindingConstants.PROPERTY_LOCATIONID);
500+
if (locationID != null) {
501+
IntellifireLocation fireplaces = getFireplaces(locationID);
502+
if (fireplaces != null) {
503+
for (int j = 0; j < fireplaces.fireplaces.size(); j++) {
504+
String serialNumber = fireplaces.fireplaces.get(j).serial;
505+
if (serialNumber.equals(properties.get(IntellifireBindingConstants.PROPERTY_SERIALNUMBER))) {
506+
apiKey = fireplaces.fireplaces.get(j).apiKey;
507+
}
508+
}
509+
}
496510
}
511+
return apiKey;
497512
}
498513

499514
public String getSerialNumberProperty(Map<String, String> properties) {

bundles/org.openhab.binding.intellifire/src/main/java/org/openhab/binding/intellifire/internal/handlers/IntellifireFanHandler.java

+5
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,9 @@ public void handleCommand(ChannelUID channelUID, Command command) {
5656
Bridge bridge = getBridge();
5757
if (bridge != null && bridge.getHandler() instanceof IntellifireBridgeHandler bridgehandler) {
5858
try {
59+
// Retrieve API Key in case it has changed since discovery
5960
String apiKey = bridgehandler.getApiKeyProperty(thing.getProperties());
61+
updateProperty(IntellifireBindingConstants.PROPERTY_APIKEY, apiKey);
6062
String serialNumber = bridgehandler.getSerialNumberProperty(thing.getProperties());
6163
String ipAddress = bridgehandler.getIPAddressProperty(thing.getProperties());
6264
String httpResponse;
@@ -81,6 +83,9 @@ public void handleCommand(ChannelUID channelUID, Command command) {
8183
this.updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR);
8284
return;
8385
}
86+
} catch (IntellifireException e) {
87+
logger.error("Intellifire handleCommand exception: {}", e.getMessage());
88+
return;
8489
} catch (InterruptedException e) {
8590
logger.error("Intellifire handleCommand exception: {}", e.getMessage());
8691
return;

bundles/org.openhab.binding.intellifire/src/main/java/org/openhab/binding/intellifire/internal/handlers/IntellifireFireplaceHandler.java

+5
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,9 @@ public void handleCommand(ChannelUID channelUID, Command command) {
7676
Bridge bridge = getBridge();
7777
if (bridge != null && bridge.getHandler() instanceof IntellifireBridgeHandler bridgehandler) {
7878
try {
79+
// Retrieve API Key in case it has changed since discovery
7980
String apiKey = bridgehandler.getApiKeyProperty(thing.getProperties());
81+
updateProperty(IntellifireBindingConstants.PROPERTY_APIKEY, apiKey);
8082
String serialNumber = bridgehandler.getSerialNumberProperty(thing.getProperties());
8183
String ipAddress = bridgehandler.getIPAddressProperty(thing.getProperties());
8284
String httpResponse;
@@ -139,6 +141,9 @@ public void handleCommand(ChannelUID channelUID, Command command) {
139141
return;
140142
}
141143

144+
} catch (IntellifireException e) {
145+
logger.error("Intellifire handleCommand exception: {}", e.getMessage());
146+
return;
142147
} catch (InterruptedException e) {
143148
logger.error("Intellifire handleCommand exception: {}", e.getMessage());
144149
return;

bundles/org.openhab.binding.intellifire/src/main/java/org/openhab/binding/intellifire/internal/handlers/IntellifireLightHandler.java

+5
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,9 @@ public void handleCommand(ChannelUID channelUID, Command command) {
5656
Bridge bridge = getBridge();
5757
if (bridge != null && bridge.getHandler() instanceof IntellifireBridgeHandler bridgehandler) {
5858
try {
59+
// Retrieve API Key in case it has changed since discovery
5960
String apiKey = bridgehandler.getApiKeyProperty(thing.getProperties());
61+
updateProperty(IntellifireBindingConstants.PROPERTY_APIKEY, apiKey);
6062
String serialNumber = bridgehandler.getSerialNumberProperty(thing.getProperties());
6163
String ipAddress = bridgehandler.getIPAddressProperty(thing.getProperties());
6264
String httpResponse;
@@ -82,6 +84,9 @@ public void handleCommand(ChannelUID channelUID, Command command) {
8284
return;
8385
}
8486

87+
} catch (IntellifireException e) {
88+
logger.error("Intellifire handleCommand exception: {}", e.getMessage());
89+
return;
8590
} catch (InterruptedException e) {
8691
logger.error("Intellifire handleCommand exception: {}", e.getMessage());
8792
return;

bundles/org.openhab.binding.intellifire/src/main/java/org/openhab/binding/intellifire/internal/handlers/IntellifireRemoteHandler.java

+5
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,9 @@ public void handleCommand(ChannelUID channelUID, Command command) {
7171
Bridge bridge = getBridge();
7272
if (bridge != null && bridge.getHandler() instanceof IntellifireBridgeHandler bridgehandler) {
7373
try {
74+
// Retrieve API Key in case it has changed since discovery
7475
String apiKey = bridgehandler.getApiKeyProperty(thing.getProperties());
76+
updateProperty(IntellifireBindingConstants.PROPERTY_APIKEY, apiKey);
7577
String serialNumber = bridgehandler.getSerialNumberProperty(thing.getProperties());
7678
String ipAddress = bridgehandler.getIPAddressProperty(thing.getProperties());
7779
String httpResponse;
@@ -140,6 +142,9 @@ public void handleCommand(ChannelUID channelUID, Command command) {
140142
return;
141143
}
142144

145+
} catch (IntellifireException e) {
146+
logger.error("Intellifire handleCommand exception: {}", e.getMessage());
147+
return;
143148
} catch (InterruptedException e) {
144149
logger.error("Intellifire handleCommand exception: {}", e.getMessage());
145150
return;

0 commit comments

Comments
 (0)