@@ -95,7 +95,7 @@ public void scheduledInitialize() {
95
95
config = getConfigAs (IntellifireConfiguration .class );
96
96
97
97
try {
98
- if (login () && getUsername () && setupAccountData () && poll (IntellifireBindingConstants .CLOUD_POLLING )) {
98
+ if (login () && setupAccountData () && getUsername () && poll (IntellifireBindingConstants .CLOUD_POLLING )) {
99
99
logger .debug ("Succesfully opened connection to Intellifire's server: {} Username:{} " ,
100
100
IntellifireBindingConstants .URI_COOKIE , config .username );
101
101
initPolling (5 );
@@ -200,10 +200,20 @@ public synchronized void initPolling(int initalDelay) {
200
200
initialize ();
201
201
return ;
202
202
}
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
+ }
207
217
}
208
218
if (this .thing .getStatus () != ThingStatus .ONLINE ) {
209
219
commFailureCount = 0 ;
@@ -248,10 +258,15 @@ public boolean poll(boolean cloudPool) throws IntellifireException, InterruptedE
248
258
} else {
249
259
// Local Poll
250
260
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
+ }
254
268
} else {
269
+ logger .error ("Intellifire local poll failed. Invalid local IP Address received from cloud." );
255
270
failureFlag = true ;
256
271
}
257
272
}
@@ -272,12 +287,7 @@ public boolean poll(boolean cloudPool) throws IntellifireException, InterruptedE
272
287
}
273
288
}
274
289
}
275
-
276
- if (failureFlag ) {
277
- return false ;
278
- } else {
279
- return true ;
280
- }
290
+ return !failureFlag ;
281
291
}
282
292
283
293
public synchronized @ Nullable IntellifirePollData cloudPollFireplace (String serialNumber )
@@ -318,12 +328,11 @@ public String sendCommand(String serialNumber, String IPaddress, String apiKeyHe
318
328
String cloudResponse = sendCloudCommand (serialNumber , cloudCommand , value );
319
329
320
330
// Log cloud error
321
- if (!("204" ).equals (localResponse )) {
331
+ if (!("204" ).equals (cloudResponse )) {
322
332
logger .warn ("Cloud command {} failed." , cloudCommand );
323
333
}
324
334
// Restart polling
325
335
initPolling (5 );
326
-
327
336
return cloudResponse ;
328
337
}
329
338
}
@@ -343,9 +352,12 @@ private String sendLocalCommand(String IPaddress, String apiKeyHexString, String
343
352
344
353
// Get challenge string from local fireplace
345
354
String challengeHexStr = getChallengeString (IPaddress );
355
+ logger .trace ("Challenge string: {} received." , challengeHexStr );
346
356
347
357
// Assemble command string
348
358
String commandStr = "post:command=" + command + "&value=" + value ;
359
+ logger .trace ("Command string: {}" , commandStr );
360
+ logger .trace ("API key hex string: {}" , apiKeyHexString );
349
361
350
362
// Concatenate apiKey, challenge, command
351
363
byte [] apiKeyBytes = decodeHexString (apiKeyHexString );
@@ -375,6 +387,8 @@ private String sendLocalCommand(String IPaddress, String apiKeyHexString, String
375
387
376
388
String responseHexString = encodeHexString (apiApiChallengePayloadHash );
377
389
390
+ logger .trace ("Username: {}" , account .userName );
391
+
378
392
// Hash the username and convert to hex string
379
393
byte [] usernameHash = digest .digest (account .userName .getBytes ());
380
394
String userNameHexString = encodeHexString (usernameHash );
@@ -400,7 +414,7 @@ private String sendLocalCommand(String IPaddress, String apiKeyHexString, String
400
414
401
415
private synchronized String httpResponseContent (String url , HttpMethod method , String contentType , String content ,
402
416
int timeout ) throws InterruptedException {
403
- for (int retry = 0 ; retry <= 5 ; retry ++) {
417
+ for (int retry = 1 ; retry <= 2 ; retry ++) {
404
418
try {
405
419
// Initialize request to load cookies into
406
420
Request request = httpRequestBuilder (url , method , timeout , contentType );
@@ -443,17 +457,10 @@ private synchronized String httpResponseContent(String url, HttpMethod method, S
443
457
return httpResponse .getContentAsString ();
444
458
}
445
459
} 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 ));
448
461
449
462
if (retry >= 2 ) {
450
- if (getCallingMethod ().equals ("localPollFireplace" )
451
- || getCallingMethod ().equals ("sendLocalCommand" )) {
452
-
453
- String test = "" ;
454
- }
455
463
return "" ;
456
- } else {
457
464
}
458
465
}
459
466
}
@@ -487,13 +494,21 @@ private byte[] decodeHexString(String hexString) {
487
494
return DatatypeConverter .parseHexBinary (hexString );
488
495
}
489
496
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
+ }
496
510
}
511
+ return apiKey ;
497
512
}
498
513
499
514
public String getSerialNumberProperty (Map <String , String > properties ) {
0 commit comments