From 0fd285d76b5f25002f84768ab490e750539509d0 Mon Sep 17 00:00:00 2001 From: nick Date: Tue, 2 Jul 2024 14:00:46 +0300 Subject: [PATCH 1/5] tbel: number to string by radix --- .../thingsboard/script/api/tbel/TbUtils.java | 489 +++++++++++++++--- .../script/api/tbel/TbUtilsTest.java | 278 ++++++++-- 2 files changed, 625 insertions(+), 142 deletions(-) diff --git a/common/script/script-api/src/main/java/org/thingsboard/script/api/tbel/TbUtils.java b/common/script/script-api/src/main/java/org/thingsboard/script/api/tbel/TbUtils.java index 283044b279..c8c66cada0 100644 --- a/common/script/script-api/src/main/java/org/thingsboard/script/api/tbel/TbUtils.java +++ b/common/script/script-api/src/main/java/org/thingsboard/script/api/tbel/TbUtils.java @@ -46,11 +46,22 @@ import java.util.Map; import java.util.Set; import java.util.regex.Matcher; +import static java.lang.Character.MAX_RADIX; +import static java.lang.Character.MIN_RADIX; + @Slf4j public class TbUtils { private static final byte[] HEX_ARRAY = "0123456789ABCDEF".getBytes(StandardCharsets.US_ASCII); + private static final int zeroRadix = 0; + private static final int octalRadix = 8; + private static final int decRadix = 10; + private static final int hexRadix = 16; + private static final int hexLenMin = -1; + private static final int hexLenIntMax = 8; + private static final int hexLenLongMax = 16; + private static final LinkedHashMap mdnEncodingReplacements = new LinkedHashMap<>(); static { @@ -103,6 +114,8 @@ public class TbUtils { String.class, int.class))); parserConfig.addImport("parseFloat", new MethodStub(TbUtils.class.getMethod("parseFloat", String.class))); + parserConfig.addImport("parseFloat", new MethodStub(TbUtils.class.getMethod("parseFloat", + String.class, int.class))); parserConfig.addImport("parseDouble", new MethodStub(TbUtils.class.getMethod("parseDouble", String.class))); parserConfig.addImport("parseLittleEndianHexToInt", new MethodStub(TbUtils.class.getMethod("parseLittleEndianHexToInt", @@ -175,6 +188,40 @@ public class TbUtils { float.class, int.class))); parserConfig.addImport("hexToBytes", new MethodStub(TbUtils.class.getMethod("hexToBytes", ExecutionContext.class, String.class))); + parserConfig.addImport("intToHex", new MethodStub(TbUtils.class.getMethod("intToHex", + Integer.class))); + parserConfig.addImport("intToHex", new MethodStub(TbUtils.class.getMethod("intToHex", + Integer.class, boolean.class))); + parserConfig.addImport("intToHex", new MethodStub(TbUtils.class.getMethod("intToHex", + Integer.class, boolean.class, boolean.class))); + parserConfig.addImport("intToHex", new MethodStub(TbUtils.class.getMethod("intToHex", + Integer.class, boolean.class, boolean.class, int.class))); + parserConfig.addImport("longToHex", new MethodStub(TbUtils.class.getMethod("longToHex", + Long.class))); + parserConfig.addImport("longToHex", new MethodStub(TbUtils.class.getMethod("longToHex", + Long.class, boolean.class))); + parserConfig.addImport("longToHex", new MethodStub(TbUtils.class.getMethod("longToHex", + Long.class, boolean.class, boolean.class))); + parserConfig.addImport("longToHex", new MethodStub(TbUtils.class.getMethod("longToHex", + Long.class, boolean.class, boolean.class, int.class))); + parserConfig.addImport("intLongToString", new MethodStub(TbUtils.class.getMethod("intLongToString", + Long.class))); + parserConfig.addImport("intLongToString", new MethodStub(TbUtils.class.getMethod("intLongToString", + Long.class, int.class))); + parserConfig.addImport("intLongToString", new MethodStub(TbUtils.class.getMethod("intLongToString", + Long.class, int.class, boolean.class))); + parserConfig.addImport("intLongToString", new MethodStub(TbUtils.class.getMethod("intLongToString", + Long.class, int.class, boolean.class, boolean.class))); + parserConfig.addImport("floatToHex", new MethodStub(TbUtils.class.getMethod("floatToHex", + Float.class))); + parserConfig.addImport("floatToHex", new MethodStub(TbUtils.class.getMethod("floatToHex", + Float.class, boolean.class))); + parserConfig.addImport("doubleToHex", new MethodStub(TbUtils.class.getMethod("doubleToHex", + Double.class))); + parserConfig.addImport("doubleToHex", new MethodStub(TbUtils.class.getMethod("doubleToHex", + Double.class, boolean.class))); + parserConfig.addImport("printUnsignedBytes", new MethodStub(TbUtils.class.getMethod("printUnsignedBytes", + ExecutionContext.class, List.class))); parserConfig.addImport("base64ToHex", new MethodStub(TbUtils.class.getMethod("base64ToHex", String.class))); parserConfig.addImport("base64ToBytes", new MethodStub(TbUtils.class.getMethod("base64ToBytes", @@ -197,6 +244,18 @@ public class TbUtils { String.class))); parserConfig.addImport("decodeURI", new MethodStub(TbUtils.class.getMethod("decodeURI", String.class))); + parserConfig.addImport("newError", new MethodStub(TbUtils.class.getMethod("newError", + String.class, Object.class))); + parserConfig.addImport("newError", new MethodStub(TbUtils.class.getMethod("newError", + String.class))); + parserConfig.addImport("isBinary", new MethodStub(TbUtils.class.getMethod("isBinary", + String.class))); + parserConfig.addImport("isOctal", new MethodStub(TbUtils.class.getMethod("isOctal", + String.class))); + parserConfig.addImport("isDecimal", new MethodStub(TbUtils.class.getMethod("isDecimal", + String.class))); + parserConfig.addImport("isHexadecimal", new MethodStub(TbUtils.class.getMethod("isHexadecimal", + String.class))); } public static String btoa(String input) { @@ -210,6 +269,7 @@ public class TbUtils { public static Object decodeToJson(ExecutionContext ctx, List bytesList) throws IOException { return TbJson.parse(ctx, bytesToString(bytesList)); } + public static Object decodeToJson(ExecutionContext ctx, String jsonStr) throws IOException { return TbJson.parse(ctx, jsonStr); } @@ -269,29 +329,28 @@ public class TbUtils { } public static Integer parseInt(String value) { - int radix = getRadix(value); - return parseInt(value, radix); + return parseInt(value, zeroRadix); } public static Integer parseInt(String value, int radix) { if (StringUtils.isNotBlank(value)) { try { String valueP = prepareNumberString(value); - isValidRadix(valueP, radix); + int radixValue = isValidStringAndRadix(valueP, radix, value); try { - return Integer.parseInt(valueP, radix); - } catch (NumberFormatException e) { - BigInteger bi = new BigInteger(valueP, radix); - if (bi.compareTo(BigInteger.valueOf(Integer.MAX_VALUE)) > 0) - throw new NumberFormatException("Value \"" + value + "\" is greater than the maximum Integer value " + Integer.MAX_VALUE + " !"); - if (bi.compareTo(BigInteger.valueOf(Integer.MIN_VALUE)) < 0) - throw new NumberFormatException("Value \"" + value + "\" is less than the minimum Integer value " + Integer.MIN_VALUE + " !"); - Float f = parseFloat(valueP); - if (f != null) { - return f.intValue(); - } else { - throw new NumberFormatException(e.getMessage()); + if (radixValue >= 25 && radixValue <= MAX_RADIX) { + return Integer.parseInt(valueP, radixValue); } + return switch (radixValue) { + case MIN_RADIX -> parseBinaryStringAsSignedInteger(valueP); + case octalRadix, decRadix, hexRadix -> Integer.parseInt(valueP, radixValue); + default -> throw new IllegalArgumentException("Invalid radix: [" + radix + "]"); + }; + } catch (NumberFormatException e) { + Integer iMax = Integer.MAX_VALUE; + Integer iMin = Integer.MIN_VALUE; + compareIntLongValueMinMax(valueP, radixValue, iMax.longValue(), iMin.longValue(), "Integer"); + throw new NumberFormatException(e.getMessage()); } } catch (NumberFormatException e) { throw new NumberFormatException(e.getMessage()); @@ -301,29 +360,26 @@ public class TbUtils { } public static Long parseLong(String value) { - int radix = getRadix(value); - return parseLong(value, radix); + return parseLong(value, zeroRadix); } public static Long parseLong(String value, int radix) { if (StringUtils.isNotBlank(value)) { try { String valueP = prepareNumberString(value); - isValidRadix(valueP, radix); + int radixValue = isValidStringAndRadix(valueP, radix, value); try { - return Long.parseLong(valueP, radix); - } catch (NumberFormatException e) { - BigInteger bi = new BigInteger(valueP, radix); - if (bi.compareTo(BigInteger.valueOf(Long.MAX_VALUE)) > 0) - throw new NumberFormatException("Value \"" + value + "\"is greater than the maximum Long value " + Long.MAX_VALUE + " !"); - if (bi.compareTo(BigInteger.valueOf(Long.MIN_VALUE)) < 0) - throw new NumberFormatException("Value \"" + value + "\" is less than the minimum Long value " + Long.MIN_VALUE + " !"); - Double dd = parseDouble(valueP); - if (dd != null) { - return dd.longValue(); - } else { - throw new NumberFormatException(e.getMessage()); + if (radixValue >= 25 && radixValue <= MAX_RADIX) { + return Long.parseLong(valueP, radixValue); } + return switch (radixValue) { + case MIN_RADIX -> parseBinaryStringAsSignedLong(valueP); + case octalRadix, decRadix, hexRadix -> Long.parseLong(valueP, radixValue); + default -> throw new IllegalArgumentException("Invalid radix: [" + radix + "]"); + }; + } catch (NumberFormatException e) { + compareIntLongValueMinMax(valueP, radixValue, Long.MAX_VALUE, Long.MIN_VALUE, "Long"); + throw new NumberFormatException(e.getMessage()); } } catch (NumberFormatException e) { throw new NumberFormatException(e.getMessage()); @@ -332,25 +388,96 @@ public class TbUtils { return null; } - private static int getRadix(String value, int... radixS) { - return radixS.length > 0 ? radixS[0] : isHexadecimal(value) ? 16 : 10; + private static int parseBinaryStringAsSignedInteger(String binaryString) { + if (binaryString.length() != 32) { + // Pad the binary string to 64 bits if it is not already + binaryString = String.format("%32s", binaryString).replace(' ', '0'); + } + + // If the MSB is 1, the number is negative in two's complement + if (binaryString.charAt(0) == '1') { + // Calculate the two's complement + String invertedBinaryString = invertBinaryString(binaryString); + int positiveValue = Integer.parseInt(invertedBinaryString, 2) + 1; + return -positiveValue; + } else { + return Integer.parseInt(binaryString, 2); + } + } + private static long parseBinaryStringAsSignedLong(String binaryString) { + if (binaryString.length() != 64) { + // Pad the binary string to 64 bits if it is not already + binaryString = String.format("%64s", binaryString).replace(' ', '0'); + } + + // If the MSB is 1, the number is negative in two's complement + if (binaryString.charAt(0) == '1') { + // Calculate the two's complement + String invertedBinaryString = invertBinaryString(binaryString); + long positiveValue = Long.parseLong(invertedBinaryString, 2) + 1; + return -positiveValue; + } else { + return Long.parseLong(binaryString, 2); + } + } + + private static String invertBinaryString(String binaryString) { + StringBuilder invertedString = new StringBuilder(); + for (char bit : binaryString.toCharArray()) { + invertedString.append(bit == '0' ? '1' : '0'); + } + return invertedString.toString(); + } + + private static int getRadix10_16(String value) { + int radix = isDecimal(value) > 0 ? decRadix : isHexadecimal(value); + if (radix > 0) { + return radix; + } else { + throw new NumberFormatException("Value: \"" + value + "\" is not numeric or hexDecimal format!"); + } } public static Float parseFloat(String value) { - if (value != null) { + return parseFloat(value, 0); + } + + public static Float parseFloat(String value, int radix) { + if (StringUtils.isNotBlank(value)) { + String valueP = prepareNumberString(value); + int radixValue = isValidStringAndRadix(valueP, radix, value); try { - return Float.parseFloat(prepareNumberString(value)); + if (radixValue == decRadix) { + return Float.parseFloat(value); + } else { + int bits = Integer.parseUnsignedInt(valueP, hexRadix); + return Float.intBitsToFloat(bits); + } } catch (NumberFormatException e) { + throw new NumberFormatException(e.getMessage()); } } return null; } public static Double parseDouble(String value) { + int radix = getRadix10_16(value); + return parseDouble(value, radix); + } + + public static Double parseDouble(String value, int radix) { if (value != null) { try { - return Double.parseDouble(prepareNumberString(value)); + String valueP = prepareNumberString(value); + int radixValue = isValidStringAndRadix(valueP, radix, value); + if (radixValue == decRadix) { + return Double.parseDouble(prepareNumberString(value)); + } else { + long bits = Long.parseUnsignedLong(valueP, hexRadix); + return Double.longBitsToDouble(bits); + } } catch (NumberFormatException e) { + throw new NumberFormatException(e.getMessage()); } } return null; @@ -368,9 +495,10 @@ public class TbUtils { return parseHexToInt(hex, true); } - public static int parseHexToInt(String hex, boolean bigEndian) { - byte[] data = prepareHexToBytesNumber(hex, 8); - return parseBytesToInt(data, 0, data.length, bigEndian); + public static Integer parseHexToInt(String value, boolean bigEndian) { + String hexValue = prepareNumberString(value); + String hex = bigEndian ? hexValue : reverseHexStringByOrder(hexValue); + return parseInt(hex, hexRadix); } public static long parseLittleEndianHexToLong(String hex) { @@ -385,9 +513,10 @@ public class TbUtils { return parseHexToLong(hex, true); } - public static long parseHexToLong(String hex, boolean bigEndian) { - byte[] data = prepareHexToBytesNumber(hex, 16); - return parseBytesToLong(data, 0, data.length, bigEndian); + public static Long parseHexToLong(String value, boolean bigEndian) { + String hexValue = prepareNumberString(value); + String hex = bigEndian ? value : reverseHexStringByOrder(hexValue); + return parseLong(hex, 16); } public static float parseLittleEndianHexToFloat(String hex) { @@ -402,9 +531,10 @@ public class TbUtils { return parseHexToFloat(hex, true); } - public static float parseHexToFloat(String hex, boolean bigEndian) { - byte[] data = prepareHexToBytesNumber(hex, 8); - return parseBytesToFloat(data, 0, bigEndian); + public static Float parseHexToFloat(String value, boolean bigEndian) { + String hexValue = prepareNumberString(value); + String hex = bigEndian ? value : reverseHexStringByOrder(hexValue); + return parseFloat(hex, hexRadix); } public static double parseLittleEndianHexToDouble(String hex) { @@ -419,39 +549,157 @@ public class TbUtils { return parseHexToDouble(hex, true); } - public static double parseHexToDouble(String hex, boolean bigEndian) { - byte[] data = prepareHexToBytesNumber(hex, 16); - return parseBytesToDouble(data, 0, bigEndian); + public static double parseHexToDouble(String value, boolean bigEndian) { + String hexValue = prepareNumberString(value); + String hex = bigEndian ? value : reverseHexStringByOrder(hexValue); + return parseDouble(hex, hexRadix); } - private static byte[] prepareHexToBytesNumber(String hex, int len) { - int length = hex.length(); - if (length > len) { - throw new IllegalArgumentException("Hex string is too large. Maximum 8 symbols allowed."); - } - if (length % 2 > 0) { + public static ExecutionArrayList hexToBytes(ExecutionContext ctx, String value) { + String hex = prepareNumberString(value); + int len = hex.length(); + if (len % 2 > 0) { throw new IllegalArgumentException("Hex string must be even-length."); } - byte[] data = new byte[length / 2]; - for (int i = 0; i < length; i += 2) { - data[i / 2] = (byte) ((Character.digit(hex.charAt(i), 16) << 4) + Character.digit(hex.charAt(i + 1), 16)); + ExecutionArrayList data = new ExecutionArrayList<>(ctx); + for (int i = 0; i < hex.length(); i += 2) { + // Extract two characters from the hex string + String byteString = hex.substring(i, i + 2); + // Parse the hex string to a byte + byte byteValue = (byte) Integer.parseInt(byteString, 16); + // Add the byte to the ArrayList + data.add(byteValue); } return data; } - public static ExecutionArrayList hexToBytes(ExecutionContext ctx, String hex) { - int len = hex.length(); - if (len % 2 > 0) { - throw new IllegalArgumentException("Hex string must be even-length."); - } - ExecutionArrayList data = new ExecutionArrayList<>(ctx); - for (int i = 0; i < len; i += 2) { - data.add((byte) ((Character.digit(hex.charAt(i), 16) << 4) - + Character.digit(hex.charAt(i + 1), 16))); + public static List printUnsignedBytes(ExecutionContext ctx, List byteArray) { + ExecutionArrayList data = new ExecutionArrayList<>(ctx); + for (Byte b : byteArray) { + // Convert signed byte to unsigned integer + int unsignedByte = Byte.toUnsignedInt(b); + data.add(unsignedByte); } return data; } + public static String intToHex(Integer i) { + return prepareNumberHexString(i.longValue(), true, false, hexLenMin, hexLenIntMax); + } + + public static String intToHex(Integer i, boolean bigEndian) { + return prepareNumberHexString(i.longValue(), bigEndian, false, hexLenMin, hexLenIntMax); + } + + public static String intToHex(Integer i, boolean bigEndian, boolean pref) { + return prepareNumberHexString(i.longValue(), bigEndian, pref, hexLenMin, hexLenIntMax); + } + + public static String intToHex(Integer i, boolean bigEndian, boolean pref, int len) { + return prepareNumberHexString(i.longValue(), bigEndian, pref, len, hexLenIntMax); + } + + public static String longToHex(Long l) { + return prepareNumberHexString(l, true, false, hexLenMin, hexLenLongMax); + } + public static String longToHex(Long l, boolean bigEndian) { + return prepareNumberHexString(l, bigEndian, false, hexLenMin, hexLenLongMax); + } + + public static String longToHex(Long l, boolean bigEndian, boolean pref) { + return prepareNumberHexString(l, bigEndian, pref, hexLenMin, hexLenLongMax); + } + + public static String longToHex(Long l, boolean bigEndian, boolean pref, int len) { + return prepareNumberHexString(l, bigEndian, pref, len, hexLenLongMax); + } + + public static String intLongToString(Long number) { + return intLongToString(number, 10); + } + + public static String intLongToString(Long number, int radix) { + return intLongToString(number, radix, true); + } + + public static String intLongToString(Long number, int radix, boolean bigEndian) { + return intLongToString(number, radix, bigEndian, false); + } + + public static String intLongToString(Long number, int radix, boolean bigEndian, boolean pref) { + if (radix >= 25 && radix <= MAX_RADIX) { + return Long.toString(number, radix); + } + return switch (radix) { + case MIN_RADIX -> Long.toBinaryString(number); + case 8 -> Long.toOctalString(number); + case 10 -> Long.toString(number); + case 16 -> prepareNumberHexString(number, bigEndian, pref, -1, -1); + default -> throw new IllegalArgumentException("Invalid radix: [" + radix + "]"); + }; + } + + private static void compareIntLongValueMinMax(String valueP, int radix, Long maxValue, Long minValue, String clazzName) { + BigInteger bi = new BigInteger(valueP, radix); + if (bi.compareTo(BigInteger.valueOf(maxValue)) > 0) { + throw new NumberFormatException("Value \"" + valueP + "\"is greater than the maximum " + clazzName + " value " + maxValue + " !"); + } else if (bi.compareTo(BigInteger.valueOf(minValue)) < 0) { + throw new NumberFormatException("Value \"" + valueP + "\" is less than the minimum " + clazzName + " value " + minValue + " !"); + } + } + + private static String prepareNumberHexString(Long number, boolean bigEndian, boolean pref, int len, int hexLenMax) { + String hex = Long.toHexString(number).toUpperCase(); + hexLenMax = hexLenMax < 0 ? hex.length() : hexLenMax; + String hexWithoutZeroFF = removeLeadingZero_FF(hex, number, hexLenMax); + hexWithoutZeroFF = bigEndian ? hexWithoutZeroFF : reverseHexStringByOrder(hexWithoutZeroFF); + len = len == hexLenMin ? hexWithoutZeroFF.length() : len; + String result = hexWithoutZeroFF.substring(hexWithoutZeroFF.length() - len); + return pref ? "0x" + result : result; + } + + private static String removeLeadingZero_FF(String hex, Long number, int hexLenMax) { + String hexWithoutZero = hex.replaceFirst("^0+(?!$)", ""); // Remove leading zeros except for the last one + if (number >= 0) { + return hexWithoutZero; + } else { + String hexWithoutZeroFF = hexWithoutZero.replaceFirst("^F+(?!$)", ""); + hexWithoutZeroFF = hexWithoutZeroFF.length() % 2 > 0 ? "F" + hexWithoutZeroFF : hexWithoutZeroFF; + if (hexWithoutZeroFF.length() > hexLenMax) { + return hexWithoutZeroFF.substring(hexWithoutZeroFF.length() - hexLenMax); + } else if (hexWithoutZeroFF.length() == hexLenMax) { + return hexWithoutZeroFF; + } else { + return "FF" + hexWithoutZeroFF; + } + } + } + + public static String floatToHex(Float f) { + return floatToHex(f, true); + } + + public static String floatToHex(Float f, boolean bigEndian) { + // Convert the float to its raw integer bits representation + int bits = Float.floatToRawIntBits(f); + + // Format the integer bits as a hexadecimal string + String result = String.format("0x%08X", bits); + return bigEndian ? result : reverseHexStringByOrder(result); + } + + public static String doubleToHex(Double d) { + return doubleToHex(d, true); + } + + public static String doubleToHex(Double d, boolean bigEndian) { + long bits = Double.doubleToRawLongBits(d); + + // Format the integer bits as a hexadecimal string + String result = String.format("0x%16X", bits); + return bigEndian ? result : reverseHexStringByOrder(result); + } + public static String base64ToHex(String base64) { return bytesToHex(Base64.getDecoder().decode(base64)); } @@ -647,6 +895,15 @@ public class TbUtils { return URLDecoder.decode(uri, StandardCharsets.UTF_8); } + public static void newError(String message) { + newError(message, null); + } + + public static void newError(String message, Object value) { + String msg = value == null ? message : message + " Value = " + value; + throw new RuntimeException(msg); + } + private static void parseRecursive(Object json, Map map, List excludeList, String path, boolean pathInKey) { if (json instanceof Map.Entry) { Map.Entry entry = (Map.Entry) json; @@ -687,42 +944,104 @@ public class TbUtils { } } - private static boolean isHexadecimal(String value) { - return value != null && (value.contains("0x") || value.contains("0X")); - } - private static String prepareNumberString(String value) { if (value != null) { value = value.trim(); - if (isHexadecimal(value)) { - value = value.replace("0x", ""); - value = value.replace("0X", ""); - } + value = value.replace("0x", ""); + value = value.replace("0X", ""); value = value.replace(",", "."); } return value; } - private static boolean isValidRadix(String value, int radix) { - for (int i = 0; i < value.length(); i++) { - if (i == 0 && value.charAt(i) == '-') { - if (value.length() == 1) - throw new NumberFormatException("Failed radix [" + radix + "] for value: \"" + value + "\"!"); - else - continue; + private static int isValidStringAndRadix(String valueP, int radix, String value) { + int radixValue; + if (radix == 0) { + radixValue = getRadix10_16(valueP); + } else if (radix >= 25 && radix <= MAX_RADIX) { + return radix; + } else { + radixValue = switch (radix) { + case MIN_RADIX -> isBinary(valueP); + case 8 -> isOctal(valueP); + case 10 -> isDecimal(valueP); + case 16 -> isHexadecimal(valueP); + default -> throw new IllegalArgumentException("Invalid radix: [" + radix + "]"); + + }; + } + + if (radixValue > 0) { + if (value.startsWith("0x")) radixValue = 16; + if (radixValue == 16) { + valueP = value.startsWith("-") ? value.substring(1) : value; + if (valueP.length() % 2 > 0) { + throw new NumberFormatException("The hexadecimal value: \"" + value + "\" must be of even length, or if the decimal value must be a number!"); + } + } + return radixValue; + } else { + if (radix > 0) { + throw new NumberFormatException("Failed radix [" + radix + "] for value: \"" + value + "\", must be [" + radixValue + "] !"); + } else { + throw new NumberFormatException("Invalid \"" + value + "\". It is not numeric or hexadecimal format!"); } - if (Character.digit(value.charAt(i), radix) < 0) - throw new NumberFormatException("Failed radix: [" + radix + "] for value: \"" + value + "\"!"); } - return true; } - private static byte isValidIntegerToByte (Integer val) { - if (val > 255 || val.intValue() < -128) { + public static int isBinary(String str) { + if (str == null || str.isEmpty()) { + return -1; + } + return str.matches("[01]+") ? MIN_RADIX : -1; + } + + public static int isOctal(String str) { + if (str == null || str.isEmpty()) { + return -1; + } + return str.matches("[0-7]+") ? octalRadix : -1; + } + + public static int isDecimal(String str) { + if (str == null || str.isEmpty()) { + return -1; + } + return str.matches("-?\\d+(\\.\\d+)?") ? decRadix : -1; + } + + public static int isHexadecimal(String str) { + if (str == null || str.isEmpty()) { + return -1; + } + return str.matches("^-?(0[xX])?[0-9a-fA-F]+$") ? hexRadix : -1; + } + + private static byte isValidIntegerToByte(Integer val) { + if (val > 255 || val < -128) { throw new NumberFormatException("The value '" + val + "' could not be correctly converted to a byte. " + "Integer to byte conversion requires the use of only 8 bits (with a range of min/max = -128/255)!"); } else { return val.byteValue(); } } + + private static String reverseHexStringByOrder(String value) { + if (value.startsWith("-")) { + throw new IllegalArgumentException("The hexadecimal string must be without a negative sign."); + } + boolean isHexPref = value.startsWith("0x"); + String hex = isHexPref ? value.substring(2) : value; + if (hex.length() % 2 > 0) { + throw new IllegalArgumentException("The hexadecimal string must be even-length."); + } + // Split the hex string into bytes (2 characters each) + StringBuilder reversedHex = new StringBuilder(8); + for (int i = hex.length() - 2; i >= 0; i -= 2) { + reversedHex.append(hex, i, i + 2); + } + String result = reversedHex.toString(); + return isHexPref ? "0x" + result : result; + } } + diff --git a/common/script/script-api/src/test/java/org/thingsboard/script/api/tbel/TbUtilsTest.java b/common/script/script-api/src/test/java/org/thingsboard/script/api/tbel/TbUtilsTest.java index b4e7e7411d..42c8e84b7d 100644 --- a/common/script/script-api/src/test/java/org/thingsboard/script/api/tbel/TbUtilsTest.java +++ b/common/script/script-api/src/test/java/org/thingsboard/script/api/tbel/TbUtilsTest.java @@ -15,12 +15,14 @@ */ package org.thingsboard.script.api.tbel; +import com.google.common.collect.Lists; import com.google.common.primitives.Bytes; +import com.google.common.primitives.Ints; import lombok.extern.slf4j.Slf4j; -import org.junit.jupiter.api.Test; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; import org.mvel2.ExecutionContext; import org.mvel2.ParserContext; import org.mvel2.SandboxedParserConfiguration; @@ -31,28 +33,28 @@ import java.io.IOException; import java.math.BigInteger; import java.nio.ByteBuffer; import java.util.ArrayList; +import java.util.Arrays; import java.util.Calendar; +import java.util.Collections; import java.util.List; import java.util.Random; +import static java.lang.Character.MAX_RADIX; +import static java.lang.Character.MIN_RADIX; + @Slf4j public class TbUtilsTest { private ExecutionContext ctx; - private final String intValHex = "41EA62CC"; private final float floatVal = 29.29824f; - private final String floatValStr = "29.29824"; - - private final String floatValHexRev = "CC62EA41"; private final float floatValRev = -5.948442E7f; private final long longVal = 0x409B04B10CB295EAL; private final String longValHex = "409B04B10CB295EA"; - private final long longValRev = 0xEA95B20CB1049B40L; private final String longValHexRev = "EA95B20CB1049B40"; - private final String doubleValStr = "1729.1729"; + private final double doubleVal = 1729.1729; private final double doubleValRev = -2.7208640774822924E205; @@ -86,10 +88,9 @@ public class TbUtilsTest { Assertions.assertEquals(0xBAAB, TbUtils.parseHexToInt("ABBA", false)); Assertions.assertEquals(0xAABBCC, TbUtils.parseHexToInt("AABBCC", true)); Assertions.assertEquals(0xAABBCC, TbUtils.parseHexToInt("CCBBAA", false)); - Assertions.assertEquals(0xAABBCCDD, TbUtils.parseHexToInt("AABBCCDD", true)); - Assertions.assertEquals(0xAABBCCDD, TbUtils.parseHexToInt("DDCCBBAA", false)); - Assertions.assertEquals(0xDDCCBBAA, TbUtils.parseHexToInt("DDCCBBAA", true)); - Assertions.assertEquals(0xDDCCBBAA, TbUtils.parseHexToInt("AABBCCDD", false)); + Assertions.assertThrows(NumberFormatException.class, () -> TbUtils.parseHexToInt("AABBCCDD", true)); + Assertions.assertEquals(0x11BBCC22, TbUtils.parseHexToInt("11BBCC22", true)); + Assertions.assertEquals(0x11BBCC22, TbUtils.parseHexToInt("22CCBB11", false)); } @Test @@ -208,15 +209,28 @@ public class TbUtilsTest { Assertions.assertNull(TbUtils.parseInt("")); Assertions.assertNull(TbUtils.parseInt(" ")); - Assertions.assertEquals(java.util.Optional.of(0).get(), TbUtils.parseInt("0")); - Assertions.assertEquals(java.util.Optional.of(0).get(), TbUtils.parseInt("-0")); + Assertions.assertEquals((Integer) 0, TbUtils.parseInt("0")); + Assertions.assertEquals((Integer) 0, TbUtils.parseInt("-0")); Assertions.assertEquals(java.util.Optional.of(473).get(), TbUtils.parseInt("473")); Assertions.assertEquals(java.util.Optional.of(-255).get(), TbUtils.parseInt("-0xFF")); - Assertions.assertThrows(NumberFormatException.class, () -> TbUtils.parseInt("FF")); + Assertions.assertThrows(NumberFormatException.class, () -> TbUtils.parseInt("-0xFF123")); + Assertions.assertEquals(java.util.Optional.of(-255).get(), TbUtils.parseInt("-FF")); + Assertions.assertEquals(java.util.Optional.of(255).get(), TbUtils.parseInt("FF")); + Assertions.assertThrows(IllegalArgumentException.class, () -> TbUtils.parseInt("FFF")); + Assertions.assertEquals(java.util.Optional.of(-2578).get(), TbUtils.parseInt("-0A12")); + Assertions.assertEquals(java.util.Optional.of(-2578).get(), TbUtils.parseHexToInt("-0A12")); + Assertions.assertThrows(IllegalArgumentException.class, () -> TbUtils.parseHexToInt("A12", false)); + Assertions.assertEquals(java.util.Optional.of(-14866).get(), TbUtils.parseBigEndianHexToInt("-3A12")); + Assertions.assertThrows(IllegalArgumentException.class, () -> TbUtils.parseLittleEndianHexToInt("-A12")); + Assertions.assertThrows(NumberFormatException.class, () -> TbUtils.parseInt("0xFG")); Assertions.assertEquals(java.util.Optional.of(102).get(), TbUtils.parseInt("1100110", 2)); + Assertions.assertEquals(java.util.Optional.of(-102).get(), TbUtils.parseInt("1111111111111111111111111111111111111111111111111111111110011010", 2)); Assertions.assertThrows(NumberFormatException.class, () -> TbUtils.parseInt("1100210", 2)); + Assertions.assertEquals(java.util.Optional.of(13158).get(), TbUtils.parseInt("11001101100110", 2)); + Assertions.assertEquals(java.util.Optional.of(-13158).get(), TbUtils.parseInt("1111111111111111111111111111111111111111111111111100110010011010", 2)); + Assertions.assertEquals(java.util.Optional.of(63).get(), TbUtils.parseInt("77", 8)); Assertions.assertThrows(NumberFormatException.class, () -> TbUtils.parseInt("18", 8)); @@ -224,19 +238,32 @@ public class TbUtilsTest { Assertions.assertEquals(java.util.Optional.of(-255).get(), TbUtils.parseInt("-FF", 16)); Assertions.assertThrows(NumberFormatException.class, () -> TbUtils.parseInt("FG", 16)); - Assertions.assertEquals(java.util.Optional.of(Integer.MAX_VALUE).get(), TbUtils.parseInt(Integer.toString(Integer.MAX_VALUE), 10)); Assertions.assertThrows(NumberFormatException.class, () -> TbUtils.parseInt(BigInteger.valueOf(Integer.MAX_VALUE).add(BigInteger.valueOf(1)).toString(10), 10)); Assertions.assertEquals(java.util.Optional.of(Integer.MIN_VALUE).get(), TbUtils.parseInt(Integer.toString(Integer.MIN_VALUE), 10)); Assertions.assertThrows(NumberFormatException.class, () -> TbUtils.parseInt(BigInteger.valueOf(Integer.MIN_VALUE).subtract(BigInteger.valueOf(1)).toString(10), 10)); Assertions.assertEquals(java.util.Optional.of(506070563).get(), TbUtils.parseInt("KonaIn", 30)); + Assertions.assertThrows(NumberFormatException.class, () -> TbUtils.parseInt("KonaIn", 10)); + Assertions.assertThrows(NumberFormatException.class, () -> TbUtils.parseInt(".456", 10)); + Assertions.assertThrows(NumberFormatException.class, () -> TbUtils.parseInt("4562.", 10)); + + Assertions.assertThrows(IllegalArgumentException.class, () -> TbUtils.parseInt("KonaIn", MAX_RADIX+1)); + Assertions.assertThrows(IllegalArgumentException.class, () -> TbUtils.parseInt("KonaIn", MIN_RADIX-1)); + Assertions.assertThrows(IllegalArgumentException.class, () -> TbUtils.parseInt("KonaIn", 12)); } @Test public void parseFloat() { + String floatValStr = "29.29824"; Assertions.assertEquals(java.util.Optional.of(floatVal).get(), TbUtils.parseFloat(floatValStr)); + String floatValHex = "41EA62CC"; + Assertions.assertEquals(0, Float.compare(floatVal, TbUtils.parseHexToFloat(floatValHex))); + Assertions.assertEquals(0, Float.compare(floatValRev, TbUtils.parseHexToFloat(floatValHex, false))); + Assertions.assertEquals(0, Float.compare(floatVal, TbUtils.parseBigEndianHexToFloat(floatValHex))); + String floatValHexRev = "CC62EA41"; + Assertions.assertEquals(0, Float.compare(floatVal, TbUtils.parseLittleEndianHexToFloat(floatValHexRev))); } @Test @@ -246,21 +273,13 @@ public class TbUtilsTest { Assertions.assertEquals(0, Float.compare(29.298f, actualF)); } - @Test - public void parseHexToFloat() { - Assertions.assertEquals(0, Float.compare(floatVal, TbUtils.parseHexToFloat(intValHex))); - Assertions.assertEquals(0, Float.compare(floatValRev, TbUtils.parseHexToFloat(intValHex, false))); - Assertions.assertEquals(0, Float.compare(floatVal, TbUtils.parseBigEndianHexToFloat(intValHex))); - Assertions.assertEquals(0, Float.compare(floatVal, TbUtils.parseLittleEndianHexToFloat(floatValHexRev))); - } - @Test public void arseBytesToFloat() { byte[] floatValByte = {65, -22, 98, -52}; Assertions.assertEquals(0, Float.compare(floatVal, TbUtils.parseBytesToFloat(floatValByte, 0))); Assertions.assertEquals(0, Float.compare(floatValRev, TbUtils.parseBytesToFloat(floatValByte, 0, false))); - List floatVaList = Bytes.asList(floatValByte); + List floatVaList = Bytes.asList(floatValByte); Assertions.assertEquals(0, Float.compare(floatVal, TbUtils.parseBytesToFloat(floatVaList, 0))); Assertions.assertEquals(0, Float.compare(floatValRev, TbUtils.parseBytesToFloat(floatVaList, 0, false))); } @@ -271,14 +290,15 @@ public class TbUtilsTest { Assertions.assertNull(TbUtils.parseLong("")); Assertions.assertNull(TbUtils.parseLong(" ")); - Assertions.assertEquals(java.util.Optional.of(0L).get(), TbUtils.parseLong("0")); - Assertions.assertEquals(java.util.Optional.of(0L).get(), TbUtils.parseLong("-0")); + Assertions.assertEquals((Long) 0L, TbUtils.parseLong("0")); + Assertions.assertEquals((Long) 0L, TbUtils.parseLong("-0")); Assertions.assertEquals(java.util.Optional.of(473L).get(), TbUtils.parseLong("473")); Assertions.assertEquals(java.util.Optional.of(-65535L).get(), TbUtils.parseLong("-0xFFFF")); - Assertions.assertThrows(NumberFormatException.class, () -> TbUtils.parseLong("FFFFFFFF")); + Assertions.assertEquals(java.util.Optional.of(4294967295L).get(), TbUtils.parseLong("FFFFFFFF")); Assertions.assertThrows(NumberFormatException.class, () -> TbUtils.parseLong("0xFGFFFFFF")); Assertions.assertEquals(java.util.Optional.of(13158L).get(), TbUtils.parseLong("11001101100110", 2)); + Assertions.assertEquals(java.util.Optional.of(-13158L).get(), TbUtils.parseLong("1111111111111111111111111111111111111111111111111100110010011010", 2)); Assertions.assertThrows(NumberFormatException.class, () -> TbUtils.parseLong("11001101100210", 2)); Assertions.assertEquals(java.util.Optional.of(9223372036854775807L).get(), TbUtils.parseLong("777777777777777777777", 8)); @@ -295,10 +315,7 @@ public class TbUtilsTest { Assertions.assertEquals(java.util.Optional.of(218840926543L).get(), TbUtils.parseLong("KonaLong", 27)); Assertions.assertThrows(NumberFormatException.class, () -> TbUtils.parseLong("KonaLong", 10)); - } - @Test - public void parseHexToLong() { Assertions.assertEquals(longVal, TbUtils.parseHexToLong(longValHex)); Assertions.assertEquals(longVal, TbUtils.parseHexToLong(longValHexRev, false)); Assertions.assertEquals(longVal, TbUtils.parseBigEndianHexToLong(longValHex)); @@ -312,14 +329,20 @@ public class TbUtilsTest { Bytes.reverse(longValByte); Assertions.assertEquals(longVal, TbUtils.parseBytesToLong(longValByte, 0, 8, false)); - List longVaList = Bytes.asList(longValByte); + List longVaList = Bytes.asList(longValByte); Assertions.assertEquals(longVal, TbUtils.parseBytesToLong(longVaList, 0, 8, false)); + long longValRev = 0xEA95B20CB1049B40L; Assertions.assertEquals(longValRev, TbUtils.parseBytesToLong(longVaList, 0, 8)); } @Test public void parsDouble() { + String doubleValStr = "1729.1729"; Assertions.assertEquals(java.util.Optional.of(doubleVal).get(), TbUtils.parseDouble(doubleValStr)); + Assertions.assertEquals(0, Double.compare(doubleVal, TbUtils.parseHexToDouble(longValHex))); + Assertions.assertEquals(0, Double.compare(doubleValRev, TbUtils.parseHexToDouble(longValHex, false))); + Assertions.assertEquals(0, Double.compare(doubleVal, TbUtils.parseBigEndianHexToDouble(longValHex))); + Assertions.assertEquals(0, Double.compare(doubleVal, TbUtils.parseLittleEndianHexToDouble(longValHexRev))); } @Test @@ -329,21 +352,13 @@ public class TbUtilsTest { Assertions.assertEquals(0, Double.compare(1729.173, actualD)); } - @Test - public void parseHexToDouble() { - Assertions.assertEquals(0, Double.compare(doubleVal, TbUtils.parseHexToDouble(longValHex))); - Assertions.assertEquals(0, Double.compare(doubleValRev, TbUtils.parseHexToDouble(longValHex, false))); - Assertions.assertEquals(0, Double.compare(doubleVal, TbUtils.parseBigEndianHexToDouble(longValHex))); - Assertions.assertEquals(0, Double.compare(doubleVal, TbUtils.parseLittleEndianHexToDouble(longValHexRev))); - } - @Test public void parseBytesToDouble() { byte[] doubleValByte = {64, -101, 4, -79, 12, -78, -107, -22}; Assertions.assertEquals(0, Double.compare(doubleVal, TbUtils.parseBytesToDouble(doubleValByte, 0))); Assertions.assertEquals(0, Double.compare(doubleValRev, TbUtils.parseBytesToDouble(doubleValByte, 0, false))); - List doubleVaList = Bytes.asList(doubleValByte); + List doubleVaList = Bytes.asList(doubleValByte); Assertions.assertEquals(0, Double.compare(doubleVal, TbUtils.parseBytesToDouble(doubleVaList, 0))); Assertions.assertEquals(0, Double.compare(doubleValRev, TbUtils.parseBytesToDouble(doubleVaList, 0, false))); } @@ -354,16 +369,17 @@ public class TbUtilsTest { ExecutionHashMap expectedJson = new ExecutionHashMap<>(1, ctx); expectedJson.put("hello", "world"); List expectedBytes = TbUtils.stringToBytes(ctx, expectedStr); - Object actualJson = TbUtils.decodeToJson(ctx, expectedBytes); - Assertions.assertEquals(expectedJson,actualJson); + Object actualJson = TbUtils.decodeToJson(ctx, expectedBytes); + Assertions.assertEquals(expectedJson, actualJson); } + @Test public void parseStringDecodeToJson() throws IOException { String expectedStr = "{\"hello\": \"world\"}"; ExecutionHashMap expectedJson = new ExecutionHashMap<>(1, ctx); expectedJson.put("hello", "world"); - Object actualJson = TbUtils.decodeToJson(ctx, expectedStr); - Assertions.assertEquals(expectedJson,actualJson); + Object actualJson = TbUtils.decodeToJson(ctx, expectedStr); + Assertions.assertEquals(expectedJson, actualJson); } @Test @@ -386,8 +402,8 @@ public class TbUtilsTest { @Test public void bytesFromList() { - byte[] arrayBytes = {(byte)0x00, (byte)0x08, (byte)0x10, (byte)0x1C, (byte)0xFF, (byte)0xFC, (byte)0xAD, (byte)0x88, (byte)0x75, (byte)0x74, (byte)0x8A, (byte)0x82}; - Object[] arrayMix = { "0x00", 8, "16", "0x1C", 255, (byte)0xFC, 173, 136, 117, 116, -118, "-126"}; + byte[] arrayBytes = {(byte) 0x00, (byte) 0x08, (byte) 0x10, (byte) 0x1C, (byte) 0xFF, (byte) 0xFC, (byte) 0xAD, (byte) 0x88, (byte) 0x75, (byte) 0x74, (byte) 0x8A, (byte) 0x82}; + Object[] arrayMix = {"0x00", 8, "16", "0x1C", 255, (byte) 0xFC, 173, 136, 117, 116, -118, "-126"}; String expected = new String(arrayBytes); ArrayList listBytes = new ArrayList<>(arrayBytes.length); @@ -397,12 +413,36 @@ public class TbUtilsTest { Assertions.assertEquals(expected, TbUtils.bytesToString(listBytes)); ArrayList listMix = new ArrayList<>(arrayMix.length); - for (Object element : arrayMix) { - listMix.add(element); - } + Collections.addAll(listMix, arrayMix); Assertions.assertEquals(expected, TbUtils.bytesToString(listMix)); } + @Test + public void bytesFromList_SpecSymbol() { + List listHex = new ArrayList<>(Arrays.asList("1D", "0x1D", "1F", "0x1F", "0x20", "0x20")); + byte[] expectedBytes = new byte[]{29, 29, 31, 31, 32, 32}; + String actualStr = TbUtils.bytesToString(listHex); + byte[] actualBytes = actualStr.getBytes(); + Assertions.assertArrayEquals(expectedBytes, actualBytes); + Assertions.assertTrue(actualStr.isBlank()); + listHex = new ArrayList<>(Arrays.asList("0x21", "0x21")); + expectedBytes = new byte[]{33, 33}; + actualStr = TbUtils.bytesToString(listHex); + actualBytes = actualStr.getBytes(); + Assertions.assertArrayEquals(expectedBytes, actualBytes); + Assertions.assertFalse(actualStr.isBlank()); + Assertions.assertEquals("!!", actualStr); + listHex = new ArrayList<>(Arrays.asList("21", "0x21")); + expectedBytes = new byte[]{21, 33}; + actualStr = TbUtils.bytesToString(listHex); + actualBytes = actualStr.getBytes(); + Assertions.assertArrayEquals(expectedBytes, actualBytes); + Assertions.assertFalse(actualStr.isBlank()); + Assertions.assertEquals("!", actualStr.substring(1)); + Assertions.assertEquals('\u0015', actualStr.charAt(0)); + Assertions.assertEquals(21, actualStr.charAt(0)); + } + @Test public void bytesFromList_Error() { List listHex = new ArrayList<>(); @@ -411,14 +451,7 @@ public class TbUtilsTest { TbUtils.bytesToString(listHex); Assertions.fail("Should throw NumberFormatException"); } catch (NumberFormatException e) { - Assertions.assertTrue(e.getMessage().contains("Failed radix: [16] for value: \"FG\"!")); - } - listHex.add(0, "1F"); - try { - TbUtils.bytesToString(listHex); - Assertions.fail("Should throw NumberFormatException"); - } catch (NumberFormatException e) { - Assertions.assertTrue(e.getMessage().contains("Failed radix: [10] for value: \"1F\"!")); + Assertions.assertTrue(e.getMessage().contains("Value: \"FG\" is not numeric or hexDecimal format!")); } List listIntString = new ArrayList<>(); @@ -482,6 +515,137 @@ public class TbUtilsTest { String uriDecodeActual = TbUtils.decodeURI(uriEncodeActual); Assertions.assertEquals(uriOriginal, uriDecodeActual); } + + @Test + public void intToHex_Test() { + Assertions.assertEquals("FFF5EE", TbUtils.intToHex(-2578)); + Assertions.assertEquals("0xFFD8FFA6", TbUtils.intToHex(0xFFD8FFA6, true, true)); + Assertions.assertEquals("0xA6FFD8FF", TbUtils.intToHex(0xFFD8FFA6, false, true)); + Assertions.assertEquals("0x7FFFFFFF", TbUtils.intToHex(Integer.MAX_VALUE, true, true)); + Assertions.assertEquals("0x80000000", TbUtils.intToHex(Integer.MIN_VALUE, true, true)); + Assertions.assertEquals("0xAB", TbUtils.intToHex(0xAB, true, true)); + Assertions.assertEquals("0xABCD", TbUtils.intToHex(0xABCD, true, true)); + Assertions.assertEquals("0xABCDEF", TbUtils.intToHex(0xABCDEF, true, true)); + Assertions.assertEquals("0xCDAB", TbUtils.intToHex(0xABCDEF, false, true, 4)); + Assertions.assertEquals("0xAB", TbUtils.intToHex(171, true, true)); + Assertions.assertEquals("0xAB", TbUtils.intToHex(0xAB, false, true)); + Assertions.assertEquals("0xAB", TbUtils.intToHex(0xAB, true, true, 2)); + Assertions.assertEquals("AB", TbUtils.intToHex(0xAB, false, false, 2)); + Assertions.assertEquals("AB", TbUtils.intToHex(171, true, false)); + Assertions.assertEquals("0xAB", TbUtils.intToHex(0xAB, true, true)); + Assertions.assertEquals("0xAB", TbUtils.intToHex(0xAB, false, true)); + Assertions.assertEquals("AB", TbUtils.intToHex(0xAB, false, false)); + + Assertions.assertEquals("0xABCD", TbUtils.intToHex(0xABCD, true, true)); + Assertions.assertEquals("0xCDAB", TbUtils.intToHex(0xABCD, false, true)); + Assertions.assertEquals("0xCD", TbUtils.intToHex(0xABCD, true, true, 2)); + Assertions.assertEquals("AB", TbUtils.intToHex(0xABCD, false, false, 2)); + } + @Test + public void longToHex_Test() { + Assertions.assertEquals("0x7FFFFFFFFFFFFFFF", TbUtils.longToHex(Long.MAX_VALUE, true, true)); + Assertions.assertEquals("0x8000000000000000", TbUtils.longToHex(Long.MIN_VALUE, true, true)); + Assertions.assertEquals("0xFFD8FFA6FFD8FFA6", TbUtils.longToHex(0xFFD8FFA6FFD8FFA6L, true, true)); + Assertions.assertEquals("0xA6FFD8FFA6FFCEFF", TbUtils.longToHex(0xFFCEFFA6FFD8FFA6L, false, true)); + Assertions.assertEquals("0xAB", TbUtils.longToHex(0xABL, true, true)); + Assertions.assertEquals("0xABCD", TbUtils.longToHex(0xABCDL, true, true)); + Assertions.assertEquals("0xABCDEF", TbUtils.longToHex(0xABCDEFL, true, true)); + Assertions.assertEquals("0xABEFCDAB", TbUtils.longToHex(0xABCDEFABCDEFL, false, true, 8)); + Assertions.assertEquals("0xAB", TbUtils.longToHex(0xABL, true, true, 2)); + Assertions.assertEquals("AB", TbUtils.longToHex(0xABL, false, false, 2)); + + Assertions.assertEquals("0xFFA6", TbUtils.longToHex(0xFFD8FFA6FFD8FFA6L, true, true, 4)); + Assertions.assertEquals("D8FF", TbUtils.longToHex(0xFFD8FFA6FFD8FFA6L, false, false, 4)); + } + + @Test + public void numberToString_Test() { + // 0011 0011 0110 0110 == 13158L + // 1100 1100 1001 1010 == -13158L + Assertions.assertEquals("11001101100110", TbUtils.intLongToString(13158L, 2)); + Assertions.assertEquals("1111111111111111111111111111111111111111111111111111111110011010", TbUtils.intLongToString(-102L, 2)); + Assertions.assertEquals("1111111111111111111111111111111111111111111111111100110010011010", TbUtils.intLongToString(-13158L, 2)); + Assertions.assertEquals("777777777777777777777", TbUtils.intLongToString(Long.MAX_VALUE, 8)); + Assertions.assertEquals("1000000000000000000000", TbUtils.intLongToString(Long.MIN_VALUE, 8)); + Assertions.assertEquals("9223372036854775807", TbUtils.intLongToString(Long.MAX_VALUE)); + Assertions.assertEquals("-9223372036854775808", TbUtils.intLongToString(Long.MIN_VALUE)); + Assertions.assertEquals("3366", TbUtils.intLongToString(13158L, 16)); + Assertions.assertEquals("FFCC9A", TbUtils.intLongToString(-13158L, 16)); + Assertions.assertEquals("0xFFCC9A", TbUtils.intLongToString(-13158L, 16, true, true)); + Assertions.assertEquals("hazelnut", TbUtils.intLongToString(1356099454469L, MAX_RADIX)); + } + + @Test + public void intToHexWithPrintUnsignedBytes_Test() { + Integer value = -40; + String intToHexLe = TbUtils.intToHex(value, false, true); + String intToHexBe = TbUtils.intToHex(value, true, true); + + List hexTopByteLe = TbUtils.hexToBytes(ctx, intToHexLe); + List hexTopByteBe = TbUtils.hexToBytes(ctx, intToHexBe); + + byte[] arrayBytes = {-40, -1}; + List expectedHexTopByteLe = Bytes.asList(arrayBytes); + List expectedHexTopByteBe = Lists.reverse(expectedHexTopByteLe); + Assertions.assertEquals(expectedHexTopByteLe, hexTopByteLe); + Assertions.assertEquals(expectedHexTopByteBe, hexTopByteBe); + + List actualLe = TbUtils.printUnsignedBytes(ctx, hexTopByteLe); + List actualBe = TbUtils.printUnsignedBytes(ctx, hexTopByteBe); + List expectedLe = Ints.asList(216, 255); + List expectedBe = Lists.reverse(expectedLe); + Assertions.assertEquals(expectedLe, actualLe); + Assertions.assertEquals(expectedBe, actualBe); + } + + @Test + public void floatToHex_Test() { + Float value = 20.89f; + String expectedHex = "0x41A71EB8"; + String valueHexRev = "0xB81EA741"; + String actual = TbUtils.floatToHex(value); + Assertions.assertEquals(expectedHex, actual); + Float valueActual = TbUtils.parseHexToFloat(actual); + Assertions.assertEquals(value, valueActual); + valueActual = TbUtils.parseHexToFloat(valueHexRev, false); + Assertions.assertEquals(value, valueActual); + } + @Test + public void doubleToHex_Test() { + String expectedHex = "0x409B04B10CB295EA"; + String actual = TbUtils.doubleToHex(doubleVal); + Assertions.assertEquals(expectedHex, actual); + Double valueActual = TbUtils.parseHexToDouble(actual); + Assertions.assertEquals(doubleVal, valueActual); + actual = TbUtils.doubleToHex(doubleVal, false); + String expectedHexRev = "0xEA95B20CB1049B40"; + Assertions.assertEquals(expectedHexRev, actual); + } + + @Test + public void newError_Test() { + String message = "frequency_weighting_type must be 0, 1 or 2."; + Object value = 4; + try { + TbUtils.newError(message, value); + Assertions.fail("Should throw NumberFormatException"); + } catch (RuntimeException e) { + Assertions.assertTrue(e.getMessage().contains("frequency_weighting_type must be 0, 1 or 2. Value = 4")); + } + try { + TbUtils.newError(message); + Assertions.fail("Should throw NumberFormatException"); + } catch (RuntimeException e) { + Assertions.assertTrue(e.getMessage().contains("frequency_weighting_type must be 0, 1 or 2.")); + } + } + + @Test + public void isNumeric_Test() { + + + } + private static List toList(byte[] data) { List result = new ArrayList<>(data.length); for (Byte b : data) { From 4d5200295322f211d3b9c014da104c6d00922193 Mon Sep 17 00:00:00 2001 From: nick Date: Tue, 2 Jul 2024 15:37:19 +0300 Subject: [PATCH 2/5] Revert "tbel: number to string by radix" This reverts commit 0fd285d76b5f25002f84768ab490e750539509d0. --- .../thingsboard/script/api/tbel/TbUtils.java | 489 +++--------------- .../script/api/tbel/TbUtilsTest.java | 278 ++-------- 2 files changed, 142 insertions(+), 625 deletions(-) diff --git a/common/script/script-api/src/main/java/org/thingsboard/script/api/tbel/TbUtils.java b/common/script/script-api/src/main/java/org/thingsboard/script/api/tbel/TbUtils.java index c8c66cada0..283044b279 100644 --- a/common/script/script-api/src/main/java/org/thingsboard/script/api/tbel/TbUtils.java +++ b/common/script/script-api/src/main/java/org/thingsboard/script/api/tbel/TbUtils.java @@ -46,22 +46,11 @@ import java.util.Map; import java.util.Set; import java.util.regex.Matcher; -import static java.lang.Character.MAX_RADIX; -import static java.lang.Character.MIN_RADIX; - @Slf4j public class TbUtils { private static final byte[] HEX_ARRAY = "0123456789ABCDEF".getBytes(StandardCharsets.US_ASCII); - private static final int zeroRadix = 0; - private static final int octalRadix = 8; - private static final int decRadix = 10; - private static final int hexRadix = 16; - private static final int hexLenMin = -1; - private static final int hexLenIntMax = 8; - private static final int hexLenLongMax = 16; - private static final LinkedHashMap mdnEncodingReplacements = new LinkedHashMap<>(); static { @@ -114,8 +103,6 @@ public class TbUtils { String.class, int.class))); parserConfig.addImport("parseFloat", new MethodStub(TbUtils.class.getMethod("parseFloat", String.class))); - parserConfig.addImport("parseFloat", new MethodStub(TbUtils.class.getMethod("parseFloat", - String.class, int.class))); parserConfig.addImport("parseDouble", new MethodStub(TbUtils.class.getMethod("parseDouble", String.class))); parserConfig.addImport("parseLittleEndianHexToInt", new MethodStub(TbUtils.class.getMethod("parseLittleEndianHexToInt", @@ -188,40 +175,6 @@ public class TbUtils { float.class, int.class))); parserConfig.addImport("hexToBytes", new MethodStub(TbUtils.class.getMethod("hexToBytes", ExecutionContext.class, String.class))); - parserConfig.addImport("intToHex", new MethodStub(TbUtils.class.getMethod("intToHex", - Integer.class))); - parserConfig.addImport("intToHex", new MethodStub(TbUtils.class.getMethod("intToHex", - Integer.class, boolean.class))); - parserConfig.addImport("intToHex", new MethodStub(TbUtils.class.getMethod("intToHex", - Integer.class, boolean.class, boolean.class))); - parserConfig.addImport("intToHex", new MethodStub(TbUtils.class.getMethod("intToHex", - Integer.class, boolean.class, boolean.class, int.class))); - parserConfig.addImport("longToHex", new MethodStub(TbUtils.class.getMethod("longToHex", - Long.class))); - parserConfig.addImport("longToHex", new MethodStub(TbUtils.class.getMethod("longToHex", - Long.class, boolean.class))); - parserConfig.addImport("longToHex", new MethodStub(TbUtils.class.getMethod("longToHex", - Long.class, boolean.class, boolean.class))); - parserConfig.addImport("longToHex", new MethodStub(TbUtils.class.getMethod("longToHex", - Long.class, boolean.class, boolean.class, int.class))); - parserConfig.addImport("intLongToString", new MethodStub(TbUtils.class.getMethod("intLongToString", - Long.class))); - parserConfig.addImport("intLongToString", new MethodStub(TbUtils.class.getMethod("intLongToString", - Long.class, int.class))); - parserConfig.addImport("intLongToString", new MethodStub(TbUtils.class.getMethod("intLongToString", - Long.class, int.class, boolean.class))); - parserConfig.addImport("intLongToString", new MethodStub(TbUtils.class.getMethod("intLongToString", - Long.class, int.class, boolean.class, boolean.class))); - parserConfig.addImport("floatToHex", new MethodStub(TbUtils.class.getMethod("floatToHex", - Float.class))); - parserConfig.addImport("floatToHex", new MethodStub(TbUtils.class.getMethod("floatToHex", - Float.class, boolean.class))); - parserConfig.addImport("doubleToHex", new MethodStub(TbUtils.class.getMethod("doubleToHex", - Double.class))); - parserConfig.addImport("doubleToHex", new MethodStub(TbUtils.class.getMethod("doubleToHex", - Double.class, boolean.class))); - parserConfig.addImport("printUnsignedBytes", new MethodStub(TbUtils.class.getMethod("printUnsignedBytes", - ExecutionContext.class, List.class))); parserConfig.addImport("base64ToHex", new MethodStub(TbUtils.class.getMethod("base64ToHex", String.class))); parserConfig.addImport("base64ToBytes", new MethodStub(TbUtils.class.getMethod("base64ToBytes", @@ -244,18 +197,6 @@ public class TbUtils { String.class))); parserConfig.addImport("decodeURI", new MethodStub(TbUtils.class.getMethod("decodeURI", String.class))); - parserConfig.addImport("newError", new MethodStub(TbUtils.class.getMethod("newError", - String.class, Object.class))); - parserConfig.addImport("newError", new MethodStub(TbUtils.class.getMethod("newError", - String.class))); - parserConfig.addImport("isBinary", new MethodStub(TbUtils.class.getMethod("isBinary", - String.class))); - parserConfig.addImport("isOctal", new MethodStub(TbUtils.class.getMethod("isOctal", - String.class))); - parserConfig.addImport("isDecimal", new MethodStub(TbUtils.class.getMethod("isDecimal", - String.class))); - parserConfig.addImport("isHexadecimal", new MethodStub(TbUtils.class.getMethod("isHexadecimal", - String.class))); } public static String btoa(String input) { @@ -269,7 +210,6 @@ public class TbUtils { public static Object decodeToJson(ExecutionContext ctx, List bytesList) throws IOException { return TbJson.parse(ctx, bytesToString(bytesList)); } - public static Object decodeToJson(ExecutionContext ctx, String jsonStr) throws IOException { return TbJson.parse(ctx, jsonStr); } @@ -329,28 +269,29 @@ public class TbUtils { } public static Integer parseInt(String value) { - return parseInt(value, zeroRadix); + int radix = getRadix(value); + return parseInt(value, radix); } public static Integer parseInt(String value, int radix) { if (StringUtils.isNotBlank(value)) { try { String valueP = prepareNumberString(value); - int radixValue = isValidStringAndRadix(valueP, radix, value); + isValidRadix(valueP, radix); try { - if (radixValue >= 25 && radixValue <= MAX_RADIX) { - return Integer.parseInt(valueP, radixValue); - } - return switch (radixValue) { - case MIN_RADIX -> parseBinaryStringAsSignedInteger(valueP); - case octalRadix, decRadix, hexRadix -> Integer.parseInt(valueP, radixValue); - default -> throw new IllegalArgumentException("Invalid radix: [" + radix + "]"); - }; + return Integer.parseInt(valueP, radix); } catch (NumberFormatException e) { - Integer iMax = Integer.MAX_VALUE; - Integer iMin = Integer.MIN_VALUE; - compareIntLongValueMinMax(valueP, radixValue, iMax.longValue(), iMin.longValue(), "Integer"); - throw new NumberFormatException(e.getMessage()); + BigInteger bi = new BigInteger(valueP, radix); + if (bi.compareTo(BigInteger.valueOf(Integer.MAX_VALUE)) > 0) + throw new NumberFormatException("Value \"" + value + "\" is greater than the maximum Integer value " + Integer.MAX_VALUE + " !"); + if (bi.compareTo(BigInteger.valueOf(Integer.MIN_VALUE)) < 0) + throw new NumberFormatException("Value \"" + value + "\" is less than the minimum Integer value " + Integer.MIN_VALUE + " !"); + Float f = parseFloat(valueP); + if (f != null) { + return f.intValue(); + } else { + throw new NumberFormatException(e.getMessage()); + } } } catch (NumberFormatException e) { throw new NumberFormatException(e.getMessage()); @@ -360,26 +301,29 @@ public class TbUtils { } public static Long parseLong(String value) { - return parseLong(value, zeroRadix); + int radix = getRadix(value); + return parseLong(value, radix); } public static Long parseLong(String value, int radix) { if (StringUtils.isNotBlank(value)) { try { String valueP = prepareNumberString(value); - int radixValue = isValidStringAndRadix(valueP, radix, value); + isValidRadix(valueP, radix); try { - if (radixValue >= 25 && radixValue <= MAX_RADIX) { - return Long.parseLong(valueP, radixValue); + return Long.parseLong(valueP, radix); + } catch (NumberFormatException e) { + BigInteger bi = new BigInteger(valueP, radix); + if (bi.compareTo(BigInteger.valueOf(Long.MAX_VALUE)) > 0) + throw new NumberFormatException("Value \"" + value + "\"is greater than the maximum Long value " + Long.MAX_VALUE + " !"); + if (bi.compareTo(BigInteger.valueOf(Long.MIN_VALUE)) < 0) + throw new NumberFormatException("Value \"" + value + "\" is less than the minimum Long value " + Long.MIN_VALUE + " !"); + Double dd = parseDouble(valueP); + if (dd != null) { + return dd.longValue(); + } else { + throw new NumberFormatException(e.getMessage()); } - return switch (radixValue) { - case MIN_RADIX -> parseBinaryStringAsSignedLong(valueP); - case octalRadix, decRadix, hexRadix -> Long.parseLong(valueP, radixValue); - default -> throw new IllegalArgumentException("Invalid radix: [" + radix + "]"); - }; - } catch (NumberFormatException e) { - compareIntLongValueMinMax(valueP, radixValue, Long.MAX_VALUE, Long.MIN_VALUE, "Long"); - throw new NumberFormatException(e.getMessage()); } } catch (NumberFormatException e) { throw new NumberFormatException(e.getMessage()); @@ -388,96 +332,25 @@ public class TbUtils { return null; } - private static int parseBinaryStringAsSignedInteger(String binaryString) { - if (binaryString.length() != 32) { - // Pad the binary string to 64 bits if it is not already - binaryString = String.format("%32s", binaryString).replace(' ', '0'); - } - - // If the MSB is 1, the number is negative in two's complement - if (binaryString.charAt(0) == '1') { - // Calculate the two's complement - String invertedBinaryString = invertBinaryString(binaryString); - int positiveValue = Integer.parseInt(invertedBinaryString, 2) + 1; - return -positiveValue; - } else { - return Integer.parseInt(binaryString, 2); - } - } - private static long parseBinaryStringAsSignedLong(String binaryString) { - if (binaryString.length() != 64) { - // Pad the binary string to 64 bits if it is not already - binaryString = String.format("%64s", binaryString).replace(' ', '0'); - } - - // If the MSB is 1, the number is negative in two's complement - if (binaryString.charAt(0) == '1') { - // Calculate the two's complement - String invertedBinaryString = invertBinaryString(binaryString); - long positiveValue = Long.parseLong(invertedBinaryString, 2) + 1; - return -positiveValue; - } else { - return Long.parseLong(binaryString, 2); - } - } - - private static String invertBinaryString(String binaryString) { - StringBuilder invertedString = new StringBuilder(); - for (char bit : binaryString.toCharArray()) { - invertedString.append(bit == '0' ? '1' : '0'); - } - return invertedString.toString(); - } - - private static int getRadix10_16(String value) { - int radix = isDecimal(value) > 0 ? decRadix : isHexadecimal(value); - if (radix > 0) { - return radix; - } else { - throw new NumberFormatException("Value: \"" + value + "\" is not numeric or hexDecimal format!"); - } + private static int getRadix(String value, int... radixS) { + return radixS.length > 0 ? radixS[0] : isHexadecimal(value) ? 16 : 10; } public static Float parseFloat(String value) { - return parseFloat(value, 0); - } - - public static Float parseFloat(String value, int radix) { - if (StringUtils.isNotBlank(value)) { - String valueP = prepareNumberString(value); - int radixValue = isValidStringAndRadix(valueP, radix, value); + if (value != null) { try { - if (radixValue == decRadix) { - return Float.parseFloat(value); - } else { - int bits = Integer.parseUnsignedInt(valueP, hexRadix); - return Float.intBitsToFloat(bits); - } + return Float.parseFloat(prepareNumberString(value)); } catch (NumberFormatException e) { - throw new NumberFormatException(e.getMessage()); } } return null; } public static Double parseDouble(String value) { - int radix = getRadix10_16(value); - return parseDouble(value, radix); - } - - public static Double parseDouble(String value, int radix) { if (value != null) { try { - String valueP = prepareNumberString(value); - int radixValue = isValidStringAndRadix(valueP, radix, value); - if (radixValue == decRadix) { - return Double.parseDouble(prepareNumberString(value)); - } else { - long bits = Long.parseUnsignedLong(valueP, hexRadix); - return Double.longBitsToDouble(bits); - } + return Double.parseDouble(prepareNumberString(value)); } catch (NumberFormatException e) { - throw new NumberFormatException(e.getMessage()); } } return null; @@ -495,10 +368,9 @@ public class TbUtils { return parseHexToInt(hex, true); } - public static Integer parseHexToInt(String value, boolean bigEndian) { - String hexValue = prepareNumberString(value); - String hex = bigEndian ? hexValue : reverseHexStringByOrder(hexValue); - return parseInt(hex, hexRadix); + public static int parseHexToInt(String hex, boolean bigEndian) { + byte[] data = prepareHexToBytesNumber(hex, 8); + return parseBytesToInt(data, 0, data.length, bigEndian); } public static long parseLittleEndianHexToLong(String hex) { @@ -513,10 +385,9 @@ public class TbUtils { return parseHexToLong(hex, true); } - public static Long parseHexToLong(String value, boolean bigEndian) { - String hexValue = prepareNumberString(value); - String hex = bigEndian ? value : reverseHexStringByOrder(hexValue); - return parseLong(hex, 16); + public static long parseHexToLong(String hex, boolean bigEndian) { + byte[] data = prepareHexToBytesNumber(hex, 16); + return parseBytesToLong(data, 0, data.length, bigEndian); } public static float parseLittleEndianHexToFloat(String hex) { @@ -531,10 +402,9 @@ public class TbUtils { return parseHexToFloat(hex, true); } - public static Float parseHexToFloat(String value, boolean bigEndian) { - String hexValue = prepareNumberString(value); - String hex = bigEndian ? value : reverseHexStringByOrder(hexValue); - return parseFloat(hex, hexRadix); + public static float parseHexToFloat(String hex, boolean bigEndian) { + byte[] data = prepareHexToBytesNumber(hex, 8); + return parseBytesToFloat(data, 0, bigEndian); } public static double parseLittleEndianHexToDouble(String hex) { @@ -549,155 +419,37 @@ public class TbUtils { return parseHexToDouble(hex, true); } - public static double parseHexToDouble(String value, boolean bigEndian) { - String hexValue = prepareNumberString(value); - String hex = bigEndian ? value : reverseHexStringByOrder(hexValue); - return parseDouble(hex, hexRadix); + public static double parseHexToDouble(String hex, boolean bigEndian) { + byte[] data = prepareHexToBytesNumber(hex, 16); + return parseBytesToDouble(data, 0, bigEndian); } - public static ExecutionArrayList hexToBytes(ExecutionContext ctx, String value) { - String hex = prepareNumberString(value); - int len = hex.length(); - if (len % 2 > 0) { + private static byte[] prepareHexToBytesNumber(String hex, int len) { + int length = hex.length(); + if (length > len) { + throw new IllegalArgumentException("Hex string is too large. Maximum 8 symbols allowed."); + } + if (length % 2 > 0) { throw new IllegalArgumentException("Hex string must be even-length."); } - ExecutionArrayList data = new ExecutionArrayList<>(ctx); - for (int i = 0; i < hex.length(); i += 2) { - // Extract two characters from the hex string - String byteString = hex.substring(i, i + 2); - // Parse the hex string to a byte - byte byteValue = (byte) Integer.parseInt(byteString, 16); - // Add the byte to the ArrayList - data.add(byteValue); + byte[] data = new byte[length / 2]; + for (int i = 0; i < length; i += 2) { + data[i / 2] = (byte) ((Character.digit(hex.charAt(i), 16) << 4) + Character.digit(hex.charAt(i + 1), 16)); } return data; } - public static List printUnsignedBytes(ExecutionContext ctx, List byteArray) { - ExecutionArrayList data = new ExecutionArrayList<>(ctx); - for (Byte b : byteArray) { - // Convert signed byte to unsigned integer - int unsignedByte = Byte.toUnsignedInt(b); - data.add(unsignedByte); - } - return data; - } - - public static String intToHex(Integer i) { - return prepareNumberHexString(i.longValue(), true, false, hexLenMin, hexLenIntMax); - } - - public static String intToHex(Integer i, boolean bigEndian) { - return prepareNumberHexString(i.longValue(), bigEndian, false, hexLenMin, hexLenIntMax); - } - - public static String intToHex(Integer i, boolean bigEndian, boolean pref) { - return prepareNumberHexString(i.longValue(), bigEndian, pref, hexLenMin, hexLenIntMax); - } - - public static String intToHex(Integer i, boolean bigEndian, boolean pref, int len) { - return prepareNumberHexString(i.longValue(), bigEndian, pref, len, hexLenIntMax); - } - - public static String longToHex(Long l) { - return prepareNumberHexString(l, true, false, hexLenMin, hexLenLongMax); - } - public static String longToHex(Long l, boolean bigEndian) { - return prepareNumberHexString(l, bigEndian, false, hexLenMin, hexLenLongMax); - } - - public static String longToHex(Long l, boolean bigEndian, boolean pref) { - return prepareNumberHexString(l, bigEndian, pref, hexLenMin, hexLenLongMax); - } - - public static String longToHex(Long l, boolean bigEndian, boolean pref, int len) { - return prepareNumberHexString(l, bigEndian, pref, len, hexLenLongMax); - } - - public static String intLongToString(Long number) { - return intLongToString(number, 10); - } - - public static String intLongToString(Long number, int radix) { - return intLongToString(number, radix, true); - } - - public static String intLongToString(Long number, int radix, boolean bigEndian) { - return intLongToString(number, radix, bigEndian, false); - } - - public static String intLongToString(Long number, int radix, boolean bigEndian, boolean pref) { - if (radix >= 25 && radix <= MAX_RADIX) { - return Long.toString(number, radix); - } - return switch (radix) { - case MIN_RADIX -> Long.toBinaryString(number); - case 8 -> Long.toOctalString(number); - case 10 -> Long.toString(number); - case 16 -> prepareNumberHexString(number, bigEndian, pref, -1, -1); - default -> throw new IllegalArgumentException("Invalid radix: [" + radix + "]"); - }; - } - - private static void compareIntLongValueMinMax(String valueP, int radix, Long maxValue, Long minValue, String clazzName) { - BigInteger bi = new BigInteger(valueP, radix); - if (bi.compareTo(BigInteger.valueOf(maxValue)) > 0) { - throw new NumberFormatException("Value \"" + valueP + "\"is greater than the maximum " + clazzName + " value " + maxValue + " !"); - } else if (bi.compareTo(BigInteger.valueOf(minValue)) < 0) { - throw new NumberFormatException("Value \"" + valueP + "\" is less than the minimum " + clazzName + " value " + minValue + " !"); + public static ExecutionArrayList hexToBytes(ExecutionContext ctx, String hex) { + int len = hex.length(); + if (len % 2 > 0) { + throw new IllegalArgumentException("Hex string must be even-length."); } - } - - private static String prepareNumberHexString(Long number, boolean bigEndian, boolean pref, int len, int hexLenMax) { - String hex = Long.toHexString(number).toUpperCase(); - hexLenMax = hexLenMax < 0 ? hex.length() : hexLenMax; - String hexWithoutZeroFF = removeLeadingZero_FF(hex, number, hexLenMax); - hexWithoutZeroFF = bigEndian ? hexWithoutZeroFF : reverseHexStringByOrder(hexWithoutZeroFF); - len = len == hexLenMin ? hexWithoutZeroFF.length() : len; - String result = hexWithoutZeroFF.substring(hexWithoutZeroFF.length() - len); - return pref ? "0x" + result : result; - } - - private static String removeLeadingZero_FF(String hex, Long number, int hexLenMax) { - String hexWithoutZero = hex.replaceFirst("^0+(?!$)", ""); // Remove leading zeros except for the last one - if (number >= 0) { - return hexWithoutZero; - } else { - String hexWithoutZeroFF = hexWithoutZero.replaceFirst("^F+(?!$)", ""); - hexWithoutZeroFF = hexWithoutZeroFF.length() % 2 > 0 ? "F" + hexWithoutZeroFF : hexWithoutZeroFF; - if (hexWithoutZeroFF.length() > hexLenMax) { - return hexWithoutZeroFF.substring(hexWithoutZeroFF.length() - hexLenMax); - } else if (hexWithoutZeroFF.length() == hexLenMax) { - return hexWithoutZeroFF; - } else { - return "FF" + hexWithoutZeroFF; - } + ExecutionArrayList data = new ExecutionArrayList<>(ctx); + for (int i = 0; i < len; i += 2) { + data.add((byte) ((Character.digit(hex.charAt(i), 16) << 4) + + Character.digit(hex.charAt(i + 1), 16))); } - } - - public static String floatToHex(Float f) { - return floatToHex(f, true); - } - - public static String floatToHex(Float f, boolean bigEndian) { - // Convert the float to its raw integer bits representation - int bits = Float.floatToRawIntBits(f); - - // Format the integer bits as a hexadecimal string - String result = String.format("0x%08X", bits); - return bigEndian ? result : reverseHexStringByOrder(result); - } - - public static String doubleToHex(Double d) { - return doubleToHex(d, true); - } - - public static String doubleToHex(Double d, boolean bigEndian) { - long bits = Double.doubleToRawLongBits(d); - - // Format the integer bits as a hexadecimal string - String result = String.format("0x%16X", bits); - return bigEndian ? result : reverseHexStringByOrder(result); + return data; } public static String base64ToHex(String base64) { @@ -895,15 +647,6 @@ public class TbUtils { return URLDecoder.decode(uri, StandardCharsets.UTF_8); } - public static void newError(String message) { - newError(message, null); - } - - public static void newError(String message, Object value) { - String msg = value == null ? message : message + " Value = " + value; - throw new RuntimeException(msg); - } - private static void parseRecursive(Object json, Map map, List excludeList, String path, boolean pathInKey) { if (json instanceof Map.Entry) { Map.Entry entry = (Map.Entry) json; @@ -944,104 +687,42 @@ public class TbUtils { } } + private static boolean isHexadecimal(String value) { + return value != null && (value.contains("0x") || value.contains("0X")); + } + private static String prepareNumberString(String value) { if (value != null) { value = value.trim(); - value = value.replace("0x", ""); - value = value.replace("0X", ""); + if (isHexadecimal(value)) { + value = value.replace("0x", ""); + value = value.replace("0X", ""); + } value = value.replace(",", "."); } return value; } - private static int isValidStringAndRadix(String valueP, int radix, String value) { - int radixValue; - if (radix == 0) { - radixValue = getRadix10_16(valueP); - } else if (radix >= 25 && radix <= MAX_RADIX) { - return radix; - } else { - radixValue = switch (radix) { - case MIN_RADIX -> isBinary(valueP); - case 8 -> isOctal(valueP); - case 10 -> isDecimal(valueP); - case 16 -> isHexadecimal(valueP); - default -> throw new IllegalArgumentException("Invalid radix: [" + radix + "]"); - - }; - } - - if (radixValue > 0) { - if (value.startsWith("0x")) radixValue = 16; - if (radixValue == 16) { - valueP = value.startsWith("-") ? value.substring(1) : value; - if (valueP.length() % 2 > 0) { - throw new NumberFormatException("The hexadecimal value: \"" + value + "\" must be of even length, or if the decimal value must be a number!"); - } - } - return radixValue; - } else { - if (radix > 0) { - throw new NumberFormatException("Failed radix [" + radix + "] for value: \"" + value + "\", must be [" + radixValue + "] !"); - } else { - throw new NumberFormatException("Invalid \"" + value + "\". It is not numeric or hexadecimal format!"); + private static boolean isValidRadix(String value, int radix) { + for (int i = 0; i < value.length(); i++) { + if (i == 0 && value.charAt(i) == '-') { + if (value.length() == 1) + throw new NumberFormatException("Failed radix [" + radix + "] for value: \"" + value + "\"!"); + else + continue; } + if (Character.digit(value.charAt(i), radix) < 0) + throw new NumberFormatException("Failed radix: [" + radix + "] for value: \"" + value + "\"!"); } + return true; } - public static int isBinary(String str) { - if (str == null || str.isEmpty()) { - return -1; - } - return str.matches("[01]+") ? MIN_RADIX : -1; - } - - public static int isOctal(String str) { - if (str == null || str.isEmpty()) { - return -1; - } - return str.matches("[0-7]+") ? octalRadix : -1; - } - - public static int isDecimal(String str) { - if (str == null || str.isEmpty()) { - return -1; - } - return str.matches("-?\\d+(\\.\\d+)?") ? decRadix : -1; - } - - public static int isHexadecimal(String str) { - if (str == null || str.isEmpty()) { - return -1; - } - return str.matches("^-?(0[xX])?[0-9a-fA-F]+$") ? hexRadix : -1; - } - - private static byte isValidIntegerToByte(Integer val) { - if (val > 255 || val < -128) { + private static byte isValidIntegerToByte (Integer val) { + if (val > 255 || val.intValue() < -128) { throw new NumberFormatException("The value '" + val + "' could not be correctly converted to a byte. " + "Integer to byte conversion requires the use of only 8 bits (with a range of min/max = -128/255)!"); } else { return val.byteValue(); } } - - private static String reverseHexStringByOrder(String value) { - if (value.startsWith("-")) { - throw new IllegalArgumentException("The hexadecimal string must be without a negative sign."); - } - boolean isHexPref = value.startsWith("0x"); - String hex = isHexPref ? value.substring(2) : value; - if (hex.length() % 2 > 0) { - throw new IllegalArgumentException("The hexadecimal string must be even-length."); - } - // Split the hex string into bytes (2 characters each) - StringBuilder reversedHex = new StringBuilder(8); - for (int i = hex.length() - 2; i >= 0; i -= 2) { - reversedHex.append(hex, i, i + 2); - } - String result = reversedHex.toString(); - return isHexPref ? "0x" + result : result; - } } - diff --git a/common/script/script-api/src/test/java/org/thingsboard/script/api/tbel/TbUtilsTest.java b/common/script/script-api/src/test/java/org/thingsboard/script/api/tbel/TbUtilsTest.java index 42c8e84b7d..b4e7e7411d 100644 --- a/common/script/script-api/src/test/java/org/thingsboard/script/api/tbel/TbUtilsTest.java +++ b/common/script/script-api/src/test/java/org/thingsboard/script/api/tbel/TbUtilsTest.java @@ -15,14 +15,12 @@ */ package org.thingsboard.script.api.tbel; -import com.google.common.collect.Lists; import com.google.common.primitives.Bytes; -import com.google.common.primitives.Ints; import lombok.extern.slf4j.Slf4j; +import org.junit.jupiter.api.Test; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; import org.mvel2.ExecutionContext; import org.mvel2.ParserContext; import org.mvel2.SandboxedParserConfiguration; @@ -33,28 +31,28 @@ import java.io.IOException; import java.math.BigInteger; import java.nio.ByteBuffer; import java.util.ArrayList; -import java.util.Arrays; import java.util.Calendar; -import java.util.Collections; import java.util.List; import java.util.Random; -import static java.lang.Character.MAX_RADIX; -import static java.lang.Character.MIN_RADIX; - @Slf4j public class TbUtilsTest { private ExecutionContext ctx; + private final String intValHex = "41EA62CC"; private final float floatVal = 29.29824f; + private final String floatValStr = "29.29824"; + + private final String floatValHexRev = "CC62EA41"; private final float floatValRev = -5.948442E7f; private final long longVal = 0x409B04B10CB295EAL; private final String longValHex = "409B04B10CB295EA"; + private final long longValRev = 0xEA95B20CB1049B40L; private final String longValHexRev = "EA95B20CB1049B40"; - + private final String doubleValStr = "1729.1729"; private final double doubleVal = 1729.1729; private final double doubleValRev = -2.7208640774822924E205; @@ -88,9 +86,10 @@ public class TbUtilsTest { Assertions.assertEquals(0xBAAB, TbUtils.parseHexToInt("ABBA", false)); Assertions.assertEquals(0xAABBCC, TbUtils.parseHexToInt("AABBCC", true)); Assertions.assertEquals(0xAABBCC, TbUtils.parseHexToInt("CCBBAA", false)); - Assertions.assertThrows(NumberFormatException.class, () -> TbUtils.parseHexToInt("AABBCCDD", true)); - Assertions.assertEquals(0x11BBCC22, TbUtils.parseHexToInt("11BBCC22", true)); - Assertions.assertEquals(0x11BBCC22, TbUtils.parseHexToInt("22CCBB11", false)); + Assertions.assertEquals(0xAABBCCDD, TbUtils.parseHexToInt("AABBCCDD", true)); + Assertions.assertEquals(0xAABBCCDD, TbUtils.parseHexToInt("DDCCBBAA", false)); + Assertions.assertEquals(0xDDCCBBAA, TbUtils.parseHexToInt("DDCCBBAA", true)); + Assertions.assertEquals(0xDDCCBBAA, TbUtils.parseHexToInt("AABBCCDD", false)); } @Test @@ -209,28 +208,15 @@ public class TbUtilsTest { Assertions.assertNull(TbUtils.parseInt("")); Assertions.assertNull(TbUtils.parseInt(" ")); - Assertions.assertEquals((Integer) 0, TbUtils.parseInt("0")); - Assertions.assertEquals((Integer) 0, TbUtils.parseInt("-0")); + Assertions.assertEquals(java.util.Optional.of(0).get(), TbUtils.parseInt("0")); + Assertions.assertEquals(java.util.Optional.of(0).get(), TbUtils.parseInt("-0")); Assertions.assertEquals(java.util.Optional.of(473).get(), TbUtils.parseInt("473")); Assertions.assertEquals(java.util.Optional.of(-255).get(), TbUtils.parseInt("-0xFF")); - Assertions.assertThrows(NumberFormatException.class, () -> TbUtils.parseInt("-0xFF123")); - Assertions.assertEquals(java.util.Optional.of(-255).get(), TbUtils.parseInt("-FF")); - Assertions.assertEquals(java.util.Optional.of(255).get(), TbUtils.parseInt("FF")); - Assertions.assertThrows(IllegalArgumentException.class, () -> TbUtils.parseInt("FFF")); - Assertions.assertEquals(java.util.Optional.of(-2578).get(), TbUtils.parseInt("-0A12")); - Assertions.assertEquals(java.util.Optional.of(-2578).get(), TbUtils.parseHexToInt("-0A12")); - Assertions.assertThrows(IllegalArgumentException.class, () -> TbUtils.parseHexToInt("A12", false)); - Assertions.assertEquals(java.util.Optional.of(-14866).get(), TbUtils.parseBigEndianHexToInt("-3A12")); - Assertions.assertThrows(IllegalArgumentException.class, () -> TbUtils.parseLittleEndianHexToInt("-A12")); - + Assertions.assertThrows(NumberFormatException.class, () -> TbUtils.parseInt("FF")); Assertions.assertThrows(NumberFormatException.class, () -> TbUtils.parseInt("0xFG")); Assertions.assertEquals(java.util.Optional.of(102).get(), TbUtils.parseInt("1100110", 2)); - Assertions.assertEquals(java.util.Optional.of(-102).get(), TbUtils.parseInt("1111111111111111111111111111111111111111111111111111111110011010", 2)); Assertions.assertThrows(NumberFormatException.class, () -> TbUtils.parseInt("1100210", 2)); - Assertions.assertEquals(java.util.Optional.of(13158).get(), TbUtils.parseInt("11001101100110", 2)); - Assertions.assertEquals(java.util.Optional.of(-13158).get(), TbUtils.parseInt("1111111111111111111111111111111111111111111111111100110010011010", 2)); - Assertions.assertEquals(java.util.Optional.of(63).get(), TbUtils.parseInt("77", 8)); Assertions.assertThrows(NumberFormatException.class, () -> TbUtils.parseInt("18", 8)); @@ -238,32 +224,19 @@ public class TbUtilsTest { Assertions.assertEquals(java.util.Optional.of(-255).get(), TbUtils.parseInt("-FF", 16)); Assertions.assertThrows(NumberFormatException.class, () -> TbUtils.parseInt("FG", 16)); + Assertions.assertEquals(java.util.Optional.of(Integer.MAX_VALUE).get(), TbUtils.parseInt(Integer.toString(Integer.MAX_VALUE), 10)); Assertions.assertThrows(NumberFormatException.class, () -> TbUtils.parseInt(BigInteger.valueOf(Integer.MAX_VALUE).add(BigInteger.valueOf(1)).toString(10), 10)); Assertions.assertEquals(java.util.Optional.of(Integer.MIN_VALUE).get(), TbUtils.parseInt(Integer.toString(Integer.MIN_VALUE), 10)); Assertions.assertThrows(NumberFormatException.class, () -> TbUtils.parseInt(BigInteger.valueOf(Integer.MIN_VALUE).subtract(BigInteger.valueOf(1)).toString(10), 10)); Assertions.assertEquals(java.util.Optional.of(506070563).get(), TbUtils.parseInt("KonaIn", 30)); - Assertions.assertThrows(NumberFormatException.class, () -> TbUtils.parseInt("KonaIn", 10)); - Assertions.assertThrows(NumberFormatException.class, () -> TbUtils.parseInt(".456", 10)); - Assertions.assertThrows(NumberFormatException.class, () -> TbUtils.parseInt("4562.", 10)); - - Assertions.assertThrows(IllegalArgumentException.class, () -> TbUtils.parseInt("KonaIn", MAX_RADIX+1)); - Assertions.assertThrows(IllegalArgumentException.class, () -> TbUtils.parseInt("KonaIn", MIN_RADIX-1)); - Assertions.assertThrows(IllegalArgumentException.class, () -> TbUtils.parseInt("KonaIn", 12)); } @Test public void parseFloat() { - String floatValStr = "29.29824"; Assertions.assertEquals(java.util.Optional.of(floatVal).get(), TbUtils.parseFloat(floatValStr)); - String floatValHex = "41EA62CC"; - Assertions.assertEquals(0, Float.compare(floatVal, TbUtils.parseHexToFloat(floatValHex))); - Assertions.assertEquals(0, Float.compare(floatValRev, TbUtils.parseHexToFloat(floatValHex, false))); - Assertions.assertEquals(0, Float.compare(floatVal, TbUtils.parseBigEndianHexToFloat(floatValHex))); - String floatValHexRev = "CC62EA41"; - Assertions.assertEquals(0, Float.compare(floatVal, TbUtils.parseLittleEndianHexToFloat(floatValHexRev))); } @Test @@ -273,13 +246,21 @@ public class TbUtilsTest { Assertions.assertEquals(0, Float.compare(29.298f, actualF)); } + @Test + public void parseHexToFloat() { + Assertions.assertEquals(0, Float.compare(floatVal, TbUtils.parseHexToFloat(intValHex))); + Assertions.assertEquals(0, Float.compare(floatValRev, TbUtils.parseHexToFloat(intValHex, false))); + Assertions.assertEquals(0, Float.compare(floatVal, TbUtils.parseBigEndianHexToFloat(intValHex))); + Assertions.assertEquals(0, Float.compare(floatVal, TbUtils.parseLittleEndianHexToFloat(floatValHexRev))); + } + @Test public void arseBytesToFloat() { byte[] floatValByte = {65, -22, 98, -52}; Assertions.assertEquals(0, Float.compare(floatVal, TbUtils.parseBytesToFloat(floatValByte, 0))); Assertions.assertEquals(0, Float.compare(floatValRev, TbUtils.parseBytesToFloat(floatValByte, 0, false))); - List floatVaList = Bytes.asList(floatValByte); + List floatVaList = Bytes.asList(floatValByte); Assertions.assertEquals(0, Float.compare(floatVal, TbUtils.parseBytesToFloat(floatVaList, 0))); Assertions.assertEquals(0, Float.compare(floatValRev, TbUtils.parseBytesToFloat(floatVaList, 0, false))); } @@ -290,15 +271,14 @@ public class TbUtilsTest { Assertions.assertNull(TbUtils.parseLong("")); Assertions.assertNull(TbUtils.parseLong(" ")); - Assertions.assertEquals((Long) 0L, TbUtils.parseLong("0")); - Assertions.assertEquals((Long) 0L, TbUtils.parseLong("-0")); + Assertions.assertEquals(java.util.Optional.of(0L).get(), TbUtils.parseLong("0")); + Assertions.assertEquals(java.util.Optional.of(0L).get(), TbUtils.parseLong("-0")); Assertions.assertEquals(java.util.Optional.of(473L).get(), TbUtils.parseLong("473")); Assertions.assertEquals(java.util.Optional.of(-65535L).get(), TbUtils.parseLong("-0xFFFF")); - Assertions.assertEquals(java.util.Optional.of(4294967295L).get(), TbUtils.parseLong("FFFFFFFF")); + Assertions.assertThrows(NumberFormatException.class, () -> TbUtils.parseLong("FFFFFFFF")); Assertions.assertThrows(NumberFormatException.class, () -> TbUtils.parseLong("0xFGFFFFFF")); Assertions.assertEquals(java.util.Optional.of(13158L).get(), TbUtils.parseLong("11001101100110", 2)); - Assertions.assertEquals(java.util.Optional.of(-13158L).get(), TbUtils.parseLong("1111111111111111111111111111111111111111111111111100110010011010", 2)); Assertions.assertThrows(NumberFormatException.class, () -> TbUtils.parseLong("11001101100210", 2)); Assertions.assertEquals(java.util.Optional.of(9223372036854775807L).get(), TbUtils.parseLong("777777777777777777777", 8)); @@ -315,7 +295,10 @@ public class TbUtilsTest { Assertions.assertEquals(java.util.Optional.of(218840926543L).get(), TbUtils.parseLong("KonaLong", 27)); Assertions.assertThrows(NumberFormatException.class, () -> TbUtils.parseLong("KonaLong", 10)); + } + @Test + public void parseHexToLong() { Assertions.assertEquals(longVal, TbUtils.parseHexToLong(longValHex)); Assertions.assertEquals(longVal, TbUtils.parseHexToLong(longValHexRev, false)); Assertions.assertEquals(longVal, TbUtils.parseBigEndianHexToLong(longValHex)); @@ -329,20 +312,14 @@ public class TbUtilsTest { Bytes.reverse(longValByte); Assertions.assertEquals(longVal, TbUtils.parseBytesToLong(longValByte, 0, 8, false)); - List longVaList = Bytes.asList(longValByte); + List longVaList = Bytes.asList(longValByte); Assertions.assertEquals(longVal, TbUtils.parseBytesToLong(longVaList, 0, 8, false)); - long longValRev = 0xEA95B20CB1049B40L; Assertions.assertEquals(longValRev, TbUtils.parseBytesToLong(longVaList, 0, 8)); } @Test public void parsDouble() { - String doubleValStr = "1729.1729"; Assertions.assertEquals(java.util.Optional.of(doubleVal).get(), TbUtils.parseDouble(doubleValStr)); - Assertions.assertEquals(0, Double.compare(doubleVal, TbUtils.parseHexToDouble(longValHex))); - Assertions.assertEquals(0, Double.compare(doubleValRev, TbUtils.parseHexToDouble(longValHex, false))); - Assertions.assertEquals(0, Double.compare(doubleVal, TbUtils.parseBigEndianHexToDouble(longValHex))); - Assertions.assertEquals(0, Double.compare(doubleVal, TbUtils.parseLittleEndianHexToDouble(longValHexRev))); } @Test @@ -352,13 +329,21 @@ public class TbUtilsTest { Assertions.assertEquals(0, Double.compare(1729.173, actualD)); } + @Test + public void parseHexToDouble() { + Assertions.assertEquals(0, Double.compare(doubleVal, TbUtils.parseHexToDouble(longValHex))); + Assertions.assertEquals(0, Double.compare(doubleValRev, TbUtils.parseHexToDouble(longValHex, false))); + Assertions.assertEquals(0, Double.compare(doubleVal, TbUtils.parseBigEndianHexToDouble(longValHex))); + Assertions.assertEquals(0, Double.compare(doubleVal, TbUtils.parseLittleEndianHexToDouble(longValHexRev))); + } + @Test public void parseBytesToDouble() { byte[] doubleValByte = {64, -101, 4, -79, 12, -78, -107, -22}; Assertions.assertEquals(0, Double.compare(doubleVal, TbUtils.parseBytesToDouble(doubleValByte, 0))); Assertions.assertEquals(0, Double.compare(doubleValRev, TbUtils.parseBytesToDouble(doubleValByte, 0, false))); - List doubleVaList = Bytes.asList(doubleValByte); + List doubleVaList = Bytes.asList(doubleValByte); Assertions.assertEquals(0, Double.compare(doubleVal, TbUtils.parseBytesToDouble(doubleVaList, 0))); Assertions.assertEquals(0, Double.compare(doubleValRev, TbUtils.parseBytesToDouble(doubleVaList, 0, false))); } @@ -369,17 +354,16 @@ public class TbUtilsTest { ExecutionHashMap expectedJson = new ExecutionHashMap<>(1, ctx); expectedJson.put("hello", "world"); List expectedBytes = TbUtils.stringToBytes(ctx, expectedStr); - Object actualJson = TbUtils.decodeToJson(ctx, expectedBytes); - Assertions.assertEquals(expectedJson, actualJson); + Object actualJson = TbUtils.decodeToJson(ctx, expectedBytes); + Assertions.assertEquals(expectedJson,actualJson); } - @Test public void parseStringDecodeToJson() throws IOException { String expectedStr = "{\"hello\": \"world\"}"; ExecutionHashMap expectedJson = new ExecutionHashMap<>(1, ctx); expectedJson.put("hello", "world"); - Object actualJson = TbUtils.decodeToJson(ctx, expectedStr); - Assertions.assertEquals(expectedJson, actualJson); + Object actualJson = TbUtils.decodeToJson(ctx, expectedStr); + Assertions.assertEquals(expectedJson,actualJson); } @Test @@ -402,8 +386,8 @@ public class TbUtilsTest { @Test public void bytesFromList() { - byte[] arrayBytes = {(byte) 0x00, (byte) 0x08, (byte) 0x10, (byte) 0x1C, (byte) 0xFF, (byte) 0xFC, (byte) 0xAD, (byte) 0x88, (byte) 0x75, (byte) 0x74, (byte) 0x8A, (byte) 0x82}; - Object[] arrayMix = {"0x00", 8, "16", "0x1C", 255, (byte) 0xFC, 173, 136, 117, 116, -118, "-126"}; + byte[] arrayBytes = {(byte)0x00, (byte)0x08, (byte)0x10, (byte)0x1C, (byte)0xFF, (byte)0xFC, (byte)0xAD, (byte)0x88, (byte)0x75, (byte)0x74, (byte)0x8A, (byte)0x82}; + Object[] arrayMix = { "0x00", 8, "16", "0x1C", 255, (byte)0xFC, 173, 136, 117, 116, -118, "-126"}; String expected = new String(arrayBytes); ArrayList listBytes = new ArrayList<>(arrayBytes.length); @@ -413,36 +397,12 @@ public class TbUtilsTest { Assertions.assertEquals(expected, TbUtils.bytesToString(listBytes)); ArrayList listMix = new ArrayList<>(arrayMix.length); - Collections.addAll(listMix, arrayMix); + for (Object element : arrayMix) { + listMix.add(element); + } Assertions.assertEquals(expected, TbUtils.bytesToString(listMix)); } - @Test - public void bytesFromList_SpecSymbol() { - List listHex = new ArrayList<>(Arrays.asList("1D", "0x1D", "1F", "0x1F", "0x20", "0x20")); - byte[] expectedBytes = new byte[]{29, 29, 31, 31, 32, 32}; - String actualStr = TbUtils.bytesToString(listHex); - byte[] actualBytes = actualStr.getBytes(); - Assertions.assertArrayEquals(expectedBytes, actualBytes); - Assertions.assertTrue(actualStr.isBlank()); - listHex = new ArrayList<>(Arrays.asList("0x21", "0x21")); - expectedBytes = new byte[]{33, 33}; - actualStr = TbUtils.bytesToString(listHex); - actualBytes = actualStr.getBytes(); - Assertions.assertArrayEquals(expectedBytes, actualBytes); - Assertions.assertFalse(actualStr.isBlank()); - Assertions.assertEquals("!!", actualStr); - listHex = new ArrayList<>(Arrays.asList("21", "0x21")); - expectedBytes = new byte[]{21, 33}; - actualStr = TbUtils.bytesToString(listHex); - actualBytes = actualStr.getBytes(); - Assertions.assertArrayEquals(expectedBytes, actualBytes); - Assertions.assertFalse(actualStr.isBlank()); - Assertions.assertEquals("!", actualStr.substring(1)); - Assertions.assertEquals('\u0015', actualStr.charAt(0)); - Assertions.assertEquals(21, actualStr.charAt(0)); - } - @Test public void bytesFromList_Error() { List listHex = new ArrayList<>(); @@ -451,7 +411,14 @@ public class TbUtilsTest { TbUtils.bytesToString(listHex); Assertions.fail("Should throw NumberFormatException"); } catch (NumberFormatException e) { - Assertions.assertTrue(e.getMessage().contains("Value: \"FG\" is not numeric or hexDecimal format!")); + Assertions.assertTrue(e.getMessage().contains("Failed radix: [16] for value: \"FG\"!")); + } + listHex.add(0, "1F"); + try { + TbUtils.bytesToString(listHex); + Assertions.fail("Should throw NumberFormatException"); + } catch (NumberFormatException e) { + Assertions.assertTrue(e.getMessage().contains("Failed radix: [10] for value: \"1F\"!")); } List listIntString = new ArrayList<>(); @@ -515,137 +482,6 @@ public class TbUtilsTest { String uriDecodeActual = TbUtils.decodeURI(uriEncodeActual); Assertions.assertEquals(uriOriginal, uriDecodeActual); } - - @Test - public void intToHex_Test() { - Assertions.assertEquals("FFF5EE", TbUtils.intToHex(-2578)); - Assertions.assertEquals("0xFFD8FFA6", TbUtils.intToHex(0xFFD8FFA6, true, true)); - Assertions.assertEquals("0xA6FFD8FF", TbUtils.intToHex(0xFFD8FFA6, false, true)); - Assertions.assertEquals("0x7FFFFFFF", TbUtils.intToHex(Integer.MAX_VALUE, true, true)); - Assertions.assertEquals("0x80000000", TbUtils.intToHex(Integer.MIN_VALUE, true, true)); - Assertions.assertEquals("0xAB", TbUtils.intToHex(0xAB, true, true)); - Assertions.assertEquals("0xABCD", TbUtils.intToHex(0xABCD, true, true)); - Assertions.assertEquals("0xABCDEF", TbUtils.intToHex(0xABCDEF, true, true)); - Assertions.assertEquals("0xCDAB", TbUtils.intToHex(0xABCDEF, false, true, 4)); - Assertions.assertEquals("0xAB", TbUtils.intToHex(171, true, true)); - Assertions.assertEquals("0xAB", TbUtils.intToHex(0xAB, false, true)); - Assertions.assertEquals("0xAB", TbUtils.intToHex(0xAB, true, true, 2)); - Assertions.assertEquals("AB", TbUtils.intToHex(0xAB, false, false, 2)); - Assertions.assertEquals("AB", TbUtils.intToHex(171, true, false)); - Assertions.assertEquals("0xAB", TbUtils.intToHex(0xAB, true, true)); - Assertions.assertEquals("0xAB", TbUtils.intToHex(0xAB, false, true)); - Assertions.assertEquals("AB", TbUtils.intToHex(0xAB, false, false)); - - Assertions.assertEquals("0xABCD", TbUtils.intToHex(0xABCD, true, true)); - Assertions.assertEquals("0xCDAB", TbUtils.intToHex(0xABCD, false, true)); - Assertions.assertEquals("0xCD", TbUtils.intToHex(0xABCD, true, true, 2)); - Assertions.assertEquals("AB", TbUtils.intToHex(0xABCD, false, false, 2)); - } - @Test - public void longToHex_Test() { - Assertions.assertEquals("0x7FFFFFFFFFFFFFFF", TbUtils.longToHex(Long.MAX_VALUE, true, true)); - Assertions.assertEquals("0x8000000000000000", TbUtils.longToHex(Long.MIN_VALUE, true, true)); - Assertions.assertEquals("0xFFD8FFA6FFD8FFA6", TbUtils.longToHex(0xFFD8FFA6FFD8FFA6L, true, true)); - Assertions.assertEquals("0xA6FFD8FFA6FFCEFF", TbUtils.longToHex(0xFFCEFFA6FFD8FFA6L, false, true)); - Assertions.assertEquals("0xAB", TbUtils.longToHex(0xABL, true, true)); - Assertions.assertEquals("0xABCD", TbUtils.longToHex(0xABCDL, true, true)); - Assertions.assertEquals("0xABCDEF", TbUtils.longToHex(0xABCDEFL, true, true)); - Assertions.assertEquals("0xABEFCDAB", TbUtils.longToHex(0xABCDEFABCDEFL, false, true, 8)); - Assertions.assertEquals("0xAB", TbUtils.longToHex(0xABL, true, true, 2)); - Assertions.assertEquals("AB", TbUtils.longToHex(0xABL, false, false, 2)); - - Assertions.assertEquals("0xFFA6", TbUtils.longToHex(0xFFD8FFA6FFD8FFA6L, true, true, 4)); - Assertions.assertEquals("D8FF", TbUtils.longToHex(0xFFD8FFA6FFD8FFA6L, false, false, 4)); - } - - @Test - public void numberToString_Test() { - // 0011 0011 0110 0110 == 13158L - // 1100 1100 1001 1010 == -13158L - Assertions.assertEquals("11001101100110", TbUtils.intLongToString(13158L, 2)); - Assertions.assertEquals("1111111111111111111111111111111111111111111111111111111110011010", TbUtils.intLongToString(-102L, 2)); - Assertions.assertEquals("1111111111111111111111111111111111111111111111111100110010011010", TbUtils.intLongToString(-13158L, 2)); - Assertions.assertEquals("777777777777777777777", TbUtils.intLongToString(Long.MAX_VALUE, 8)); - Assertions.assertEquals("1000000000000000000000", TbUtils.intLongToString(Long.MIN_VALUE, 8)); - Assertions.assertEquals("9223372036854775807", TbUtils.intLongToString(Long.MAX_VALUE)); - Assertions.assertEquals("-9223372036854775808", TbUtils.intLongToString(Long.MIN_VALUE)); - Assertions.assertEquals("3366", TbUtils.intLongToString(13158L, 16)); - Assertions.assertEquals("FFCC9A", TbUtils.intLongToString(-13158L, 16)); - Assertions.assertEquals("0xFFCC9A", TbUtils.intLongToString(-13158L, 16, true, true)); - Assertions.assertEquals("hazelnut", TbUtils.intLongToString(1356099454469L, MAX_RADIX)); - } - - @Test - public void intToHexWithPrintUnsignedBytes_Test() { - Integer value = -40; - String intToHexLe = TbUtils.intToHex(value, false, true); - String intToHexBe = TbUtils.intToHex(value, true, true); - - List hexTopByteLe = TbUtils.hexToBytes(ctx, intToHexLe); - List hexTopByteBe = TbUtils.hexToBytes(ctx, intToHexBe); - - byte[] arrayBytes = {-40, -1}; - List expectedHexTopByteLe = Bytes.asList(arrayBytes); - List expectedHexTopByteBe = Lists.reverse(expectedHexTopByteLe); - Assertions.assertEquals(expectedHexTopByteLe, hexTopByteLe); - Assertions.assertEquals(expectedHexTopByteBe, hexTopByteBe); - - List actualLe = TbUtils.printUnsignedBytes(ctx, hexTopByteLe); - List actualBe = TbUtils.printUnsignedBytes(ctx, hexTopByteBe); - List expectedLe = Ints.asList(216, 255); - List expectedBe = Lists.reverse(expectedLe); - Assertions.assertEquals(expectedLe, actualLe); - Assertions.assertEquals(expectedBe, actualBe); - } - - @Test - public void floatToHex_Test() { - Float value = 20.89f; - String expectedHex = "0x41A71EB8"; - String valueHexRev = "0xB81EA741"; - String actual = TbUtils.floatToHex(value); - Assertions.assertEquals(expectedHex, actual); - Float valueActual = TbUtils.parseHexToFloat(actual); - Assertions.assertEquals(value, valueActual); - valueActual = TbUtils.parseHexToFloat(valueHexRev, false); - Assertions.assertEquals(value, valueActual); - } - @Test - public void doubleToHex_Test() { - String expectedHex = "0x409B04B10CB295EA"; - String actual = TbUtils.doubleToHex(doubleVal); - Assertions.assertEquals(expectedHex, actual); - Double valueActual = TbUtils.parseHexToDouble(actual); - Assertions.assertEquals(doubleVal, valueActual); - actual = TbUtils.doubleToHex(doubleVal, false); - String expectedHexRev = "0xEA95B20CB1049B40"; - Assertions.assertEquals(expectedHexRev, actual); - } - - @Test - public void newError_Test() { - String message = "frequency_weighting_type must be 0, 1 or 2."; - Object value = 4; - try { - TbUtils.newError(message, value); - Assertions.fail("Should throw NumberFormatException"); - } catch (RuntimeException e) { - Assertions.assertTrue(e.getMessage().contains("frequency_weighting_type must be 0, 1 or 2. Value = 4")); - } - try { - TbUtils.newError(message); - Assertions.fail("Should throw NumberFormatException"); - } catch (RuntimeException e) { - Assertions.assertTrue(e.getMessage().contains("frequency_weighting_type must be 0, 1 or 2.")); - } - } - - @Test - public void isNumeric_Test() { - - - } - private static List toList(byte[] data) { List result = new ArrayList<>(data.length); for (Byte b : data) { From 569ab0b87b4f52a43aeec669eeb8bc4bcd003eff Mon Sep 17 00:00:00 2001 From: dashevchenko Date: Fri, 26 Jul 2024 18:42:08 +0300 Subject: [PATCH 3/5] added support for new efento measurement types --- .../efento/CoapEfentoTransportResource.java | 289 +++++++++++------- .../src/main/proto/efento/proto_config.proto | 90 ++++-- .../main/proto/efento/proto_device_info.proto | 25 +- .../efento/proto_measurement_types.proto | 227 +++++++++----- .../proto/efento/proto_measurements.proto | 139 +++++---- .../src/main/proto/efento/proto_rule.proto | 20 ++ 6 files changed, 506 insertions(+), 284 deletions(-) diff --git a/common/transport/coap/src/main/java/org/thingsboard/server/transport/coap/efento/CoapEfentoTransportResource.java b/common/transport/coap/src/main/java/org/thingsboard/server/transport/coap/efento/CoapEfentoTransportResource.java index 370c03c27f..6e481cc21d 100644 --- a/common/transport/coap/src/main/java/org/thingsboard/server/transport/coap/efento/CoapEfentoTransportResource.java +++ b/common/transport/coap/src/main/java/org/thingsboard/server/transport/coap/efento/CoapEfentoTransportResource.java @@ -35,14 +35,13 @@ import org.thingsboard.server.common.data.device.profile.CoapDeviceProfileTransp import org.thingsboard.server.common.data.device.profile.DeviceProfileTransportConfiguration; import org.thingsboard.server.common.data.device.profile.EfentoCoapDeviceTypeConfiguration; import org.thingsboard.server.common.adaptor.AdaptorException; -import org.thingsboard.server.common.adaptor.ProtoConverter; -import org.thingsboard.server.common.adaptor.AdaptorException; import org.thingsboard.server.common.transport.auth.SessionInfoCreator; import org.thingsboard.server.gen.transport.TransportProtos; import org.thingsboard.server.gen.transport.coap.ConfigProtos; import org.thingsboard.server.gen.transport.coap.DeviceInfoProtos; import org.thingsboard.server.gen.transport.coap.MeasurementTypeProtos; import org.thingsboard.server.gen.transport.coap.MeasurementsProtos; +import org.thingsboard.server.gen.transport.coap.MeasurementsProtos.ProtoChannel; import org.thingsboard.server.transport.coap.AbstractCoapTransportResource; import org.thingsboard.server.transport.coap.CoapTransportContext; import org.thingsboard.server.transport.coap.callback.CoapDeviceAuthCallback; @@ -51,15 +50,18 @@ import org.thingsboard.server.transport.coap.efento.utils.CoapEfentoUtils; import java.nio.ByteBuffer; import java.text.SimpleDateFormat; -import java.util.ArrayList; import java.util.Date; import java.util.List; import java.util.Map; import java.util.TreeMap; import java.util.UUID; import java.util.concurrent.TimeUnit; +import java.util.stream.Collectors; import static com.google.gson.JsonParser.parseString; +import static org.thingsboard.server.gen.transport.coap.MeasurementTypeProtos.MeasurementType.MEASUREMENT_TYPE_FLOODING; +import static org.thingsboard.server.gen.transport.coap.MeasurementTypeProtos.MeasurementType.MEASUREMENT_TYPE_OK_ALARM; +import static org.thingsboard.server.gen.transport.coap.MeasurementTypeProtos.MeasurementType.MEASUREMENT_TYPE_OUTPUT_CONTROL; import static org.thingsboard.server.transport.coap.CoapTransportService.CONFIGURATION; import static org.thingsboard.server.transport.coap.CoapTransportService.CURRENT_TIMESTAMP; import static org.thingsboard.server.transport.coap.CoapTransportService.DEVICE_INFO; @@ -228,121 +230,190 @@ public class CoapEfentoTransportResource extends AbstractCoapTransportResource { int measurementPeriodBase = protoMeasurements.getMeasurementPeriodBase(); int measurementPeriodFactor = protoMeasurements.getMeasurementPeriodFactor(); int signal = protoMeasurements.getSignal(); - List channelsList = protoMeasurements.getChannelsList(); + long nextTransmissionAtMillis = TimeUnit.SECONDS.toMillis(protoMeasurements.getNextTransmissionAt()); + + List channelsList = protoMeasurements.getChannelsList(); + if (CollectionUtils.isEmpty(channelsList)) { + throw new IllegalStateException("[" + sessionId + "]: Failed to get Efento measurements, reason: channels list is empty!"); + } + Map valuesMap = new TreeMap<>(); - if (!CollectionUtils.isEmpty(channelsList)) { - int channel = 0; - JsonObject values; - for (MeasurementsProtos.ProtoChannel protoChannel : channelsList) { - channel++; - boolean isBinarySensor = false; - MeasurementTypeProtos.MeasurementType measurementType = protoChannel.getType(); - String measurementTypeName = measurementType.name(); - if (measurementType.equals(MeasurementTypeProtos.MeasurementType.OK_ALARM) - || measurementType.equals(MeasurementTypeProtos.MeasurementType.FLOODING)) { - isBinarySensor = true; - } - if (measurementPeriodFactor == 0 && isBinarySensor) { - measurementPeriodFactor = 14; - } else { - measurementPeriodFactor = 1; + for (int channel = 0; channel < channelsList.size(); channel++) { + ProtoChannel protoChannel = channelsList.get(channel); + boolean isBinarySensor = isBinarySensor(protoChannel.getType()); + int channelPeriodFactor = (measurementPeriodFactor == 0 ? (isBinarySensor ? 14 : 1) : measurementPeriodFactor); + int measurementPeriod = measurementPeriodBase * channelPeriodFactor; + long measurementPeriodMillis = TimeUnit.SECONDS.toMillis(measurementPeriod); + long startTimestampMillis = TimeUnit.SECONDS.toMillis(protoChannel.getTimestamp()); + List sampleOffsetsList = protoChannel.getSampleOffsetsList(); + + if (CollectionUtils.isEmpty(sampleOffsetsList)) { + log.trace("[{}][{}] sampleOffsetsList list is empty!", sessionId, protoChannel.getType().name()); + continue; + } + + for (int i = 0; i < sampleOffsetsList.size(); i++) { + int sampleOffset = sampleOffsetsList.get(i); + if (isSensorError(sampleOffset)) { + log.warn("[{}],[{}] Sensor error value! Ignoring.", sessionId, sampleOffset); + continue; } - int measurementPeriod = measurementPeriodBase * measurementPeriodFactor; - long measurementPeriodMillis = TimeUnit.SECONDS.toMillis(measurementPeriod); - long nextTransmissionAtMillis = TimeUnit.SECONDS.toMillis(protoMeasurements.getNextTransmissionAt()); - int startPoint = protoChannel.getStartPoint(); - int startTimestamp = protoChannel.getTimestamp(); - long startTimestampMillis = TimeUnit.SECONDS.toMillis(startTimestamp); - List sampleOffsetsList = protoChannel.getSampleOffsetsList(); - if (!CollectionUtils.isEmpty(sampleOffsetsList)) { - int sampleOfssetsListSize = sampleOffsetsList.size(); - for (int i = 0; i < sampleOfssetsListSize; i++) { - int sampleOffset = sampleOffsetsList.get(i); - Integer previousSampleOffset = isBinarySensor && i > 0 ? sampleOffsetsList.get(i - 1) : null; - if (sampleOffset == -32768) { - log.warn("[{}],[{}] Sensor error value! Ignoring.", sessionId, sampleOffset); - } else { - switch (measurementType) { - case TEMPERATURE: - values = valuesMap.computeIfAbsent(startTimestampMillis, k -> - CoapEfentoUtils.setDefaultMeasurements(serialNumber, batteryStatus, measurementPeriod, nextTransmissionAtMillis, signal, k)); - values.addProperty("temperature_" + channel, ((double) (startPoint + sampleOffset)) / 10f); - startTimestampMillis = startTimestampMillis + measurementPeriodMillis; - break; - case WATER_METER: - values = valuesMap.computeIfAbsent(startTimestampMillis, k -> - CoapEfentoUtils.setDefaultMeasurements(serialNumber, batteryStatus, measurementPeriod, nextTransmissionAtMillis, signal, k)); - values.addProperty("pulse_counter_water_" + channel, ((double) (startPoint + sampleOffset))); - startTimestampMillis = startTimestampMillis + measurementPeriodMillis; - break; - case HUMIDITY: - values = valuesMap.computeIfAbsent(startTimestampMillis, k -> - CoapEfentoUtils.setDefaultMeasurements(serialNumber, batteryStatus, measurementPeriod, nextTransmissionAtMillis, signal, k)); - values.addProperty("humidity_" + channel, (double) (startPoint + sampleOffset)); - startTimestampMillis = startTimestampMillis + measurementPeriodMillis; - break; - case ATMOSPHERIC_PRESSURE: - values = valuesMap.computeIfAbsent(startTimestampMillis, k -> - CoapEfentoUtils.setDefaultMeasurements(serialNumber, batteryStatus, measurementPeriod, nextTransmissionAtMillis, signal, k)); - values.addProperty("pressure_" + channel, (double) (startPoint + sampleOffset) / 10f); - startTimestampMillis = startTimestampMillis + measurementPeriodMillis; - break; - case DIFFERENTIAL_PRESSURE: - values = valuesMap.computeIfAbsent(startTimestampMillis, k -> - CoapEfentoUtils.setDefaultMeasurements(serialNumber, batteryStatus, measurementPeriod, nextTransmissionAtMillis, signal, k)); - values.addProperty("pressure_diff_" + channel, (double) (startPoint + sampleOffset)); - startTimestampMillis = startTimestampMillis + measurementPeriodMillis; - break; - case OK_ALARM: - boolean currentIsOk = sampleOffset < 0; - if (previousSampleOffset != null) { - boolean previousIsOk = previousSampleOffset < 0; - boolean isOk = previousIsOk && currentIsOk; - boolean isAlarm = !previousIsOk && !currentIsOk; - if (isOk || isAlarm) { - break; - } - } - String data = currentIsOk ? "OK" : "ALARM"; - long sampleOffsetMillis = TimeUnit.SECONDS.toMillis(sampleOffset); - long measurementTimestamp = startTimestampMillis + Math.abs(sampleOffsetMillis); - values = valuesMap.computeIfAbsent(measurementTimestamp - 1000, k -> - CoapEfentoUtils.setDefaultMeasurements(serialNumber, batteryStatus, measurementPeriod, nextTransmissionAtMillis, signal, k)); - values.addProperty("ok_alarm_" + channel, data); - break; - case PULSE_CNT: - values = valuesMap.computeIfAbsent(startTimestampMillis, k -> - CoapEfentoUtils.setDefaultMeasurements(serialNumber, batteryStatus, measurementPeriod, nextTransmissionAtMillis, signal, k)); - values.addProperty("pulse_cnt_" + channel, (double) (startPoint + sampleOffset)); - startTimestampMillis = startTimestampMillis + measurementPeriodMillis; - break; - case NO_SENSOR: - case UNRECOGNIZED: - log.trace("[{}][{}] Sensor error value! Ignoring.", sessionId, measurementTypeName); - break; - default: - log.trace("[{}],[{}] Unsupported measurementType! Ignoring.", sessionId, measurementTypeName); - break; - } - } + + JsonObject values; + if (isBinarySensor) { + long sampleOffsetMillis = TimeUnit.SECONDS.toMillis(sampleOffset); + long measurementTimestamp = startTimestampMillis + Math.abs(sampleOffsetMillis) - 1; + values = valuesMap.computeIfAbsent(measurementTimestamp - 1000, k -> + CoapEfentoUtils.setDefaultMeasurements(serialNumber, batteryStatus, measurementPeriod, nextTransmissionAtMillis, signal, k)); + Integer previousSampleOffset = i > 0 ? sampleOffsetsList.get(i - 1) : null; + boolean previousValueIsOk = previousSampleOffset != null && previousSampleOffset < 0; + boolean valueIsOk = sampleOffset < 0; + if (binaryValueNotChanged(valueIsOk, previousValueIsOk)) { + break; } + addBinarySample(protoChannel, valueIsOk, values, channel + 1); } else { - log.trace("[{}][{}] sampleOffsetsList list is empty!", sessionId, measurementTypeName); + long timestampMillis = startTimestampMillis + i * measurementPeriodMillis; + values = valuesMap.computeIfAbsent(timestampMillis, k -> CoapEfentoUtils.setDefaultMeasurements( + serialNumber, batteryStatus, measurementPeriod, nextTransmissionAtMillis, signal, k)); + addContinuesSample(protoChannel, sampleOffset, values, channel + 1, sessionId); } } - } else { - throw new IllegalStateException("[" + sessionId + "]: Failed to get Efento measurements, reason: channels list is empty!"); } - if (!CollectionUtils.isEmpty(valuesMap)) { - List efentoMeasurements = new ArrayList<>(); - for (Long ts : valuesMap.keySet()) { - EfentoTelemetry measurement = new EfentoTelemetry(ts, valuesMap.get(ts)); - efentoMeasurements.add(measurement); - } - return efentoMeasurements; - } else { + + if (CollectionUtils.isEmpty(valuesMap)) { throw new IllegalStateException("[" + sessionId + "]: Failed to collect Efento measurements, reason, values map is empty!"); } + + return valuesMap.entrySet().stream() + .map(entry -> new EfentoTelemetry(entry.getKey(), entry.getValue())) + .collect(Collectors.toList()); + } + + private boolean isBinarySensor(MeasurementTypeProtos.MeasurementType type) { + return type == MEASUREMENT_TYPE_OK_ALARM || type == MEASUREMENT_TYPE_FLOODING || type == MEASUREMENT_TYPE_OUTPUT_CONTROL; + } + + private boolean isSensorError(int sampleOffset) { + return sampleOffset >= 8355840 && sampleOffset <= 8388607; + } + + private void addContinuesSample(ProtoChannel protoChannel, int sampleOffset, JsonObject values, int channelNumber, UUID sessionId) { + int startPoint = protoChannel.getStartPoint(); + + switch (protoChannel.getType()) { + case MEASUREMENT_TYPE_TEMPERATURE: + values.addProperty("temperature_" + channelNumber, ((double) (startPoint + sampleOffset)) / 10f); + break; + case MEASUREMENT_TYPE_WATER_METER: + values.addProperty("pulse_counter_water_" + channelNumber, ((double) (startPoint + sampleOffset))); + break; + case MEASUREMENT_TYPE_HUMIDITY: + values.addProperty("humidity_" + channelNumber, (double) (startPoint + sampleOffset)); + break; + case MEASUREMENT_TYPE_ATMOSPHERIC_PRESSURE: + values.addProperty("pressure_" + channelNumber, (double) (startPoint + sampleOffset) / 10f); + break; + case MEASUREMENT_TYPE_DIFFERENTIAL_PRESSURE: + values.addProperty("pressure_diff_" + channelNumber, (double) (startPoint + sampleOffset)); + break; + case MEASUREMENT_TYPE_PULSE_CNT: + values.addProperty("pulse_cnt_" + channelNumber, (double) (startPoint + sampleOffset)); + break; + case MEASUREMENT_TYPE_IAQ: + values.addProperty("iaq_" + channelNumber, (startPoint + sampleOffset)); + break; + case MEASUREMENT_TYPE_ELECTRICITY_METER: + values.addProperty("watt_hour_" + channelNumber, (double) (startPoint + sampleOffset)); + break; + case MEASUREMENT_TYPE_SOIL_MOISTURE: + values.addProperty("soil_moisture_" + channelNumber, (double) (startPoint + sampleOffset)); + break; + case MEASUREMENT_TYPE_AMBIENT_LIGHT: + values.addProperty("ambient_light_" + channelNumber, (double) (startPoint + sampleOffset) / 10f); + break; + case MEASUREMENT_TYPE_HIGH_PRESSURE: + values.addProperty("high_pressure_" + channelNumber, (double) (startPoint + sampleOffset)); + break; + case MEASUREMENT_TYPE_DISTANCE_MM: + values.addProperty("distance_mm_" + channelNumber, (double) (startPoint + sampleOffset)); + break; + case MEASUREMENT_TYPE_WATER_METER_ACC_MINOR: + values.addProperty("acc_counter_water_minor_" + channelNumber, (double) (startPoint + sampleOffset)); + break; + case MEASUREMENT_TYPE_WATER_METER_ACC_MAJOR: + values.addProperty("acc_counter_water_major_" + channelNumber, (double) (startPoint + sampleOffset)); + break; + case MEASUREMENT_TYPE_HUMIDITY_ACCURATE: + values.addProperty("humidity_relative_" + channelNumber, (double) (startPoint + sampleOffset) / 10f); + break; + case MEASUREMENT_TYPE_STATIC_IAQ: + values.addProperty("static_iaq_" + channelNumber, (double) (startPoint + sampleOffset)); + break; + case MEASUREMENT_TYPE_CO2_EQUIVALENT: + values.addProperty("co2_ppm_" + channelNumber, (double) (startPoint + sampleOffset)); + break; + case MEASUREMENT_TYPE_BREATH_VOC: + values.addProperty("breath_voc_ppm_" + channelNumber, (double) (startPoint + sampleOffset)); + break; + case MEASUREMENT_TYPE_PERCENTAGE: + values.addProperty("percentage_" + channelNumber, (double) (startPoint + sampleOffset) / 100f); + break; + case MEASUREMENT_TYPE_VOLTAGE: + values.addProperty("voltage_" + channelNumber, (double) (startPoint + sampleOffset) / 10f); + break; + case MEASUREMENT_TYPE_CURRENT: + values.addProperty("current_" + channelNumber, (double) (startPoint + sampleOffset) / 100f); + break; + case MEASUREMENT_TYPE_PULSE_CNT_ACC_MINOR: + values.addProperty("pulse_cnt_minor_" + channelNumber, (double) (startPoint + sampleOffset)); + break; + case MEASUREMENT_TYPE_PULSE_CNT_ACC_MAJOR: + values.addProperty("pulse_cnt_major_" + channelNumber, (double) (startPoint + sampleOffset)); + break; + case MEASUREMENT_TYPE_ELEC_METER_ACC_MINOR: + values.addProperty("elec_meter_minor_" + channelNumber, (double) (startPoint + sampleOffset)); + break; + case MEASUREMENT_TYPE_ELEC_METER_ACC_MAJOR: + values.addProperty("elec_meter_major_" + channelNumber, (double) (startPoint + sampleOffset)); + break; + case MEASUREMENT_TYPE_PULSE_CNT_ACC_WIDE_MINOR: + values.addProperty("pulse_cnt_wide_minor_" + channelNumber, (double) (startPoint + sampleOffset)); + break; + case MEASUREMENT_TYPE_PULSE_CNT_ACC_WIDE_MAJOR: + values.addProperty("pulse_cnt_wide_major_" + channelNumber, (double) (startPoint + sampleOffset)); + break; + case MEASUREMENT_TYPE_CURRENT_PRECISE: + values.addProperty("current_precise_" + channelNumber, (double) (startPoint + sampleOffset)/1000f); + break; + case MEASUREMENT_TYPE_NO_SENSOR: + case UNRECOGNIZED: + log.trace("[{}][{}] Sensor error value! Ignoring.", sessionId, protoChannel.getType().name()); + break; + default: + log.trace("[{}],[{}] Unsupported measurementType! Ignoring.", sessionId, protoChannel.getType().name()); + break; + } + } + + private void addBinarySample(ProtoChannel protoChannel, boolean valueIsOk, JsonObject values, int channel) { + switch (protoChannel.getType()) { + case MEASUREMENT_TYPE_OK_ALARM: + values.addProperty("ok_alarm_" + channel, valueIsOk ? "OK" : "ALARM"); + case MEASUREMENT_TYPE_FLOODING: + values.addProperty("flooding_" + channel, valueIsOk ? "OK" : "WATER_DETECTED"); + case MEASUREMENT_TYPE_OUTPUT_CONTROL: + values.addProperty("output_control_" + channel, valueIsOk ? "OFF" : "ON"); + } + } + + private static boolean binaryValueNotChanged(boolean currentIsOk, boolean previousIsOk) { + boolean isOk = previousIsOk && currentIsOk; + boolean isAlarm = !previousIsOk && !currentIsOk; + if (isOk || isAlarm) { + return true; + } + return false; } private EfentoTelemetry getEfentoDeviceInfo(DeviceInfoProtos.ProtoDeviceInfo protoDeviceInfo) { diff --git a/common/transport/coap/src/main/proto/efento/proto_config.proto b/common/transport/coap/src/main/proto/efento/proto_config.proto index 6212681403..25dbec883f 100644 --- a/common/transport/coap/src/main/proto/efento/proto_config.proto +++ b/common/transport/coap/src/main/proto/efento/proto_config.proto @@ -22,26 +22,13 @@ option java_package = "org.thingsboard.server.gen.transport.coap"; option java_outer_classname = "ConfigProtos"; /* Message containing optional channels control parameters */ -message ProtoChannelControl { +message ProtoOutputControlState { /* Channel index */ uint32 channel_index = 1; - /* Control parameters. Maximal number equals 4. This field is channel specific: */ - /* IO_control channel: */ - /* - control_params[0]: */ - /* - Byte 0: On state configuration */ - /* 0x01 - Low */ - /* 0x02 - High */ - /* 0x03 - High-Z (disconnected) */ - /* - Byte 1: Off state configuration */ - /* 0x01 - Low */ - /* 0x02 - High */ - /* 0x03 - High-Z (disconnected) */ - /* - Byte 2: Power on channel state */ - /* 0x01 - On */ - /* 0x02 - Off */ - repeated uint32 control_params = 2; + /* Channel state ON/OFF. Range (1 - OFF; 2 - ON) */ + uint32 channel_state = 2; } /* Message containing request data for accesing calibration parameters */ @@ -58,6 +45,39 @@ message ProtoCalibrationParameters { repeated int32 parameters = 3; } +enum BleAdvertisingPeriodMode { + + /* Invalid value */ + BLE_ADVERTISING_PERIOD_MODE_UNSPECIFIED = 0; + + /* Default behavior - faster advertising when measurement period is < 15s. */ + BLE_ADVERTISING_PERIOD_MODE_DEFAULT = 1; + + /* User-configured normal interval is used. */ + BLE_ADVERTISING_PERIOD_MODE_NORMAL = 2; + + /* User-configured fast interval is used. */ + BLE_ADVERTISING_PERIOD_MODE_FAST = 3; +} + +/* Message containing BLE advertising period configuration */ +message ProtoBleAdvertisingPeriod { + + /* BLE advertising mode: */ + /* - 1: Default, BLE advertising interval is set to 1022.5ms or some lower value, based on continuous measurement period. */ + /* - 2: Normal, uses user-configured value from 'normal' field. */ + /* - 3: Fast, uses user-configured value from 'fast' field (must be lower than or equal to 'normal' field). */ + BleAdvertisingPeriodMode mode = 1; + + /* BLE advertising interval when in normal mode, configured in 0.625ms steps. */ + /* Range: [32:16384] */ + uint32 normal = 2; + + /* BLE advertising interval when in fast mode, configured in 0.625ms steps. */ + /* Range: [32:16384] */ + uint32 fast = 3; +} + /* Main message sent in the payload. Each field in this message is independent of the others - only parameters that should be */ /* changed need to be sent in the payload. */ /* If the value of a selected parameter shall not be changed, do not include it in the payload */ @@ -115,16 +135,26 @@ message ProtoConfig { /* 65535 - disable transfer limit function */ uint32 transfer_limit_timer = 10; - /* Data (measurements) server IP address */ - /* IP of the data server as string in form x.x.x.x. For example: 18.184.24.239 */ + /* For firmware >= 6.07.00: */ + /* IP or URL address of the data (measurements) server */ + /* The IP or URL of the data server, provided as string with a maximum length of 31 characters */ + /* For example, use "18.184.24.239" for an IP address or "efento.test.io" for a URL */ + /* For firmware < 6.07.00: */ + /* IP address of the data (measurements) server */ + /* For example, use "18.184.24.239" */ string data_server_ip = 11; /* Data (measurements) server port */ /* Range: [1:65535] */ uint32 data_server_port = 12; - /* Update server IP address */ - /* IP of data server as string in form x.x.x.x. For example: 18.184.24.239 */ + /* For firmware >= 6.07.00: */ + /* IP or URL address of the update server */ + /* The IP or URL of the update server, provided as string with a maximum length of 31 characters */ + /* For example, use "18.184.24.239" for an IP address or "efento.test.io" for a URL */ + /* For firmware < 6.07.00: */ + /* IP address of the update server */ + /* For example, use "18.184.24.239" */ string update_server_ip = 13; /* Update server port for UDP transmission */ @@ -144,7 +174,8 @@ message ProtoConfig { /* 0xFFFFFFFF or 1000000 - automatic selection */ uint32 plmn_selection = 17; - /* Device will power off its cellular modem for requested number of seconds. Maximum number of seconds 604800 (7 days) */ + /* Device will power off its cellular modem for requested number of seconds. */ + /* Range: [60:604800] (1 minute : 7 days) */ /* This field is only sent by server */ uint32 disable_modem_request = 18; @@ -263,9 +294,8 @@ message ProtoConfig { /* Calendar configuration. Up to 6 calendars are supported */ repeated ProtoCalendar calendars = 47; - /* Control parameters for channels. Maximal number of requests equals 6 */ - /* This field is only sent by server */ - repeated ProtoChannelControl channels_control_request = 48; + /* DEPRECATED - Used for backward compatibility */ + reserved 48; /* Set/get calibration parameters for single channel. */ ProtoCalibrationParameters calibration_parameters_request = 49; @@ -295,7 +325,7 @@ message ProtoConfig { reserved 52, 53; /* Encryption key configuration. Sensor sends in this field two last bytes of SHA256 hash calculated from its current */ - /* encryption_key configuration. */ + /* encryption_key configuration. When encryption key is disabled one byte 0x7F (DEL) is sent. */ /* Max length: 16 bytes. */ /* 0x7F - encryption key disabled. */ bytes encryption_key = 54; @@ -309,4 +339,14 @@ message ProtoConfig { /* String with special character 0x7F (DEL) only indicates that automatic password is turn on */ /* Password can only be set to custom value if apn_user_name has been configured (is not automatic) */ string apn_password = 56; + + /* Reserved by versions above 06.20.00 */ + reserved 57; + + /* Control output state on channel pin. Maximal number of requests equals 3 */ + /* This field is only sent by server */ + repeated ProtoOutputControlState output_control_state_request = 58; + + /* BLE advertising period configuration. */ + ProtoBleAdvertisingPeriod ble_advertising_period = 59; } \ No newline at end of file diff --git a/common/transport/coap/src/main/proto/efento/proto_device_info.proto b/common/transport/coap/src/main/proto/efento/proto_device_info.proto index 791d01acd7..a0cb9bc2f2 100644 --- a/common/transport/coap/src/main/proto/efento/proto_device_info.proto +++ b/common/transport/coap/src/main/proto/efento/proto_device_info.proto @@ -106,6 +106,11 @@ message ProtoModem /* parameters[32] - Rx_time - [0.1s] - Range: [0:2147483647]. Unknown value: -1 */ /* parameters[33] - Tx_time - [0.1s] - Range: [0:2147483647]. Unknown value: -1 */ repeated sint32 parameters = 2; + + /* ICCID of inserted/soldered sim card. String up to 22 characters long. */ + /* 0x7F if sim card is not detected, empty (not sent) if device does not have modem. */ + /* This field is only sent by device */ + string sim_card_identification = 3; } message ProtoUpdateInfo @@ -115,16 +120,16 @@ message ProtoUpdateInfo uint32 timestamp = 1; /* Update status, possible values: */ - /* - 0x1 - No update yet */ - /* - 0x2 - No error */ - /* - 0x3 - UDP socekt error */ - /* - 0x4 - Hash error */ - /* - 0x5 - Missing packet error */ - /* - 0x6 - Invalid data error */ - /* - 0x7 - Sending timeout error */ - /* - 0x8 - No SW to update error */ - /* - 0x9 - Sending unexpected error */ - /* - 0x10 - Unexpected error */ + /* - 1 - No update yet */ + /* - 2 - No error */ + /* - 3 - UDP socekt error */ + /* - 4 - Hash error */ + /* - 5 - Missing packet error */ + /* - 6 - Invalid data error */ + /* - 7 - Sending timeout error */ + /* - 8 - No SW to update error */ + /* - 9 - Sending unexpected error */ + /* - 10 - Unexpected error */ uint32 status = 2; } diff --git a/common/transport/coap/src/main/proto/efento/proto_measurement_types.proto b/common/transport/coap/src/main/proto/efento/proto_measurement_types.proto index 66a6c95d09..98029bed51 100644 --- a/common/transport/coap/src/main/proto/efento/proto_measurement_types.proto +++ b/common/transport/coap/src/main/proto/efento/proto_measurement_types.proto @@ -19,79 +19,160 @@ option java_package = "org.thingsboard.server.gen.transport.coap"; option java_outer_classname = "MeasurementTypeProtos"; enum MeasurementType { - NO_SENSOR = 0; - - /* [°C] - Celsius degree. Resolution 0.1°C. Range [-273.2-4000.0]. Type: Continuous */ - TEMPERATURE = 1; - - /* [% RH] - Relative humidity. Resolution 1%. Range [0-100]. Type: Continuous */ - HUMIDITY = 2; - - /* [hPa] - Hectopascal (1hPa = 100Pa). Resolution 0.1hPa. Range: [1.0-2000.0]. Type: Continuous */ - ATMOSPHERIC_PRESSURE = 3; - - /* [Pa] - Pascal. Resolution 1Pa. Range [-10000-10000]Type: Continuous */ - DIFFERENTIAL_PRESSURE = 4; - - /* Sign indicates state: (+) ALARM, (-) OK. Type: Binary */ - OK_ALARM = 5; - - /* [IAQ] - Iaq index. Resolution 1IAQ. Range [0-500]. Sensor return also calibration status */ - /* as offset to measured value: */ - /* - offset 3000: Sensor not stabilized (always returns 25 IAQ value) */ - /* - offset 2000: Calibration required (sensor returns not accurate values) */ - /* - offset 1000: Calibration on-going (sensor returns not accurate values) */ - /* - offset 0: Calibration done (best accuracy of IAQ sensor) */ - /* Type: Continuous */ - IAQ = 6; - - /* Sign indicates water presence: (+) water not detected, (-) water detected. Type: Binary */ - FLOODING = 7; - - /* [NB] Number of pulses. Resolution 1 pulse. Range [0-16711679]. Type: Continuous */ - PULSE_CNT = 8; - - /* [Wh] - Watthour; Resolution 1Wh. Range [0-16711679]. Number of Watthours in a single period. Type: Continuous */ - ELECTRICITY_METER = 9; - - /* [l] - Liter. Resolution 1l. Range [0-16711679]. Number of litres in a single period. Type: Continuous */ - WATER_METER = 10; - - /* [kPa] - Kilopascal (1kPa = 1000Pa); Resolution 1kPa. Range [-1000-0]. Soil moisture (tension). Type: Continuous */ - SOIL_MOISTURE = 11; - - /* [ppm] - Parts per million. Resolution 1ppm. Range [0-1000000]. Carbon monoxide concentration. Type: Continuous */ - CO_GAS = 12; - - /* [ppm] - Parts per million. Resolution 0.01ppm. Range [0-1000000.00]. Nitrogen dioxide concentration. Type: Continuous*/ - NO2_GAS = 13; - - /* [ppm] - Parts per million. Resolution 1ppm. Range [0-1000000]. Hydrogen sulfide concentration. Type: Continuous */ - H2S_GAS = 14; - - /* [lx] - Illuminance. Resolution 0.1lx. Range [0-100000.0]. Type: Continuous */ - AMBIENT_LIGHT = 15; - - /* [µg/m^3] - Micro gram per cubic meter. Resolution 1µg/m^3 Range [0-1000]. */ - /* particles with an aerodynamic diameter less than 1 micrometer. Type: Continuous */ - PM_1_0 = 16; // µg/m^3 - - /* [µg/m^3] - Micro gram per cubic meter. Resolution 1µg/m^3 Range [0-1000]. */ - /* particles with an aerodynamic diameter less than 2.5 micrometers. Type: Continuous */ - PM_2_5 = 17; // µg/m^3 - - /* [µg/m^3] - Micro gram per cubic meter. Resolution 1µg/m^3 Range [0-1000]. */ - /* particles with an aerodynamic diameter less than 10 micrometers. Type: Continuous */ - PM_10_0 = 18; // µg/m^3 - - /* [dB] - Decibels. Resolution 0.1 dB. Range: [0-130.0]. Noise level. Type: Continuous */ - NOISE_LEVEL = 19; // 0.1 dB - - /* [ppm] - Parts per million. Resolution 1ppm. Range [0-1000000]. Ammonia concentration. Type: Continuous */ - NH3_GAS = 20; - - /* [ppm] - Parts per million. Resolution 1ppm. Range [0-1000000]. Methane concentration. Type: Continuous */ - CH4_GAS = 21; + /* [] - No sensor on the channel */ + MEASUREMENT_TYPE_NO_SENSOR = 0; + + /* [°C] - Celsius degree. Resolution 0.1°C. Range [-273.2:4000.0]. Type: Continuous */ + MEASUREMENT_TYPE_TEMPERATURE = 1; + + /* [% RH] - Relative humidity. Resolution 1%. Range [0:100]. Type: Continuous */ + MEASUREMENT_TYPE_HUMIDITY = 2; + + /* [hPa] - Hectopascal (1hPa = 100Pa). Resolution 0.1hPa. Range: [1.0:2000.0]. Atmospheric pressure. Type: Continuous */ + MEASUREMENT_TYPE_ATMOSPHERIC_PRESSURE = 3; + + /* [Pa] - Pascal. Resolution 1Pa. Range [-10000:10000]. Differential pressure. Type: Continuous */ + MEASUREMENT_TYPE_DIFFERENTIAL_PRESSURE = 4; + + /* Sign indicates state: (+) ALARM, (-) OK. Type: Binary */ + MEASUREMENT_TYPE_OK_ALARM = 5; + + /* [IAQ] - IAQ index. Resolution 1IAQ. Range [0:500]. To get IAQ index the value should be divided by 3. */ + /* Sensor return also calibration status as metadata (is the remainder when the absolute value is divided by 3): */ + /* - 0: Calibration required (sensor returns not accurate values) */ + /* - 1: Calibration on-going (sensor returns not accurate values) */ + /* - 2: Calibration done (best accuracy of IAQ sensor) */ + /* Type: Continuous */ + MEASUREMENT_TYPE_IAQ = 6; + + /* Sign indicates water presence: (+) water not detected, (-) water detected. Type: Binary */ + MEASUREMENT_TYPE_FLOODING = 7; + + /* [NB] Number of pulses. Resolution 1 pulse. Range [0:8000000]. Type: Continuous */ + MEASUREMENT_TYPE_PULSE_CNT = 8; + + /* [Wh] - Watthour; Resolution 1Wh. Range [0:8000000]. Number of Watthours in a single period. Type: Continuous */ + MEASUREMENT_TYPE_ELECTRICITY_METER = 9; + + /* [l] - Liter. Resolution 1l. Range [0:8000000]. Number of litres in a single period. Type: Continuous */ + MEASUREMENT_TYPE_WATER_METER = 10; + + /* [kPa] - Kilopascal (1kPa = 1000Pa); Resolution 1kPa. Range [-1000:0]. Soil moisture (tension). Type: Continuous */ + MEASUREMENT_TYPE_SOIL_MOISTURE = 11; + + /* [ppm] - Parts per million. Resolution 1ppm. Range [0:1000000]. Carbon monoxide concentration. Type: Continuous */ + MEASUREMENT_TYPE_CO_GAS = 12; + + /* [ppm] - Parts per million. Resolution 1.0ppm. Range [0:1000000]. Nitrogen dioxide concentration. Type: Continuous */ + MEASUREMENT_TYPE_NO2_GAS = 13; + + /* [ppm] - Parts per million. Resolution 0.01ppm. Range [0.00:80000.00]. Hydrogen sulfide concentration. Type: Continuous */ + MEASUREMENT_TYPE_H2S_GAS = 14; + + /* [lx] - Lux. Resolution 0.1lx. Range [0.0:100000.0]. Illuminance. Type: Continuous */ + MEASUREMENT_TYPE_AMBIENT_LIGHT = 15; + + /* [µg/m^3] - Micro gram per cubic meter. Resolution 1µg/m^3. Range [0:1000]. */ + /* Particles with an aerodynamic diameter less than 1 micrometer. Type: Continuous */ + MEASUREMENT_TYPE_PM_1_0 = 16; + + /* [µg/m^3] - Micro gram per cubic meter. Resolution 1µg/m^3. Range [0:1000]. */ + /* Particles with an aerodynamic diameter less than 2.5 micrometers. Type: Continuous */ + MEASUREMENT_TYPE_PM_2_5 = 17; + + /* [µg/m^3] - Micro gram per cubic meter. Resolution 1µg/m^3. Range [0:1000]. */ + /* Particles with an aerodynamic diameter less than 10 micrometers. Type: Continuous */ + MEASUREMENT_TYPE_PM_10_0 = 18; + + /* [dB] - Decibels. Resolution 0.1 dB. Range: [0.0:200.0]. Noise level. Type: Continuous */ + MEASUREMENT_TYPE_NOISE_LEVEL = 19; + + /* [ppm] - Parts per million. Resolution 1ppm. Range [0:1000000]. Ammonia concentration. Type: Continuous */ + MEASUREMENT_TYPE_NH3_GAS = 20; + + /* [ppm] - Parts per million. Resolution 1ppm. Range [0:1000000]. Methane concentration. Type: Continuous */ + MEASUREMENT_TYPE_CH4_GAS = 21; + + /* [kPa] - Kilopascal (1kPa = 1000Pa, 100kPa = 1bar). Resolution 1kPa. Range [0:200000]. Pressure. Type: Continuous */ + MEASUREMENT_TYPE_HIGH_PRESSURE = 22; + + /* [mm] - Millimeter. Resolution 1mm. Range [0:100000]. Distance. Type: Continuous */ + MEASUREMENT_TYPE_DISTANCE_MM = 23; + + /* [l] - Liter. Resolution 1l. Range [0:1000000]. Accumulative water meter (minor). Type: Continuous */ + MEASUREMENT_TYPE_WATER_METER_ACC_MINOR = 24; + + /* [hl] - Hectoliter. Resolution 1hl. Range [0:1000000]. Accumulative water meter (major). Type: Continuous */ + MEASUREMENT_TYPE_WATER_METER_ACC_MAJOR = 25; + + /* [ppm] - Parts per million. Resolution 1ppm. Range [0:1000000]. Carbon dioxide concentration. Type: Continuous */ + MEASUREMENT_TYPE_CO2_GAS = 26; + + /* [% RH] - Relative humidity. Resolution 0.1%. Range [0.0:100.0]. Type: Continuous */ + MEASUREMENT_TYPE_HUMIDITY_ACCURATE = 27; + + /* [sIAQ] - Static IAQ index. Resolution 1IAQ. Range [0:10000]. To get static IAQ index the value should be divided by 3. */ + /* Sensor return also calibration status as metadata (is the remainder when the absolute value is divided by 3): */ + /* - 0: Calibration required (sensor returns not accurate values) */ + /* - 1: Calibration on-going (sensor returns not accurate values) */ + /* - 2: Calibration done (best accuracy of IAQ sensor) */ + /* Type: Continuous */ + MEASUREMENT_TYPE_STATIC_IAQ = 28; + + /* [ppm] - Parts per million. Resolution 1ppm. Range [0:1000000]. CO2 equivalent. */ + /* To get CO2 equivalent the value should be divided by 3. */ + /* Sensor return also calibration status as metadata (is the remainder when the absolute value is divided by 3): */ + /* - 0: Calibration required (sensor returns not accurate values) */ + /* - 1: Calibration on-going (sensor returns not accurate values) */ + /* - 2: Calibration done (best accuracy of IAQ sensor) */ + /* Type: Continuous */ + MEASUREMENT_TYPE_CO2_EQUIVALENT = 29; + + /* [ppm] - Parts per million. Resolution 1ppm. Range [0:100000]. Breath VOC estimate. */ + /* To get breath VOC estimate the value should be divided by 3. */ + /* Sensor return also calibration status as metadata (is the remainder when the absolute value is divided by 3): */ + /* - 0: Calibration required (sensor returns not accurate values) */ + /* - 1: Calibration on-going (sensor returns not accurate values) */ + /* - 2: Calibration done (best accuracy of IAQ sensor) */ + /* Type: Continuous */ + MEASUREMENT_TYPE_BREATH_VOC = 30; + + /* Special measurement type reserved for cellular gateway. */ + /* Type: Continuous */ + MEASUREMENT_TYPE_CELLULAR_GATEWAY = 31; + + /* [%] - Percentage. Resolution 0.01%. Range [0.00:100.00]. Type: Continuous */ + MEASUREMENT_TYPE_PERCENTAGE = 32; + + /* [mV] - Milivolt. Resolution 0.1mV. Range [0.0:100000.0]. Type: Continuous */ + MEASUREMENT_TYPE_VOLTAGE = 33; + + /* [mA] - Milliampere. Resolution 0.01mA. Range [0.0:10000.00]. Type: Continuous */ + MEASUREMENT_TYPE_CURRENT = 34; + + /* [NB] Number of pulses. Resolution 1 pulse. Range [0:1000000]. Type: Continuous */ + MEASUREMENT_TYPE_PULSE_CNT_ACC_MINOR = 35; + + /* [kNB] Number of kilopulses. Resolution 1 kilopulse. Range [0:1000000]. Type: Continuous */ + MEASUREMENT_TYPE_PULSE_CNT_ACC_MAJOR = 36; + + /* [Wh] - Watt-hour; Resolution 1Wh. Range [0:1000000]. Number of watt-hours in a single period. Type: Continuous */ + MEASUREMENT_TYPE_ELEC_METER_ACC_MINOR = 37; + + /* [kWh] - Kilowatt-hour; Resolution 1kWh. Range [0:1000000]. Number of kilowatt-hours in a single period. Type: Continuous */ + MEASUREMENT_TYPE_ELEC_METER_ACC_MAJOR = 38; + + /* [NB] Number of pulses (wide range). Resolution 1 pulse. Range [0:999999]. Type: Continuous */ + MEASUREMENT_TYPE_PULSE_CNT_ACC_WIDE_MINOR = 39; + + /* [MNB] Number of megapulses (wide range). Resolution 1 megapulse. Range [0:999999]. Type: Continuous */ + MEASUREMENT_TYPE_PULSE_CNT_ACC_WIDE_MAJOR = 40; + + /* [mA] - Milliampere. Resolution 0.001mA. Range [-4 000.000:4 000.000]. Type: Continuous */ + MEASUREMENT_TYPE_CURRENT_PRECISE = 41; + + /* Sign indicates state: (+) ON, (-) OFF. Type: Binary */ + MEASUREMENT_TYPE_OUTPUT_CONTROL = 42; } diff --git a/common/transport/coap/src/main/proto/efento/proto_measurements.proto b/common/transport/coap/src/main/proto/efento/proto_measurements.proto index f8549feb54..80af07d958 100644 --- a/common/transport/coap/src/main/proto/efento/proto_measurements.proto +++ b/common/transport/coap/src/main/proto/efento/proto_measurements.proto @@ -19,103 +19,108 @@ import "efento/proto_measurement_types.proto"; option java_package = "org.thingsboard.server.gen.transport.coap"; option java_outer_classname = "MeasurementsProtos"; -message ProtoChannel{ - /* Type of channel */ +message ProtoChannel { + + /* Type of channel */ MeasurementType type = 1; - /* Timestamp of the first sample (the oldest one) in seconds since UNIX EPOCH 01-01-1970 */ + /* Timestamp of the first sample (the oldest one) in seconds since UNIX EPOCH 01-01-1970 */ int32 timestamp = 2; - /* Only used for 'Continuous' sensor types. Value used as the starting point for calculating the values of all */ - /* measurements in the package. */ - /* Format defined by 'MeasurementType' field */ + /* Only used for 'Continuous' sensor types. Value used as the starting point for calculating the values of all */ + /* measurements in the package. */ + /* Format defined by 'MeasurementType' field */ sint32 start_point = 4; - /* 'Continuous' sensor types */ - /* Value of the offset from the 'start_point' for each measurement in the package. The oldest sample first ([0]). */ - /* 'sample_offsets' format defined by 'MeasurementType' field. */ - /* Example: MeasurementType = 1 (temperature), start_point = 100, sample_offsets[0] = 15, sample_offsets[1] = 20 */ - /* 1st sample in the package temperature value = 11.5 °C, 2nd sample in the package temperature value = 12 °C */ - /* Calculating timestamps of the measurements: timestamp = 1606391700, measurement_period_base = 60, */ - /* measurement_period_factor = 1. Timestamp of the 1st sample = 1606391700, timestamp of the 2nd sample = 1606391760 */ - /* 'Binary' sensor types: */ - /* Absolute value of the 'sample_offsets' field indicates the offset in seconds from 'timestamp' field. */ - /* Sign (- or +) indicates the state of measurements depend of sensor type. */ - /* Value of this field equals to '1' or '-1' indicates the state at the 'timestamp'. Other values */ - /* indicate the state of the relay at the time (in seconds) equal to 'timestamp' + value. */ - /* Values of this field are incremented starting from 1 (1->0: state at the time */ - /* of 'timestamp', 2->1: state at the time equal to 'timestamp' + 1 s, 3->2 : */ - /* state at the time equal to 'timestamp' + 2 s, etc.). The first and the last sample define the time range of the */ - /* measurements. Only state changes in the time range are included in the 'sample_offsets' field */ - /* Examples: if 'timestamp' value is 1553518060 and 'sample_offsets' equals '1', it means that at 1553518060 the state */ - /* was high, if 'timestamp' value is 1553518060 and 'sample_offsets' equals '-9', it means at 1553518068 the state was low */ + /* 'Continuous' sensor types */ + /* Value of the offset from the 'start_point' for each measurement in the package. The oldest sample first ([0]). */ + /* 'sample_offsets' format defined by 'MeasurementType' field. */ + /* If the 'sample_offset' has a value from the range [8355840: 8388607], it should be interpreted as a sensor error code. */ + /* In that case value of the 'start_point' field should not be added to this 'sample_offset'. See ES6-264 for error codes. */ + /* Example: MeasurementType = 1 (temperature), start_point = 100, sample_offsets[0] = 15, sample_offsets[1] = 20, */ + /* sample_offset[2] = 8388605 */ + /* 1st sample in the package temperature value = 11.5 °C, 2nd sample in the package temperature value = 12 °C */ + /* 3rd sample in the package has no temperature value. It has information about failure of MCP9808 (temperature) sensor. */ + /* Calculating timestamps of the measurements: timestamp = 1606391700, measurement_period_base = 60, */ + /* measurement_period_factor = 1. Timestamp of the 1st sample = 1606391700, timestamp of the 2nd sample = 1606391760, */ + /* timestamp of the 3rd sample 1606391820 */ + + /* 'Binary' sensor types: */ + /* Absolute value of the 'sample_offsets' field indicates the offset in seconds from 'timestamp' field. */ + /* Sign (- or +) indicates the state of measurements depending on the sensor type. */ + /* Value of this field equals to '1' or '-1' indicates the state at the 'timestamp'. Other values */ + /* indicate the state of the relay at the time (in seconds) equal to 'timestamp' + absolute value -1. */ + /* Values of this field are incremented starting from 1 (1->0: state at the time */ + /* of 'timestamp', 2->1: state at the time equal to 'timestamp' + 1 s, 3->2 : */ + /* state at the time equal to 'timestamp' + 2 s, etc.). The first and the last sample define the time range of the */ + /* measurements. Only state changes in the time range are included in the 'sample_offsets' field */ + /* Examples: if 'timestamp' value is 1553518060 and 'sample_offsets' equals '1', it means that at 1553518060 the state */ + /* was high, if 'timestamp' value is 1553518060 and 'sample_offsets' equals '-9', it means at 1553518068 the state was low */ repeated sint32 sample_offsets = 5 [packed=true]; - /* Deprecated - configuration is sent to endpoint 'c' */ - //int32 lo_threshold = 6; + /* Deprecated - configuration is sent to endpoint 'c' */ + /* int32 lo_threshold = 6; */ + reserved 6; - /* Deprecated - configuration is sent to endpoint 'c' */ - //int32 hi_threshold = 7; + /* Deprecated - configuration is sent to endpoint 'c' */ + /* int32 hi_threshold = 7; */ + reserved 7; - /* Deprecated - configurationis sent to endpoint 'c' */ - //int32 diff_threshold = 8; - } + /* Deprecated - configurations sent to endpoint 'c' */ + /* int32 diff_threshold = 8; */ + reserved 8; +} message ProtoMeasurements { - /* serial number of the device */ + /* Serial number of the device */ bytes serial_num = 1; - /* true - battery ok, false - battery low */ + /* Battery status: true - battery ok, false - battery low */ bool battery_status = 2; - /* 'Measurement_period_base' and 'measurement_period_factor' define how often the measurements are taken. */ - /* Sensors of 'Continuous' type take measurement each Measurement_period_base * measurement_period_factor. */ - /* Sensors of 'Binary' type take measurement each Measurement_period_base. */ - /* For backward compatibility with versions 5.x in case of binary/mixed sensors, if the 'measurement_period_factor' is */ - /* not sent (equal to 0), then the default value '14' shall be used for period calculation. */ - /* For backward compatibility with versions 5.x in case of continues sensors, if the measurement_period_factor is */ - /* not sent (equal to 0), then the default value '1' shall be used for period calculation. */ - /* measurement period base in seconds */ + /* 'Measurement_period_base' and 'measurement_period_factor' define how often the measurements are taken. */ + /* Sensors of 'Continuous' type take measurement each Measurement_period_base * measurement_period_factor. */ + /* Sensors of 'Binary' type take measurement each Measurement_period_base. */ + /* For backward compatibility with versions 5.x in case of binary/mixed sensors, if the 'measurement_period_factor' is */ + /* not sent (equal to 0), then the default value '14' shall be used for period calculation. */ + /* For backward compatibility with versions 5.x in case of continues sensors, if the measurement_period_factor is */ + /* not sent (equal to 0), then the default value '1' shall be used for period calculation. */ + /* measurement period base in seconds */ uint32 measurement_period_base = 3; - /* Measurement period factor */ + /* Measurement period factor */ uint32 measurement_period_factor = 8; repeated ProtoChannel channels = 4; - /* Timestamp of the next scheduled transmission. If the device will not send data until this time, */ - /* it should be considered as 'lost' */ + /* Timestamp of the next scheduled transmission. If the device will not send data until this time, */ + /* it should be considered as 'lost' */ uint32 next_transmission_at = 5; - /* reason of transmission - unsigned integer where each bit indicates different */ - /* possible communication reason. Can be more than one */ - /* - bit 0: first message after sensor reset */ - /* - bit 1: user button triggered */ - /* - bit 2: user BLE triggered */ - /* - bit 3-7: number of retries -> incremented after each unsuccessful transmission. Max value 4. */ - /* Set to 0 after a successful transmission. */ - /* - bit 8: channel 1 lower threshold exceeded */ - /* - bit 9: channel 1 lower threshold returned */ - /* - bit 10: channel 1 higher threshold exceeded */ - /* - bit 11: channel 1 higher threshold returned */ - /* - bit 12: channel 1 differential threshold crossed */ - /* - bits 13-17: channel 2 thresholds (same as for channel 1) */ - /* - bits 18-22: channel 3 thresholds (same as for channel 1) */ - /* - bits 23-27: channel 4 or 5 or 6 thresholds (same as for channel 1) */ + /* Reason of transmission - unsigned integer where each bit indicates different possible communication reason. */ + /* Can be more than one: */ + /* - bit 0: first message after sensor reset */ + /* - bit 1: user button triggered */ + /* - bit 2: user BLE triggered */ + /* - bit 3-7: number of retries -> incremented after each unsuccessful transmission. Max value 31. */ + /* Set to 0 after a successful transmission. */ + /* - bit 8...19: rule 1...12 was met */ + /* - bit 20: triggered after the end of the limit */ uint32 transfer_reason = 6; - /* Signal strength level mapped from RSSI */ - /* - 0 : 113 dBm or less */ - /* - 1 : 111 dBm */ - /* - 2...30 : 109...-53 dBm */ - /* - 31 : -51 dBm or greater */ - /* - 99 : Not known or not detectable */ + /* Signal strength level mapped from RSSI: */ + /* - 0: RSSI < -110 dBm */ + /* - 1: -110 dBm <= RSSI < -109 dBm */ + /* - 2...61: -109 <= RSSI < -108 dBm ... -50 dBm <= RSSI < -49 dBm */ + /* - 62: -49 dBm <= RSSI < -48 dBm */ + /* - 63: RSSI >= -48 dBm */ + /* - 99: Not known or not detectable */ uint32 signal = 7; - /* Hash of the current configuration. Hash value changes each time a device receives a new configuration */ + /* Hash of the current configuration. Hash value changes each time a device receives a new configuration */ uint32 hash = 9; - /* Optional string up to 36 bytes long. Can be set to any user define value or hold device's IMEI */ + /* Optional string up to 36 bytes long. Can be set to any user define value or hold device's IMEI */ string cloud_token = 16; } \ No newline at end of file diff --git a/common/transport/coap/src/main/proto/efento/proto_rule.proto b/common/transport/coap/src/main/proto/efento/proto_rule.proto index 4079683a3f..12550426b1 100644 --- a/common/transport/coap/src/main/proto/efento/proto_rule.proto +++ b/common/transport/coap/src/main/proto/efento/proto_rule.proto @@ -50,11 +50,19 @@ option java_outer_classname = "ProtoRuleProtos"; /* - CO2_EQUIVALENT - [ppm] - Parts per million. Resolution 1ppm. Range [0:1000000]. Carbon dioxide equivalent. */ /* - BREATH_VOC - [ppm] - Parts per million. Resolution 1ppm. Range [0:100000]. Breath VOC estimate. */ /* - PERCENTAGE - [%] - Percentage. Resolution 0.01%. Range [0.00:100.00]. */ +/* - VOLTAGE - [mV] - Milivolt. Resolution 0.1mV. Range [0.0:100000.0]. */ +/* - CURRENT - [mA] - Miliampere. Resolution 0.01mA. Range [0.00:10000.00]. */ /* - PULSE_CNT_ACC_MINOR - [NB] - Number of pulses. Resolution 1 pulse. Range [0:1000000]. Accumulative pulse counter (minor). */ /* - PULSE_CNT_ACC_MAJOR - [kNB] - Number of kilopulses. Resolution 1 kilopulse. Range [0:1000000]. */ /* Accumulative pulse counter (major). */ /* - ELEC_METER_ACC_MINOR - [Wh] - Watt-hour. Resolution 1Wh. Range [0:1000000]. Accumulative electricity meter (minor). */ /* - ELEC_METER_ACC_MAJOR - [kWh] - Kilowatt-hour. Resolution 1kWh. Range [0:1000000]. Accumulative electricity meter (major). */ +/* - PULSE_CNT_ACC_WIDE_MINOR - [NB] - Number of pulses. Resolution 1 pulse. Range [0:999999]. */ +/* Accumulative pulse counter wide range (minor). */ +/* - PULSE_CNT_ACC_WIDE_MAJOR - [MNB] - Number of megapulses. Resolution 1 megapulse. Range [0:999999]. */ +/* Accumulative pulse counter wide range (major). */ +/* - CURRENT_PRECISE - [mA] - Miliampere. Resolution 0.001mA. Range [-4 000.000:4 000.000]. */ +/* - OUTPUT_CONTROL - Not applicable */ /* Encoding R: used to set relative values in the Rules (e.g. differential threshold and hysteresis) */ /* - TEMPERATURE - [°C] - Celsius degree. Resolution 0.1°C. Range [0.1:4273.2]. */ @@ -88,11 +96,19 @@ option java_outer_classname = "ProtoRuleProtos"; /* - CO2_EQUIVALENT - [ppm] - Parts per million. Resolution 1ppm. Range [1:1000000]. Carbon dioxide equivalent. */ /* - BREATH_VOC - [ppm] - Parts per million. Resolution 1ppm. Range [1:100000]. Breath VOC estimate. */ /* - PERCENTAGE - [%] - Percentage. Resolution 0.01%. Range [0.01:100.00]. */ +/* - VOLTAGE - [mV] - Milivolt. Resolution 0.1mV. Range [0.1:100000.0]. */ +/* - CURRENT - [mA] - Miliampere. Resolution 0.01mA. Range [0.01:10000.00]. */ /* - PULSE_CNT_ACC_MINOR - [NB] - Number of pulses. Resolution 1 pulse. Range [1:1000000]. Accumulative pulse counter (minor). */ /* - PULSE_CNT_ACC_MAJOR - [kNB] - Number of kilopulses. Resolution 1 kilopulse. Range [1:1000000]. */ /* Accumulative pulse counter (major). */ /* - ELEC_METER_ACC_MINOR - [Wh] - Watt-hour. Resolution 1Wh. Range [1:1000000]. Accumulative electricity meter (minor). */ /* - ELEC_METER_ACC_MAJOR - [kWh] - Kilowatt-hour. Resolution 1kWh. Range [1:1000000]. Accumulative electricity meter (major). */ +/* - PULSE_CNT_ACC_WIDE_MINOR - [NB] - Number of pulses. Resolution 1 pulse. Range [1:999999]. */ +/* Accumulative pulse counter wide range (minor). */ +/* - PULSE_CNT_ACC_WIDE_MAJOR - [MNB] - Number of megapulses. Resolution 1 megapulse. Range [1:999999]. */ +/* Accumulative pulse counter wide range (major). */ +/* - CURRENT_PRECISE - [mA] - Miliampere. Resolution 0.001mA. Range [0.001:8 000.000]. */ +/* - OUTPUT_CONTROL - Not applicable */ /* Condition to be checked by the device. If the condition is true, an action is triggered */ enum Condition { @@ -202,6 +218,10 @@ enum Action { /* To trigger the transmission with ACK */ ACTION_TRIGGER_TRANSMISSION_WITH_ACK = 3; + + /* To change BLE advertising period mode to fast (with lower user-configured advertising interval). */ + /* Once the rule is deactived avertising period mode returns to previously configured value. */ + ACTION_FAST_ADVERTISING_MODE = 4; } /* Type of a rule calendars. */ From 560708328ee0376d43865b2a3121def076981a63 Mon Sep 17 00:00:00 2001 From: dashevchenko Date: Fri, 2 Aug 2024 17:28:26 +0300 Subject: [PATCH 4/5] extracted some methods to CoapEfentoUtils, added tests --- .../efento/CoapEfentoTransportResource.java | 64 ++++---- .../coap/efento/utils/CoapEfentoUtils.java | 13 ++ .../CoapEfentTransportResourceTest.java | 144 ++++++++++++++++++ 3 files changed, 183 insertions(+), 38 deletions(-) create mode 100644 common/transport/coap/src/test/java/org/thingsboard/server/transport/coap/efento/CoapEfentTransportResourceTest.java diff --git a/common/transport/coap/src/main/java/org/thingsboard/server/transport/coap/efento/CoapEfentoTransportResource.java b/common/transport/coap/src/main/java/org/thingsboard/server/transport/coap/efento/CoapEfentoTransportResource.java index 6e481cc21d..562a633920 100644 --- a/common/transport/coap/src/main/java/org/thingsboard/server/transport/coap/efento/CoapEfentoTransportResource.java +++ b/common/transport/coap/src/main/java/org/thingsboard/server/transport/coap/efento/CoapEfentoTransportResource.java @@ -28,18 +28,17 @@ import org.eclipse.californium.core.network.Exchange; import org.eclipse.californium.core.server.resources.CoapExchange; import org.eclipse.californium.core.server.resources.Resource; import org.springframework.util.CollectionUtils; +import org.thingsboard.server.common.adaptor.AdaptorException; import org.thingsboard.server.common.adaptor.ProtoConverter; import org.thingsboard.server.common.data.DeviceProfile; import org.thingsboard.server.common.data.DeviceTransportType; import org.thingsboard.server.common.data.device.profile.CoapDeviceProfileTransportConfiguration; import org.thingsboard.server.common.data.device.profile.DeviceProfileTransportConfiguration; import org.thingsboard.server.common.data.device.profile.EfentoCoapDeviceTypeConfiguration; -import org.thingsboard.server.common.adaptor.AdaptorException; import org.thingsboard.server.common.transport.auth.SessionInfoCreator; import org.thingsboard.server.gen.transport.TransportProtos; import org.thingsboard.server.gen.transport.coap.ConfigProtos; import org.thingsboard.server.gen.transport.coap.DeviceInfoProtos; -import org.thingsboard.server.gen.transport.coap.MeasurementTypeProtos; import org.thingsboard.server.gen.transport.coap.MeasurementsProtos; import org.thingsboard.server.gen.transport.coap.MeasurementsProtos.ProtoChannel; import org.thingsboard.server.transport.coap.AbstractCoapTransportResource; @@ -59,13 +58,12 @@ import java.util.concurrent.TimeUnit; import java.util.stream.Collectors; import static com.google.gson.JsonParser.parseString; -import static org.thingsboard.server.gen.transport.coap.MeasurementTypeProtos.MeasurementType.MEASUREMENT_TYPE_FLOODING; -import static org.thingsboard.server.gen.transport.coap.MeasurementTypeProtos.MeasurementType.MEASUREMENT_TYPE_OK_ALARM; -import static org.thingsboard.server.gen.transport.coap.MeasurementTypeProtos.MeasurementType.MEASUREMENT_TYPE_OUTPUT_CONTROL; import static org.thingsboard.server.transport.coap.CoapTransportService.CONFIGURATION; import static org.thingsboard.server.transport.coap.CoapTransportService.CURRENT_TIMESTAMP; import static org.thingsboard.server.transport.coap.CoapTransportService.DEVICE_INFO; import static org.thingsboard.server.transport.coap.CoapTransportService.MEASUREMENTS; +import static org.thingsboard.server.transport.coap.efento.utils.CoapEfentoUtils.isBinarySensor; +import static org.thingsboard.server.transport.coap.efento.utils.CoapEfentoUtils.isSensorError; @Slf4j public class CoapEfentoTransportResource extends AbstractCoapTransportResource { @@ -224,7 +222,7 @@ public class CoapEfentoTransportResource extends AbstractCoapTransportResource { } } - private List getEfentoMeasurements(MeasurementsProtos.ProtoMeasurements protoMeasurements, UUID sessionId) { + public List getEfentoMeasurements(MeasurementsProtos.ProtoMeasurements protoMeasurements, UUID sessionId) { String serialNumber = CoapEfentoUtils.convertByteArrayToString(protoMeasurements.getSerialNum().toByteArray()); boolean batteryStatus = protoMeasurements.getBatteryStatus(); int measurementPeriodBase = protoMeasurements.getMeasurementPeriodBase(); @@ -240,17 +238,16 @@ public class CoapEfentoTransportResource extends AbstractCoapTransportResource { Map valuesMap = new TreeMap<>(); for (int channel = 0; channel < channelsList.size(); channel++) { ProtoChannel protoChannel = channelsList.get(channel); - boolean isBinarySensor = isBinarySensor(protoChannel.getType()); - int channelPeriodFactor = (measurementPeriodFactor == 0 ? (isBinarySensor ? 14 : 1) : measurementPeriodFactor); - int measurementPeriod = measurementPeriodBase * channelPeriodFactor; - long measurementPeriodMillis = TimeUnit.SECONDS.toMillis(measurementPeriod); - long startTimestampMillis = TimeUnit.SECONDS.toMillis(protoChannel.getTimestamp()); List sampleOffsetsList = protoChannel.getSampleOffsetsList(); - if (CollectionUtils.isEmpty(sampleOffsetsList)) { log.trace("[{}][{}] sampleOffsetsList list is empty!", sessionId, protoChannel.getType().name()); continue; } + boolean isBinarySensor = isBinarySensor(protoChannel.getType()); + int channelPeriodFactor = (measurementPeriodFactor == 0 ? (isBinarySensor ? 14 : 1) : measurementPeriodFactor); + int measurementPeriod = measurementPeriodBase * channelPeriodFactor; + long measurementPeriodMillis = TimeUnit.SECONDS.toMillis(measurementPeriod); + long startTimestampMillis = TimeUnit.SECONDS.toMillis(protoChannel.getTimestamp()); for (int i = 0; i < sampleOffsetsList.size(); i++) { int sampleOffset = sampleOffsetsList.get(i); @@ -261,17 +258,19 @@ public class CoapEfentoTransportResource extends AbstractCoapTransportResource { JsonObject values; if (isBinarySensor) { + boolean currentIsOk = sampleOffset < 0; + Integer previousSampleOffset = i > 0 ? sampleOffsetsList.get(i - 1) : null; + if (previousSampleOffset != null) { //compare with previous value + boolean previousIsOk = previousSampleOffset < 0; + if (currentIsOk == previousIsOk) { + break; + } + } long sampleOffsetMillis = TimeUnit.SECONDS.toMillis(sampleOffset); - long measurementTimestamp = startTimestampMillis + Math.abs(sampleOffsetMillis) - 1; + long measurementTimestamp = startTimestampMillis + Math.abs(sampleOffsetMillis); values = valuesMap.computeIfAbsent(measurementTimestamp - 1000, k -> CoapEfentoUtils.setDefaultMeasurements(serialNumber, batteryStatus, measurementPeriod, nextTransmissionAtMillis, signal, k)); - Integer previousSampleOffset = i > 0 ? sampleOffsetsList.get(i - 1) : null; - boolean previousValueIsOk = previousSampleOffset != null && previousSampleOffset < 0; - boolean valueIsOk = sampleOffset < 0; - if (binaryValueNotChanged(valueIsOk, previousValueIsOk)) { - break; - } - addBinarySample(protoChannel, valueIsOk, values, channel + 1); + addBinarySample(protoChannel, currentIsOk, values, channel + 1, sessionId); } else { long timestampMillis = startTimestampMillis + i * measurementPeriodMillis; values = valuesMap.computeIfAbsent(timestampMillis, k -> CoapEfentoUtils.setDefaultMeasurements( @@ -290,14 +289,6 @@ public class CoapEfentoTransportResource extends AbstractCoapTransportResource { .collect(Collectors.toList()); } - private boolean isBinarySensor(MeasurementTypeProtos.MeasurementType type) { - return type == MEASUREMENT_TYPE_OK_ALARM || type == MEASUREMENT_TYPE_FLOODING || type == MEASUREMENT_TYPE_OUTPUT_CONTROL; - } - - private boolean isSensorError(int sampleOffset) { - return sampleOffset >= 8355840 && sampleOffset <= 8388607; - } - private void addContinuesSample(ProtoChannel protoChannel, int sampleOffset, JsonObject values, int channelNumber, UUID sessionId) { int startPoint = protoChannel.getStartPoint(); @@ -396,26 +387,23 @@ public class CoapEfentoTransportResource extends AbstractCoapTransportResource { } } - private void addBinarySample(ProtoChannel protoChannel, boolean valueIsOk, JsonObject values, int channel) { + private void addBinarySample(ProtoChannel protoChannel, boolean valueIsOk, JsonObject values, int channel, UUID sessionId) { switch (protoChannel.getType()) { case MEASUREMENT_TYPE_OK_ALARM: values.addProperty("ok_alarm_" + channel, valueIsOk ? "OK" : "ALARM"); + break; case MEASUREMENT_TYPE_FLOODING: values.addProperty("flooding_" + channel, valueIsOk ? "OK" : "WATER_DETECTED"); + break; case MEASUREMENT_TYPE_OUTPUT_CONTROL: values.addProperty("output_control_" + channel, valueIsOk ? "OFF" : "ON"); + break; + default: + log.trace("[{}],[{}] Unsupported binary measurementType! Ignoring.", sessionId, protoChannel.getType().name()); + break; } } - private static boolean binaryValueNotChanged(boolean currentIsOk, boolean previousIsOk) { - boolean isOk = previousIsOk && currentIsOk; - boolean isAlarm = !previousIsOk && !currentIsOk; - if (isOk || isAlarm) { - return true; - } - return false; - } - private EfentoTelemetry getEfentoDeviceInfo(DeviceInfoProtos.ProtoDeviceInfo protoDeviceInfo) { JsonObject values = new JsonObject(); values.addProperty("sw_version", protoDeviceInfo.getSwVersion()); diff --git a/common/transport/coap/src/main/java/org/thingsboard/server/transport/coap/efento/utils/CoapEfentoUtils.java b/common/transport/coap/src/main/java/org/thingsboard/server/transport/coap/efento/utils/CoapEfentoUtils.java index d87c97543e..96c1805be3 100644 --- a/common/transport/coap/src/main/java/org/thingsboard/server/transport/coap/efento/utils/CoapEfentoUtils.java +++ b/common/transport/coap/src/main/java/org/thingsboard/server/transport/coap/efento/utils/CoapEfentoUtils.java @@ -16,11 +16,16 @@ package org.thingsboard.server.transport.coap.efento.utils; import com.google.gson.JsonObject; +import org.thingsboard.server.gen.transport.coap.MeasurementTypeProtos.MeasurementType; import java.text.SimpleDateFormat; import java.util.Date; import java.util.TimeZone; +import static org.thingsboard.server.gen.transport.coap.MeasurementTypeProtos.MeasurementType.MEASUREMENT_TYPE_FLOODING; +import static org.thingsboard.server.gen.transport.coap.MeasurementTypeProtos.MeasurementType.MEASUREMENT_TYPE_OK_ALARM; +import static org.thingsboard.server.gen.transport.coap.MeasurementTypeProtos.MeasurementType.MEASUREMENT_TYPE_OUTPUT_CONTROL; + public class CoapEfentoUtils { public static String convertByteArrayToString(byte[] a) { @@ -50,4 +55,12 @@ public class CoapEfentoUtils { return values; } + public static boolean isBinarySensor(MeasurementType type) { + return type == MEASUREMENT_TYPE_OK_ALARM || type == MEASUREMENT_TYPE_FLOODING || type == MEASUREMENT_TYPE_OUTPUT_CONTROL; + } + + public static boolean isSensorError(int sampleOffset) { + return sampleOffset >= 8355840 && sampleOffset <= 8388607; + } + } diff --git a/common/transport/coap/src/test/java/org/thingsboard/server/transport/coap/efento/CoapEfentTransportResourceTest.java b/common/transport/coap/src/test/java/org/thingsboard/server/transport/coap/efento/CoapEfentTransportResourceTest.java new file mode 100644 index 0000000000..5faaaee1d4 --- /dev/null +++ b/common/transport/coap/src/test/java/org/thingsboard/server/transport/coap/efento/CoapEfentTransportResourceTest.java @@ -0,0 +1,144 @@ +/** + * Copyright © 2016-2024 The Thingsboard Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.thingsboard.server.transport.coap.efento; + +import com.google.protobuf.ByteString; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; +import org.thingsboard.server.gen.transport.coap.MeasurementTypeProtos; +import org.thingsboard.server.gen.transport.coap.MeasurementsProtos; +import org.thingsboard.server.transport.coap.CoapTransportContext; + +import java.nio.ByteBuffer; +import java.time.Instant; +import java.util.List; +import java.util.UUID; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.Mockito.mock; + +class CoapEfentTransportResourceTest { + + private static CoapEfentoTransportResource coapEfentoTransportResource; + + @BeforeAll + static void setUp() { + var ctxMock = mock(CoapTransportContext.class); + coapEfentoTransportResource = new CoapEfentoTransportResource(ctxMock, "testName"); + } + + @Test + void checkContinuousSensor() { + long tsInSec = Instant.now().getEpochSecond(); + MeasurementsProtos.ProtoMeasurements measurements = MeasurementsProtos.ProtoMeasurements.newBuilder() + .setSerialNum(integerToByteString(1234)) + .setCloudToken("test_token") + .setMeasurementPeriodBase(180) + .setMeasurementPeriodFactor(1) + .setBatteryStatus(true) + .setSignal(0) + .setNextTransmissionAt(1000) + .setTransferReason(0) + .setHash(0) + .addAllChannels(List.of(MeasurementsProtos.ProtoChannel.newBuilder() + .setType(MeasurementTypeProtos.MeasurementType.MEASUREMENT_TYPE_TEMPERATURE) + .setTimestamp(Math.toIntExact(tsInSec)) + .addAllSampleOffsets(List.of(223, 224)) + .build(), + MeasurementsProtos.ProtoChannel.newBuilder() + .setType(MeasurementTypeProtos.MeasurementType.MEASUREMENT_TYPE_HUMIDITY) + .setTimestamp(Math.toIntExact(tsInSec)) + .addAllSampleOffsets(List.of(20, 30)) + .build() + )) + .build(); + List efentoMeasurements = coapEfentoTransportResource.getEfentoMeasurements(measurements, UUID.randomUUID()); + assertThat(efentoMeasurements).hasSize(2); + assertThat(efentoMeasurements.get(0).getTs()).isEqualTo(tsInSec * 1000); + assertThat(efentoMeasurements.get(0).getValues().getAsJsonObject().get("temperature_1").getAsDouble()).isEqualTo(22.3); + assertThat(efentoMeasurements.get(0).getValues().getAsJsonObject().get("humidity_2").getAsDouble()).isEqualTo(20); + assertThat(efentoMeasurements.get(1).getTs()).isEqualTo((tsInSec + 180) * 1000); + assertThat(efentoMeasurements.get(1).getValues().getAsJsonObject().get("temperature_1").getAsDouble()).isEqualTo(22.4); + assertThat(efentoMeasurements.get(1).getValues().getAsJsonObject().get("humidity_2").getAsDouble()).isEqualTo(30); + } + + @Test + void checkBinarySensor() { + long tsInSec = Instant.now().getEpochSecond(); + MeasurementsProtos.ProtoMeasurements measurements = MeasurementsProtos.ProtoMeasurements.newBuilder() + .setSerialNum(integerToByteString(1234)) + .setCloudToken("test_token") + .setMeasurementPeriodBase(180) + .setMeasurementPeriodFactor(1) + .setBatteryStatus(true) + .setSignal(0) + .setNextTransmissionAt(1000) + .setTransferReason(0) + .setHash(0) + .addChannels(MeasurementsProtos.ProtoChannel.newBuilder() + .setType(MeasurementTypeProtos.MeasurementType.MEASUREMENT_TYPE_OK_ALARM) + .setTimestamp(Math.toIntExact(tsInSec)) + .addAllSampleOffsets(List.of(1, 1)) + .build()) + .build(); + List efentoMeasurements = coapEfentoTransportResource.getEfentoMeasurements(measurements, UUID.randomUUID()); + assertThat(efentoMeasurements).hasSize(1); + assertThat(efentoMeasurements.get(0).getTs()).isEqualTo(tsInSec * 1000); + assertThat(efentoMeasurements.get(0).getValues().getAsJsonObject().get("ok_alarm_1").getAsString()).isEqualTo("ALARM"); + } + + @Test + void checkBinarySensorWhenValueIsVarying() { + long tsInSec = Instant.now().getEpochSecond(); + MeasurementsProtos.ProtoMeasurements measurements = MeasurementsProtos.ProtoMeasurements.newBuilder() + .setSerialNum(integerToByteString(1234)) + .setCloudToken("test_token") + .setMeasurementPeriodBase(180) + .setMeasurementPeriodFactor(1) + .setBatteryStatus(true) + .setSignal(0) + .setNextTransmissionAt(1000) + .setTransferReason(0) + .setHash(0) + .addChannels(MeasurementsProtos.ProtoChannel.newBuilder() + .setType(MeasurementTypeProtos.MeasurementType.MEASUREMENT_TYPE_OK_ALARM) + .setTimestamp(Math.toIntExact(tsInSec)) + .addAllSampleOffsets(List.of(1, -10)) + .build()) + .build(); + List efentoMeasurements = coapEfentoTransportResource.getEfentoMeasurements(measurements, UUID.randomUUID()); + assertThat(efentoMeasurements).hasSize(2); + assertThat(efentoMeasurements.get(0).getTs()).isEqualTo(tsInSec * 1000); + assertThat(efentoMeasurements.get(0).getValues().getAsJsonObject().get("ok_alarm_1").getAsString()).isEqualTo("ALARM"); + assertThat(efentoMeasurements.get(1).getTs()).isEqualTo((tsInSec + 9) * 1000); + assertThat(efentoMeasurements.get(1).getValues().getAsJsonObject().get("ok_alarm_1").getAsString()).isEqualTo("OK"); + } + + public static ByteString integerToByteString(Integer intValue) { + // Allocate a ByteBuffer with the size of an integer (4 bytes) + ByteBuffer buffer = ByteBuffer.allocate(Integer.BYTES); + + // Put the integer value into the ByteBuffer + buffer.putInt(intValue); + + // Convert the ByteBuffer to a byte array + byte[] byteArray = buffer.array(); + + // Create a ByteString from the byte array + return ByteString.copyFrom(byteArray); + } + +} From 4391d0b474af635c3282f9c4465878eefb52b96d Mon Sep 17 00:00:00 2001 From: dashevchenko Date: Mon, 5 Aug 2024 13:03:37 +0300 Subject: [PATCH 5/5] changed access modifier from public to package-private --- .../transport/coap/efento/CoapEfentoTransportResource.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/common/transport/coap/src/main/java/org/thingsboard/server/transport/coap/efento/CoapEfentoTransportResource.java b/common/transport/coap/src/main/java/org/thingsboard/server/transport/coap/efento/CoapEfentoTransportResource.java index 562a633920..1ba7c9aff1 100644 --- a/common/transport/coap/src/main/java/org/thingsboard/server/transport/coap/efento/CoapEfentoTransportResource.java +++ b/common/transport/coap/src/main/java/org/thingsboard/server/transport/coap/efento/CoapEfentoTransportResource.java @@ -222,7 +222,7 @@ public class CoapEfentoTransportResource extends AbstractCoapTransportResource { } } - public List getEfentoMeasurements(MeasurementsProtos.ProtoMeasurements protoMeasurements, UUID sessionId) { + List getEfentoMeasurements(MeasurementsProtos.ProtoMeasurements protoMeasurements, UUID sessionId) { String serialNumber = CoapEfentoUtils.convertByteArrayToString(protoMeasurements.getSerialNum().toByteArray()); boolean batteryStatus = protoMeasurements.getBatteryStatus(); int measurementPeriodBase = protoMeasurements.getMeasurementPeriodBase();