From 18411a4a8b91a658fac6d44104f955a25b44a888 Mon Sep 17 00:00:00 2001 From: Christian Wild <40909464+wildcs@users.noreply.github.com> Date: Mon, 10 Jul 2023 12:30:20 +0200 Subject: [PATCH] [asuswrt] Apply suggestions from code review Co-authored-by: Wouter Born Signed-off-by: Christian Wild --- bundles/org.openhab.binding.asuswrt/README.md | 60 +++---- .../internal/api/AsuswrtConnector.java | 3 +- .../constants/AsuswrtBindingConstants.java | 4 +- .../internal/helpers/AsuswrtErrorHandler.java | 8 +- .../internal/helpers/AsuswrtUtils.java | 147 ++++++------------ .../structures/AsuswrtClientInfo.java | 62 ++++---- .../structures/AsuswrtClientList.java | 1 + .../structures/AsuswrtCredentials.java | 2 +- .../structures/AsuswrtInterfaceList.java | 10 +- .../internal/structures/AsuswrtIpInfo.java | 26 ++-- .../structures/AsuswrtRouterInfo.java | 20 +-- .../internal/structures/AsuswrtTraffic.java | 57 ++++--- .../internal/structures/AsuswrtUsage.java | 14 +- .../internal/things/AsuswrtRouter.java | 74 +++++---- 14 files changed, 216 insertions(+), 272 deletions(-) diff --git a/bundles/org.openhab.binding.asuswrt/README.md b/bundles/org.openhab.binding.asuswrt/README.md index 80ae67539a4ca..264905e935956 100644 --- a/bundles/org.openhab.binding.asuswrt/README.md +++ b/bundles/org.openhab.binding.asuswrt/README.md @@ -53,38 +53,38 @@ All devices support some of the following properties: All devices support some of the following channels: -| group | channel |type | description | things supporting this channel | -|------------------|--------------------|------------------------|--------------------------------------------|-----------------------------------| -| networkInfo | macAddress | text (RO) | HW-Address | interface, client | -| | ipAddress | text (RO) | IP-Address | interface | -| | ipMethod | text (RO) | Ip-Method (static/dhcp) | interface, client | -| | subnet | text (RO) | Subnetmask | interface | -| | gateway | text (RO) | Default-Gateway | interface | -| | dnsServers | text (RO) | DNS-Servers | interface | -| | networkState | Switch (RO) | Client is online | interface, client | -| | internetState | Switch (RO) | Client connected to Internet | client | -| sysInfoGroup | memTotal | Number:DataAmountype | Total memory in MB | router | -| | memUsed | Number:DataAmountype | Used memory in MB | router | -| | memFree | Number:DataAmountype | Free memory in MB | router | -| | memUsedPercent | Number:Dimensionles | Used memory in % | router | -| | cpuUsedPercent | Number:Dimensionles | Total CPU usage in percent over all cores | router | -| clientListGroup | knownClients | text (RO) | Known clients with name and MAC-Addresses | router | -| | onlineClients | text (RO) | Online clients with name and MAC-Addresses | router | -| | onlineMACs | text (RO) | List with mac-addresses of online clients | router | -| | onlineClientsCount | Number:Dimensionless | Count of online clients | router | -| traffic | curRX | Number:DataTransferRate| Current DataTransferRate MBits/s (receive) | interface, client | -| | curTX | Number:DataTransferRate| Current DataTransferRate MBits/s (send) | interface, client | -| | todayRX | Number:DataAmount | Data received since 0:00 a clock in MB | interface, client | -| | todayTX | Number:DataAmount | Data sent since 0:00 a clock in MB | interface, client | -| | totalRX | Number:DataAmount | Data received since reboot in MB | interface, client | -| | totalTX | Number:DataAmount | Data sent since reboot in MB | interface, client | +| group | channel |type | description | things supporting this channel | +|------------------|----------------------|------------------------|--------------------------------------------|-----------------------------------| +| network-info | mac-address | text (RO) | HW-Address | interface, client | +| | ip-address | text (RO) | IP-Address | interface | +| | ip-method | text (RO) | Ip-Method (static/dhcp) | interface, client | +| | subnet | text (RO) | Subnetmask | interface | +| | gateway | text (RO) | Default-Gateway | interface | +| | dns-servers | text (RO) | DNS-Servers | interface | +| | network-state | Switch (RO) | Client is online | interface, client | +| | internet-state | Switch (RO) | Client connected to Internet | client | +| sys-info | mem-total | Number:DataAmountype | Total memory in MB | router | +| | mem-used | Number:DataAmountype | Used memory in MB | router | +| | mem-free | Number:DataAmountype | Free memory in MB | router | +| | mem-used-percent | Number:Dimensionles | Used memory in % | router | +| | cpu-used-percent | Number:Dimensionles | Total CPU usage in percent over all cores | router | +| client-list | known-clients | text (RO) | Known clients with name and MAC-Addresses | router | +| | online-clients | text (RO) | Online clients with name and MAC-Addresses | router | +| | online-macs | text (RO) | List with mac-addresses of online clients | router | +| | online-clients-count | Number:Dimensionless | Count of online clients | router | +| traffic | current-rx | Number:DataTransferRate| Current DataTransferRate MBits/s (receive) | interface, client | +| | current-tx | Number:DataTransferRate| Current DataTransferRate MBits/s (send) | interface, client | +| | today-rx | Number:DataAmount | Data received since 0:00 a clock in MB | interface, client | +| | today-tx | Number:DataAmount | Data sent since 0:00 a clock in MB | interface, client | +| | total-rx | Number:DataAmount | Data received since reboot in MB | interface, client | +| | total-tx | Number:DataAmount | Data sent since reboot in MB | interface, client | ## Events All devices support some of the following Events: -| group | event |kind | description | things supporting this event | -|------------------|-------------------|------------|------------------------------------------------------------------------|---------------------------------| -| networkInfo | connectionEvent | Trigger | Fired if connection is established ('connected') or ('disconnected') | interface | -| | clientOnlineEvent | Trigger | Fired if Client leaves ('gone') or enters ('connected') the network | client | -| clientListGroup | clientOnlineEvent | Trigger | Fired if Client leaves ('gone') or enters ('connected') the network | router | +| group | event |kind | description | things supporting this event | +|------------------|---------------------|------------|------------------------------------------------------------------------|---------------------------------| +| network-info | connection-event | Trigger | Fired if connection is established ('connected') or ('disconnected') | interface | +| | client-online-event | Trigger | Fired if Client leaves ('gone') or enters ('connected') the network | client | +| client-list | client-online-event | Trigger | Fired if Client leaves ('gone') or enters ('connected') the network | router | diff --git a/bundles/org.openhab.binding.asuswrt/src/main/java/org/openhab/binding/asuswrt/internal/api/AsuswrtConnector.java b/bundles/org.openhab.binding.asuswrt/src/main/java/org/openhab/binding/asuswrt/internal/api/AsuswrtConnector.java index 2d1509a5715ad..1c05a8d3438b2 100644 --- a/bundles/org.openhab.binding.asuswrt/src/main/java/org/openhab/binding/asuswrt/internal/api/AsuswrtConnector.java +++ b/bundles/org.openhab.binding.asuswrt/src/main/java/org/openhab/binding/asuswrt/internal/api/AsuswrtConnector.java @@ -104,7 +104,7 @@ public void querySysInfo(boolean asyncRequest) { } /** - * Query Data From Device asynchron + * Query Data From Device * * @param command command constant to sent * @param asyncRequest True if request should be sent asynchron, false if synchron @@ -206,5 +206,4 @@ protected String getURL(String site) { } return url + "/" + site; } - } diff --git a/bundles/org.openhab.binding.asuswrt/src/main/java/org/openhab/binding/asuswrt/internal/constants/AsuswrtBindingConstants.java b/bundles/org.openhab.binding.asuswrt/src/main/java/org/openhab/binding/asuswrt/internal/constants/AsuswrtBindingConstants.java index 9e21625bad9bb..ef92dc8518cc0 100644 --- a/bundles/org.openhab.binding.asuswrt/src/main/java/org/openhab/binding/asuswrt/internal/constants/AsuswrtBindingConstants.java +++ b/bundles/org.openhab.binding.asuswrt/src/main/java/org/openhab/binding/asuswrt/internal/constants/AsuswrtBindingConstants.java @@ -95,8 +95,8 @@ public class AsuswrtBindingConstants { public static final String CHANNEL_TRAFFIC_TOTAL_TX = "total-tx"; public static final String CHANNEL_TRAFFIC_TODAY_RX = "today-rx"; public static final String CHANNEL_TRAFFIC_TODAY_TX = "today-tx"; - public static final String CHANNEL_TRAFFIC_CURRENT_RX = "cur-rx"; - public static final String CHANNEL_TRAFFIC_CURRENT_TX = "cur-tx"; + public static final String CHANNEL_TRAFFIC_CURRENT_RX = "current-rx"; + public static final String CHANNEL_TRAFFIC_CURRENT_TX = "current-tx"; /** * PROPERTIES diff --git a/bundles/org.openhab.binding.asuswrt/src/main/java/org/openhab/binding/asuswrt/internal/helpers/AsuswrtErrorHandler.java b/bundles/org.openhab.binding.asuswrt/src/main/java/org/openhab/binding/asuswrt/internal/helpers/AsuswrtErrorHandler.java index 6471a5277aef9..a16cf7e4f0c7f 100644 --- a/bundles/org.openhab.binding.asuswrt/src/main/java/org/openhab/binding/asuswrt/internal/helpers/AsuswrtErrorHandler.java +++ b/bundles/org.openhab.binding.asuswrt/src/main/java/org/openhab/binding/asuswrt/internal/helpers/AsuswrtErrorHandler.java @@ -80,8 +80,8 @@ public void raiseError(String errorMessage, @Nullable String infoMessage) { * Reset Error */ public void reset() { - this.errorMessage = ""; - this.infoMessage = ""; + errorMessage = ""; + infoMessage = ""; } /*********************************** @@ -94,13 +94,13 @@ public void reset() { * Get Error Message */ public String getErrorMessage() { - return this.errorMessage; + return errorMessage; } /** * Get Error Message */ public String getInfoMessage() { - return this.infoMessage; + return infoMessage; } } diff --git a/bundles/org.openhab.binding.asuswrt/src/main/java/org/openhab/binding/asuswrt/internal/helpers/AsuswrtUtils.java b/bundles/org.openhab.binding.asuswrt/src/main/java/org/openhab/binding/asuswrt/internal/helpers/AsuswrtUtils.java index 09013d4192a97..cd5ee3e8cd97c 100644 --- a/bundles/org.openhab.binding.asuswrt/src/main/java/org/openhab/binding/asuswrt/internal/helpers/AsuswrtUtils.java +++ b/bundles/org.openhab.binding.asuswrt/src/main/java/org/openhab/binding/asuswrt/internal/helpers/AsuswrtUtils.java @@ -12,8 +12,6 @@ */ package org.openhab.binding.asuswrt.internal.helpers; -import java.util.Calendar; -import java.util.Date; import java.util.regex.Pattern; import javax.measure.Unit; @@ -36,15 +34,15 @@ import com.google.gson.JsonPrimitive; /** - * {@link AsuswrtUtils} TapoUtils - + * {@link AsuswrtUtils} AsuswrtUtils - * Utility Helper Functions * * @author Christian Wild - Initial Initial contribution */ @NonNullByDefault public class AsuswrtUtils { - private static Pattern patternMacPairs = Pattern.compile("^([a-fA-F0-9]{2}[:\\.-]?){5}[a-fA-F0-9]{2}$"); - private static Pattern patternMacTriples = Pattern.compile("^([a-fA-F0-9]{3}[:\\.-]?){3}[a-fA-F0-9]{3}$"); + private static final Pattern PATTERN_MAC_PAIRS = Pattern.compile("^([a-fA-F0-9]{2}[:\\.-]?){5}[a-fA-F0-9]{2}$"); + private static final Pattern PATTERN_MAC_TRIPLES = Pattern.compile("^([a-fA-F0-9]{3}[:\\.-]?){3}[a-fA-F0-9]{3}$"); /************************************ * CALCULATION UTILS @@ -52,12 +50,11 @@ public class AsuswrtUtils { /** * Limit Value between limits * - * @param value Integer - * @param lowerLimit - * @param upperLimit - * @return limited value + * @param value Integer value should be limited + * @param lowerLimit will be returned if value is below + * @param upperLimit will be returned if value is higher */ - public static Integer limitVal(@Nullable Integer value, Integer lowerLimit, Integer upperLimit) { + public static int limitVal(@Nullable Integer value, int lowerLimit, int upperLimit) { if (value == null || value < lowerLimit) { return lowerLimit; } else if (value > upperLimit) { @@ -73,9 +70,8 @@ public static Integer limitVal(@Nullable Integer value, Integer lowerLimit, Inte * return value or default val if it's null * * @param Type of value - * @param value value - * @param defaultValue defaut value - * @return value or default + * @param value value which should be checked + * @param defaultValue default value will be returned if value is null */ public static T getValueOrDefault(@Nullable T value, T defaultValue) { return value == null ? defaultValue : value; @@ -86,7 +82,6 @@ public static T getValueOrDefault(@Nullable T value, T defaultValue) { * * @param mac unformated mac-Address * @param newDivisionChar new division char (e.g. ":","-" ) - * @return new formated mac-Address */ public static String formatMac(String mac, char newDivisionChar) { String unformatedMac = unformatMac(mac); @@ -102,7 +97,8 @@ public static String formatMac(String mac, char newDivisionChar) { /** * unformat MAC-Address replace all division chars */ - public static String unformatMac(String mac) { + public static String unformatMac(String rawMac) { + String mac = rawMac; mac = mac.replace("-", ""); mac = mac.replace(":", ""); mac = mac.replace(".", ""); @@ -117,7 +113,7 @@ public static boolean isValidMacAddress(String mac) { // MAC-Addresses usually are 6 * 2 hex nibbles separated by colons, // but apparently it is legal to have 4 * 3 hex nibbles as well, // and the separators can be any of : or - or . or nothing. - return (patternMacPairs.matcher(mac).find() || patternMacTriples.matcher(mac).find()); + return (PATTERN_MAC_PAIRS.matcher(mac).find() || PATTERN_MAC_TRIPLES.matcher(mac).find()); } /** @@ -138,10 +134,10 @@ public static byte[] hexStringToByteArray(String s) { /** * Return Boolean from string * - * @param s - string to be converted - * @param defVal - Default Value + * @param s string to be converted ('0', '1', '-1', 'true', 'false') + * @param defVal default value if no match was found */ - public static Boolean stringToBool(@Nullable String s, boolean defVal) { + public static boolean stringToBool(@Nullable String s, boolean defVal) { if (s == null) { return defVal; } else if ("1".equals(s) || "-1".equals(s)) { @@ -161,9 +157,9 @@ public static Boolean stringToBool(@Nullable String s, boolean defVal) { * Return Integer from string * * @param s - string to be converted - * @param defVal - Default Value + * @param defVal - default value if it is not a number */ - public static Integer stringToInteger(@Nullable String s, Integer defVal) { + public static int stringToInteger(@Nullable String s, int defVal) { if (s == null) { return defVal; } @@ -175,6 +171,7 @@ public static Integer stringToInteger(@Nullable String s, Integer defVal) { } /** + * Get String if not null or empty * * @param s - string to get value * @param defVal - default if is blank or null @@ -206,9 +203,9 @@ public static boolean isValidJson(String json) { /** * JSON Object to String * - * @param name parameter name - * @param defVal - default value; - * @return string value + * @param jsonObject where will be searched for key + * @param name name of key will be returned if found + * @param defVal - default value if key was not found */ public static String jsonObjectToString(@Nullable JsonObject jsonObject, String name, String defVal) { if (jsonObject != null && jsonObject.has(name)) { @@ -221,20 +218,21 @@ public static String jsonObjectToString(@Nullable JsonObject jsonObject, String /** * JSON Object to String * - * @param name parameter name - * @return string value + * @param jsonObject where will be searched for key + * @param name name of key will be returned if found */ public static String jsonObjectToString(@Nullable JsonObject jsonObject, String name) { return jsonObjectToString(jsonObject, name, ""); } /** + * JSON Object to boolean * - * @param name parameter name - * @param defVal - default value; - * @return boolean value + * @param jsonObject where will be searched for key + * @param name name of key will be returned if found + * @param defVal - default value if key was not found */ - public static Boolean jsonObjectToBool(@Nullable JsonObject jsonObject, String name, Boolean defVal) { + public static boolean jsonObjectToBool(@Nullable JsonObject jsonObject, String name, boolean defVal) { if (jsonObject != null && jsonObject.has(name)) { JsonPrimitive o = jsonObject.getAsJsonPrimitive(name); if (o.isBoolean()) { @@ -251,23 +249,23 @@ public static Boolean jsonObjectToBool(@Nullable JsonObject jsonObject, String n } /** - * JSON Object to Boolean + * JSON Object to boolean * - * @param name parameter name - * @return boolean value + * @param jsonObject where will be searched for key + * @param name name of key will be returned if found */ - public static Boolean jsonObjectToBool(@Nullable JsonObject jsonObject, String name) { + public static boolean jsonObjectToBool(@Nullable JsonObject jsonObject, String name) { return jsonObjectToBool(jsonObject, name, false); } /** * JSON Object to Integer * - * @param name parameter name - * @param defVal - default value; - * @return integer value + * @param jsonObject where will be searched for key + * @param name name of key will be returned if found + * @param defVal - default value if key was not found */ - public static Integer jsonObjectToInt(@Nullable JsonObject jsonObject, String name, Integer defVal) { + public static int jsonObjectToInt(@Nullable JsonObject jsonObject, String name, int defVal) { if (jsonObject != null && jsonObject.has(name)) { JsonPrimitive o = jsonObject.getAsJsonPrimitive(name); if (o.isNumber()) { @@ -283,19 +281,19 @@ public static Integer jsonObjectToInt(@Nullable JsonObject jsonObject, String na /** * JSON Object to Integer * - * @param name parameter name - * @return integer value + * @param jsonObject where will be searched for key + * @param name name of key will be returned if found */ - public static Integer jsonObjectToInt(@Nullable JsonObject jsonObject, String name) { + public static int jsonObjectToInt(@Nullable JsonObject jsonObject, String name) { return jsonObjectToInt(jsonObject, name, 0); } /** * JSON Object to Number * - * @param name parameter name - * @param defVal - default value; - * @return number value + * @param jsonObject where will be searched for key + * @param name name of key will be returned if found + * @param defVal - default value if key was not found */ public static Number jsonObjectToNumber(@Nullable JsonObject jsonObject, String name, Number defVal) { if (jsonObject != null && jsonObject.has(name)) { @@ -308,8 +306,8 @@ public static Number jsonObjectToNumber(@Nullable JsonObject jsonObject, String /** * JSON Object to Number * - * @param name parameter name - * @return number value + * @param jsonObject where will be searched for key + * @param name name of key will be returned if found */ public static Number jsonObjectToNumber(@Nullable JsonObject jsonObject, String name) { return jsonObjectToNumber(jsonObject, name, 0); @@ -321,8 +319,6 @@ public static Number jsonObjectToNumber(@Nullable JsonObject jsonObject, String /** * Return OnOffType from bool - * - * @param boolVal */ public static OnOffType getOnOffType(@Nullable Boolean boolVal) { return (boolVal != null ? boolVal ? OnOffType.ON : OnOffType.OFF : OnOffType.OFF); @@ -330,8 +326,6 @@ public static OnOffType getOnOffType(@Nullable Boolean boolVal) { /** * Return OnOffType from bool - * - * @param boolVal */ public static OnOffType getOnOffType(Integer intVal) { return intVal == 0 ? OnOffType.OFF : OnOffType.ON; @@ -339,8 +333,6 @@ public static OnOffType getOnOffType(Integer intVal) { /** * Return StringType from String - * - * @param strVal */ public static StringType getStringType(@Nullable String strVal) { return new StringType(strVal != null ? strVal : ""); @@ -348,8 +340,6 @@ public static StringType getStringType(@Nullable String strVal) { /** * Return DecimalType from Double - * - * @param numVal */ public static DecimalType getDecimalType(@Nullable Double numVal) { return new DecimalType((numVal != null ? numVal : 0)); @@ -357,8 +347,6 @@ public static DecimalType getDecimalType(@Nullable Double numVal) { /** * Return DecimalType from Integer - * - * @param numVal */ public static DecimalType getDecimalType(@Nullable Integer numVal) { return new DecimalType((numVal != null ? numVal : 0)); @@ -366,18 +354,13 @@ public static DecimalType getDecimalType(@Nullable Integer numVal) { /** * Return DecimalType from Long - * - * @param numVal */ public static DecimalType getDecimalType(@Nullable Long numVal) { return new DecimalType((numVal != null ? numVal : 0)); } /** - * JSON Object to PercentType - * - * @param numVal value 0-100 - * @return PercentType + * Return PercentType from Integer */ public static PercentType getPercentType(@Nullable Integer numVal) { Integer val = limitVal(numVal, 0, 100); @@ -388,9 +371,8 @@ public static PercentType getPercentType(@Nullable Integer numVal) { * Return HSBType from integers * * @param hue integer hue-color - * @param saturation integer saturation - * @param brightness integer brightness - * @return HSBType + * @param saturation integer saturation 0-100 + * @param brightness integer brightness 0-100 */ public static HSBType getHSBType(Integer hue, Integer saturation, Integer brightness) { DecimalType h = new DecimalType(hue); @@ -401,10 +383,6 @@ public static HSBType getHSBType(Integer hue, Integer saturation, Integer bright /** * Return QuantityType with Time - * - * @param numVal Number with value - * @param unit TimeUnit (Unit