Skip to content

Commit

Permalink
[tapocontrol] Moved error messages to i18n
Browse files Browse the repository at this point in the history
moved constants from class to enum

Signed-off-by: Christian Wild <[email protected]>
  • Loading branch information
wildcs committed May 5, 2023
1 parent 831e590 commit 9bbcd36
Show file tree
Hide file tree
Showing 9 changed files with 273 additions and 204 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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");
Expand Down Expand Up @@ -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();
}
}

Expand All @@ -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 {
Expand All @@ -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();
}
}

Expand All @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -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
}
Loading

0 comments on commit 9bbcd36

Please sign in to comment.