13
13
package org .openhab .binding .tapocontrol .internal .api ;
14
14
15
15
import static org .openhab .binding .tapocontrol .internal .constants .TapoBindingSettings .*;
16
- import static org .openhab .binding .tapocontrol .internal .constants .TapoErrorConstants .*;
16
+ import static org .openhab .binding .tapocontrol .internal .constants .TapoErrorCode .*;
17
17
import static org .openhab .binding .tapocontrol .internal .helpers .TapoUtils .*;
18
18
19
19
import java .util .concurrent .TimeUnit ;
@@ -167,7 +167,7 @@ protected String createHandshake() {
167
167
}
168
168
} catch (Exception e ) {
169
169
logger .debug ("({}) could not createHandshake: {}" , uid , e .toString ());
170
- handleError (new TapoErrorHandler (ERR_HAND_SHAKE_FAILED , "could not createHandshake" ));
170
+ handleError (new TapoErrorHandler (ERR_API_HAND_SHAKE_FAILED , "could not createHandshake" ));
171
171
}
172
172
return cookie ;
173
173
}
@@ -186,7 +186,7 @@ private String getKeyFromResponse(ContentResponse response) {
186
186
return jsonObjectToString (jsonObj .getAsJsonObject ("result" ), "key" );
187
187
} else {
188
188
logger .warn ("({}) could not getKeyFromResponse '{}'" , uid , rBody );
189
- handleError (new TapoErrorHandler (ERR_HAND_SHAKE_FAILED , "could not getKeyFromResponse" ));
189
+ handleError (new TapoErrorHandler (ERR_API_HAND_SHAKE_FAILED , "could not getKeyFromResponse" ));
190
190
}
191
191
return "" ;
192
192
}
@@ -204,7 +204,7 @@ private String getCookieFromResponse(ContentResponse response) {
204
204
logger .trace ("({}) got cookie: '{}'" , uid , cookie );
205
205
} catch (Exception e ) {
206
206
logger .warn ("({}) could not getCookieFromResponse" , uid );
207
- handleError (new TapoErrorHandler (ERR_HAND_SHAKE_FAILED , "could not getCookieFromResponse" ));
207
+ handleError (new TapoErrorHandler (ERR_API_HAND_SHAKE_FAILED , "could not getCookieFromResponse" ));
208
208
}
209
209
return cookie ;
210
210
}
@@ -258,7 +258,7 @@ private String getTokenFromResponse(@Nullable ContentResponse response) {
258
258
/* get errocode (0=success) */
259
259
JsonObject jsonObject = GSON .fromJson (decryptedResponse , JsonObject .class );
260
260
if (jsonObject != null ) {
261
- Integer errorCode = jsonObjectToInt (jsonObject , "error_code" , ERR_JSON_DECODE_FAIL );
261
+ Integer errorCode = jsonObjectToInt (jsonObject , "error_code" , ERR_API_JSON_DECODE_FAIL . getCode () );
262
262
if (errorCode == 0 ) {
263
263
/* return result if set / else request was successful */
264
264
result = jsonObjectToString (jsonObject .getAsJsonObject ("result" ), "token" );
@@ -269,11 +269,11 @@ private String getTokenFromResponse(@Nullable ContentResponse response) {
269
269
}
270
270
} else {
271
271
logger .debug ("({}) unexpected json-response '{}'" , uid , decryptedResponse );
272
- tapoError .raiseError (ERR_JSON_ENCODE_FAIL , "could not get token" );
272
+ tapoError .raiseError (ERR_API_JSON_ENCODE_FAIL , "could not get token" );
273
273
}
274
274
} else {
275
275
logger .debug ("({}) invalid response while login" , uid );
276
- tapoError .raiseError (ERR_HTTP_RESPONSE , "invalid response while login" );
276
+ tapoError .raiseError (ERR_BINDING_HTTP_RESPONSE , "invalid response while login" );
277
277
}
278
278
/* handle error */
279
279
if (tapoError .hasError ()) {
@@ -315,7 +315,7 @@ protected ContentResponse sendRequest(String url, String payload) {
315
315
handleError (new TapoErrorHandler (e ));
316
316
} catch (TimeoutException e ) {
317
317
logger .debug ("({}) sending request timeout: {}" , uid , e .toString ());
318
- handleError (new TapoErrorHandler (ERR_CONNECT_TIMEOUT , e .toString ()));
318
+ handleError (new TapoErrorHandler (ERR_BINDING_CONNECT_TIMEOUT , e .toString ()));
319
319
} catch (Exception e ) {
320
320
logger .debug ("({}) sending request failed: {}" , uid , e .toString ());
321
321
handleError (new TapoErrorHandler (e ));
@@ -355,14 +355,14 @@ public void onComplete(Result result) {
355
355
String errorMessage = getValueOrDefault (e .getMessage (), "" );
356
356
if (e instanceof TimeoutException ) {
357
357
logger .debug ("({}) sendAsyncRequest timeout'{}'" , uid , errorMessage );
358
- handleError (new TapoErrorHandler (ERR_CONNECT_TIMEOUT , errorMessage ));
358
+ handleError (new TapoErrorHandler (ERR_BINDING_CONNECT_TIMEOUT , errorMessage ));
359
359
} else {
360
360
logger .debug ("({}) sendAsyncRequest failed'{}'" , uid , errorMessage );
361
361
handleError (new TapoErrorHandler (new Exception (e ), errorMessage ));
362
362
}
363
363
} else if (response .getStatus () != 200 ) {
364
364
logger .debug ("({}) sendAsyncRequest response error'{}'" , uid , response .getStatus ());
365
- handleError (new TapoErrorHandler (ERR_HTTP_RESPONSE , getContentAsString ()));
365
+ handleError (new TapoErrorHandler (ERR_BINDING_HTTP_RESPONSE , getContentAsString ()));
366
366
} else {
367
367
/* request successful */
368
368
String rBody = getContentAsString ();
@@ -411,10 +411,10 @@ protected Integer getErrorCode(@Nullable ContentResponse response) {
411
411
String responseBody = response .getContentAsString ();
412
412
return getErrorCode (responseBody );
413
413
} else {
414
- return ERR_HTTP_RESPONSE ;
414
+ return ERR_BINDING_HTTP_RESPONSE . getCode () ;
415
415
}
416
416
} catch (Exception e ) {
417
- return ERR_HTTP_RESPONSE ;
417
+ return ERR_BINDING_HTTP_RESPONSE . getCode () ;
418
418
}
419
419
}
420
420
@@ -428,7 +428,7 @@ protected Integer getErrorCode(String responseBody) {
428
428
try {
429
429
JsonObject jsonObject = GSON .fromJson (responseBody , JsonObject .class );
430
430
/* get errocode (0=success) */
431
- Integer errorCode = jsonObjectToInt (jsonObject , "error_code" , ERR_JSON_DECODE_FAIL );
431
+ Integer errorCode = jsonObjectToInt (jsonObject , "error_code" , ERR_API_JSON_DECODE_FAIL . getCode () );
432
432
if (errorCode == 0 ) {
433
433
return 0 ;
434
434
} else {
@@ -437,7 +437,7 @@ protected Integer getErrorCode(String responseBody) {
437
437
return errorCode ;
438
438
}
439
439
} catch (Exception e ) {
440
- return ERR_HTTP_RESPONSE ;
440
+ return ERR_BINDING_HTTP_RESPONSE . getCode () ;
441
441
}
442
442
}
443
443
@@ -451,7 +451,7 @@ protected Boolean hasErrorCode(String responseBody) {
451
451
if (isValidJson (responseBody )) {
452
452
JsonObject jsonObject = GSON .fromJson (responseBody , JsonObject .class );
453
453
/* get errocode (0=success) */
454
- Integer errorCode = jsonObjectToInt (jsonObject , "error_code" , ERR_JSON_DECODE_FAIL );
454
+ Integer errorCode = jsonObjectToInt (jsonObject , "error_code" , ERR_API_JSON_DECODE_FAIL . getCode () );
455
455
if (errorCode > 0 ) {
456
456
return true ;
457
457
}
@@ -491,7 +491,7 @@ protected String decryptResponse(String responseBody) {
491
491
String encryptedResponse = jsonObjectToString (jsonObject .getAsJsonObject ("result" ), "response" );
492
492
return tapoCipher .decode (encryptedResponse );
493
493
} else {
494
- handleError (new TapoErrorHandler (ERR_JSON_DECODE_FAIL ));
494
+ handleError (new TapoErrorHandler (ERR_API_JSON_DECODE_FAIL ));
495
495
}
496
496
} catch (Exception ex ) {
497
497
logger .debug ("({}) exception '{}' decryptingResponse: '{}'" , uid , ex .toString (), responseBody );
@@ -549,7 +549,7 @@ public Boolean loggedIn(Boolean raiseError) {
549
549
} else {
550
550
logger .trace ("({}) not logged in" , uid );
551
551
if (raiseError ) {
552
- handleError (new TapoErrorHandler (ERR_LOGIN ));
552
+ handleError (new TapoErrorHandler (ERR_API_LOGIN ));
553
553
}
554
554
return false ;
555
555
}
0 commit comments