From 9bbcd36c8bd39e3d102bec5838607a79ee898762 Mon Sep 17 00:00:00 2001 From: Christian Wild Date: Sun, 30 Apr 2023 20:27:39 +0200 Subject: [PATCH] [tapocontrol] Moved error messages to i18n moved constants from class to enum Signed-off-by: Christian Wild --- .../internal/api/TapoCloudConnector.java | 2 +- .../internal/api/TapoDeviceConnector.java | 4 +- .../internal/api/TapoDeviceHttpApi.java | 14 +-- .../internal/constants/TapoErrorCode.java | 119 ++++++++++++++++++ .../constants/TapoErrorConstants.java | 104 --------------- .../internal/constants/TapoErrorType.java | 29 +++++ .../internal/device/TapoDevice.java | 35 ++++-- .../internal/helpers/TapoErrorHandler.java | 97 ++++++++------ .../OH-INF/i18n/tapocontrol.properties | 73 ++++++----- 9 files changed, 273 insertions(+), 204 deletions(-) create mode 100644 bundles/org.openhab.binding.tapocontrol/src/main/java/org/openhab/binding/tapocontrol/internal/constants/TapoErrorCode.java delete mode 100644 bundles/org.openhab.binding.tapocontrol/src/main/java/org/openhab/binding/tapocontrol/internal/constants/TapoErrorConstants.java create mode 100644 bundles/org.openhab.binding.tapocontrol/src/main/java/org/openhab/binding/tapocontrol/internal/constants/TapoErrorType.java diff --git a/bundles/org.openhab.binding.tapocontrol/src/main/java/org/openhab/binding/tapocontrol/internal/api/TapoCloudConnector.java b/bundles/org.openhab.binding.tapocontrol/src/main/java/org/openhab/binding/tapocontrol/internal/api/TapoCloudConnector.java index 583368d1ebb8b..81cf005e91128 100644 --- a/bundles/org.openhab.binding.tapocontrol/src/main/java/org/openhab/binding/tapocontrol/internal/api/TapoCloudConnector.java +++ b/bundles/org.openhab.binding.tapocontrol/src/main/java/org/openhab/binding/tapocontrol/internal/api/TapoCloudConnector.java @@ -13,7 +13,7 @@ package org.openhab.binding.tapocontrol.internal.api; import static org.openhab.binding.tapocontrol.internal.constants.TapoBindingSettings.*; -import static org.openhab.binding.tapocontrol.internal.constants.TapoErrorConstants.*; +import static org.openhab.binding.tapocontrol.internal.constants.TapoErrorCode.*; import java.util.UUID; import java.util.concurrent.TimeoutException; diff --git a/bundles/org.openhab.binding.tapocontrol/src/main/java/org/openhab/binding/tapocontrol/internal/api/TapoDeviceConnector.java b/bundles/org.openhab.binding.tapocontrol/src/main/java/org/openhab/binding/tapocontrol/internal/api/TapoDeviceConnector.java index 3988a4681361a..0913654bc7dd5 100644 --- a/bundles/org.openhab.binding.tapocontrol/src/main/java/org/openhab/binding/tapocontrol/internal/api/TapoDeviceConnector.java +++ b/bundles/org.openhab.binding.tapocontrol/src/main/java/org/openhab/binding/tapocontrol/internal/api/TapoDeviceConnector.java @@ -13,7 +13,7 @@ package org.openhab.binding.tapocontrol.internal.api; import static org.openhab.binding.tapocontrol.internal.constants.TapoBindingSettings.*; -import static org.openhab.binding.tapocontrol.internal.constants.TapoErrorConstants.*; +import static org.openhab.binding.tapocontrol.internal.constants.TapoErrorCode.*; import static org.openhab.binding.tapocontrol.internal.constants.TapoThingConstants.*; import static org.openhab.binding.tapocontrol.internal.helpers.TapoUtils.jsonObjectToInt; @@ -296,7 +296,7 @@ protected void sendSecurePasstrhroug(String payload, String command) { @Override protected void handleSuccessResponse(String responseBody) { JsonObject jsnResult = getJsonFromResponse(responseBody); - Integer errorCode = jsonObjectToInt(jsnResult, "error_code", ERR_API_JSON_DECODE_FAIL); + Integer errorCode = jsonObjectToInt(jsnResult, "error_code", ERR_API_JSON_DECODE_FAIL.getCode()); if (errorCode != 0) { logger.debug("({}) set deviceInfo not successful: {}", uid, jsnResult); this.device.handleConnectionState(); diff --git a/bundles/org.openhab.binding.tapocontrol/src/main/java/org/openhab/binding/tapocontrol/internal/api/TapoDeviceHttpApi.java b/bundles/org.openhab.binding.tapocontrol/src/main/java/org/openhab/binding/tapocontrol/internal/api/TapoDeviceHttpApi.java index 47f0e742bb435..26ed25aebaee1 100644 --- a/bundles/org.openhab.binding.tapocontrol/src/main/java/org/openhab/binding/tapocontrol/internal/api/TapoDeviceHttpApi.java +++ b/bundles/org.openhab.binding.tapocontrol/src/main/java/org/openhab/binding/tapocontrol/internal/api/TapoDeviceHttpApi.java @@ -13,7 +13,7 @@ package org.openhab.binding.tapocontrol.internal.api; import static org.openhab.binding.tapocontrol.internal.constants.TapoBindingSettings.*; -import static org.openhab.binding.tapocontrol.internal.constants.TapoErrorConstants.*; +import static org.openhab.binding.tapocontrol.internal.constants.TapoErrorCode.*; import static org.openhab.binding.tapocontrol.internal.helpers.TapoUtils.*; import java.util.concurrent.TimeUnit; @@ -258,7 +258,7 @@ private String getTokenFromResponse(@Nullable ContentResponse response) { /* get errocode (0=success) */ JsonObject jsonObject = GSON.fromJson(decryptedResponse, JsonObject.class); if (jsonObject != null) { - Integer errorCode = jsonObjectToInt(jsonObject, "error_code", ERR_API_JSON_DECODE_FAIL); + Integer errorCode = jsonObjectToInt(jsonObject, "error_code", ERR_API_JSON_DECODE_FAIL.getCode()); if (errorCode == 0) { /* return result if set / else request was successful */ result = jsonObjectToString(jsonObject.getAsJsonObject("result"), "token"); @@ -411,10 +411,10 @@ protected Integer getErrorCode(@Nullable ContentResponse response) { String responseBody = response.getContentAsString(); return getErrorCode(responseBody); } else { - return ERR_BINDING_HTTP_RESPONSE; + return ERR_BINDING_HTTP_RESPONSE.getCode(); } } catch (Exception e) { - return ERR_BINDING_HTTP_RESPONSE; + return ERR_BINDING_HTTP_RESPONSE.getCode(); } } @@ -428,7 +428,7 @@ protected Integer getErrorCode(String responseBody) { try { JsonObject jsonObject = GSON.fromJson(responseBody, JsonObject.class); /* get errocode (0=success) */ - Integer errorCode = jsonObjectToInt(jsonObject, "error_code", ERR_API_JSON_DECODE_FAIL); + Integer errorCode = jsonObjectToInt(jsonObject, "error_code", ERR_API_JSON_DECODE_FAIL.getCode()); if (errorCode == 0) { return 0; } else { @@ -437,7 +437,7 @@ protected Integer getErrorCode(String responseBody) { return errorCode; } } catch (Exception e) { - return ERR_BINDING_HTTP_RESPONSE; + return ERR_BINDING_HTTP_RESPONSE.getCode(); } } @@ -451,7 +451,7 @@ protected Boolean hasErrorCode(String responseBody) { if (isValidJson(responseBody)) { JsonObject jsonObject = GSON.fromJson(responseBody, JsonObject.class); /* get errocode (0=success) */ - Integer errorCode = jsonObjectToInt(jsonObject, "error_code", ERR_API_JSON_DECODE_FAIL); + Integer errorCode = jsonObjectToInt(jsonObject, "error_code", ERR_API_JSON_DECODE_FAIL.getCode()); if (errorCode > 0) { return true; } diff --git a/bundles/org.openhab.binding.tapocontrol/src/main/java/org/openhab/binding/tapocontrol/internal/constants/TapoErrorCode.java b/bundles/org.openhab.binding.tapocontrol/src/main/java/org/openhab/binding/tapocontrol/internal/constants/TapoErrorCode.java new file mode 100644 index 0000000000000..a96b8f23d007c --- /dev/null +++ b/bundles/org.openhab.binding.tapocontrol/src/main/java/org/openhab/binding/tapocontrol/internal/constants/TapoErrorCode.java @@ -0,0 +1,119 @@ +/** + * Copyright (c) 2010-2023 Contributors to the openHAB project + * + * See the NOTICE file(s) distributed with this work for additional + * information. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License 2.0 which is available at + * http://www.eclipse.org/legal/epl-2.0 + * + * SPDX-License-Identifier: EPL-2.0 + */ +package org.openhab.binding.tapocontrol.internal.constants; + +import org.eclipse.jdt.annotation.NonNullByDefault; + +/** + * The {@link TapoErrorCode} enum lists known errorcodes can be received or thrown by binding + * + * @author Christian Wild - Initial contribution + */ +@NonNullByDefault +public enum TapoErrorCode { + NO_ERROR(0), + ERR_UNKNOWN(-1, TapoErrorType.UNKNOWN), + ERR_API_SESSION_TIMEOUT(9999, TapoErrorType.COMMUNICATION_RETRY), + ERR_API_NULL_TRANSPORT(1000), + ERR_API_REQUEST(1002), + ERR_API_HAND_SHAKE_FAILED(1100, TapoErrorType.COMMUNICATION_RETRY), + ERR_API_LOGIN_FAILED(1111), + ERR_API_HTTP_TRANSPORT_FAILED(1112), + ERR_API_MULTI_REQUEST_FAILED(1200), + ERR_API_JSON_DECODE_FAIL(-1003), + ERR_API_JSON_ENCODE_FAIL(-1004), + ERR_API_AES_DECODE_FAIL(-1005), + ERR_API_REQUEST_LEN_ERROR(-1006), + ERR_API_CLOUD_FAILED(-1007), + ERR_API_PARAMS(-1008), + ERR_API_RSA_KEY_LENGTH(-1010), + ERR_API_SESSION_PARAM(-1101), + ERR_API_QUICK_SETUP(-1201), + ERR_API_DEVICE(-1301), + ERR_API_DEVICE_NEXT_EVENT(-1302), + ERR_API_FIRMWARE(-1401), + ERR_API_FIRMWARE_VER_ERROR(-1402), + ERR_API_LOGIN(-1501), + ERR_API_TIME(-1601), + ERR_API_TIME_SYS(-1602), + ERR_API_TIME_SAVE(-1603), + ERR_API_WIRELESS(-1701), + ERR_API_WIRELESS_UNSUPPORTED(-1702), + ERR_API_SCHEDULE(-1801), + ERR_API_SCHEDULE_FULL(-1802), + ERR_API_SCHEDULE_CONFLICT(-1803), + ERR_API_SCHEDULE_SAVE(-1804), + ERR_API_SCHEDULE_INDEX(-1805), + ERR_API_COUNTDOWN(-1901), + ERR_API_COUNTDOWN_CONFLICT(-1902), + ERR_API_COUNTDOWN_SAVE(-1903), + ERR_API_ANTITHEFT(-2001), + ERR_API_ANTITHEFT_CONFLICT(-2002), + ERR_API_ANTITHEFT_SAVE(-2003), + ERR_API_ACCOUNT(-2101), + ERR_API_STAT(-2201), + ERR_API_STAT_SAVE(-2202), + ERR_API_DST(-2301), + ERR_API_DST_SAVE(-2302), + + // List of Cloud-ErrorCodes + ERR_CLOUD_API_RATE(-20004), + ERR_CLOUD_CREDENTIALS(-20601), + ERR_CLOUD_JSON_FORMAT(-10100), + ERR_CLOUD_METHOD_MISSING(-20103), + ERR_CLOUD_PARAMETER_MISSING(-20104), + ERR_CLOUD_TOKEN_EXPIRED(-20651), + + // List of Binding-ErrorCodes + ERR_BINDING_HTTP_RESPONSE(9001, TapoErrorType.COMMUNICATION_ERROR), + ERR_BINDING_COOKIE(9002, TapoErrorType.COMMUNICATION_ERROR), + ERR_BINDING_CREDENTIALS(9003, TapoErrorType.CONFIGURATION_ERROR), + ERR_BINDING_DEVICE_OFFLINE(9009, TapoErrorType.COMMUNICATION_ERROR), + ERR_BINDING_CONNECT_TIMEOUT(9010, TapoErrorType.COMMUNICATION_ERROR), + + // List of Binding-Config-ErrorCodes + ERR_CONFIG_IP(10001, TapoErrorType.CONFIGURATION_ERROR), // ip not set + ERR_CONFIG_CREDENTIALS(10002, TapoErrorType.CONFIGURATION_ERROR), // credentials not set + ERR_CONFIG_NO_BRIDGE(10003, TapoErrorType.CONFIGURATION_ERROR); // no bridge configured + + private Integer code; + private TapoErrorType errorType; + + /* set code */ + private TapoErrorCode(Integer code) { + this.code = code; + this.errorType = TapoErrorType.GENERAL; + } + + private TapoErrorCode(Integer code, TapoErrorType errorType) { + this.code = code; + this.errorType = errorType; + } + + /* get vlaues */ + public Integer getCode() { + return this.code; + } + + public TapoErrorType getType() { + return this.errorType; + } + + public static TapoErrorCode fromCode(int errorCode) { + for (TapoErrorCode e : TapoErrorCode.values()) { + if (e.code.equals(errorCode)) + return e; + } + return ERR_UNKNOWN; + } +} diff --git a/bundles/org.openhab.binding.tapocontrol/src/main/java/org/openhab/binding/tapocontrol/internal/constants/TapoErrorConstants.java b/bundles/org.openhab.binding.tapocontrol/src/main/java/org/openhab/binding/tapocontrol/internal/constants/TapoErrorConstants.java deleted file mode 100644 index ae6cc7880da20..0000000000000 --- a/bundles/org.openhab.binding.tapocontrol/src/main/java/org/openhab/binding/tapocontrol/internal/constants/TapoErrorConstants.java +++ /dev/null @@ -1,104 +0,0 @@ -/** - * Copyright (c) 2010-2023 Contributors to the openHAB project - * - * See the NOTICE file(s) distributed with this work for additional - * information. - * - * This program and the accompanying materials are made available under the - * terms of the Eclipse Public License 2.0 which is available at - * http://www.eclipse.org/legal/epl-2.0 - * - * SPDX-License-Identifier: EPL-2.0 - */ -package org.openhab.binding.tapocontrol.internal.constants; - -import java.util.Set; - -import org.eclipse.jdt.annotation.NonNullByDefault; - -/** - * The {@link TapoErrorConstants} class defines error-message constants - * - * @author Christian Wild - Initial contribution - */ -@NonNullByDefault -public class TapoErrorConstants { - /**************************************** - * LIST OF ERROR CODES - ****************************************/ - // List of API-ErrorCodes - public static final Integer ERR_API_COMMON_FAILED = -1; - public static final Integer ERR_API_SESSION_TIMEOUT = 9999; - public static final Integer ERR_API_NULL_TRANSPORT = 1000; - public static final Integer ERR_API_REQUEST = 1002; - public static final Integer ERR_API_HAND_SHAKE_FAILED = 1100; - public static final Integer ERR_API_LOGIN_FAILED = 1111; - public static final Integer ERR_API_HTTP_TRANSPORT_FAILED = 1112; - public static final Integer ERR_API_MULTI_REQUEST_FAILED = 1200; - public static final Integer ERR_API_JSON_DECODE_FAIL = -1003; - public static final Integer ERR_API_JSON_ENCODE_FAIL = -1004; - public static final Integer ERR_API_AES_DECODE_FAIL = -1005; - public static final Integer ERR_API_REQUEST_LEN_ERROR = -1006; - public static final Integer ERR_API_CLOUD_FAILED = -1007; - public static final Integer ERR_API_PARAMS = -1008; - public static final Integer ERR_API_RSA_KEY_LENGTH = -1010; - public static final Integer ERR_API_SESSION_PARAM = -1101; - public static final Integer ERR_API_QUICK_SETUP = -1201; - public static final Integer ERR_API_DEVICE = -1301; - public static final Integer ERR_API_DEVICE_NEXT_EVENT = -1302; - public static final Integer ERR_API_FIRMWARE = -1401; - public static final Integer ERR_API_FIRMWARE_VER_ERROR = -1402; - public static final Integer ERR_API_LOGIN = -1501; - public static final Integer ERR_API_TIME = -1601; - public static final Integer ERR_API_TIME_SYS = -1602; - public static final Integer ERR_API_TIME_SAVE = -1603; - public static final Integer ERR_API_WIRELESS = -1701; - public static final Integer ERR_API_WIRELESS_UNSUPPORTED = -1702; - public static final Integer ERR_API_SCHEDULE = -1801; - public static final Integer ERR_API_SCHEDULE_FULL = -1802; - public static final Integer ERR_API_SCHEDULE_CONFLICT = -1803; - public static final Integer ERR_API_SCHEDULE_SAVE = -1804; - public static final Integer ERR_API_SCHEDULE_INDEX = -1805; - public static final Integer ERR_API_COUNTDOWN = -1901; - public static final Integer ERR_API_COUNTDOWN_CONFLICT = -1902; - public static final Integer ERR_API_COUNTDOWN_SAVE = -1903; - public static final Integer ERR_API_ANTITHEFT = -2001; - public static final Integer ERR_API_ANTITHEFT_CONFLICT = -2002; - public static final Integer ERR_API_ANTITHEFT_SAVE = -2003; - public static final Integer ERR_API_ACCOUNT = -2101; - public static final Integer ERR_API_STAT = -2201; - public static final Integer ERR_API_STAT_SAVE = -2202; - public static final Integer ERR_API_DST = -2301; - public static final Integer ERR_API_DST_SAVE = -2302; - - // List of Cloud-ErrorCodes - public static final Integer ERR_CLOUD_API_RATE = -20004; - public static final Integer ERR_CLOUD_CREDENTIALS = -20601; - public static final Integer ERR_CLOUD_JSON_FORMAT = -10100; - public static final Integer ERR_CLOUD_METHOD_MISSING = -20103; - public static final Integer ERR_CLOUD_PARAMETER_MISSING = -20104; - public static final Integer ERR_CLOUD_TOKEN_EXPIRED = -20651; - - // List of Binding-ErrorCodes - public static final Integer ERR_BINDING_HTTP_RESPONSE = 9001; - public static final Integer ERR_BINDING_COOKIE = 9002; - public static final Integer ERR_BINDING_CREDENTIALS = 9003; - public static final Integer ERR_BINDING_DEVICE_OFFLINE = 9009; - public static final Integer ERR_BINDING_CONNECT_TIMEOUT = 9010; - - // List of Config-ErrorCodes - public static final Integer ERR_CONFIG_IP = 10001; // ip not set - public static final Integer ERR_CONFIG_CREDENTIALS = 10002; // credentials not set - public static final Integer ERR_CONFIG_NO_BRIDGE = 10003; // no bridge configured - - /**************************************** - * ErrorTypes - ****************************************/ - // communication errors - set device to offline (retry connect) - public static final Set LIST_COMMUNICATION_ERRORS = Set.of(ERR_BINDING_HTTP_RESPONSE, ERR_BINDING_COOKIE, - ERR_BINDING_DEVICE_OFFLINE, ERR_BINDING_CONNECT_TIMEOUT); - // configuration errors - set device to state configuration error (don't retry) - public static final Set LIST_CONFIGURATION_ERRORS = Set.of(ERR_BINDING_CREDENTIALS); - // reauthenticate errors (trying login immediatly) - public static final Set LIST_REAUTH_ERRORS = Set.of(ERR_API_SESSION_TIMEOUT, ERR_API_HAND_SHAKE_FAILED); -} diff --git a/bundles/org.openhab.binding.tapocontrol/src/main/java/org/openhab/binding/tapocontrol/internal/constants/TapoErrorType.java b/bundles/org.openhab.binding.tapocontrol/src/main/java/org/openhab/binding/tapocontrol/internal/constants/TapoErrorType.java new file mode 100644 index 0000000000000..ee7288da104c8 --- /dev/null +++ b/bundles/org.openhab.binding.tapocontrol/src/main/java/org/openhab/binding/tapocontrol/internal/constants/TapoErrorType.java @@ -0,0 +1,29 @@ +/** + * Copyright (c) 2010-2023 Contributors to the openHAB project + * + * See the NOTICE file(s) distributed with this work for additional + * information. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License 2.0 which is available at + * http://www.eclipse.org/legal/epl-2.0 + * + * SPDX-License-Identifier: EPL-2.0 + */ +package org.openhab.binding.tapocontrol.internal.constants; + +import org.eclipse.jdt.annotation.NonNullByDefault; + +/** + * The {@link TapoErrorType} enum lists known errortypes can be received or thrown by binding + * + * @author Christian Wild - Initial contribution + */ +@NonNullByDefault +public enum TapoErrorType { + COMMUNICATION_ERROR, // communication error + COMMUNICATION_RETRY, // communication error - retry to connect immediately + CONFIGURATION_ERROR, // configuration error + GENERAL, // general error (e.g. known api err) + UNKNOWN // unknown error +} diff --git a/bundles/org.openhab.binding.tapocontrol/src/main/java/org/openhab/binding/tapocontrol/internal/device/TapoDevice.java b/bundles/org.openhab.binding.tapocontrol/src/main/java/org/openhab/binding/tapocontrol/internal/device/TapoDevice.java index 4584ceef4bf4f..dc2455f338095 100644 --- a/bundles/org.openhab.binding.tapocontrol/src/main/java/org/openhab/binding/tapocontrol/internal/device/TapoDevice.java +++ b/bundles/org.openhab.binding.tapocontrol/src/main/java/org/openhab/binding/tapocontrol/internal/device/TapoDevice.java @@ -13,7 +13,7 @@ package org.openhab.binding.tapocontrol.internal.device; import static org.openhab.binding.tapocontrol.internal.constants.TapoBindingSettings.*; -import static org.openhab.binding.tapocontrol.internal.constants.TapoErrorConstants.*; +import static org.openhab.binding.tapocontrol.internal.constants.TapoErrorCode.*; import static org.openhab.binding.tapocontrol.internal.constants.TapoThingConstants.*; import static org.openhab.binding.tapocontrol.internal.helpers.TapoUtils.*; @@ -25,6 +25,7 @@ import org.eclipse.jdt.annotation.NonNullByDefault; import org.eclipse.jdt.annotation.Nullable; import org.openhab.binding.tapocontrol.internal.api.TapoDeviceConnector; +import org.openhab.binding.tapocontrol.internal.constants.TapoErrorCode; import org.openhab.binding.tapocontrol.internal.helpers.TapoErrorHandler; import org.openhab.binding.tapocontrol.internal.structures.TapoChildData; import org.openhab.binding.tapocontrol.internal.structures.TapoDeviceConfiguration; @@ -232,10 +233,14 @@ protected void pollingSchedulerAction() { * * @return */ - public TapoErrorHandler getError() { + public TapoErrorHandler getErrorHandler() { return this.deviceError; } + public TapoErrorCode getError() { + return this.deviceError.getError(); + } + /** * set device error * @@ -456,21 +461,27 @@ public void disconnect() { */ public void handleConnectionState() { ThingStatus deviceState = getThing().getStatus(); - Integer errorCode = deviceError.getCode(); + TapoErrorCode errorCode = deviceError.getError(); - if (errorCode == 0) { + if (errorCode == TapoErrorCode.NO_ERROR) { if (deviceState != ThingStatus.ONLINE) { updateStatus(ThingStatus.ONLINE); } - } else if (LIST_REAUTH_ERRORS.contains(errorCode)) { - connect(); - } else if (LIST_COMMUNICATION_ERRORS.contains(errorCode)) { - updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR, deviceError.getMessage()); - disconnect(); - } else if (LIST_CONFIGURATION_ERRORS.contains(errorCode)) { - updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR, deviceError.getMessage()); } else { - updateStatus(ThingStatus.UNKNOWN, ThingStatusDetail.NONE, deviceError.getMessage()); + switch (errorCode.getType()) { + case COMMUNICATION_RETRY: + connect(); + break; + case COMMUNICATION_ERROR: + updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR, deviceError.getMessage()); + disconnect(); + break; + case CONFIGURATION_ERROR: + updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR, deviceError.getMessage()); + break; + default: + updateStatus(ThingStatus.UNKNOWN, ThingStatusDetail.NONE, deviceError.getMessage()); + } } } diff --git a/bundles/org.openhab.binding.tapocontrol/src/main/java/org/openhab/binding/tapocontrol/internal/helpers/TapoErrorHandler.java b/bundles/org.openhab.binding.tapocontrol/src/main/java/org/openhab/binding/tapocontrol/internal/helpers/TapoErrorHandler.java index 627ceda399699..7efa99280ba3d 100644 --- a/bundles/org.openhab.binding.tapocontrol/src/main/java/org/openhab/binding/tapocontrol/internal/helpers/TapoErrorHandler.java +++ b/bundles/org.openhab.binding.tapocontrol/src/main/java/org/openhab/binding/tapocontrol/internal/helpers/TapoErrorHandler.java @@ -12,13 +12,11 @@ */ package org.openhab.binding.tapocontrol.internal.helpers; -import static org.openhab.binding.tapocontrol.internal.helpers.TapoUtils.*; - -import java.lang.reflect.Field; +import static org.openhab.binding.tapocontrol.internal.constants.TapoErrorCode.*; import org.eclipse.jdt.annotation.NonNullByDefault; import org.eclipse.jdt.annotation.Nullable; -import org.openhab.binding.tapocontrol.internal.constants.TapoErrorConstants; +import org.openhab.binding.tapocontrol.internal.constants.TapoErrorCode; import com.google.gson.Gson; import com.google.gson.JsonObject; @@ -30,9 +28,8 @@ */ @NonNullByDefault public class TapoErrorHandler extends Exception { + private TapoErrorCode errorCode = TapoErrorCode.NO_ERROR; private static final long serialVersionUID = 0L; - private Integer errorCode = 0; - private String errorMessage = ""; private String infoMessage = ""; private Gson gson = new Gson(); @@ -81,6 +78,25 @@ public TapoErrorHandler(Exception ex, String infoMessage) { raiseError(ex, infoMessage); } + /** + * Constructor TapoErrorCodeEnum + * + * @param errorCode error code (TapoErrorCodeEnum) + */ + public TapoErrorHandler(TapoErrorCode errorCode) { + raiseError(errorCode); + } + + /** + * Constructor + * + * @param errorCode error code (TapoErrorCodeEnum) + * @param infoMessage optional info-message + */ + public TapoErrorHandler(TapoErrorCode errorCode, String infoMessage) { + raiseError(errorCode, infoMessage); + } + /*********************************** * * Private Functions @@ -91,27 +107,11 @@ public TapoErrorHandler(Exception ex, String infoMessage) { * GET ERROR-MESSAGE * * @param errCode error Number (or constant ERR_API_CODE ) - * @return error-message if set constant ERR_API_CODE_MSG. if not name of ERR_API_CODE is returned + * @return error-message if code found in i18n, else return code */ private String getErrorMessage(Integer errCode) { - String i18nKey = ""; - Field[] fields = TapoErrorConstants.class.getDeclaredFields(); - /* loop ErrorConstants and search for integer-code in value */ - for (Field f : fields) { - String constName = f.getName(); - try { - Integer val = (Integer) f.get(this); - if (val != null && val.equals(errCode)) { - /* convert constatn name to i18n key */ - i18nKey = i18nKey.replaceFirst("ERR_", "error-"); - i18nKey = constName.replace("_", "-").toLowerCase(); - return "@text/" + i18nKey; - } - } catch (Exception e) { - // next loop (not a good idea to raise error in error-handling) - } - } - return infoMessage + " (" + errCode.toString() + ")"; + String key = TapoErrorCode.fromCode(errCode).name().replace("ERR_", "error-").replace("_", "-").toLowerCase(); + return String.format("@text/%s [ \"%s\" ]", key, errCode.toString()); } /*********************************** @@ -136,9 +136,7 @@ public void raiseError(Integer errorCode) { * @param infoMessage optional info-message */ public void raiseError(Integer errorCode, String infoMessage) { - this.errorCode = errorCode; - this.infoMessage = infoMessage; - this.errorMessage = getErrorMessage(errorCode); + raiseError(TapoErrorCode.fromCode(errorCode), infoMessage); } /** @@ -157,9 +155,27 @@ public void raiseError(Exception ex) { * @param infoMessage optional info-message */ public void raiseError(Exception ex, String infoMessage) { - this.errorCode = ex.hashCode(); + raiseError(TapoErrorCode.fromCode(ex.hashCode()), infoMessage); + } + + /** + * Raises new error + * + * @param errorCode error code (TapoErrorCodeEnum) + */ + public void raiseError(TapoErrorCode errorCode) { + raiseError(errorCode, ""); + } + + /** + * Raises new error + * + * @param errorCode error code (TapoErrorCodeEnum) + * @param infoMessage optional info-message + */ + public void raiseError(TapoErrorCode errorCode, String infoMessage) { + this.errorCode = errorCode; this.infoMessage = infoMessage; - this.errorMessage = getValueOrDefault(ex.getMessage(), ex.toString()); } /** @@ -168,17 +184,15 @@ public void raiseError(Exception ex, String infoMessage) { * @param tapoError */ public void set(TapoErrorHandler tapoError) { - this.errorCode = tapoError.getNumber(); + this.errorCode = TapoErrorCode.fromCode(tapoError.getCode()); this.infoMessage = tapoError.getExtendedInfo(); - this.errorMessage = getErrorMessage(this.errorCode); } /** * Reset Error */ public void reset() { - this.errorCode = 0; - this.errorMessage = ""; + this.errorCode = NO_ERROR; this.infoMessage = ""; } @@ -196,7 +210,7 @@ public void reset() { @Override @Nullable public String getMessage() { - return this.errorMessage; + return getErrorMessage(errorCode.getCode()); } /** @@ -215,7 +229,7 @@ public String getMessage(Integer errorCode) { * @return error code (integer) */ public Integer getCode() { - return this.errorCode; + return this.errorCode.getCode(); } /** @@ -228,11 +242,11 @@ public String getExtendedInfo() { } /** - * Get Error Number + * Get Error Code * - * @return error number + * @return error code */ - public Integer getNumber() { + public TapoErrorCode getError() { return this.errorCode; } @@ -242,7 +256,7 @@ public Integer getNumber() { * @return true if has error */ public Boolean hasError() { - return this.errorCode != 0; + return this.errorCode != NO_ERROR; } /** @@ -252,7 +266,8 @@ public Boolean hasError() { */ public JsonObject getJson() { JsonObject json; - json = gson.fromJson("{'error_code': '" + errorCode + "', 'error_message':'" + errorMessage + "'}", + json = gson.fromJson( + "{'error_code': '" + errorCode + "', 'error_message':'" + getErrorMessage(getCode()) + "'}", JsonObject.class); if (json == null) { json = new JsonObject(); diff --git a/bundles/org.openhab.binding.tapocontrol/src/main/resources/OH-INF/i18n/tapocontrol.properties b/bundles/org.openhab.binding.tapocontrol/src/main/resources/OH-INF/i18n/tapocontrol.properties index 39f63caa4c9f5..c53e9bb902f77 100644 --- a/bundles/org.openhab.binding.tapocontrol/src/main/resources/OH-INF/i18n/tapocontrol.properties +++ b/bundles/org.openhab.binding.tapocontrol/src/main/resources/OH-INF/i18n/tapocontrol.properties @@ -166,61 +166,60 @@ channel-type.tapocontrol.effectOn.description = Switches the lightning effect on # error messages -error-api-account = received error code (-2101); +error-api-account = received account error (-2101) error-api-aes-decode-fail = aes decode failed (-1005) -error-api-antitheft-conflict = received error code (-2002); -error-api-antitheft = received error code (-2001); -error-api-antitheft-save = received error code (-2003); +error-api-antitheft-conflict = device antitheft conflict (-2002) +error-api-antitheft = device antitheft error (-2001) +error-api-antitheft-save = antitheft save error (-2003) error-api-cloud-failed = cloud failed (-1007) -error-api-common-failed = ; -error-api-countdown-conflict = received error code (-1902); -error-api-countdown = received error code (-1901); -error-api-countdown-save = received error code (-1903); -error-api-device = received error code (-1301); -error-api-device-next-event = received error code (-1302); -error-api-dst = received error code (-2301); -error-api-dst-save = received error code (-2302); -error-api-firmware = received error code (-1401); -error-api-firmware-ver-error = received error code (-1402); +error-api-countdown-conflict = api-countdown-conflict (-1902) +error-api-countdown = api-countdown error (-1901) +error-api-countdown-save = api-countdown-save error (-1903) +error-api-device = received device error (-1301) +error-api-device-next-event = device next event error (-1302) +error-api-dst = received dst error (-2301) +error-api-dst-save = received dst-save error (-2302) +error-api-firmware = firmware error (-1401) +error-api-firmware-ver-error = firmware version error (-1402) error-api-hand-shake-failed = can't create handshake (1100) error-api-http-transport-failed = http transport failed (1112) error-api-json-decode-fail = json decode failed (-1003) error-api-json-encode-fail = json encode failed (-1004) error-api-login-failed = login failed (1111) -error-api-login = invalid request or credentials (-1501); +error-api-login = invalid request or credentials (-1501) error-api-multi-request-failed = multi request failed (1200) -error-api-null-transport = -1 +error-api-null-transport = null transport error (1000) error-api-params = received invalid parameter (-1008) -error-api-quick-setup = received error code (-1201) +error-api-quick-setup = quick setup error (-1201) error-api-request-len-error = request length error (-1006) error-api-request = invalid request or command (1002) error-api-rsa-key-length = invalid public key length (-1010) -error-api-schedule-conflict = received error code (-1803); -error-api-schedule-full = received error code (-1802); -error-api-schedule-index = received error code (-1805); -error-api-schedule = received error code (-1801); -error-api-schedule-save = received error code (-1804); +error-api-schedule-conflict = schedule conflict error (-1803) +error-api-schedule-full = schedule full error (-1802) +error-api-schedule-index = schedule index error (-1805) +error-api-schedule = schedule error (-1801) +error-api-schedule-save = saving schedule rule failed (-1804) error-api-session-param = invalid session key (-1101) -error-api-session-timeout = "device session expired (9999)"; -error-api-stat = received error code (-2201); -error-api-stat-save = received error code (-2202); -error-api-time = received error code (-1601); -error-api-time-save = received error code (-1603); -error-api-time-sys = received error code (-1602); -error-api-cloud-token-expired = cloud token expired or invalid -error-api-wireless = received error code (-1701); -error-api-wireless-unsupported = received error code (-1702); +error-api-session-timeout = device session expired (9999) +error-api-stat = state failed (-2201) +error-api-stat-save = saving state failed (-2202) +error-api-time = device time error (-1601) +error-api-time-save = saving time failed (-1603) +error-api-time-sys = time system error (-1602) +error-api-wireless = wireless error (-1701) +error-api-wireless-unsupported = wireless unsuported error (-1702) error-binding-connect-timeout = connection timeout - device not reachable (9010) error-binding-cookie = cookie error (9002) error-binding-credentials = invalid request or credentials (9003) error-binding-device-offline = device offline (9009) error-binding-http-response = invalid http-response (9001) -error-cloud-api-rate = api rate limit exceeded (-20004); -error-cloud-credentials = incorrect email or password (-20601); -error-cloud-json-format = json format error (-10100); -error-cloud-method-missing = method does not exist or is not available (-20103); -error-cloud-parameter-missing = parameter doesn't exist (-20104); -error-cloud-token-expired = token expired (-20651); +error-cloud-api-rate = api rate limit exceeded (-20004) +error-cloud-credentials = incorrect email or password (-20601) +error-cloud-json-format = json format error (-10100) +error-cloud-method-missing = method does not exist or is not available (-20103) +error-cloud-parameter-missing = parameter doesn't exist (-20104) +error-cloud-token-expired = token expired (-20651) error-config-credentials = credentials not set (bridge) (10002) error-config-ip = ip-address not valid (10001) error-config-no-bridge = no bridge configured (10003) +error-unknown = unknown api error ({0})