From 7e0a9bbebfd48dfb3cfb88a4687bd284638f6f2b Mon Sep 17 00:00:00 2001 From: nick Date: Mon, 22 Jul 2024 17:39:46 +0300 Subject: [PATCH 01/20] tbel: added some float methods --- .../thingsboard/script/api/tbel/TbUtils.java | 163 ++++++++++++------ .../script/api/tbel/TbUtilsTest.java | 75 +++++++- 2 files changed, 181 insertions(+), 57 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 852f7a054f..034d6b4686 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 @@ -61,6 +61,7 @@ public class TbUtils { private static final int HEX_LEN_MIN = -1; private static final int HEX_LEN_INT_MAX = 8; private static final int HEX_LEN_LONG_MAX = 16; + private static final int BYTES_LEN_INT_MAX = 4; private static final int BYTES_LEN_LONG_MAX = 8; private static final LinkedHashMap mdnEncodingReplacements = new LinkedHashMap<>(); @@ -117,6 +118,8 @@ public class TbUtils { String.class))); parserConfig.addImport("parseFloat", new MethodStub(TbUtils.class.getMethod("parseFloat", String.class, int.class))); + parserConfig.addImport("parseHexIntLongToFloat", new MethodStub(TbUtils.class.getMethod("parseHexIntLongToFloat", + String.class, boolean.class))); parserConfig.addImport("parseDouble", new MethodStub(TbUtils.class.getMethod("parseDouble", String.class))); parserConfig.addImport("parseLittleEndianHexToInt", new MethodStub(TbUtils.class.getMethod("parseLittleEndianHexToInt", @@ -159,14 +162,18 @@ public class TbUtils { String.class))); parserConfig.addImport("parseHexToFloat", new MethodStub(TbUtils.class.getMethod("parseHexToFloat", String.class, boolean.class))); - parserConfig.addImport("parseBytesToFloat", new MethodStub(TbUtils.class.getMethod("parseBytesToFloat", - byte[].class, int.class, boolean.class))); parserConfig.addImport("parseBytesToFloat", new MethodStub(TbUtils.class.getMethod("parseBytesToFloat", byte[].class, int.class))); + parserConfig.addImport("parseBytesToFloat", new MethodStub(TbUtils.class.getMethod("parseBytesToFloat", + List.class, int.class))); + parserConfig.addImport("parseBytesToFloat", new MethodStub(TbUtils.class.getMethod("parseBytesToFloat", + byte[].class, int.class, boolean.class))); parserConfig.addImport("parseBytesToFloat", new MethodStub(TbUtils.class.getMethod("parseBytesToFloat", List.class, int.class, boolean.class))); + parserConfig.addImport("parseBytesToFloat", new MethodStub(TbUtils.class.getMethod("parseBytesToFloat", + byte[].class, int.class, int.class, boolean.class))); parserConfig.addImport("parseBytesToFloat", new MethodStub(TbUtils.class.getMethod("parseBytesToFloat", - List.class, int.class))); + List.class, int.class, int.class, boolean.class))); parserConfig.addImport("parseLittleEndianHexToDouble", new MethodStub(TbUtils.class.getMethod("parseLittleEndianHexToDouble", String.class))); parserConfig.addImport("parseBigEndianHexToDouble", new MethodStub(TbUtils.class.getMethod("parseBigEndianHexToDouble", @@ -201,17 +208,17 @@ public class TbUtils { Long.class))); parserConfig.addImport("longToHex", new MethodStub(TbUtils.class.getMethod("longToHex", Long.class, boolean.class))); - parserConfig.addImport("longToHex", new MethodStub(TbUtils.class.getMethod("longToHex", + parserConfig.addImport("longToHex", new MethodStub(TbUtils.class.getMethod("longToHex", Long.class, boolean.class, boolean.class))); - parserConfig.addImport("longToHex", new MethodStub(TbUtils.class.getMethod("longToHex", + 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", + parserConfig.addImport("intLongToString", new MethodStub(TbUtils.class.getMethod("intLongToString", Long.class))); - parserConfig.addImport("intLongToString", new MethodStub(TbUtils.class.getMethod("intLongToString", + parserConfig.addImport("intLongToString", new MethodStub(TbUtils.class.getMethod("intLongToString", Long.class, int.class))); - parserConfig.addImport("intLongToString", new MethodStub(TbUtils.class.getMethod("intLongToString", + parserConfig.addImport("intLongToString", new MethodStub(TbUtils.class.getMethod("intLongToString", Long.class, int.class, boolean.class))); - parserConfig.addImport("intLongToString", new MethodStub(TbUtils.class.getMethod("intLongToString", + 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))); @@ -334,10 +341,14 @@ public class TbUtils { } public static Integer parseInt(String value, int radix) { - if (StringUtils.isNotBlank(value)) { - String valueP = prepareNumberString(value); + return parseInt(value, radix, true); + } + + private static Integer parseInt(String value, int radix, boolean bigEndian) { + String valueP = prepareNumberString(value, bigEndian); + if (valueP != null) { int radixValue = isValidStringAndRadix(valueP, radix, value); - if (radixValue >= 25 && radixValue <= MAX_RADIX) { + if (radixValue >= 25 && radixValue <= MAX_RADIX) { return (Integer) compareIntLongValueMinMax(valueP, radixValue, Integer.MAX_VALUE, Integer.MIN_VALUE); } return switch (radixValue) { @@ -354,10 +365,14 @@ public class TbUtils { } public static Long parseLong(String value, int radix) { - if (StringUtils.isNotBlank(value)) { - String valueP = prepareNumberString(value); + return parseLong(value, radix, true); + } + + private static Long parseLong(String value, int radix, boolean bigEndian) { + String valueP = prepareNumberString(value, bigEndian); + if (valueP != null) { int radixValue = isValidStringAndRadix(valueP, radix, value); - if (radixValue >= 25 && radixValue <= MAX_RADIX) { + if (radixValue >= 25 && radixValue <= MAX_RADIX) { return (Long) compareIntLongValueMinMax(valueP, radixValue, Long.MAX_VALUE, Long.MIN_VALUE); } return switch (radixValue) { @@ -385,6 +400,7 @@ public class TbUtils { return Integer.parseInt(binaryString, MIN_RADIX); } } + private static long parseBinaryStringAsSignedLong(String binaryString) { if (binaryString.length() != 64) { // Pad the binary string to 64 bits if it is not already @@ -420,34 +436,58 @@ public class TbUtils { } public static Float parseFloat(String value) { - return parseFloat(value, 0); + return parseFloat(value, ZERO_RADIX); } public static Float parseFloat(String value, int radix) { - if (StringUtils.isNotBlank(value)) { - String valueP = prepareNumberString(value); - int radixValue = isValidStringAndRadix(valueP, radix, value); - if (radixValue == DEC_RADIX) { - return Float.parseFloat(value); - } else { - int bits = Integer.parseUnsignedInt(valueP, HEX_RADIX); - return Float.intBitsToFloat(bits); + String valueP = prepareNumberString(value, true); + if (valueP != null) { + return parseFloatFromString(value, valueP, radix); + } + return null; + } + + private static Float parseFloatFromString(String value, String valueP, int radix) { + int radixValue = isValidStringAndRadix(valueP, radix, value); + if (radixValue == HEX_RADIX) { + int bits = (int) Long.parseLong(valueP, HEX_RADIX); + // Hex representation is a standard IEEE 754 float value (eg "0x41200000" for 10.0f). + return Float.intBitsToFloat(bits); + } else { + return Float.parseFloat(value); + } + } + + public static Float parseHexIntLongToFloat(String value, boolean bigEndian) { + String valueP = prepareNumberString(value, bigEndian); + if (valueP != null) { + int radixValue = isValidStringAndRadix(valueP, HEX_RADIX, value); + if (radixValue == HEX_RADIX) { + int bits = (int) Long.parseLong(valueP, HEX_RADIX); + // If the length is not equal to 8 characters, we process it as an integer (eg "0x0A" for 10.0f). + float floatValue = (float) bits; + return Float.valueOf(floatValue); } } 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) { - String valueP = prepareNumberString(value); + return parseDouble(value, radix, true); + } + + private static Double parseDouble(String value, int radix, boolean bigEndian) { + String valueP = prepareNumberString(value, bigEndian); + if (valueP != null) { int radixValue = isValidStringAndRadix(valueP, radix, value); if (radixValue == DEC_RADIX) { - return Double.parseDouble(prepareNumberString(value)); + return Double.parseDouble(valueP); } else { long bits = Long.parseUnsignedLong(valueP, HEX_RADIX); return Double.longBitsToDouble(bits); @@ -469,9 +509,7 @@ public class TbUtils { } public static Integer parseHexToInt(String value, boolean bigEndian) { - String hexValue = prepareNumberString(value); - String hex = bigEndian ? hexValue : reverseHexStringByOrder(hexValue); - return parseInt(hex, HEX_RADIX); + return parseInt(value, HEX_RADIX, bigEndian); } public static long parseLittleEndianHexToLong(String hex) { @@ -487,9 +525,7 @@ public class TbUtils { } public static Long parseHexToLong(String value, boolean bigEndian) { - String hexValue = prepareNumberString(value); - String hex = bigEndian ? value : reverseHexStringByOrder(hexValue); - return parseLong(hex, HEX_RADIX); + return parseLong(value, HEX_RADIX, bigEndian); } public static float parseLittleEndianHexToFloat(String hex) { @@ -505,9 +541,11 @@ public class TbUtils { } public static Float parseHexToFloat(String value, boolean bigEndian) { - String hexValue = prepareNumberString(value); - String hex = bigEndian ? value : reverseHexStringByOrder(hexValue); - return parseFloat(hex, HEX_RADIX); + String valueP = prepareNumberString(value, bigEndian); + if (valueP != null) { + return parseFloatFromString(value, valueP, HEX_RADIX); + } + return null; } public static double parseLittleEndianHexToDouble(String hex) { @@ -523,13 +561,11 @@ public class TbUtils { } public static double parseHexToDouble(String value, boolean bigEndian) { - String hexValue = prepareNumberString(value); - String hex = bigEndian ? value : reverseHexStringByOrder(hexValue); - return parseDouble(hex, HEX_RADIX); + return parseDouble(value, HEX_RADIX, bigEndian); } public static ExecutionArrayList hexToBytes(ExecutionContext ctx, String value) { - String hex = prepareNumberString(value); + String hex = prepareNumberString(value, true); int len = hex.length(); if (len % 2 > 0) { throw new IllegalArgumentException("Hex string must be even-length."); @@ -575,6 +611,7 @@ public class TbUtils { public static String longToHex(Long l) { return prepareNumberHexString(l, true, false, HEX_LEN_MIN, HEX_LEN_LONG_MAX); } + public static String longToHex(Long l, boolean bigEndian) { return prepareNumberHexString(l, bigEndian, false, HEX_LEN_MIN, HEX_LEN_LONG_MAX); } @@ -604,7 +641,7 @@ public class TbUtils { return Long.toString(number, radix); } return switch (radix) { - case MIN_RADIX -> Long.toBinaryString(number); + case MIN_RADIX -> Long.toBinaryString(number); case OCTAL_RADIX -> Long.toOctalString(number); case DEC_RADIX -> Long.toString(number); case HEX_RADIX -> prepareNumberHexString(number, bigEndian, pref, -1, -1); @@ -646,13 +683,13 @@ public class TbUtils { private static String removeLeadingZero_FF(String hex, Long number, int hexLenMax) { String hexWithoutZero = hex.replaceFirst("^0+(?!$)", ""); // Remove leading zeros except for the last one - hexWithoutZero = hexWithoutZero.length() % 2 > 0 ? "0" + hexWithoutZero : hexWithoutZero; + hexWithoutZero = hexWithoutZero.length() % 2 > 0 ? "0" + hexWithoutZero : hexWithoutZero; if (number >= 0) { return hexWithoutZero; } else { String hexWithoutZeroFF = hexWithoutZero.replaceFirst("^F+(?!$)", ""); - hexWithoutZeroFF = hexWithoutZeroFF.length() % 2 > 0 ? "F" + hexWithoutZeroFF : hexWithoutZeroFF; - if (hexWithoutZeroFF.length() > hexLenMax) { + 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; @@ -668,7 +705,7 @@ public class TbUtils { public static String floatToHex(Float f, boolean bigEndian) { // Convert the float to its raw integer bits representation - int bits = Float.floatToRawIntBits(f); + int bits = Float.floatToIntBits(f); // Format the integer bits as a hexadecimal string String result = String.format("0x%08X", bits); @@ -719,7 +756,7 @@ public class TbUtils { if (offset > data.length) { throw new IllegalArgumentException("Offset: " + offset + " is out of bounds for array with length: " + data.length + "!"); } - if (length > 4) { + if (length > BYTES_LEN_INT_MAX) { throw new IllegalArgumentException("Length: " + length + " is too large. Maximum 4 bytes is allowed!"); } if (offset + length > data.length) { @@ -784,10 +821,33 @@ public class TbUtils { } public static float parseBytesToFloat(byte[] data, int offset, boolean bigEndian) { - byte[] bytesToNumber = prepareBytesToNumber(data, offset, 4, bigEndian); - return ByteBuffer.wrap(bytesToNumber).getFloat(); + return parseBytesToFloat(data, offset, BYTES_LEN_INT_MAX, bigEndian); + } + + public static float parseBytesToFloat(List data, int offset, int length, boolean bigEndian) { + return parseBytesToFloat(Bytes.toArray(data), offset, length, bigEndian); } + public static float parseBytesToFloat(byte[] data, int offset, int length, boolean bigEndian) { + byte[] bytesToNumber = prepareBytesToNumber(data, offset, length, bigEndian); + if (length > BYTES_LEN_INT_MAX) { + throw new IllegalArgumentException("Length: " + length + " is too large. Maximum 4 bytes is allowed!"); + } + if (bytesToNumber.length < BYTES_LEN_INT_MAX) { + byte[] extendedBytes = new byte[4]; + Arrays.fill(extendedBytes, (byte) 0); + System.arraycopy(bytesToNumber, 0, extendedBytes, 0, bytesToNumber.length); + bytesToNumber = extendedBytes; + } + float floatValue = ByteBuffer.wrap(bytesToNumber).getFloat(); + if (!Float.isNaN(floatValue)){ + return floatValue; + } else { + long longValue = parseBytesToLong(bytesToNumber, offset, length, bigEndian); + BigDecimal bigDecimalValue = new BigDecimal(longValue); + return bigDecimalValue.floatValue(); + } + } public static double parseBytesToDouble(byte[] data, int offset) { return parseBytesToDouble(data, offset, true); @@ -931,14 +991,15 @@ public class TbUtils { } } - private static String prepareNumberString(String value) { - if (value != null) { + private static String prepareNumberString(String value, boolean bigEndian) { + if (StringUtils.isNotBlank(value)) { value = value.trim(); value = value.replace("0x", ""); value = value.replace("0X", ""); value = value.replace(",", "."); + return bigEndian ? value : reverseHexStringByOrder(value); } - return value; + return null; } private static int isValidStringAndRadix(String valueP, int radix, String value) { 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 1d4e12c00e..1df1fc93cc 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 @@ -47,7 +47,7 @@ public class TbUtilsTest { private ExecutionContext ctx; - private final float floatVal = 29.29824f; + private final Float floatVal = 29.29824f; private final float floatValRev = -5.948442E7f; @@ -256,7 +256,7 @@ public class TbUtilsTest { @Test public void parseFloat() { - String floatValStr = "29.29824"; + String floatValStr = floatVal.toString(); Assertions.assertEquals(java.util.Optional.of(floatVal).get(), TbUtils.parseFloat(floatValStr)); String floatValHex = "41EA62CC"; Assertions.assertEquals(0, Float.compare(floatVal, TbUtils.parseHexToFloat(floatValHex))); @@ -274,7 +274,7 @@ public class TbUtilsTest { } @Test - public void arseBytesToFloat() { + public void parseBytesToFloat() { 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))); @@ -282,6 +282,33 @@ public class TbUtilsTest { 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))); + + // 4 294 967 295L == {0xFF, 0xFF, 0xFF, 0xFF} + floatValByte = new byte[] {-1, -1, -1, -1}; + float floatExpectedBe = 4294.9673f; + float floatExpectedLe = 4.2949673E9f; + float actualBe = TbUtils.parseBytesToFloat(floatValByte, 0, 4, true); + Assertions.assertEquals(0, Float.compare(floatExpectedBe, actualBe/1000000)); + Assertions.assertEquals(0, Float.compare(floatExpectedLe, TbUtils.parseBytesToFloat(floatValByte, 0, false))); + + floatVaList = Bytes.asList(floatValByte); + actualBe = TbUtils.parseBytesToFloat(floatVaList, 0); + Assertions.assertEquals(0, Float.compare(floatExpectedBe, actualBe/1000000)); + Assertions.assertEquals(0, Float.compare(floatExpectedLe, TbUtils.parseBytesToFloat(floatVaList, 0, false))); + + // 2 143 289 344L == {0x7F, 0xC0, 0x00, 0x00} + floatValByte = new byte[] {0x7F, (byte) 0xC0, (byte) 0xFF, 0x00}; + floatExpectedBe = 2143.3547f; + floatExpectedLe = -3.984375f; + actualBe = TbUtils.parseBytesToFloat(floatValByte, 0, 4, true); + Assertions.assertEquals(0, Float.compare(floatExpectedBe, actualBe/1000000)); + Assertions.assertEquals(0, Float.compare(floatExpectedLe, TbUtils.parseBytesToFloat(floatValByte, 0, 2,false))); + + floatVaList = Bytes.asList(floatValByte); + floatExpectedLe = 8372479.0f; + actualBe = TbUtils.parseBytesToFloat(floatVaList, 0); + Assertions.assertEquals(0, Float.compare(floatExpectedBe, actualBe/1000000)); + Assertions.assertEquals(0, Float.compare(floatExpectedLe, TbUtils.parseBytesToFloat(floatVaList, 0, 3, false))); } @Test @@ -605,15 +632,51 @@ public class TbUtilsTest { @Test public void floatToHex_Test() { - Float value = 20.89f; - String expectedHex = "0x41A71EB8"; - String valueHexRev = "0xB81EA741"; + Float value = 123456789.00f; + String expectedHex = "0x4CEB79A3"; + String valueHexRev = "0xA379EB4C"; 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); + value = 123456789.67f; + expectedHex = "0x4CEB79A3"; + valueHexRev = "0xA379EB4C"; + actual = TbUtils.floatToHex(value); + Assertions.assertEquals(expectedHex, actual); + valueActual = TbUtils.parseHexToFloat(actual); + Assertions.assertEquals(value, valueActual); + valueActual = TbUtils.parseHexToFloat(valueHexRev, false); + Assertions.assertEquals(value, valueActual); + value = 10.0f; + expectedHex = "0x41200000"; + valueHexRev = "0x00002041"; + actual = TbUtils.floatToHex(value); + Assertions.assertEquals(expectedHex, actual); + valueActual = TbUtils.parseHexToFloat(actual); + Assertions.assertEquals(value, valueActual); + valueActual = TbUtils.parseHexToFloat(valueHexRev, false); + Assertions.assertEquals(value, valueActual); + } + // If the length is not equal to 8 characters, we process it as an integer (eg "0x0A" for 10.0f). + @Test + public void parseHexIntLongToFloat_Test() { + Float valueExpected = 10.0f; + Float valueActual = TbUtils.parseHexIntLongToFloat("0x0A", true); + Assertions.assertEquals(valueExpected, valueActual); + valueActual = TbUtils.parseHexIntLongToFloat("0x0A", false); + Assertions.assertEquals(valueExpected, valueActual); + valueActual = TbUtils.parseHexIntLongToFloat("0x00000A", true); + Assertions.assertEquals(valueExpected, valueActual); + valueActual = TbUtils.parseHexIntLongToFloat("0x0A0000", false); + Assertions.assertEquals(valueExpected, valueActual); + valueExpected = 2570.0f; + valueActual = TbUtils.parseHexIntLongToFloat("0x000A0A", true); + Assertions.assertEquals(valueExpected, valueActual); + valueActual = TbUtils.parseHexIntLongToFloat("0x0A0A00", false); + Assertions.assertEquals(valueExpected, valueActual); } @Test From 53bd5418acd3ee98e4b2d5bdfdcadda13f975ff6 Mon Sep 17 00:00:00 2001 From: mpetrov Date: Mon, 22 Jul 2024 18:00:04 +0300 Subject: [PATCH 02/20] adjustments --- .../modbus-data-keys-panel.component.html | 3 ++- .../modbus-security-config.component.ts | 2 +- .../modbus-slave-config.component.html | 3 ++- .../modbus-slave-config.component.ts | 8 +++--- .../modbus-slave-dialog.component.html | 14 ++++++---- .../modbus-slave-dialog.component.ts | 15 ++++++++--- .../lib/gateway/gateway-widget.models.ts | 26 +++++++++++++++++-- 7 files changed, 55 insertions(+), 16 deletions(-) diff --git a/ui-ngx/src/app/modules/home/components/widget/lib/gateway/connectors-configuration/modbus/modbus-data-keys-panel/modbus-data-keys-panel.component.html b/ui-ngx/src/app/modules/home/components/widget/lib/gateway/connectors-configuration/modbus/modbus-data-keys-panel/modbus-data-keys-panel.component.html index a38b7e4dda..048ee94553 100644 --- a/ui-ngx/src/app/modules/home/components/widget/lib/gateway/connectors-configuration/modbus/modbus-data-keys-panel/modbus-data-keys-panel.component.html +++ b/ui-ngx/src/app/modules/home/components/widget/lib/gateway/connectors-configuration/modbus/modbus-data-keys-panel/modbus-data-keys-panel.component.html @@ -89,6 +89,7 @@ matInput type="number" min="1" + max="50000" name="value" formControlName="objectsCount" placeholder="{{ 'gateway.set' | translate }}" @@ -101,7 +102,7 @@
gateway.address
- +
diff --git a/ui-ngx/src/app/modules/home/components/widget/lib/gateway/connectors-configuration/modbus/modbus-security-config/modbus-security-config.component.ts b/ui-ngx/src/app/modules/home/components/widget/lib/gateway/connectors-configuration/modbus/modbus-security-config/modbus-security-config.component.ts index f056c9c92f..1e58cb4a40 100644 --- a/ui-ngx/src/app/modules/home/components/widget/lib/gateway/connectors-configuration/modbus/modbus-security-config/modbus-security-config.component.ts +++ b/ui-ngx/src/app/modules/home/components/widget/lib/gateway/connectors-configuration/modbus/modbus-security-config/modbus-security-config.component.ts @@ -138,7 +138,7 @@ export class ModbusSecurityConfigComponent implements ControlValueAccessor, Vali this.securityConfigFormGroup.valueChanges.pipe( takeUntil(this.destroy$) ).subscribe((value: ModbusSecurity) => { - this.onChange(value); + this.onChange(this.disabled ? {} : value); this.onTouched(); }); } diff --git a/ui-ngx/src/app/modules/home/components/widget/lib/gateway/connectors-configuration/modbus/modbus-slave-config/modbus-slave-config.component.html b/ui-ngx/src/app/modules/home/components/widget/lib/gateway/connectors-configuration/modbus/modbus-slave-config/modbus-slave-config.component.html index 4b0789fb4c..41a7d2aee7 100644 --- a/ui-ngx/src/app/modules/home/components/widget/lib/gateway/connectors-configuration/modbus/modbus-slave-config/modbus-slave-config.component.html +++ b/ui-ngx/src/app/modules/home/components/widget/lib/gateway/connectors-configuration/modbus/modbus-slave-config/modbus-slave-config.component.html @@ -102,7 +102,8 @@
- {{ ModbusMethodLabelsMap.get(method) }} + {{ ModbusMethodLabelsMap.get(method) }}
diff --git a/ui-ngx/src/app/modules/home/components/widget/lib/gateway/connectors-configuration/modbus/modbus-slave-config/modbus-slave-config.component.ts b/ui-ngx/src/app/modules/home/components/widget/lib/gateway/connectors-configuration/modbus/modbus-slave-config/modbus-slave-config.component.ts index f1234ccfb1..d328a97baf 100644 --- a/ui-ngx/src/app/modules/home/components/widget/lib/gateway/connectors-configuration/modbus/modbus-slave-config/modbus-slave-config.component.ts +++ b/ui-ngx/src/app/modules/home/components/widget/lib/gateway/connectors-configuration/modbus/modbus-slave-config/modbus-slave-config.component.ts @@ -33,6 +33,7 @@ import { ModbusProtocolLabelsMap, ModbusProtocolType, ModbusRegisterValues, + ModbusSerialMethodType, ModbusSlave, noLeadTrailSpacesRegex, PortLimits, @@ -90,6 +91,7 @@ export class ModbusSlaveConfigComponent implements ControlValueAccessor, Validat readonly modbusProtocolTypes = Object.values(ModbusProtocolType); readonly modbusMethodTypes = Object.values(ModbusMethodType); + readonly modbusSerialMethodTypes = Object.values(ModbusSerialMethodType); readonly modbusOrderType = Object.values(ModbusOrderType); readonly ModbusProtocolType = ModbusProtocolType; readonly serialSpecificControlKeys = ['serialPort', 'baudrate']; @@ -107,7 +109,7 @@ export class ModbusSlaveConfigComponent implements ControlValueAccessor, Validat host: ['', [Validators.required, Validators.pattern(noLeadTrailSpacesRegex)]], port: [null, [Validators.required, Validators.min(PortLimits.MIN), Validators.max(PortLimits.MAX)]], serialPort: ['', [Validators.required, Validators.pattern(noLeadTrailSpacesRegex)]], - method: [ModbusMethodType.SOCKET, []], + method: [ModbusMethodType.RTU, []], unitId: [null, [Validators.required]], baudrate: [null, []], deviceName: ['', [Validators.required, Validators.pattern(noLeadTrailSpacesRegex)]], @@ -133,7 +135,7 @@ export class ModbusSlaveConfigComponent implements ControlValueAccessor, Validat value.port = value.serialPort; delete value.serialPort; } - this.onChange(value); + this.onChange(this.slaveConfigFormGroup.get('sendDataToThingsBoard').value ? value : {} as SlaveConfig); this.onTouched(); }); @@ -226,7 +228,7 @@ export class ModbusSlaveConfigComponent implements ControlValueAccessor, Validat let slaveState: ModbusSlave = { host: host ?? '', type: type ?? ModbusProtocolType.TCP, - method: method ?? ModbusMethodType.SOCKET, + method: method ?? ModbusMethodType.RTU, unitId: unitId ?? null, deviceName: deviceName ?? '', deviceType: deviceType ?? '', diff --git a/ui-ngx/src/app/modules/home/components/widget/lib/gateway/connectors-configuration/modbus/modbus-slave-dialog/modbus-slave-dialog.component.html b/ui-ngx/src/app/modules/home/components/widget/lib/gateway/connectors-configuration/modbus/modbus-slave-dialog/modbus-slave-dialog.component.html index 3ac441447c..d3c06aeb4b 100644 --- a/ui-ngx/src/app/modules/home/components/widget/lib/gateway/connectors-configuration/modbus/modbus-slave-dialog/modbus-slave-dialog.component.html +++ b/ui-ngx/src/app/modules/home/components/widget/lib/gateway/connectors-configuration/modbus/modbus-slave-dialog/modbus-slave-dialog.component.html @@ -124,7 +124,8 @@
- {{ ModbusMethodLabelsMap.get(method) }} + {{ ModbusMethodLabelsMap.get(method) }}
@@ -143,7 +144,9 @@
gateway.bytesize
- + + {{ size }} +
@@ -159,7 +162,9 @@
gateway.parity
- + + {{ ModbusParityLabelsMap.get(parity) }} +
@@ -274,7 +279,7 @@ - +
@@ -332,7 +337,6 @@
-
gateway.values
diff --git a/ui-ngx/src/app/modules/home/components/widget/lib/gateway/connectors-configuration/modbus/modbus-slave-dialog/modbus-slave-dialog.component.ts b/ui-ngx/src/app/modules/home/components/widget/lib/gateway/connectors-configuration/modbus/modbus-slave-dialog/modbus-slave-dialog.component.ts index 006ae7a482..f0c8c2bd45 100644 --- a/ui-ngx/src/app/modules/home/components/widget/lib/gateway/connectors-configuration/modbus/modbus-slave-dialog/modbus-slave-dialog.component.ts +++ b/ui-ngx/src/app/modules/home/components/widget/lib/gateway/connectors-configuration/modbus/modbus-slave-dialog/modbus-slave-dialog.component.ts @@ -25,11 +25,15 @@ import { } from '@angular/forms'; import { MappingInfo, + ModbusByteSizes, ModbusMethodLabelsMap, ModbusMethodType, ModbusOrderType, + ModbusParity, + ModbusParityLabelsMap, ModbusProtocolLabelsMap, ModbusProtocolType, + ModbusSerialMethodType, noLeadTrailSpacesRegex, PortLimits, SlaveConfig, } from '@home/components/widget/lib/gateway/gateway-widget.models'; @@ -92,8 +96,12 @@ export class ModbusSlaveDialogComponent extends DialogComponent( +export enum ModbusSerialMethodType { + RTU = 'rtu', + ASCII = 'ascii', +} + +export const ModbusMethodLabelsMap = new Map( [ [ModbusMethodType.SOCKET, 'Socket'], [ModbusMethodType.RTU, 'RTU'], + [ModbusSerialMethodType.ASCII, 'ASCII'], + ] +); + +export const ModbusByteSizes = [5, 6, 7 ,8]; + +export enum ModbusParity { + Even = 'E', + Odd = 'O', + None = 'N' +} + +export const ModbusParityLabelsMap = new Map( + [ + [ModbusParity.Even, 'Even'], + [ModbusParity.Odd, 'Odd'], + [ModbusParity.None, 'None'], ] ); @@ -872,7 +894,7 @@ export const ModbusKeysDeleteKeyTranslationsMap = new Map Date: Mon, 22 Jul 2024 19:16:27 +0300 Subject: [PATCH 03/20] refactoring --- .../table/table-datasource.abstract.ts} | 2 +- .../broker-config-control.component.ts | 6 ++- .../mapping-table/mapping-table.component.ts | 4 +- .../modbus-basic-config.component.ts | 2 +- .../modbus-data-keys-panel.component.ts | 2 +- .../modbus-master-table.component.ts | 4 +- .../modbus-security-config.component.ts | 4 +- .../modbus-slave-config.component.ts | 2 +- .../modbus-slave-dialog.component.ts | 2 +- .../modbus-values/modbus-values.component.ts | 2 +- .../modbus/public-api.ts | 23 ---------- .../mqtt-basic-config.component.ts | 18 +++++--- .../opc-server-config.component.ts | 4 +- .../opc-ua-basic-config.component.ts | 22 ++++++---- .../connectors-configuration/public-api.ts | 27 ------------ .../dialog/mapping-dialog.component.ts | 4 +- .../widget/widget-components.module.ts | 42 +++++++++++++------ .../gateway-help-link.pipe.ts | 0 .../gateway-port-tooltip.pipe.ts | 2 +- .../src/app/modules/home/pipes/public-api.ts | 18 -------- ui-ngx/src/app/shared/abstract/public-api.ts | 17 -------- .../ellipsis-chip-list.directive.ts | 0 .../src/app/shared/directives/public-api.ts | 17 -------- 23 files changed, 81 insertions(+), 143 deletions(-) rename ui-ngx/src/app/{shared/abstract/datasource/datasource.abstract.ts => modules/home/components/table/table-datasource.abstract.ts} (94%) delete mode 100644 ui-ngx/src/app/modules/home/components/widget/lib/gateway/connectors-configuration/modbus/public-api.ts delete mode 100644 ui-ngx/src/app/modules/home/components/widget/lib/gateway/connectors-configuration/public-api.ts rename ui-ngx/src/app/modules/home/pipes/{gateway-help-link => }/gateway-help-link.pipe.ts (100%) rename ui-ngx/src/app/modules/home/pipes/{gateway-port-tooltip => }/gateway-port-tooltip.pipe.ts (93%) delete mode 100644 ui-ngx/src/app/modules/home/pipes/public-api.ts delete mode 100644 ui-ngx/src/app/shared/abstract/public-api.ts rename ui-ngx/src/app/shared/directives/{ellipsis-chip-list => }/ellipsis-chip-list.directive.ts (100%) delete mode 100644 ui-ngx/src/app/shared/directives/public-api.ts diff --git a/ui-ngx/src/app/shared/abstract/datasource/datasource.abstract.ts b/ui-ngx/src/app/modules/home/components/table/table-datasource.abstract.ts similarity index 94% rename from ui-ngx/src/app/shared/abstract/datasource/datasource.abstract.ts rename to ui-ngx/src/app/modules/home/components/table/table-datasource.abstract.ts index 43de3789a0..6146b5ebec 100644 --- a/ui-ngx/src/app/shared/abstract/datasource/datasource.abstract.ts +++ b/ui-ngx/src/app/modules/home/components/table/table-datasource.abstract.ts @@ -18,7 +18,7 @@ import { DataSource } from '@angular/cdk/collections'; import { BehaviorSubject, Observable } from 'rxjs'; import { map } from 'rxjs/operators'; -export abstract class TbDatasource implements DataSource { +export abstract class TbTableDatasource implements DataSource { protected dataSubject = new BehaviorSubject>([]); diff --git a/ui-ngx/src/app/modules/home/components/widget/lib/gateway/connectors-configuration/broker-config-control/broker-config-control.component.ts b/ui-ngx/src/app/modules/home/components/widget/lib/gateway/connectors-configuration/broker-config-control/broker-config-control.component.ts index 6de9c8b310..031388b502 100644 --- a/ui-ngx/src/app/modules/home/components/widget/lib/gateway/connectors-configuration/broker-config-control/broker-config-control.component.ts +++ b/ui-ngx/src/app/modules/home/components/widget/lib/gateway/connectors-configuration/broker-config-control/broker-config-control.component.ts @@ -35,9 +35,11 @@ import { SharedModule } from '@shared/shared.module'; import { CommonModule } from '@angular/common'; import { TranslateService } from '@ngx-translate/core'; import { generateSecret } from '@core/utils'; -import { SecurityConfigComponent } from '@home/components/widget/lib/gateway/connectors-configuration/public-api'; import { Subject } from 'rxjs'; -import { GatewayPortTooltipPipe } from '@home/pipes/public-api'; +import { GatewayPortTooltipPipe } from '@home/pipes/gateway-port-tooltip.pipe'; +import { + SecurityConfigComponent +} from '@home/components/widget/lib/gateway/connectors-configuration/security-config/security-config.component'; @Component({ selector: 'tb-broker-config-control', diff --git a/ui-ngx/src/app/modules/home/components/widget/lib/gateway/connectors-configuration/mapping-table/mapping-table.component.ts b/ui-ngx/src/app/modules/home/components/widget/lib/gateway/connectors-configuration/mapping-table/mapping-table.component.ts index d0df9bc30d..292cb43b6e 100644 --- a/ui-ngx/src/app/modules/home/components/widget/lib/gateway/connectors-configuration/mapping-table/mapping-table.component.ts +++ b/ui-ngx/src/app/modules/home/components/widget/lib/gateway/connectors-configuration/mapping-table/mapping-table.component.ts @@ -57,7 +57,7 @@ import { isDefinedAndNotNull, isUndefinedOrNull } from '@core/utils'; import { coerceBoolean } from '@shared/decorators/coercion'; import { SharedModule } from '@shared/shared.module'; import { CommonModule } from '@angular/common'; -import { TbDatasource } from '@shared/abstract/public-api'; +import { TbTableDatasource } from '@home/components/table/table-datasource.abstract'; @Component({ selector: 'tb-mapping-table', @@ -310,7 +310,7 @@ export class MappingTableComponent implements ControlValueAccessor, Validator, A } } -export class MappingDatasource extends TbDatasource { +export class MappingDatasource extends TbTableDatasource { constructor() { super(); } diff --git a/ui-ngx/src/app/modules/home/components/widget/lib/gateway/connectors-configuration/modbus/modbus-basic-config/modbus-basic-config.component.ts b/ui-ngx/src/app/modules/home/components/widget/lib/gateway/connectors-configuration/modbus/modbus-basic-config/modbus-basic-config.component.ts index 6f2d9aff02..445cb39983 100644 --- a/ui-ngx/src/app/modules/home/components/widget/lib/gateway/connectors-configuration/modbus/modbus-basic-config/modbus-basic-config.component.ts +++ b/ui-ngx/src/app/modules/home/components/widget/lib/gateway/connectors-configuration/modbus/modbus-basic-config/modbus-basic-config.component.ts @@ -30,7 +30,7 @@ import { CommonModule } from '@angular/common'; import { takeUntil } from 'rxjs/operators'; import { Subject } from 'rxjs'; -import { EllipsisChipListDirective } from '@shared/directives/public-api'; +import { EllipsisChipListDirective } from '@shared/directives/ellipsis-chip-list.directive'; import { ModbusSlaveConfigComponent } from '../modbus-slave-config/modbus-slave-config.component'; import { ModbusMasterTableComponent } from '../modbus-master-table/modbus-master-table.component'; diff --git a/ui-ngx/src/app/modules/home/components/widget/lib/gateway/connectors-configuration/modbus/modbus-data-keys-panel/modbus-data-keys-panel.component.ts b/ui-ngx/src/app/modules/home/components/widget/lib/gateway/connectors-configuration/modbus/modbus-data-keys-panel/modbus-data-keys-panel.component.ts index 3f57658443..1a696dd75f 100644 --- a/ui-ngx/src/app/modules/home/components/widget/lib/gateway/connectors-configuration/modbus/modbus-data-keys-panel/modbus-data-keys-panel.component.ts +++ b/ui-ngx/src/app/modules/home/components/widget/lib/gateway/connectors-configuration/modbus/modbus-data-keys-panel/modbus-data-keys-panel.component.ts @@ -28,7 +28,7 @@ import { } from '@home/components/widget/lib/gateway/gateway-widget.models'; import { CommonModule } from '@angular/common'; import { SharedModule } from '@shared/shared.module'; -import { GatewayHelpLinkPipe } from '@home/pipes/public-api'; +import { GatewayHelpLinkPipe } from '@home/pipes/gateway-help-link.pipe'; import { generateSecret } from '@core/utils'; @Component({ diff --git a/ui-ngx/src/app/modules/home/components/widget/lib/gateway/connectors-configuration/modbus/modbus-master-table/modbus-master-table.component.ts b/ui-ngx/src/app/modules/home/components/widget/lib/gateway/connectors-configuration/modbus/modbus-master-table/modbus-master-table.component.ts index 69613b9207..6644fbfd5e 100644 --- a/ui-ngx/src/app/modules/home/components/widget/lib/gateway/connectors-configuration/modbus/modbus-master-table/modbus-master-table.component.ts +++ b/ui-ngx/src/app/modules/home/components/widget/lib/gateway/connectors-configuration/modbus/modbus-master-table/modbus-master-table.component.ts @@ -45,8 +45,8 @@ import { import { isDefinedAndNotNull, isUndefinedOrNull } from '@core/utils'; import { SharedModule } from '@shared/shared.module'; import { CommonModule } from '@angular/common'; -import { TbDatasource } from '@shared/abstract/datasource/datasource.abstract'; import { ModbusSlaveDialogComponent } from '../modbus-slave-dialog/modbus-slave-dialog.component'; +import { TbTableDatasource } from '@home/components/table/table-datasource.abstract'; @Component({ selector: 'tb-modbus-master-table', @@ -224,7 +224,7 @@ export class ModbusMasterTableComponent implements ControlValueAccessor, Validat } } -export class SlavesDatasource extends TbDatasource { +export class SlavesDatasource extends TbTableDatasource { constructor() { super(); } diff --git a/ui-ngx/src/app/modules/home/components/widget/lib/gateway/connectors-configuration/modbus/modbus-security-config/modbus-security-config.component.ts b/ui-ngx/src/app/modules/home/components/widget/lib/gateway/connectors-configuration/modbus/modbus-security-config/modbus-security-config.component.ts index 1e58cb4a40..7efcf095ee 100644 --- a/ui-ngx/src/app/modules/home/components/widget/lib/gateway/connectors-configuration/modbus/modbus-security-config/modbus-security-config.component.ts +++ b/ui-ngx/src/app/modules/home/components/widget/lib/gateway/connectors-configuration/modbus/modbus-security-config/modbus-security-config.component.ts @@ -31,9 +31,11 @@ import { } from '@home/components/widget/lib/gateway/gateway-widget.models'; import { SharedModule } from '@shared/shared.module'; import { CommonModule } from '@angular/common'; -import { SecurityConfigComponent } from '@home/components/widget/lib/gateway/connectors-configuration/public-api'; import { Subject } from 'rxjs'; import { takeUntil } from 'rxjs/operators'; +import { + SecurityConfigComponent +} from '@home/components/widget/lib/gateway/connectors-configuration/security-config/security-config.component'; @Component({ selector: 'tb-modbus-security-config', diff --git a/ui-ngx/src/app/modules/home/components/widget/lib/gateway/connectors-configuration/modbus/modbus-slave-config/modbus-slave-config.component.ts b/ui-ngx/src/app/modules/home/components/widget/lib/gateway/connectors-configuration/modbus/modbus-slave-config/modbus-slave-config.component.ts index d328a97baf..13a4eefb8c 100644 --- a/ui-ngx/src/app/modules/home/components/widget/lib/gateway/connectors-configuration/modbus/modbus-slave-config/modbus-slave-config.component.ts +++ b/ui-ngx/src/app/modules/home/components/widget/lib/gateway/connectors-configuration/modbus/modbus-slave-config/modbus-slave-config.component.ts @@ -43,7 +43,7 @@ import { SharedModule } from '@shared/shared.module'; import { CommonModule } from '@angular/common'; import { Subject } from 'rxjs'; import { startWith, takeUntil } from 'rxjs/operators'; -import { GatewayPortTooltipPipe } from '@home/pipes/public-api'; +import { GatewayPortTooltipPipe } from '@home/pipes/gateway-port-tooltip.pipe'; import { ModbusSecurityConfigComponent } from '../modbus-security-config/modbus-security-config.component'; import { ModbusValuesComponent, } from '../modbus-values/modbus-values.component'; diff --git a/ui-ngx/src/app/modules/home/components/widget/lib/gateway/connectors-configuration/modbus/modbus-slave-dialog/modbus-slave-dialog.component.ts b/ui-ngx/src/app/modules/home/components/widget/lib/gateway/connectors-configuration/modbus/modbus-slave-dialog/modbus-slave-dialog.component.ts index f0c8c2bd45..5ac897b91a 100644 --- a/ui-ngx/src/app/modules/home/components/widget/lib/gateway/connectors-configuration/modbus/modbus-slave-dialog/modbus-slave-dialog.component.ts +++ b/ui-ngx/src/app/modules/home/components/widget/lib/gateway/connectors-configuration/modbus/modbus-slave-dialog/modbus-slave-dialog.component.ts @@ -47,7 +47,7 @@ import { Store } from '@ngrx/store'; import { AppState } from '@core/core.state'; import { Router } from '@angular/router'; import { MAT_DIALOG_DATA, MatDialogRef } from '@angular/material/dialog'; -import { GatewayPortTooltipPipe } from '@home/pipes/gateway-port-tooltip/gateway-port-tooltip.pipe'; +import { GatewayPortTooltipPipe } from '@home/pipes/gateway-port-tooltip.pipe'; import { takeUntil } from 'rxjs/operators'; @Component({ diff --git a/ui-ngx/src/app/modules/home/components/widget/lib/gateway/connectors-configuration/modbus/modbus-values/modbus-values.component.ts b/ui-ngx/src/app/modules/home/components/widget/lib/gateway/connectors-configuration/modbus/modbus-values/modbus-values.component.ts index f1df094e8b..f3e9a28bce 100644 --- a/ui-ngx/src/app/modules/home/components/widget/lib/gateway/connectors-configuration/modbus/modbus-values/modbus-values.component.ts +++ b/ui-ngx/src/app/modules/home/components/widget/lib/gateway/connectors-configuration/modbus/modbus-values/modbus-values.component.ts @@ -50,7 +50,7 @@ import { SharedModule } from '@shared/shared.module'; import { CommonModule } from '@angular/common'; import { takeUntil } from 'rxjs/operators'; import { Subject } from 'rxjs'; -import { EllipsisChipListDirective } from '@shared/directives/public-api'; +import { EllipsisChipListDirective } from '@shared/directives/ellipsis-chip-list.directive'; import { MatButton } from '@angular/material/button'; import { TbPopoverService } from '@shared/components/popover.service'; import { ModbusDataKeysPanelComponent } from '../modbus-data-keys-panel/modbus-data-keys-panel.component'; diff --git a/ui-ngx/src/app/modules/home/components/widget/lib/gateway/connectors-configuration/modbus/public-api.ts b/ui-ngx/src/app/modules/home/components/widget/lib/gateway/connectors-configuration/modbus/public-api.ts deleted file mode 100644 index 87a3ba5c2f..0000000000 --- a/ui-ngx/src/app/modules/home/components/widget/lib/gateway/connectors-configuration/modbus/public-api.ts +++ /dev/null @@ -1,23 +0,0 @@ -/// -/// 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. -/// - -export * from './modbus-basic-config/modbus-basic-config.component'; -export * from './modbus-values/modbus-values.component'; -export * from './modbus-data-keys-panel/modbus-data-keys-panel.component'; -export * from './modbus-slave-config/modbus-slave-config.component'; -export * from './modbus-master-table/modbus-master-table.component'; -export * from './modbus-slave-dialog/modbus-slave-dialog.component'; -export * from './modbus-security-config/modbus-security-config.component'; diff --git a/ui-ngx/src/app/modules/home/components/widget/lib/gateway/connectors-configuration/mqtt-basic-config/mqtt-basic-config.component.ts b/ui-ngx/src/app/modules/home/components/widget/lib/gateway/connectors-configuration/mqtt-basic-config/mqtt-basic-config.component.ts index 62613dc28c..f4296b5303 100644 --- a/ui-ngx/src/app/modules/home/components/widget/lib/gateway/connectors-configuration/mqtt-basic-config/mqtt-basic-config.component.ts +++ b/ui-ngx/src/app/modules/home/components/widget/lib/gateway/connectors-configuration/mqtt-basic-config/mqtt-basic-config.component.ts @@ -32,15 +32,21 @@ import { } from '@home/components/widget/lib/gateway/gateway-widget.models'; import { SharedModule } from '@shared/shared.module'; import { CommonModule } from '@angular/common'; -import { - BrokerConfigControlComponent, - MappingTableComponent, - SecurityConfigComponent, - WorkersConfigControlComponent -} from '@home/components/widget/lib/gateway/connectors-configuration/public-api'; import { takeUntil } from 'rxjs/operators'; import { Subject } from 'rxjs'; import { isObject } from 'lodash'; +import { + SecurityConfigComponent +} from '@home/components/widget/lib/gateway/connectors-configuration/security-config/security-config.component'; +import { + WorkersConfigControlComponent +} from '@home/components/widget/lib/gateway/connectors-configuration/workers-config-control/workers-config-control.component'; +import { + BrokerConfigControlComponent +} from '@home/components/widget/lib/gateway/connectors-configuration/broker-config-control/broker-config-control.component'; +import { + MappingTableComponent +} from '@home/components/widget/lib/gateway/connectors-configuration/mapping-table/mapping-table.component'; @Component({ selector: 'tb-mqtt-basic-config', diff --git a/ui-ngx/src/app/modules/home/components/widget/lib/gateway/connectors-configuration/opc-server-config/opc-server-config.component.ts b/ui-ngx/src/app/modules/home/components/widget/lib/gateway/connectors-configuration/opc-server-config/opc-server-config.component.ts index 955e7e8145..258f295bcb 100644 --- a/ui-ngx/src/app/modules/home/components/widget/lib/gateway/connectors-configuration/opc-server-config/opc-server-config.component.ts +++ b/ui-ngx/src/app/modules/home/components/widget/lib/gateway/connectors-configuration/opc-server-config/opc-server-config.component.ts @@ -33,9 +33,11 @@ import { } from '@home/components/widget/lib/gateway/gateway-widget.models'; import { SharedModule } from '@shared/shared.module'; import { CommonModule } from '@angular/common'; -import { SecurityConfigComponent } from '@home/components/widget/lib/gateway/connectors-configuration/public-api'; import { Subject } from 'rxjs'; import { takeUntil } from 'rxjs/operators'; +import { + SecurityConfigComponent +} from '@home/components/widget/lib/gateway/connectors-configuration/security-config/security-config.component'; @Component({ selector: 'tb-opc-server-config', diff --git a/ui-ngx/src/app/modules/home/components/widget/lib/gateway/connectors-configuration/opc-ua-basic-config/opc-ua-basic-config.component.ts b/ui-ngx/src/app/modules/home/components/widget/lib/gateway/connectors-configuration/opc-ua-basic-config/opc-ua-basic-config.component.ts index dfe5c5e51e..39198c9696 100644 --- a/ui-ngx/src/app/modules/home/components/widget/lib/gateway/connectors-configuration/opc-ua-basic-config/opc-ua-basic-config.component.ts +++ b/ui-ngx/src/app/modules/home/components/widget/lib/gateway/connectors-configuration/opc-ua-basic-config/opc-ua-basic-config.component.ts @@ -31,15 +31,23 @@ import { } from '@home/components/widget/lib/gateway/gateway-widget.models'; import { SharedModule } from '@shared/shared.module'; import { CommonModule } from '@angular/common'; -import { - BrokerConfigControlComponent, - MappingTableComponent, - SecurityConfigComponent, - OpcServerConfigComponent, - WorkersConfigControlComponent -} from '@home/components/widget/lib/gateway/connectors-configuration/public-api'; import { takeUntil } from 'rxjs/operators'; import { Subject } from 'rxjs'; +import { + SecurityConfigComponent +} from '@home/components/widget/lib/gateway/connectors-configuration/security-config/security-config.component'; +import { + WorkersConfigControlComponent +} from '@home/components/widget/lib/gateway/connectors-configuration/workers-config-control/workers-config-control.component'; +import { + BrokerConfigControlComponent +} from '@home/components/widget/lib/gateway/connectors-configuration/broker-config-control/broker-config-control.component'; +import { + MappingTableComponent +} from '@home/components/widget/lib/gateway/connectors-configuration/mapping-table/mapping-table.component'; +import { + OpcServerConfigComponent +} from '@home/components/widget/lib/gateway/connectors-configuration/opc-server-config/opc-server-config.component'; @Component({ selector: 'tb-opc-ua-basic-config', diff --git a/ui-ngx/src/app/modules/home/components/widget/lib/gateway/connectors-configuration/public-api.ts b/ui-ngx/src/app/modules/home/components/widget/lib/gateway/connectors-configuration/public-api.ts deleted file mode 100644 index f98e6c569e..0000000000 --- a/ui-ngx/src/app/modules/home/components/widget/lib/gateway/connectors-configuration/public-api.ts +++ /dev/null @@ -1,27 +0,0 @@ -/// -/// 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. -/// - -export * from './mapping-table/mapping-table.component'; -export * from './device-info-table/device-info-table.component'; -export * from './security-config/security-config.component'; -export * from './opc-server-config/opc-server-config.component'; -export * from './mapping-data-keys-panel/mapping-data-keys-panel.component'; -export * from './type-value-panel/type-value-panel.component'; -export * from './broker-config-control/broker-config-control.component'; -export * from './workers-config-control/workers-config-control.component'; -export * from './opc-ua-basic-config/opc-ua-basic-config.component'; -export * from './mqtt-basic-config/mqtt-basic-config.component'; -export * from './modbus/public-api'; diff --git a/ui-ngx/src/app/modules/home/components/widget/lib/gateway/dialog/mapping-dialog.component.ts b/ui-ngx/src/app/modules/home/components/widget/lib/gateway/dialog/mapping-dialog.component.ts index 3812fa161e..40bff51fb4 100644 --- a/ui-ngx/src/app/modules/home/components/widget/lib/gateway/dialog/mapping-dialog.component.ts +++ b/ui-ngx/src/app/modules/home/components/widget/lib/gateway/dialog/mapping-dialog.component.ts @@ -55,7 +55,9 @@ import { startWith, takeUntil } from 'rxjs/operators'; import { MatButton } from '@angular/material/button'; import { TbPopoverService } from '@shared/components/popover.service'; import { TranslateService } from '@ngx-translate/core'; -import { MappingDataKeysPanelComponent } from '@home/components/widget/lib/gateway/connectors-configuration/public-api'; +import { + MappingDataKeysPanelComponent +} from '@home/components/widget/lib/gateway/connectors-configuration/mapping-data-keys-panel/mapping-data-keys-panel.component'; @Component({ selector: 'tb-mapping-dialog', diff --git a/ui-ngx/src/app/modules/home/components/widget/widget-components.module.ts b/ui-ngx/src/app/modules/home/components/widget/widget-components.module.ts index 3c2b7affc1..1e4093c22c 100644 --- a/ui-ngx/src/app/modules/home/components/widget/widget-components.module.ts +++ b/ui-ngx/src/app/modules/home/components/widget/widget-components.module.ts @@ -109,20 +109,38 @@ import { import { NotificationTypeFilterPanelComponent } from '@home/components/widget/lib/cards/notification-type-filter-panel.component'; -import { GatewayHelpLinkPipe } from '@home/pipes/public-api'; +import { GatewayHelpLinkPipe } from '@home/pipes/gateway-help-link.pipe'; +import { EllipsisChipListDirective } from '@shared/directives/ellipsis-chip-list.directive'; +import { + BrokerConfigControlComponent +} from '@home/components/widget/lib/gateway/connectors-configuration/broker-config-control/broker-config-control.component'; +import { + WorkersConfigControlComponent +} from '@home/components/widget/lib/gateway/connectors-configuration/workers-config-control/workers-config-control.component'; +import { + OpcServerConfigComponent +} from '@home/components/widget/lib/gateway/connectors-configuration/opc-server-config/opc-server-config.component'; +import { + MqttBasicConfigComponent +} from '@home/components/widget/lib/gateway/connectors-configuration/mqtt-basic-config/mqtt-basic-config.component'; +import { + MappingTableComponent +} from '@home/components/widget/lib/gateway/connectors-configuration/mapping-table/mapping-table.component'; +import { + OpcUaBasicConfigComponent +} from '@home/components/widget/lib/gateway/connectors-configuration/opc-ua-basic-config/opc-ua-basic-config.component'; import { - BrokerConfigControlComponent, - DeviceInfoTableComponent, - MappingDataKeysPanelComponent, - MappingTableComponent, - MqttBasicConfigComponent, - OpcUaBasicConfigComponent, - OpcServerConfigComponent, - TypeValuePanelComponent, - WorkersConfigControlComponent, ModbusBasicConfigComponent -} from '@home/components/widget/lib/gateway/connectors-configuration/public-api'; -import { EllipsisChipListDirective } from '@shared/directives/public-api'; +} from '@home/components/widget/lib/gateway/connectors-configuration/modbus/modbus-basic-config/modbus-basic-config.component'; +import { + DeviceInfoTableComponent +} from '@home/components/widget/lib/gateway/connectors-configuration/device-info-table/device-info-table.component'; +import { + MappingDataKeysPanelComponent +} from '@home/components/widget/lib/gateway/connectors-configuration/mapping-data-keys-panel/mapping-data-keys-panel.component'; +import { + TypeValuePanelComponent +} from '@home/components/widget/lib/gateway/connectors-configuration/type-value-panel/type-value-panel.component'; @NgModule({ declarations: [ diff --git a/ui-ngx/src/app/modules/home/pipes/gateway-help-link/gateway-help-link.pipe.ts b/ui-ngx/src/app/modules/home/pipes/gateway-help-link.pipe.ts similarity index 100% rename from ui-ngx/src/app/modules/home/pipes/gateway-help-link/gateway-help-link.pipe.ts rename to ui-ngx/src/app/modules/home/pipes/gateway-help-link.pipe.ts diff --git a/ui-ngx/src/app/modules/home/pipes/gateway-port-tooltip/gateway-port-tooltip.pipe.ts b/ui-ngx/src/app/modules/home/pipes/gateway-port-tooltip.pipe.ts similarity index 93% rename from ui-ngx/src/app/modules/home/pipes/gateway-port-tooltip/gateway-port-tooltip.pipe.ts rename to ui-ngx/src/app/modules/home/pipes/gateway-port-tooltip.pipe.ts index 9ff3e134c6..3981499a26 100644 --- a/ui-ngx/src/app/modules/home/pipes/gateway-port-tooltip/gateway-port-tooltip.pipe.ts +++ b/ui-ngx/src/app/modules/home/pipes/gateway-port-tooltip.pipe.ts @@ -15,7 +15,7 @@ /// import { Pipe, PipeTransform } from '@angular/core'; -import { PortLimits, } from '@home/components/widget/lib/gateway/gateway-widget.models'; +import { PortLimits } from '@home/components/widget/lib/gateway/gateway-widget.models'; import { AbstractControl } from '@angular/forms'; import { TranslateService } from '@ngx-translate/core'; diff --git a/ui-ngx/src/app/modules/home/pipes/public-api.ts b/ui-ngx/src/app/modules/home/pipes/public-api.ts deleted file mode 100644 index 9db71f9a06..0000000000 --- a/ui-ngx/src/app/modules/home/pipes/public-api.ts +++ /dev/null @@ -1,18 +0,0 @@ -/// -/// 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. -/// - -export * from './gateway-help-link/gateway-help-link.pipe'; -export * from './gateway-port-tooltip/gateway-port-tooltip.pipe'; diff --git a/ui-ngx/src/app/shared/abstract/public-api.ts b/ui-ngx/src/app/shared/abstract/public-api.ts deleted file mode 100644 index 562011065a..0000000000 --- a/ui-ngx/src/app/shared/abstract/public-api.ts +++ /dev/null @@ -1,17 +0,0 @@ -/// -/// 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. -/// - -export * from './datasource/datasource.abstract'; diff --git a/ui-ngx/src/app/shared/directives/ellipsis-chip-list/ellipsis-chip-list.directive.ts b/ui-ngx/src/app/shared/directives/ellipsis-chip-list.directive.ts similarity index 100% rename from ui-ngx/src/app/shared/directives/ellipsis-chip-list/ellipsis-chip-list.directive.ts rename to ui-ngx/src/app/shared/directives/ellipsis-chip-list.directive.ts diff --git a/ui-ngx/src/app/shared/directives/public-api.ts b/ui-ngx/src/app/shared/directives/public-api.ts deleted file mode 100644 index f1fae708d0..0000000000 --- a/ui-ngx/src/app/shared/directives/public-api.ts +++ /dev/null @@ -1,17 +0,0 @@ -/// -/// 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. -/// - -export * from './ellipsis-chip-list/ellipsis-chip-list.directive'; From 6cc1d28f25f11e491082c51c54af535cce224bab Mon Sep 17 00:00:00 2001 From: mpetrov Date: Mon, 22 Jul 2024 19:25:56 +0300 Subject: [PATCH 04/20] refactoring --- .../modbus-slave-dialog/modbus-slave-dialog.component.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/ui-ngx/src/app/modules/home/components/widget/lib/gateway/connectors-configuration/modbus/modbus-slave-dialog/modbus-slave-dialog.component.ts b/ui-ngx/src/app/modules/home/components/widget/lib/gateway/connectors-configuration/modbus/modbus-slave-dialog/modbus-slave-dialog.component.ts index 5ac897b91a..941f6d49ee 100644 --- a/ui-ngx/src/app/modules/home/components/widget/lib/gateway/connectors-configuration/modbus/modbus-slave-dialog/modbus-slave-dialog.component.ts +++ b/ui-ngx/src/app/modules/home/components/widget/lib/gateway/connectors-configuration/modbus/modbus-slave-dialog/modbus-slave-dialog.component.ts @@ -35,7 +35,8 @@ import { ModbusProtocolType, ModbusSerialMethodType, noLeadTrailSpacesRegex, - PortLimits, SlaveConfig, + PortLimits, + SlaveConfig, } from '@home/components/widget/lib/gateway/gateway-widget.models'; import { SharedModule } from '@shared/shared.module'; import { CommonModule } from '@angular/common'; From 6810b17b42395e51e0e4559678b4231266114c75 Mon Sep 17 00:00:00 2001 From: mpetrov Date: Tue, 23 Jul 2024 11:41:01 +0300 Subject: [PATCH 05/20] added suggestions --- .../modbus-data-keys-panel.component.ts | 2 ++ .../modbus-security-config.component.ts | 16 ++++++++++++++-- .../modbus-slave-config.component.html | 4 ++-- .../modbus-slave-config.component.ts | 13 ++++++++++++- .../modbus-values/modbus-values.component.ts | 9 ++++++++- 5 files changed, 38 insertions(+), 6 deletions(-) diff --git a/ui-ngx/src/app/modules/home/components/widget/lib/gateway/connectors-configuration/modbus/modbus-data-keys-panel/modbus-data-keys-panel.component.ts b/ui-ngx/src/app/modules/home/components/widget/lib/gateway/connectors-configuration/modbus/modbus-data-keys-panel/modbus-data-keys-panel.component.ts index 1a696dd75f..d128e3c246 100644 --- a/ui-ngx/src/app/modules/home/components/widget/lib/gateway/connectors-configuration/modbus/modbus-data-keys-panel/modbus-data-keys-panel.component.ts +++ b/ui-ngx/src/app/modules/home/components/widget/lib/gateway/connectors-configuration/modbus/modbus-data-keys-panel/modbus-data-keys-panel.component.ts @@ -30,6 +30,7 @@ import { CommonModule } from '@angular/common'; import { SharedModule } from '@shared/shared.module'; import { GatewayHelpLinkPipe } from '@home/pipes/gateway-help-link.pipe'; import { generateSecret } from '@core/utils'; +import { coerceBoolean } from '@shared/decorators/coercion'; @Component({ selector: 'tb-modbus-data-keys-panel', @@ -44,6 +45,7 @@ import { generateSecret } from '@core/utils'; }) export class ModbusDataKeysPanelComponent implements OnInit { + @coerceBoolean() @Input() isMaster = false; @Input() panelTitle: string; @Input() addKeyTitle: string; diff --git a/ui-ngx/src/app/modules/home/components/widget/lib/gateway/connectors-configuration/modbus/modbus-security-config/modbus-security-config.component.ts b/ui-ngx/src/app/modules/home/components/widget/lib/gateway/connectors-configuration/modbus/modbus-security-config/modbus-security-config.component.ts index 7efcf095ee..caaff5ea40 100644 --- a/ui-ngx/src/app/modules/home/components/widget/lib/gateway/connectors-configuration/modbus/modbus-security-config/modbus-security-config.component.ts +++ b/ui-ngx/src/app/modules/home/components/widget/lib/gateway/connectors-configuration/modbus/modbus-security-config/modbus-security-config.component.ts @@ -36,6 +36,7 @@ import { takeUntil } from 'rxjs/operators'; import { SecurityConfigComponent } from '@home/components/widget/lib/gateway/connectors-configuration/security-config/security-config.component'; +import { coerceBoolean } from '@shared/decorators/coercion'; @Component({ selector: 'tb-modbus-security-config', @@ -62,8 +63,10 @@ import { }) export class ModbusSecurityConfigComponent implements ControlValueAccessor, Validator, OnChanges, OnDestroy { + @coerceBoolean() @Input() isMaster = false; - @Input() disabled = false; + + disabled = false; securityConfigFormGroup: UntypedFormGroup; @@ -113,8 +116,17 @@ export class ModbusSecurityConfigComponent implements ControlValueAccessor, Vali this.onTouched = fn; } + setDisabledState(isDisabled: boolean): void { + this.disabled = isDisabled; + if (this.disabled) { + this.securityConfigFormGroup.disable({emitEvent: false}); + } else { + this.securityConfigFormGroup.enable({emitEvent: false}); + } + } + validate(): ValidationErrors | null { - return this.securityConfigFormGroup.valid || this.disabled ? null : { + return this.securityConfigFormGroup.valid ? null : { securityConfigFormGroup: { valid: false } }; } diff --git a/ui-ngx/src/app/modules/home/components/widget/lib/gateway/connectors-configuration/modbus/modbus-slave-config/modbus-slave-config.component.html b/ui-ngx/src/app/modules/home/components/widget/lib/gateway/connectors-configuration/modbus/modbus-slave-config/modbus-slave-config.component.html index 41a7d2aee7..0ccbc734bf 100644 --- a/ui-ngx/src/app/modules/home/components/widget/lib/gateway/connectors-configuration/modbus/modbus-slave-config/modbus-slave-config.component.html +++ b/ui-ngx/src/app/modules/home/components/widget/lib/gateway/connectors-configuration/modbus/modbus-slave-config/modbus-slave-config.component.html @@ -204,7 +204,7 @@ - +
@@ -252,6 +252,6 @@
gateway.values
- +
diff --git a/ui-ngx/src/app/modules/home/components/widget/lib/gateway/connectors-configuration/modbus/modbus-slave-config/modbus-slave-config.component.ts b/ui-ngx/src/app/modules/home/components/widget/lib/gateway/connectors-configuration/modbus/modbus-slave-config/modbus-slave-config.component.ts index 13a4eefb8c..1d34914b8b 100644 --- a/ui-ngx/src/app/modules/home/components/widget/lib/gateway/connectors-configuration/modbus/modbus-slave-config/modbus-slave-config.component.ts +++ b/ui-ngx/src/app/modules/home/components/widget/lib/gateway/connectors-configuration/modbus/modbus-slave-config/modbus-slave-config.component.ts @@ -135,12 +135,13 @@ export class ModbusSlaveConfigComponent implements ControlValueAccessor, Validat value.port = value.serialPort; delete value.serialPort; } - this.onChange(this.slaveConfigFormGroup.get('sendDataToThingsBoard').value ? value : {} as SlaveConfig); + this.onChange(value); this.onTouched(); }); this.observeTypeChange(); this.observeFormEnable(); + this.observeShowSecurity(); } ngOnDestroy(): void { @@ -194,6 +195,16 @@ export class ModbusSlaveConfigComponent implements ControlValueAccessor, Validat this.updateEnablingByProtocol(this.slaveConfigFormGroup.get('type').value); } + private observeShowSecurity(): void { + this.showSecurityControl.valueChanges.pipe(takeUntil(this.destroy$)).subscribe(value => { + if (value && this.slaveConfigFormGroup.get('sendDataToThingsBoard').value) { + this.slaveConfigFormGroup.get('security').enable({emitEvent: false}); + } else { + this.slaveConfigFormGroup.get('security').disable({emitEvent: false}); + } + }); + } + private updateEnablingByProtocol(type: ModbusProtocolType): void { if (type === ModbusProtocolType.Serial) { if (this.slaveConfigFormGroup.get('sendDataToThingsBoard').value) { diff --git a/ui-ngx/src/app/modules/home/components/widget/lib/gateway/connectors-configuration/modbus/modbus-values/modbus-values.component.ts b/ui-ngx/src/app/modules/home/components/widget/lib/gateway/connectors-configuration/modbus/modbus-values/modbus-values.component.ts index f3e9a28bce..fee7e72616 100644 --- a/ui-ngx/src/app/modules/home/components/widget/lib/gateway/connectors-configuration/modbus/modbus-values/modbus-values.component.ts +++ b/ui-ngx/src/app/modules/home/components/widget/lib/gateway/connectors-configuration/modbus/modbus-values/modbus-values.component.ts @@ -54,6 +54,7 @@ import { EllipsisChipListDirective } from '@shared/directives/ellipsis-chip-list import { MatButton } from '@angular/material/button'; import { TbPopoverService } from '@shared/components/popover.service'; import { ModbusDataKeysPanelComponent } from '../modbus-data-keys-panel/modbus-data-keys-panel.component'; +import { coerceBoolean } from '@shared/decorators/coercion'; @Component({ selector: 'tb-modbus-values', @@ -88,9 +89,10 @@ import { ModbusDataKeysPanelComponent } from '../modbus-data-keys-panel/modbus-d export class ModbusValuesComponent implements ControlValueAccessor, Validator, OnChanges, OnDestroy { + @coerceBoolean() @Input() singleMode = false; - @Input() disabled = false; + disabled = false; modbusRegisterTypes: ModbusRegisterType[] = Object.values(ModbusRegisterType); modbusValueKeys = Object.values(ModbusValueKey); ModbusValuesTranslationsMap = ModbusRegisterTranslationsMap; @@ -159,6 +161,11 @@ export class ModbusValuesComponent implements ControlValueAccessor, Validator, O }; } + setDisabledState(isDisabled: boolean): void { + this.disabled = isDisabled; + this.cdr.markForCheck(); + } + getValueGroup(valueKey: ModbusValueKey, register?: ModbusRegisterType) { return register ? this.valuesFormGroup.get(register).get(valueKey).value : this.valuesFormGroup.get(valueKey).value; } From 30f57ecd5a12fbc8faeaad5f8d208ed915e59672 Mon Sep 17 00:00:00 2001 From: mpetrov Date: Tue, 23 Jul 2024 11:43:45 +0300 Subject: [PATCH 06/20] added suggestions --- .../modbus-security-config.component.ts | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/ui-ngx/src/app/modules/home/components/widget/lib/gateway/connectors-configuration/modbus/modbus-security-config/modbus-security-config.component.ts b/ui-ngx/src/app/modules/home/components/widget/lib/gateway/connectors-configuration/modbus/modbus-security-config/modbus-security-config.component.ts index caaff5ea40..9c7cad5f83 100644 --- a/ui-ngx/src/app/modules/home/components/widget/lib/gateway/connectors-configuration/modbus/modbus-security-config/modbus-security-config.component.ts +++ b/ui-ngx/src/app/modules/home/components/widget/lib/gateway/connectors-configuration/modbus/modbus-security-config/modbus-security-config.component.ts @@ -96,11 +96,6 @@ export class ModbusSecurityConfigComponent implements ControlValueAccessor, Vali }); this.observeValueChanges(); } - if (this.disabled) { - this.securityConfigFormGroup.disable({emitEvent:false}); - } else { - this.securityConfigFormGroup.enable({emitEvent:false}); - } } ngOnDestroy(): void { @@ -152,7 +147,7 @@ export class ModbusSecurityConfigComponent implements ControlValueAccessor, Vali this.securityConfigFormGroup.valueChanges.pipe( takeUntil(this.destroy$) ).subscribe((value: ModbusSecurity) => { - this.onChange(this.disabled ? {} : value); + this.onChange(value); this.onTouched(); }); } From 2f838c4fbf1b5819f93dea946ab70109129767ad Mon Sep 17 00:00:00 2001 From: nick Date: Tue, 23 Jul 2024 12:37:48 +0300 Subject: [PATCH 07/20] tbel: added some double methods --- .../thingsboard/script/api/tbel/TbUtils.java | 46 +++++++++--- .../script/api/tbel/TbUtilsTest.java | 71 +++++++++++++------ 2 files changed, 88 insertions(+), 29 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 034d6b4686..b1d0ba6dfd 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 @@ -170,7 +170,7 @@ public class TbUtils { byte[].class, int.class, boolean.class))); parserConfig.addImport("parseBytesToFloat", new MethodStub(TbUtils.class.getMethod("parseBytesToFloat", List.class, int.class, boolean.class))); - parserConfig.addImport("parseBytesToFloat", new MethodStub(TbUtils.class.getMethod("parseBytesToFloat", + parserConfig.addImport("parseBytesToFloat", new MethodStub(TbUtils.class.getMethod("parseBytesToFloat", byte[].class, int.class, int.class, boolean.class))); parserConfig.addImport("parseBytesToFloat", new MethodStub(TbUtils.class.getMethod("parseBytesToFloat", List.class, int.class, int.class, boolean.class))); @@ -186,10 +186,14 @@ public class TbUtils { byte[].class, int.class))); parserConfig.addImport("parseBytesToDouble", new MethodStub(TbUtils.class.getMethod("parseBytesToDouble", byte[].class, int.class, boolean.class))); + parserConfig.addImport("parseBytesToDouble", new MethodStub(TbUtils.class.getMethod("parseBytesToDouble", + byte[].class, int.class, int.class, boolean.class))); parserConfig.addImport("parseBytesToDouble", new MethodStub(TbUtils.class.getMethod("parseBytesToDouble", List.class, int.class))); parserConfig.addImport("parseBytesToDouble", new MethodStub(TbUtils.class.getMethod("parseBytesToDouble", List.class, int.class, boolean.class))); + parserConfig.addImport("parseBytesToDouble", new MethodStub(TbUtils.class.getMethod("parseBytesToDouble", + List.class, int.class, int.class, boolean.class))); parserConfig.addImport("toFixed", new MethodStub(TbUtils.class.getMethod("toFixed", double.class, int.class))); parserConfig.addImport("toFixed", new MethodStub(TbUtils.class.getMethod("toFixed", @@ -798,11 +802,11 @@ public class TbUtils { if (offset + length > data.length) { throw new IllegalArgumentException("Offset: " + offset + " and Length: " + length + " is out of bounds for array with length: " + data.length + "!"); } - var bb = ByteBuffer.allocate(8); + var bb = ByteBuffer.allocate(BYTES_LEN_LONG_MAX); if (!bigEndian) { bb.order(ByteOrder.LITTLE_ENDIAN); } - bb.position(bigEndian ? 8 - length : 0); + bb.position(bigEndian ? BYTES_LEN_LONG_MAX - length : 0); bb.put(data, offset, length); bb.position(0); return bb.getLong(); @@ -831,21 +835,21 @@ public class TbUtils { public static float parseBytesToFloat(byte[] data, int offset, int length, boolean bigEndian) { byte[] bytesToNumber = prepareBytesToNumber(data, offset, length, bigEndian); if (length > BYTES_LEN_INT_MAX) { - throw new IllegalArgumentException("Length: " + length + " is too large. Maximum 4 bytes is allowed!"); + throw new IllegalArgumentException("Length: " + length + " is too large. Maximum " + BYTES_LEN_INT_MAX + " bytes is allowed!"); } if (bytesToNumber.length < BYTES_LEN_INT_MAX) { - byte[] extendedBytes = new byte[4]; + byte[] extendedBytes = new byte[BYTES_LEN_INT_MAX]; Arrays.fill(extendedBytes, (byte) 0); System.arraycopy(bytesToNumber, 0, extendedBytes, 0, bytesToNumber.length); bytesToNumber = extendedBytes; } float floatValue = ByteBuffer.wrap(bytesToNumber).getFloat(); - if (!Float.isNaN(floatValue)){ + if (!Float.isNaN(floatValue)) { return floatValue; } else { long longValue = parseBytesToLong(bytesToNumber, offset, length, bigEndian); BigDecimal bigDecimalValue = new BigDecimal(longValue); - return bigDecimalValue.floatValue(); + return bigDecimalValue.floatValue(); } } @@ -861,9 +865,33 @@ public class TbUtils { return parseBytesToDouble(Bytes.toArray(data), offset, bigEndian); } + public static double parseBytesToDouble(List data, int offset, int length, boolean bigEndian) { + return parseBytesToDouble(Bytes.toArray(data), offset, length, bigEndian); + } + public static double parseBytesToDouble(byte[] data, int offset, boolean bigEndian) { - byte[] bytesToNumber = prepareBytesToNumber(data, offset, BYTES_LEN_LONG_MAX, bigEndian); - return ByteBuffer.wrap(bytesToNumber).getDouble(); + return parseBytesToDouble(data, offset, BYTES_LEN_LONG_MAX, bigEndian); + } + + public static double parseBytesToDouble(byte[] data, int offset, int length, boolean bigEndian) { + byte[] bytesToNumber = prepareBytesToNumber(data, offset, length, bigEndian); + if (length > BYTES_LEN_LONG_MAX) { + throw new IllegalArgumentException("Length: " + length + " is too large. Maximum " + BYTES_LEN_LONG_MAX + " bytes is allowed!"); + } + if (bytesToNumber.length < BYTES_LEN_LONG_MAX) { + byte[] extendedBytes = new byte[BYTES_LEN_LONG_MAX]; + Arrays.fill(extendedBytes, (byte) 0); + System.arraycopy(bytesToNumber, 0, extendedBytes, 0, bytesToNumber.length); + bytesToNumber = extendedBytes; + } + double doubleValue = ByteBuffer.wrap(bytesToNumber).getDouble(); + if (!Double.isNaN(doubleValue)) { + return doubleValue; + } else { + BigInteger bigInt = new BigInteger(1, bytesToNumber); + BigDecimal bigDecimalValue = new BigDecimal(bigInt); + return bigDecimalValue.doubleValue(); + } } private static byte[] prepareBytesToNumber(byte[] data, int offset, int length, boolean bigEndian) { 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 1df1fc93cc..e0eb90802c 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 @@ -30,6 +30,7 @@ import org.mvel2.execution.ExecutionArrayList; import org.mvel2.execution.ExecutionHashMap; import java.io.IOException; +import java.math.BigDecimal; import java.math.BigInteger; import java.nio.ByteBuffer; import java.util.ArrayList; @@ -279,36 +280,36 @@ public class TbUtilsTest { 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); - Assertions.assertEquals(0, Float.compare(floatVal, TbUtils.parseBytesToFloat(floatVaList, 0))); - Assertions.assertEquals(0, Float.compare(floatValRev, TbUtils.parseBytesToFloat(floatVaList, 0, false))); + List floatValList = Bytes.asList(floatValByte); + Assertions.assertEquals(0, Float.compare(floatVal, TbUtils.parseBytesToFloat(floatValList, 0))); + Assertions.assertEquals(0, Float.compare(floatValRev, TbUtils.parseBytesToFloat(floatValList, 0, false))); // 4 294 967 295L == {0xFF, 0xFF, 0xFF, 0xFF} - floatValByte = new byte[] {-1, -1, -1, -1}; + floatValByte = new byte[]{-1, -1, -1, -1}; float floatExpectedBe = 4294.9673f; float floatExpectedLe = 4.2949673E9f; float actualBe = TbUtils.parseBytesToFloat(floatValByte, 0, 4, true); - Assertions.assertEquals(0, Float.compare(floatExpectedBe, actualBe/1000000)); + Assertions.assertEquals(0, Float.compare(floatExpectedBe, actualBe / 1000000)); Assertions.assertEquals(0, Float.compare(floatExpectedLe, TbUtils.parseBytesToFloat(floatValByte, 0, false))); - floatVaList = Bytes.asList(floatValByte); - actualBe = TbUtils.parseBytesToFloat(floatVaList, 0); - Assertions.assertEquals(0, Float.compare(floatExpectedBe, actualBe/1000000)); - Assertions.assertEquals(0, Float.compare(floatExpectedLe, TbUtils.parseBytesToFloat(floatVaList, 0, false))); + floatValList = Bytes.asList(floatValByte); + actualBe = TbUtils.parseBytesToFloat(floatValList, 0); + Assertions.assertEquals(0, Float.compare(floatExpectedBe, actualBe / 1000000)); + Assertions.assertEquals(0, Float.compare(floatExpectedLe, TbUtils.parseBytesToFloat(floatValList, 0, false))); // 2 143 289 344L == {0x7F, 0xC0, 0x00, 0x00} - floatValByte = new byte[] {0x7F, (byte) 0xC0, (byte) 0xFF, 0x00}; + floatValByte = new byte[]{0x7F, (byte) 0xC0, (byte) 0xFF, 0x00}; floatExpectedBe = 2143.3547f; floatExpectedLe = -3.984375f; actualBe = TbUtils.parseBytesToFloat(floatValByte, 0, 4, true); - Assertions.assertEquals(0, Float.compare(floatExpectedBe, actualBe/1000000)); - Assertions.assertEquals(0, Float.compare(floatExpectedLe, TbUtils.parseBytesToFloat(floatValByte, 0, 2,false))); + Assertions.assertEquals(0, Float.compare(floatExpectedBe, actualBe / 1000000)); + Assertions.assertEquals(0, Float.compare(floatExpectedLe, TbUtils.parseBytesToFloat(floatValByte, 0, 2, false))); - floatVaList = Bytes.asList(floatValByte); + floatValList = Bytes.asList(floatValByte); floatExpectedLe = 8372479.0f; - actualBe = TbUtils.parseBytesToFloat(floatVaList, 0); - Assertions.assertEquals(0, Float.compare(floatExpectedBe, actualBe/1000000)); - Assertions.assertEquals(0, Float.compare(floatExpectedLe, TbUtils.parseBytesToFloat(floatVaList, 0, 3, false))); + actualBe = TbUtils.parseBytesToFloat(floatValList, 0); + Assertions.assertEquals(0, Float.compare(floatExpectedBe, actualBe / 1000000)); + Assertions.assertEquals(0, Float.compare(floatExpectedLe, TbUtils.parseBytesToFloat(floatValList, 0, 3, false))); } @Test @@ -385,9 +386,38 @@ public class TbUtilsTest { 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); - Assertions.assertEquals(0, Double.compare(doubleVal, TbUtils.parseBytesToDouble(doubleVaList, 0))); - Assertions.assertEquals(0, Double.compare(doubleValRev, TbUtils.parseBytesToDouble(doubleVaList, 0, false))); + List doubleValList = Bytes.asList(doubleValByte); + Assertions.assertEquals(0, Double.compare(doubleVal, TbUtils.parseBytesToDouble(doubleValList, 0))); + Assertions.assertEquals(0, Double.compare(doubleValRev, TbUtils.parseBytesToDouble(doubleValList, 0, false))); + + // 4 294 967 295L == {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF} + doubleValByte = new byte[]{-1, -1, -1, -1, -1, -1, -1, -1}; + double doubleExpectedBe = 18446.744073709553d; + double doubleExpectedLe = 1.8446744073709552E19d; + double actualBe = TbUtils.parseBytesToDouble(doubleValByte, 0, 8, true); + Assertions.assertEquals(0, Double.compare(doubleExpectedBe, actualBe / 1000000000000000L)); + Assertions.assertEquals(0, Double.compare(doubleExpectedLe, TbUtils.parseBytesToDouble(doubleValByte, 0, false))); + + doubleValList = Bytes.asList(doubleValByte); + Assertions.assertEquals(0, Double.compare(doubleExpectedBe, TbUtils.parseBytesToDouble(doubleValList, 0) / 1000000000000000L)); + Assertions.assertEquals(0, Double.compare(doubleExpectedLe, TbUtils.parseBytesToDouble(doubleValList, 0, false))); + + doubleValByte = new byte[]{0x7F, (byte) 0xC0, (byte) 0xFF, 0x00, 0x7F, (byte) 0xC0, (byte) 0xFF, 0x00}; + doubleExpectedBe = 2387013.651780523d; + doubleExpectedLe = 7.234601680440024E-304d; + actualBe = TbUtils.parseBytesToDouble(doubleValByte, 0, 8, true); + BigDecimal bigDecimal = new BigDecimal(actualBe); + // We move the decimal point to the left by 301 positions + actualBe = bigDecimal.movePointLeft(301).doubleValue(); + Assertions.assertEquals(0, Double.compare(doubleExpectedBe, actualBe)); + Assertions.assertEquals(0, Double.compare(doubleExpectedLe, TbUtils.parseBytesToDouble(doubleValByte, 0, 8, false))); + doubleValList = Bytes.asList(doubleValByte); + doubleExpectedLe = 5.828674572203954E303d; + actualBe = TbUtils.parseBytesToDouble(doubleValList, 0); + bigDecimal = new BigDecimal(actualBe); + actualBe = bigDecimal.movePointLeft(301).doubleValue(); + Assertions.assertEquals(0, Double.compare(doubleExpectedBe, actualBe)); + Assertions.assertEquals(0, Double.compare(doubleExpectedLe, TbUtils.parseBytesToDouble(doubleValList, 0, 5, false))); } @Test @@ -660,7 +690,8 @@ public class TbUtilsTest { valueActual = TbUtils.parseHexToFloat(valueHexRev, false); Assertions.assertEquals(value, valueActual); } - // If the length is not equal to 8 characters, we process it as an integer (eg "0x0A" for 10.0f). + + // If the length is not equal to 8 characters, we process it as an integer (eg "0x0A" for 10.0f). @Test public void parseHexIntLongToFloat_Test() { Float valueExpected = 10.0f; From c347c067e7468f4b8a248145966a515207e9a4a0 Mon Sep 17 00:00:00 2001 From: nick Date: Tue, 23 Jul 2024 16:28:17 +0300 Subject: [PATCH 08/20] tbel: added some parseBytesTo Int/Long/Float/Double methods with length --- .../thingsboard/script/api/tbel/TbUtils.java | 154 +++++++++++++----- .../script/api/tbel/TbUtilsTest.java | 26 ++- 2 files changed, 129 insertions(+), 51 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 b1d0ba6dfd..9286ffec46 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 @@ -130,10 +130,18 @@ public class TbUtils { String.class))); parserConfig.addImport("parseHexToInt", new MethodStub(TbUtils.class.getMethod("parseHexToInt", String.class, boolean.class))); + parserConfig.addImport("parseBytesToInt", new MethodStub(TbUtils.class.getMethod("parseBytesToInt", + List.class))); + parserConfig.addImport("parseBytesToInt", new MethodStub(TbUtils.class.getMethod("parseBytesToInt", + List.class, int.class))); parserConfig.addImport("parseBytesToInt", new MethodStub(TbUtils.class.getMethod("parseBytesToInt", List.class, int.class, int.class))); parserConfig.addImport("parseBytesToInt", new MethodStub(TbUtils.class.getMethod("parseBytesToInt", List.class, int.class, int.class, boolean.class))); + parserConfig.addImport("parseBytesToInt", new MethodStub(TbUtils.class.getMethod("parseBytesToInt", + byte[].class))); + parserConfig.addImport("parseBytesToInt", new MethodStub(TbUtils.class.getMethod("parseBytesToInt", + byte[].class, int.class))); parserConfig.addImport("parseBytesToInt", new MethodStub(TbUtils.class.getMethod("parseBytesToInt", byte[].class, int.class, int.class))); parserConfig.addImport("parseBytesToInt", new MethodStub(TbUtils.class.getMethod("parseBytesToInt", @@ -146,10 +154,18 @@ public class TbUtils { String.class))); parserConfig.addImport("parseHexToLong", new MethodStub(TbUtils.class.getMethod("parseHexToLong", String.class, boolean.class))); + parserConfig.addImport("parseBytesToLong", new MethodStub(TbUtils.class.getMethod("parseBytesToLong", + List.class))); + parserConfig.addImport("parseBytesToLong", new MethodStub(TbUtils.class.getMethod("parseBytesToLong", + List.class, int.class))); parserConfig.addImport("parseBytesToLong", new MethodStub(TbUtils.class.getMethod("parseBytesToLong", List.class, int.class, int.class))); parserConfig.addImport("parseBytesToLong", new MethodStub(TbUtils.class.getMethod("parseBytesToLong", List.class, int.class, int.class, boolean.class))); + parserConfig.addImport("parseBytesToLong", new MethodStub(TbUtils.class.getMethod("parseBytesToLong", + byte[].class))); + parserConfig.addImport("parseBytesToLong", new MethodStub(TbUtils.class.getMethod("parseBytesToLong", + byte[].class, int.class))); parserConfig.addImport("parseBytesToLong", new MethodStub(TbUtils.class.getMethod("parseBytesToLong", byte[].class, int.class, int.class))); parserConfig.addImport("parseBytesToLong", new MethodStub(TbUtils.class.getMethod("parseBytesToLong", @@ -163,17 +179,21 @@ public class TbUtils { parserConfig.addImport("parseHexToFloat", new MethodStub(TbUtils.class.getMethod("parseHexToFloat", String.class, boolean.class))); parserConfig.addImport("parseBytesToFloat", new MethodStub(TbUtils.class.getMethod("parseBytesToFloat", - byte[].class, int.class))); + List.class))); parserConfig.addImport("parseBytesToFloat", new MethodStub(TbUtils.class.getMethod("parseBytesToFloat", List.class, int.class))); parserConfig.addImport("parseBytesToFloat", new MethodStub(TbUtils.class.getMethod("parseBytesToFloat", - byte[].class, int.class, boolean.class))); + List.class, int.class, int.class))); parserConfig.addImport("parseBytesToFloat", new MethodStub(TbUtils.class.getMethod("parseBytesToFloat", - List.class, int.class, boolean.class))); + List.class, int.class, int.class, boolean.class))); parserConfig.addImport("parseBytesToFloat", new MethodStub(TbUtils.class.getMethod("parseBytesToFloat", - byte[].class, int.class, int.class, boolean.class))); + byte[].class))); parserConfig.addImport("parseBytesToFloat", new MethodStub(TbUtils.class.getMethod("parseBytesToFloat", - List.class, int.class, int.class, boolean.class))); + byte[].class, int.class))); + parserConfig.addImport("parseBytesToFloat", new MethodStub(TbUtils.class.getMethod("parseBytesToFloat", + byte[].class, int.class, int.class))); + parserConfig.addImport("parseBytesToFloat", new MethodStub(TbUtils.class.getMethod("parseBytesToFloat", + byte[].class, int.class, int.class, boolean.class))); parserConfig.addImport("parseLittleEndianHexToDouble", new MethodStub(TbUtils.class.getMethod("parseLittleEndianHexToDouble", String.class))); parserConfig.addImport("parseBigEndianHexToDouble", new MethodStub(TbUtils.class.getMethod("parseBigEndianHexToDouble", @@ -183,17 +203,21 @@ public class TbUtils { parserConfig.addImport("parseHexToDouble", new MethodStub(TbUtils.class.getMethod("parseHexToDouble", String.class, boolean.class))); parserConfig.addImport("parseBytesToDouble", new MethodStub(TbUtils.class.getMethod("parseBytesToDouble", - byte[].class, int.class))); - parserConfig.addImport("parseBytesToDouble", new MethodStub(TbUtils.class.getMethod("parseBytesToDouble", - byte[].class, int.class, boolean.class))); - parserConfig.addImport("parseBytesToDouble", new MethodStub(TbUtils.class.getMethod("parseBytesToDouble", - byte[].class, int.class, int.class, boolean.class))); + List.class))); parserConfig.addImport("parseBytesToDouble", new MethodStub(TbUtils.class.getMethod("parseBytesToDouble", List.class, int.class))); parserConfig.addImport("parseBytesToDouble", new MethodStub(TbUtils.class.getMethod("parseBytesToDouble", - List.class, int.class, boolean.class))); + List.class, int.class, int.class))); parserConfig.addImport("parseBytesToDouble", new MethodStub(TbUtils.class.getMethod("parseBytesToDouble", List.class, int.class, int.class, boolean.class))); + parserConfig.addImport("parseBytesToDouble", new MethodStub(TbUtils.class.getMethod("parseBytesToDouble", + byte[].class))); + parserConfig.addImport("parseBytesToDouble", new MethodStub(TbUtils.class.getMethod("parseBytesToDouble", + byte[].class, int.class))); + parserConfig.addImport("parseBytesToDouble", new MethodStub(TbUtils.class.getMethod("parseBytesToDouble", + byte[].class, int.class, int.class))); + parserConfig.addImport("parseBytesToDouble", new MethodStub(TbUtils.class.getMethod("parseBytesToDouble", + byte[].class, int.class, int.class, boolean.class))); parserConfig.addImport("toFixed", new MethodStub(TbUtils.class.getMethod("toFixed", double.class, int.class))); parserConfig.addImport("toFixed", new MethodStub(TbUtils.class.getMethod("toFixed", @@ -740,16 +764,28 @@ public class TbUtils { return Base64.getDecoder().decode(input); } + public static int parseBytesToInt(List data) { + return parseBytesToInt(Bytes.toArray(data)); + } + + public static int parseBytesToInt(List data, int offset) { + return parseBytesToInt(Bytes.toArray(data), offset); + } + public static int parseBytesToInt(List data, int offset, int length) { - return parseBytesToInt(data, offset, length, true); + return parseBytesToInt(Bytes.toArray(data), offset, length); } public static int parseBytesToInt(List data, int offset, int length, boolean bigEndian) { - final byte[] bytes = new byte[data.size()]; - for (int i = 0; i < bytes.length; i++) { - bytes[i] = data.get(i); - } - return parseBytesToInt(bytes, offset, length, bigEndian); + return parseBytesToInt(Bytes.toArray(data), offset, length, bigEndian); + } + + public static int parseBytesToInt(byte[] data) { + return parseBytesToInt(data, 0); + } + + public static int parseBytesToInt(byte[] data, int offset) { + return parseBytesToInt(data, offset, BYTES_LEN_INT_MAX); } public static int parseBytesToInt(byte[] data, int offset, int length) { @@ -776,16 +812,28 @@ public class TbUtils { return bb.getInt(); } + public static long parseBytesToLong(List data) { + return parseBytesToLong(Bytes.toArray(data)); + } + + public static long parseBytesToLong(List data, int offset) { + return parseBytesToLong(Bytes.toArray(data), offset); + } + public static long parseBytesToLong(List data, int offset, int length) { - return parseBytesToLong(data, offset, length, true); + return parseBytesToLong(Bytes.toArray(data), offset, length); } public static long parseBytesToLong(List data, int offset, int length, boolean bigEndian) { - final byte[] bytes = new byte[data.size()]; - for (int i = 0; i < bytes.length; i++) { - bytes[i] = data.get(i); - } - return parseBytesToLong(bytes, offset, length, bigEndian); + return parseBytesToLong(Bytes.toArray(data), offset, length, bigEndian); + } + + public static long parseBytesToLong(byte[] data) { + return parseBytesToLong(data, 0); + } + + public static long parseBytesToLong(byte[] data, int offset) { + return parseBytesToLong(data, offset, BYTES_LEN_LONG_MAX); } public static long parseBytesToLong(byte[] data, int offset, int length) { @@ -812,31 +860,42 @@ public class TbUtils { return bb.getLong(); } - public static float parseBytesToFloat(byte[] data, int offset) { - return parseBytesToFloat(data, offset, true); + public static float parseBytesToFloat(List data) { + return parseBytesToFloat(Bytes.toArray(data), 0); } public static float parseBytesToFloat(List data, int offset) { - return parseBytesToFloat(data, offset, true); + return parseBytesToFloat(Bytes.toArray(data), offset, BYTES_LEN_INT_MAX); } - public static float parseBytesToFloat(List data, int offset, boolean bigEndian) { - return parseBytesToFloat(Bytes.toArray(data), offset, bigEndian); - } - - public static float parseBytesToFloat(byte[] data, int offset, boolean bigEndian) { - return parseBytesToFloat(data, offset, BYTES_LEN_INT_MAX, bigEndian); + public static float parseBytesToFloat(List data, int offset, int length) { + return parseBytesToFloat(Bytes.toArray(data), offset, length, true); } public static float parseBytesToFloat(List data, int offset, int length, boolean bigEndian) { return parseBytesToFloat(Bytes.toArray(data), offset, length, bigEndian); } + public static float parseBytesToFloat(byte[] data) { + return parseBytesToFloat(data, 0); + } + + public static float parseBytesToFloat(byte[] data, int offset) { + return parseBytesToFloat(data, offset, BYTES_LEN_INT_MAX); + } + + public static float parseBytesToFloat(byte[] data, int offset, int length) { + return parseBytesToFloat(data, offset, length, true); + } + public static float parseBytesToFloat(byte[] data, int offset, int length, boolean bigEndian) { - byte[] bytesToNumber = prepareBytesToNumber(data, offset, length, bigEndian); if (length > BYTES_LEN_INT_MAX) { throw new IllegalArgumentException("Length: " + length + " is too large. Maximum " + BYTES_LEN_INT_MAX + " bytes is allowed!"); } + if (offset + length > data.length) { + throw new IllegalArgumentException("Offset: " + offset + " and Length: " + length + " is out of bounds for array with length: " + data.length + "!"); + } + byte[] bytesToNumber = prepareBytesToNumber(data, offset, length, bigEndian); if (bytesToNumber.length < BYTES_LEN_INT_MAX) { byte[] extendedBytes = new byte[BYTES_LEN_INT_MAX]; Arrays.fill(extendedBytes, (byte) 0); @@ -847,37 +906,48 @@ public class TbUtils { if (!Float.isNaN(floatValue)) { return floatValue; } else { - long longValue = parseBytesToLong(bytesToNumber, offset, length, bigEndian); + long longValue = parseBytesToLong(bytesToNumber, 0, BYTES_LEN_INT_MAX); BigDecimal bigDecimalValue = new BigDecimal(longValue); return bigDecimalValue.floatValue(); } } - public static double parseBytesToDouble(byte[] data, int offset) { - return parseBytesToDouble(data, offset, true); + public static double parseBytesToDouble(List data) { + return parseBytesToDouble(Bytes.toArray(data)); } public static double parseBytesToDouble(List data, int offset) { - return parseBytesToDouble(data, offset, true); + return parseBytesToDouble(Bytes.toArray(data), offset); } - public static double parseBytesToDouble(List data, int offset, boolean bigEndian) { - return parseBytesToDouble(Bytes.toArray(data), offset, bigEndian); + public static double parseBytesToDouble(List data, int offset, int length) { + return parseBytesToDouble(Bytes.toArray(data), offset, length); } public static double parseBytesToDouble(List data, int offset, int length, boolean bigEndian) { return parseBytesToDouble(Bytes.toArray(data), offset, length, bigEndian); } - public static double parseBytesToDouble(byte[] data, int offset, boolean bigEndian) { - return parseBytesToDouble(data, offset, BYTES_LEN_LONG_MAX, bigEndian); + public static double parseBytesToDouble(byte[] data) { + return parseBytesToDouble(data, 0); + } + + public static double parseBytesToDouble(byte[] data, int offset) { + return parseBytesToDouble(data, offset, BYTES_LEN_LONG_MAX); + } + + public static double parseBytesToDouble(byte[] data, int offset, int length) { + return parseBytesToDouble(data, offset, length, true); } public static double parseBytesToDouble(byte[] data, int offset, int length, boolean bigEndian) { - byte[] bytesToNumber = prepareBytesToNumber(data, offset, length, bigEndian); if (length > BYTES_LEN_LONG_MAX) { throw new IllegalArgumentException("Length: " + length + " is too large. Maximum " + BYTES_LEN_LONG_MAX + " bytes is allowed!"); } + if (offset + length > data.length) { + throw new IllegalArgumentException("Offset: " + offset + " and Length: " + length + " is out of bounds for array with length: " + data.length + "!"); + } + byte[] bytesToNumber = prepareBytesToNumber(data, offset, length, bigEndian); if (bytesToNumber.length < BYTES_LEN_LONG_MAX) { byte[] extendedBytes = new byte[BYTES_LEN_LONG_MAX]; Arrays.fill(extendedBytes, (byte) 0); 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 e0eb90802c..52bafe2fd2 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 @@ -278,11 +278,11 @@ public class TbUtilsTest { public void parseBytesToFloat() { 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))); + Assertions.assertEquals(0, Float.compare(floatValRev, TbUtils.parseBytesToFloat(floatValByte, 0, 4, false))); List floatValList = Bytes.asList(floatValByte); Assertions.assertEquals(0, Float.compare(floatVal, TbUtils.parseBytesToFloat(floatValList, 0))); - Assertions.assertEquals(0, Float.compare(floatValRev, TbUtils.parseBytesToFloat(floatValList, 0, false))); + Assertions.assertEquals(0, Float.compare(floatValRev, TbUtils.parseBytesToFloat(floatValList, 0, 4, false))); // 4 294 967 295L == {0xFF, 0xFF, 0xFF, 0xFF} floatValByte = new byte[]{-1, -1, -1, -1}; @@ -290,12 +290,12 @@ public class TbUtilsTest { float floatExpectedLe = 4.2949673E9f; float actualBe = TbUtils.parseBytesToFloat(floatValByte, 0, 4, true); Assertions.assertEquals(0, Float.compare(floatExpectedBe, actualBe / 1000000)); - Assertions.assertEquals(0, Float.compare(floatExpectedLe, TbUtils.parseBytesToFloat(floatValByte, 0, false))); + Assertions.assertEquals(0, Float.compare(floatExpectedLe, TbUtils.parseBytesToFloat(floatValByte, 0, 4, false))); floatValList = Bytes.asList(floatValByte); actualBe = TbUtils.parseBytesToFloat(floatValList, 0); Assertions.assertEquals(0, Float.compare(floatExpectedBe, actualBe / 1000000)); - Assertions.assertEquals(0, Float.compare(floatExpectedLe, TbUtils.parseBytesToFloat(floatValList, 0, false))); + Assertions.assertEquals(0, Float.compare(floatExpectedLe, TbUtils.parseBytesToFloat(floatValList, 0, 4, false))); // 2 143 289 344L == {0x7F, 0xC0, 0x00, 0x00} floatValByte = new byte[]{0x7F, (byte) 0xC0, (byte) 0xFF, 0x00}; @@ -306,10 +306,18 @@ public class TbUtilsTest { Assertions.assertEquals(0, Float.compare(floatExpectedLe, TbUtils.parseBytesToFloat(floatValByte, 0, 2, false))); floatValList = Bytes.asList(floatValByte); - floatExpectedLe = 8372479.0f; + floatExpectedLe = 4.2908055E9f; actualBe = TbUtils.parseBytesToFloat(floatValList, 0); Assertions.assertEquals(0, Float.compare(floatExpectedBe, actualBe / 1000000)); Assertions.assertEquals(0, Float.compare(floatExpectedLe, TbUtils.parseBytesToFloat(floatValList, 0, 3, false))); + // "01752B0367FA000500010488 FFFFFFFF FFFFFFFF 33"; + String intToHexBe = "01752B0367FA000500010488FFFFFFFFFFFFFFFF33"; + floatExpectedLe = 4294.9673f; + floatValList = TbUtils.hexToBytes(ctx, intToHexBe); + float actualLe = TbUtils.parseBytesToFloat(floatValList, 12, 4, false); + Assertions.assertEquals(0, Float.compare(floatExpectedLe, actualLe / 1000000)); + actualLe = TbUtils.parseBytesToFloat(floatValList, 12 + 4, 4, false); + Assertions.assertEquals(0, Float.compare(floatExpectedLe, actualLe / 1000000)); } @Test @@ -384,11 +392,11 @@ public class TbUtilsTest { 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))); + Assertions.assertEquals(0, Double.compare(doubleValRev, TbUtils.parseBytesToDouble(doubleValByte, 0, 8, false))); List doubleValList = Bytes.asList(doubleValByte); Assertions.assertEquals(0, Double.compare(doubleVal, TbUtils.parseBytesToDouble(doubleValList, 0))); - Assertions.assertEquals(0, Double.compare(doubleValRev, TbUtils.parseBytesToDouble(doubleValList, 0, false))); + Assertions.assertEquals(0, Double.compare(doubleValRev, TbUtils.parseBytesToDouble(doubleValList, 0, 8, false))); // 4 294 967 295L == {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF} doubleValByte = new byte[]{-1, -1, -1, -1, -1, -1, -1, -1}; @@ -396,11 +404,11 @@ public class TbUtilsTest { double doubleExpectedLe = 1.8446744073709552E19d; double actualBe = TbUtils.parseBytesToDouble(doubleValByte, 0, 8, true); Assertions.assertEquals(0, Double.compare(doubleExpectedBe, actualBe / 1000000000000000L)); - Assertions.assertEquals(0, Double.compare(doubleExpectedLe, TbUtils.parseBytesToDouble(doubleValByte, 0, false))); + Assertions.assertEquals(0, Double.compare(doubleExpectedLe, TbUtils.parseBytesToDouble(doubleValByte, 0, 8, false))); doubleValList = Bytes.asList(doubleValByte); Assertions.assertEquals(0, Double.compare(doubleExpectedBe, TbUtils.parseBytesToDouble(doubleValList, 0) / 1000000000000000L)); - Assertions.assertEquals(0, Double.compare(doubleExpectedLe, TbUtils.parseBytesToDouble(doubleValList, 0, false))); + Assertions.assertEquals(0, Double.compare(doubleExpectedLe, TbUtils.parseBytesToDouble(doubleValList, 0, 8, false))); doubleValByte = new byte[]{0x7F, (byte) 0xC0, (byte) 0xFF, 0x00, 0x7F, (byte) 0xC0, (byte) 0xFF, 0x00}; doubleExpectedBe = 2387013.651780523d; From 3a9a654fd298c77c7445c47edc35892d968caabb Mon Sep 17 00:00:00 2001 From: mpetrov Date: Tue, 23 Jul 2024 17:59:18 +0300 Subject: [PATCH 09/20] functionality adjustments --- .../modbus-basic-config.component.ts | 7 + .../modbus-slave-config.component.html | 408 +++++++++--------- .../modbus-slave-config.component.scss | 18 + .../modbus-slave-config.component.ts | 27 +- .../modbus-slave-dialog.component.html | 6 +- .../modbus-slave-dialog.component.ts | 22 +- .../modbus-values/modbus-values.component.ts | 4 + .../lib/gateway/gateway-widget.models.ts | 10 +- .../assets/locale/locale.constant-en_US.json | 14 +- 9 files changed, 277 insertions(+), 239 deletions(-) create mode 100644 ui-ngx/src/app/modules/home/components/widget/lib/gateway/connectors-configuration/modbus/modbus-slave-config/modbus-slave-config.component.scss diff --git a/ui-ngx/src/app/modules/home/components/widget/lib/gateway/connectors-configuration/modbus/modbus-basic-config/modbus-basic-config.component.ts b/ui-ngx/src/app/modules/home/components/widget/lib/gateway/connectors-configuration/modbus/modbus-basic-config/modbus-basic-config.component.ts index 445cb39983..35e0ee6e61 100644 --- a/ui-ngx/src/app/modules/home/components/widget/lib/gateway/connectors-configuration/modbus/modbus-basic-config/modbus-basic-config.component.ts +++ b/ui-ngx/src/app/modules/home/components/widget/lib/gateway/connectors-configuration/modbus/modbus-basic-config/modbus-basic-config.component.ts @@ -62,6 +62,13 @@ import { ModbusMasterTableComponent } from '../modbus-master-table/modbus-master :host { height: 100%; } + + :host ::ng-deep { + .mat-mdc-tab-body-content { + overflow: hidden !important; + } + } + :host ::ng-deep { .mat-mdc-tab-group, .mat-mdc-tab-body-wrapper { height: 100%; diff --git a/ui-ngx/src/app/modules/home/components/widget/lib/gateway/connectors-configuration/modbus/modbus-slave-config/modbus-slave-config.component.html b/ui-ngx/src/app/modules/home/components/widget/lib/gateway/connectors-configuration/modbus/modbus-slave-config/modbus-slave-config.component.html index 0ccbc734bf..1c1b55e6c2 100644 --- a/ui-ngx/src/app/modules/home/components/widget/lib/gateway/connectors-configuration/modbus/modbus-slave-config/modbus-slave-config.component.html +++ b/ui-ngx/src/app/modules/home/components/widget/lib/gateway/connectors-configuration/modbus/modbus-slave-config/modbus-slave-config.component.html @@ -15,243 +15,251 @@ limitations under the License. --> -
-
{{ 'gateway.hints.modbus-server' | translate }}
-
- - - {{ 'gateway.enable' | translate }} - - -
-
-
gateway.server-slave-config
- - {{ ModbusProtocolLabelsMap.get(type) }} - -
+
-
-
gateway.host
+
{{ 'gateway.hints.modbus-server' | translate }}
+
+ + + {{ 'gateway.enable' | translate }} + + +
+
+
+
+
gateway.server-slave-config
+ + {{ ModbusProtocolLabelsMap.get(type) }} + +
+
+
+
gateway.host
+
+ + + + warning + + +
+
+
+
gateway.port
+
+ + + + warning + + +
+
+ +
+
gateway.port
+
+ + + + warning + + +
+
+
+
+
+ gateway.method +
+
+ + + {{ ModbusMethodLabelsMap.get(method) }} + + +
+
+
+
+
gateway.unit-id
- + warning
-
-
gateway.port
+
+
gateway.device-name
- + warning
- -
-
gateway.port
-
- - - - warning - - -
+
+
gateway.device-profile
+
+ + + + warning + +
- +
-
- gateway.method +
gateway.poll-period
+
+ + +
+
+
+
gateway.baudrate
- - {{ ModbusMethodLabelsMap.get(method) }} + + {{ rate }}
-
-
-
gateway.unit-id
-
- - - - warning - - -
-
-
-
gateway.device-name
-
- - - - warning - - -
-
-
-
gateway.device-profile
-
- - - - warning - - -
-
-
-
gateway.poll-period
-
- - - -
-
-
-
gateway.baudrate
-
- - - -
-
- - - -
gateway.advanced-connection-settings
-
-
-
-
-
gateway.byte-order
-
- - - {{ order }} - - -
-
- - +
+ + - - - {{ 'gateway.tls-connection' | translate }} - - +
gateway.advanced-connection-settings
- -
- -
-
gateway.vendor-name
-
- - - -
-
-
-
gateway.product-code
-
- - - -
-
-
-
gateway.vendor-url
-
- - - -
-
-
-
gateway.product-name
-
- - - +
+
+
gateway.byte-order
+
+ + + {{ order }} + + +
+ + + + + + {{ 'gateway.tls-connection' | translate }} + + + + + + + +
+
gateway.vendor-name
+
+ + + +
+
+
+
gateway.product-code
+
+ + + +
+
+
+
gateway.vendor-url
+
+ + + +
+
+
+
gateway.product-name
+
+ + + +
+
+
+
gateway.model-name
+
+ + + +
+
+
-
-
gateway.model-name
-
- - - -
-
- + +
+
+
gateway.values
+
- -
-
gateway.values
-
diff --git a/ui-ngx/src/app/modules/home/components/widget/lib/gateway/connectors-configuration/modbus/modbus-slave-config/modbus-slave-config.component.scss b/ui-ngx/src/app/modules/home/components/widget/lib/gateway/connectors-configuration/modbus/modbus-slave-config/modbus-slave-config.component.scss new file mode 100644 index 0000000000..f41241f63a --- /dev/null +++ b/ui-ngx/src/app/modules/home/components/widget/lib/gateway/connectors-configuration/modbus/modbus-slave-config/modbus-slave-config.component.scss @@ -0,0 +1,18 @@ +$server-config-header-height: 132px; + +:host { + .nested-expansion-header { + ::ng-deep .mat-content { + height: 100%; + } + } + + .slave-content { + height: calc(100% - #{$server-config-header-height}); + overflow: auto; + } + + .slave-container { + display: inherit; + } +} diff --git a/ui-ngx/src/app/modules/home/components/widget/lib/gateway/connectors-configuration/modbus/modbus-slave-config/modbus-slave-config.component.ts b/ui-ngx/src/app/modules/home/components/widget/lib/gateway/connectors-configuration/modbus/modbus-slave-config/modbus-slave-config.component.ts index 1d34914b8b..33a18ebbb7 100644 --- a/ui-ngx/src/app/modules/home/components/widget/lib/gateway/connectors-configuration/modbus/modbus-slave-config/modbus-slave-config.component.ts +++ b/ui-ngx/src/app/modules/home/components/widget/lib/gateway/connectors-configuration/modbus/modbus-slave-config/modbus-slave-config.component.ts @@ -27,6 +27,7 @@ import { Validators, } from '@angular/forms'; import { + ModbusBaudrates, ModbusMethodLabelsMap, ModbusMethodType, ModbusOrderType, @@ -71,15 +72,7 @@ import { ModbusValuesComponent, } from '../modbus-values/modbus-values.component ModbusSecurityConfigComponent, GatewayPortTooltipPipe, ], - styles: [` - :host { - .nested-expansion-header { - .mat-content { - height: 100%; - } - } - } - `], + styleUrls: ['modbus-slave-config.component.scss'], }) export class ModbusSlaveConfigComponent implements ControlValueAccessor, Validator, OnDestroy { @@ -94,6 +87,7 @@ export class ModbusSlaveConfigComponent implements ControlValueAccessor, Validat readonly modbusSerialMethodTypes = Object.values(ModbusSerialMethodType); readonly modbusOrderType = Object.values(ModbusOrderType); readonly ModbusProtocolType = ModbusProtocolType; + readonly modbusBaudrates = ModbusBaudrates; readonly serialSpecificControlKeys = ['serialPort', 'baudrate']; readonly tcpUdpSpecificControlKeys = ['port', 'security', 'host']; @@ -110,11 +104,11 @@ export class ModbusSlaveConfigComponent implements ControlValueAccessor, Validat port: [null, [Validators.required, Validators.min(PortLimits.MIN), Validators.max(PortLimits.MAX)]], serialPort: ['', [Validators.required, Validators.pattern(noLeadTrailSpacesRegex)]], method: [ModbusMethodType.RTU, []], - unitId: [null, [Validators.required]], - baudrate: [null, []], + unitId: [0, [Validators.required]], + baudrate: [this.ModbusProtocolType[0], []], deviceName: ['', [Validators.required, Validators.pattern(noLeadTrailSpacesRegex)]], deviceType: ['', [Validators.required, Validators.pattern(noLeadTrailSpacesRegex)]], - pollPeriod: [null, []], + pollPeriod: [5000, []], sendDataToThingsBoard: [false, []], byteOrder:[ModbusOrderType.BIG, []], security: [], @@ -240,10 +234,10 @@ export class ModbusSlaveConfigComponent implements ControlValueAccessor, Validat host: host ?? '', type: type ?? ModbusProtocolType.TCP, method: method ?? ModbusMethodType.RTU, - unitId: unitId ?? null, + unitId: unitId ?? 0, deviceName: deviceName ?? '', deviceType: deviceType ?? '', - pollPeriod: pollPeriod ?? null, + pollPeriod: pollPeriod ?? 5000, sendDataToThingsBoard: !!sendDataToThingsBoard, byteOrder: byteOrder ?? ModbusOrderType.BIG, security: security ?? {}, @@ -256,11 +250,12 @@ export class ModbusSlaveConfigComponent implements ControlValueAccessor, Validat }, values: values ?? {} as ModbusRegisterValues, port: port ?? null, + baudrate: baudrate ?? this.modbusBaudrates[0], }; if (slaveConfig.type === ModbusProtocolType.Serial) { - slaveState = { ...slaveState, baudrate, serialPort: port, host: '', port: null } as ModbusSlave; + slaveState = { ...slaveState, serialPort: port, host: '', port: null } as ModbusSlave; } else { - slaveState = { ...slaveState, serialPort: '', baudrate: null } as ModbusSlave; + slaveState = { ...slaveState, serialPort: '' } as ModbusSlave; } this.slaveConfigFormGroup.setValue(slaveState, {emitEvent: false}); } diff --git a/ui-ngx/src/app/modules/home/components/widget/lib/gateway/connectors-configuration/modbus/modbus-slave-dialog/modbus-slave-dialog.component.html b/ui-ngx/src/app/modules/home/components/widget/lib/gateway/connectors-configuration/modbus/modbus-slave-dialog/modbus-slave-dialog.component.html index d3c06aeb4b..026d1eef32 100644 --- a/ui-ngx/src/app/modules/home/components/widget/lib/gateway/connectors-configuration/modbus/modbus-slave-dialog/modbus-slave-dialog.component.html +++ b/ui-ngx/src/app/modules/home/components/widget/lib/gateway/connectors-configuration/modbus/modbus-slave-dialog/modbus-slave-dialog.component.html @@ -136,7 +136,9 @@
gateway.baudrate
- + + {{ rate }} +
@@ -162,7 +164,7 @@
gateway.parity
- + {{ ModbusParityLabelsMap.get(parity) }} diff --git a/ui-ngx/src/app/modules/home/components/widget/lib/gateway/connectors-configuration/modbus/modbus-slave-dialog/modbus-slave-dialog.component.ts b/ui-ngx/src/app/modules/home/components/widget/lib/gateway/connectors-configuration/modbus/modbus-slave-dialog/modbus-slave-dialog.component.ts index 941f6d49ee..8f78372e0f 100644 --- a/ui-ngx/src/app/modules/home/components/widget/lib/gateway/connectors-configuration/modbus/modbus-slave-dialog/modbus-slave-dialog.component.ts +++ b/ui-ngx/src/app/modules/home/components/widget/lib/gateway/connectors-configuration/modbus/modbus-slave-dialog/modbus-slave-dialog.component.ts @@ -25,6 +25,7 @@ import { } from '@angular/forms'; import { MappingInfo, + ModbusBaudrates, ModbusByteSizes, ModbusMethodLabelsMap, ModbusMethodType, @@ -100,6 +101,7 @@ export class ModbusSlaveDialogComponent extends DialogComponent( [ - [ModbusRegisterType.HoldingRegister, 'gateway.holding_registers'], + [ModbusRegisterType.HoldingRegisters, 'gateway.holding_registers'], [ModbusRegisterType.CoilsInitializer, 'gateway.coils_initializer'], - [ModbusRegisterType.InputRegister, 'gateway.input_registers'], + [ModbusRegisterType.InputRegisters, 'gateway.input_registers'], [ModbusRegisterType.DiscreteInputs, 'gateway.discrete_inputs'] ] ); @@ -1023,3 +1023,5 @@ export interface ModbusIdentity { productName?: string; modelName?: string; } + +export const ModbusBaudrates = [4800, 9600, 19200, 38400, 57600, 115200, 230400, 460800, 921600]; diff --git a/ui-ngx/src/assets/locale/locale.constant-en_US.json b/ui-ngx/src/assets/locale/locale.constant-en_US.json index 15583665b7..fb8f695619 100644 --- a/ui-ngx/src/assets/locale/locale.constant-en_US.json +++ b/ui-ngx/src/assets/locale/locale.constant-en_US.json @@ -2772,7 +2772,7 @@ "delete-value": "Delete value", "delete-rpc-method": "Delete method", "delete-rpc-request": "Delete request", - "delete-attribute-update": "Add attribute update", + "delete-attribute-update": "Delete attribute update", "advanced": "Advanced", "advanced-connection-settings": "Advanced connection settings", "attributes": "Attributes", @@ -2821,7 +2821,7 @@ "connectors-table-key": "Key", "connectors-table-class": "Class", "connection-timeout": "Connection timeout (s)", - "connect-attempt-time": "Connect attempt time (s)", + "connect-attempt-time": "Connect attempt time (ms)", "connect-attempt-count": "Connect attempt count", "copy-username": "Copy username", "copy-password": "Copy password", @@ -2954,10 +2954,10 @@ "unit-id": "Unit ID", "host": "Host", "host-required": "Host is required.", - "holding_registers": "Holding register", + "holding_registers": "Holding registers", "coils_initializer": "Coils initializer", - "input_registers": "Input register", - "discrete_inputs": "Discrete input", + "input_registers": "Input registers", + "discrete_inputs": "Discrete inputs", "json-parse": "Not valid JSON.", "json-required": "Field cannot be empty.", "JSONPath-hint": "This field supports constants and JSONPath expressions.", @@ -3019,7 +3019,7 @@ "password": "Password", "password-required": "Password is required.", "permit-without-calls": "Keep alive permit without calls", - "poll-period": "Poll period (s)", + "poll-period": "Poll period (ms)", "port": "Port", "port-required": "Port is required.", "port-limits-error": "Port should be number from {{min}} to {{max}}.", @@ -3307,7 +3307,7 @@ "exactly-once": "2 - Exactly once" }, "objects-count": "Objects count", - "wait-after-failed-attempts": "Wait after failed attempts (s)", + "wait-after-failed-attempts": "Wait after failed attempts (ms)", "tls-path-private-key": "Path to private key on gateway", "toggle-fullscreen": "Toggle fullscreen", "transformer-json-config": "Configuration JSON*", From 2382badc121751f0cff695079e1fc87ea4f812a7 Mon Sep 17 00:00:00 2001 From: mpetrov Date: Tue, 23 Jul 2024 18:03:32 +0300 Subject: [PATCH 10/20] added license --- .../modbus-slave-config.component.scss | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/ui-ngx/src/app/modules/home/components/widget/lib/gateway/connectors-configuration/modbus/modbus-slave-config/modbus-slave-config.component.scss b/ui-ngx/src/app/modules/home/components/widget/lib/gateway/connectors-configuration/modbus/modbus-slave-config/modbus-slave-config.component.scss index f41241f63a..f3b39e3508 100644 --- a/ui-ngx/src/app/modules/home/components/widget/lib/gateway/connectors-configuration/modbus/modbus-slave-config/modbus-slave-config.component.scss +++ b/ui-ngx/src/app/modules/home/components/widget/lib/gateway/connectors-configuration/modbus/modbus-slave-config/modbus-slave-config.component.scss @@ -1,3 +1,18 @@ +/** + * 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. + */ $server-config-header-height: 132px; :host { From 4d264d30a24833974658415dc52af2329f5a352d Mon Sep 17 00:00:00 2001 From: mpetrov Date: Tue, 23 Jul 2024 19:33:56 +0300 Subject: [PATCH 11/20] security for slaves disable state fix --- .../modbus-security-config.component.ts | 13 +++++++++++-- .../modbus-slave-dialog.component.html | 2 +- .../modbus-slave-dialog.component.ts | 11 +++++++++++ 3 files changed, 23 insertions(+), 3 deletions(-) diff --git a/ui-ngx/src/app/modules/home/components/widget/lib/gateway/connectors-configuration/modbus/modbus-security-config/modbus-security-config.component.ts b/ui-ngx/src/app/modules/home/components/widget/lib/gateway/connectors-configuration/modbus/modbus-security-config/modbus-security-config.component.ts index 9c7cad5f83..362edbee29 100644 --- a/ui-ngx/src/app/modules/home/components/widget/lib/gateway/connectors-configuration/modbus/modbus-security-config/modbus-security-config.component.ts +++ b/ui-ngx/src/app/modules/home/components/widget/lib/gateway/connectors-configuration/modbus/modbus-security-config/modbus-security-config.component.ts @@ -14,7 +14,15 @@ /// limitations under the License. /// -import { ChangeDetectionStrategy, Component, forwardRef, Input, OnChanges, OnDestroy } from '@angular/core'; +import { + ChangeDetectionStrategy, + ChangeDetectorRef, + Component, + forwardRef, + Input, + OnChanges, + OnDestroy +} from '@angular/core'; import { ControlValueAccessor, FormBuilder, @@ -75,7 +83,7 @@ export class ModbusSecurityConfigComponent implements ControlValueAccessor, Vali private destroy$ = new Subject(); - constructor(private fb: FormBuilder) { + constructor(private fb: FormBuilder, private cdr: ChangeDetectorRef) { this.securityConfigFormGroup = this.fb.group({ certfile: ['', [Validators.pattern(noLeadTrailSpacesRegex)]], keyfile: ['', [Validators.pattern(noLeadTrailSpacesRegex)]], @@ -118,6 +126,7 @@ export class ModbusSecurityConfigComponent implements ControlValueAccessor, Vali } else { this.securityConfigFormGroup.enable({emitEvent: false}); } + this.cdr.markForCheck(); } validate(): ValidationErrors | null { diff --git a/ui-ngx/src/app/modules/home/components/widget/lib/gateway/connectors-configuration/modbus/modbus-slave-dialog/modbus-slave-dialog.component.html b/ui-ngx/src/app/modules/home/components/widget/lib/gateway/connectors-configuration/modbus/modbus-slave-dialog/modbus-slave-dialog.component.html index 026d1eef32..caa1af4029 100644 --- a/ui-ngx/src/app/modules/home/components/widget/lib/gateway/connectors-configuration/modbus/modbus-slave-dialog/modbus-slave-dialog.component.html +++ b/ui-ngx/src/app/modules/home/components/widget/lib/gateway/connectors-configuration/modbus/modbus-slave-dialog/modbus-slave-dialog.component.html @@ -281,7 +281,7 @@ - +
diff --git a/ui-ngx/src/app/modules/home/components/widget/lib/gateway/connectors-configuration/modbus/modbus-slave-dialog/modbus-slave-dialog.component.ts b/ui-ngx/src/app/modules/home/components/widget/lib/gateway/connectors-configuration/modbus/modbus-slave-dialog/modbus-slave-dialog.component.ts index 8f78372e0f..40a4459221 100644 --- a/ui-ngx/src/app/modules/home/components/widget/lib/gateway/connectors-configuration/modbus/modbus-slave-dialog/modbus-slave-dialog.component.ts +++ b/ui-ngx/src/app/modules/home/components/widget/lib/gateway/connectors-configuration/modbus/modbus-slave-dialog/modbus-slave-dialog.component.ts @@ -172,6 +172,7 @@ export class ModbusSlaveDialogComponent extends DialogComponent this.slaveConfigFormGroup.get(key)?.enable({emitEvent: false})); } }; + + private observeShowSecurity(): void { + this.showSecurityControl.valueChanges.pipe(takeUntil(this.destroy$)).subscribe(value => { + if (value) { + this.slaveConfigFormGroup.get('security').enable({emitEvent: false}); + } else { + this.slaveConfigFormGroup.get('security').disable({emitEvent: false}); + } + }); + } } From 38f07984c41d55a1f31a09183638531a3211a55a Mon Sep 17 00:00:00 2001 From: mpetrov Date: Wed, 24 Jul 2024 15:53:22 +0300 Subject: [PATCH 12/20] functionality fixes --- .../broker-config-control.component.ts | 4 +- .../modbus-master-table.component.html | 2 +- .../modbus-master-table.component.ts | 6 +- .../modbus-security-config.component.ts | 40 ++-- .../modbus-slave-config.component.html | 31 +-- .../modbus-slave-config.component.ts | 20 +- .../modbus-slave-dialog.component.html | 192 +++++++++--------- .../modbus-slave-dialog.component.ts | 10 +- .../modbus-values/modbus-values.component.ts | 43 ++-- .../lib/gateway/gateway-widget.models.ts | 14 +- 10 files changed, 187 insertions(+), 175 deletions(-) diff --git a/ui-ngx/src/app/modules/home/components/widget/lib/gateway/connectors-configuration/broker-config-control/broker-config-control.component.ts b/ui-ngx/src/app/modules/home/components/widget/lib/gateway/connectors-configuration/broker-config-control/broker-config-control.component.ts index 031388b502..da67296869 100644 --- a/ui-ngx/src/app/modules/home/components/widget/lib/gateway/connectors-configuration/broker-config-control/broker-config-control.component.ts +++ b/ui-ngx/src/app/modules/home/components/widget/lib/gateway/connectors-configuration/broker-config-control/broker-config-control.component.ts @@ -37,9 +37,7 @@ import { TranslateService } from '@ngx-translate/core'; import { generateSecret } from '@core/utils'; import { Subject } from 'rxjs'; import { GatewayPortTooltipPipe } from '@home/pipes/gateway-port-tooltip.pipe'; -import { - SecurityConfigComponent -} from '@home/components/widget/lib/gateway/connectors-configuration/security-config/security-config.component'; +import { SecurityConfigComponent } from '../security-config/security-config.component'; @Component({ selector: 'tb-broker-config-control', diff --git a/ui-ngx/src/app/modules/home/components/widget/lib/gateway/connectors-configuration/modbus/modbus-master-table/modbus-master-table.component.html b/ui-ngx/src/app/modules/home/components/widget/lib/gateway/connectors-configuration/modbus/modbus-master-table/modbus-master-table.component.html index ac5e6f3108..2e92d80c9f 100644 --- a/ui-ngx/src/app/modules/home/components/widget/lib/gateway/connectors-configuration/modbus/modbus-master-table/modbus-master-table.component.html +++ b/ui-ngx/src/app/modules/home/components/widget/lib/gateway/connectors-configuration/modbus/modbus-master-table/modbus-master-table.component.html @@ -72,7 +72,7 @@ {{ 'gateway.client-communication-type' | translate }} - {{ ModbusClientTypeLabelsMap.get(mapping['type']) }} + {{ ModbusProtocolLabelsMap.get(mapping['type']) }} diff --git a/ui-ngx/src/app/modules/home/components/widget/lib/gateway/connectors-configuration/modbus/modbus-master-table/modbus-master-table.component.ts b/ui-ngx/src/app/modules/home/components/widget/lib/gateway/connectors-configuration/modbus/modbus-master-table/modbus-master-table.component.ts index 6644fbfd5e..10b1e5bbbe 100644 --- a/ui-ngx/src/app/modules/home/components/widget/lib/gateway/connectors-configuration/modbus/modbus-master-table/modbus-master-table.component.ts +++ b/ui-ngx/src/app/modules/home/components/widget/lib/gateway/connectors-configuration/modbus/modbus-master-table/modbus-master-table.component.ts @@ -40,7 +40,9 @@ import { Validator, } from '@angular/forms'; import { - ModbusClientTypeLabelsMap, ModbusMasterConfig, SlaveConfig, + ModbusMasterConfig, + ModbusProtocolLabelsMap, + SlaveConfig } from '@home/components/widget/lib/gateway/gateway-widget.models'; import { isDefinedAndNotNull, isUndefinedOrNull } from '@core/utils'; import { SharedModule } from '@shared/shared.module'; @@ -80,7 +82,7 @@ export class ModbusMasterTableComponent implements ControlValueAccessor, Validat masterFormGroup: UntypedFormGroup; textSearch = this.fb.control('', {nonNullable: true}); - readonly ModbusClientTypeLabelsMap = ModbusClientTypeLabelsMap; + readonly ModbusProtocolLabelsMap = ModbusProtocolLabelsMap; private onChange: (value: string) => void = () => {}; private onTouched: () => void = () => {}; diff --git a/ui-ngx/src/app/modules/home/components/widget/lib/gateway/connectors-configuration/modbus/modbus-security-config/modbus-security-config.component.ts b/ui-ngx/src/app/modules/home/components/widget/lib/gateway/connectors-configuration/modbus/modbus-security-config/modbus-security-config.component.ts index 362edbee29..75328d1264 100644 --- a/ui-ngx/src/app/modules/home/components/widget/lib/gateway/connectors-configuration/modbus/modbus-security-config/modbus-security-config.component.ts +++ b/ui-ngx/src/app/modules/home/components/widget/lib/gateway/connectors-configuration/modbus/modbus-security-config/modbus-security-config.component.ts @@ -41,9 +41,6 @@ import { SharedModule } from '@shared/shared.module'; import { CommonModule } from '@angular/common'; import { Subject } from 'rxjs'; import { takeUntil } from 'rxjs/operators'; -import { - SecurityConfigComponent -} from '@home/components/widget/lib/gateway/connectors-configuration/security-config/security-config.component'; import { coerceBoolean } from '@shared/decorators/coercion'; @Component({ @@ -66,7 +63,6 @@ import { coerceBoolean } from '@shared/decorators/coercion'; imports: [ CommonModule, SharedModule, - SecurityConfigComponent, ] }) export class ModbusSecurityConfigComponent implements ControlValueAccessor, Validator, OnChanges, OnDestroy { @@ -74,10 +70,10 @@ export class ModbusSecurityConfigComponent implements ControlValueAccessor, Vali @coerceBoolean() @Input() isMaster = false; - disabled = false; - securityConfigFormGroup: UntypedFormGroup; + private disabled = false; + private onChange: (value: ModbusSecurity) => void; private onTouched: () => void; @@ -89,21 +85,14 @@ export class ModbusSecurityConfigComponent implements ControlValueAccessor, Vali keyfile: ['', [Validators.pattern(noLeadTrailSpacesRegex)]], password: ['', [Validators.pattern(noLeadTrailSpacesRegex)]], server_hostname: ['', [Validators.pattern(noLeadTrailSpacesRegex)]], + reqclicert: [{value: false, disabled: true}, []], }); this.observeValueChanges(); } ngOnChanges(): void { - if (this.isMaster) { - this.securityConfigFormGroup = this.fb.group({ - certfile: ['', [Validators.pattern(noLeadTrailSpacesRegex)]], - keyfile: ['', [Validators.pattern(noLeadTrailSpacesRegex)]], - password: ['', [Validators.pattern(noLeadTrailSpacesRegex)]], - reqclicert: [false, []], - }); - this.observeValueChanges(); - } + this.updateMasterEnabling(); } ngOnDestroy(): void { @@ -126,6 +115,7 @@ export class ModbusSecurityConfigComponent implements ControlValueAccessor, Vali } else { this.securityConfigFormGroup.enable({emitEvent: false}); } + this.updateMasterEnabling(); this.cdr.markForCheck(); } @@ -137,19 +127,29 @@ export class ModbusSecurityConfigComponent implements ControlValueAccessor, Vali writeValue(securityConfig: ModbusSecurity): void { const { certfile, password, keyfile, server_hostname } = securityConfig; - let securityState = { + const securityState = { certfile: certfile ?? '', password: password ?? '', keyfile: keyfile ?? '', - server_hostname: server_hostname?? '', + server_hostname: server_hostname ?? '', reqclicert: !!securityConfig.reqclicert, }; + + this.securityConfigFormGroup.reset(securityState, {emitEvent: false}); + } + + private updateMasterEnabling(): void { if (this.isMaster) { - securityState = { ...securityState, reqclicert: !!securityConfig.reqclicert }; + if (!this.disabled) { + this.securityConfigFormGroup.get('reqclicert').enable({emitEvent: false}); + } + this.securityConfigFormGroup.get('server_hostname').disable({emitEvent: false}); } else { - securityState = { ...securityState, server_hostname: server_hostname ?? '' }; + if (!this.disabled) { + this.securityConfigFormGroup.get('server_hostname').enable({emitEvent: false}); + } + this.securityConfigFormGroup.get('reqclicert').disable({emitEvent: false}); } - this.securityConfigFormGroup.reset(securityState, {emitEvent: false}); } private observeValueChanges(): void { diff --git a/ui-ngx/src/app/modules/home/components/widget/lib/gateway/connectors-configuration/modbus/modbus-slave-config/modbus-slave-config.component.html b/ui-ngx/src/app/modules/home/components/widget/lib/gateway/connectors-configuration/modbus/modbus-slave-config/modbus-slave-config.component.html index 1c1b55e6c2..25a07ccfa5 100644 --- a/ui-ngx/src/app/modules/home/components/widget/lib/gateway/connectors-configuration/modbus/modbus-slave-config/modbus-slave-config.component.html +++ b/ui-ngx/src/app/modules/home/components/widget/lib/gateway/connectors-configuration/modbus/modbus-slave-config/modbus-slave-config.component.html @@ -181,8 +181,8 @@
-
- +
+
gateway.advanced-connection-settings
@@ -199,19 +199,20 @@
- - - - - - {{ 'gateway.tls-connection' | translate }} - - - - - - +
+ + + + + + {{ 'gateway.tls-connection' | translate }} + + + + + + +
gateway.vendor-name
diff --git a/ui-ngx/src/app/modules/home/components/widget/lib/gateway/connectors-configuration/modbus/modbus-slave-config/modbus-slave-config.component.ts b/ui-ngx/src/app/modules/home/components/widget/lib/gateway/connectors-configuration/modbus/modbus-slave-config/modbus-slave-config.component.ts index 33a18ebbb7..189ca75762 100644 --- a/ui-ngx/src/app/modules/home/components/widget/lib/gateway/connectors-configuration/modbus/modbus-slave-config/modbus-slave-config.component.ts +++ b/ui-ngx/src/app/modules/home/components/widget/lib/gateway/connectors-configuration/modbus/modbus-slave-config/modbus-slave-config.component.ts @@ -47,6 +47,7 @@ import { startWith, takeUntil } from 'rxjs/operators'; import { GatewayPortTooltipPipe } from '@home/pipes/gateway-port-tooltip.pipe'; import { ModbusSecurityConfigComponent } from '../modbus-security-config/modbus-security-config.component'; import { ModbusValuesComponent, } from '../modbus-values/modbus-values.component'; +import { isEqual } from '@core/utils'; @Component({ selector: 'tb-modbus-slave-config', @@ -158,7 +159,7 @@ export class ModbusSlaveConfigComponent implements ControlValueAccessor, Validat } writeValue(slaveConfig: ModbusSlave): void { - this.showSecurityControl.patchValue(!!slaveConfig.security); + this.showSecurityControl.patchValue(!!slaveConfig.security && !isEqual(slaveConfig.security, {})); this.updateSlaveConfig(slaveConfig); this.updateFormEnableState(slaveConfig.sendDataToThingsBoard); } @@ -187,16 +188,19 @@ export class ModbusSlaveConfigComponent implements ControlValueAccessor, Validat this.slaveConfigFormGroup.get('sendDataToThingsBoard').enable({emitEvent: false}); } this.updateEnablingByProtocol(this.slaveConfigFormGroup.get('type').value); + this.updateSecurityEnable(this.showSecurityControl.value); } private observeShowSecurity(): void { - this.showSecurityControl.valueChanges.pipe(takeUntil(this.destroy$)).subscribe(value => { - if (value && this.slaveConfigFormGroup.get('sendDataToThingsBoard').value) { - this.slaveConfigFormGroup.get('security').enable({emitEvent: false}); - } else { - this.slaveConfigFormGroup.get('security').disable({emitEvent: false}); - } - }); + this.showSecurityControl.valueChanges.pipe(takeUntil(this.destroy$)).subscribe(value => this.updateSecurityEnable(value)); + } + + private updateSecurityEnable(isEnabled: boolean): void { + if (isEnabled && this.slaveConfigFormGroup.get('sendDataToThingsBoard').value) { + this.slaveConfigFormGroup.get('security').enable({emitEvent: false}); + } else { + this.slaveConfigFormGroup.get('security').disable({emitEvent: false}); + } } private updateEnablingByProtocol(type: ModbusProtocolType): void { diff --git a/ui-ngx/src/app/modules/home/components/widget/lib/gateway/connectors-configuration/modbus/modbus-slave-dialog/modbus-slave-dialog.component.html b/ui-ngx/src/app/modules/home/components/widget/lib/gateway/connectors-configuration/modbus/modbus-slave-dialog/modbus-slave-dialog.component.html index caa1af4029..a8aa70c878 100644 --- a/ui-ngx/src/app/modules/home/components/widget/lib/gateway/connectors-configuration/modbus/modbus-slave-dialog/modbus-slave-dialog.component.html +++ b/ui-ngx/src/app/modules/home/components/widget/lib/gateway/connectors-configuration/modbus/modbus-slave-dialog/modbus-slave-dialog.component.html @@ -236,108 +236,112 @@
- - - -
gateway.advanced-connection-settings
-
-
-
-
-
gateway.connection-timeout
-
- - - +
+ + + +
gateway.advanced-connection-settings
+
+
+
+
+
gateway.connection-timeout
+
+ + + +
-
-
-
gateway.byte-order
-
- - - {{ order }} - - +
+
gateway.byte-order
+
+ + + {{ order }} + + +
-
-
-
gateway.word-order
-
- - - {{ order }} - - +
+
gateway.word-order
+
+ + + {{ order }} + + +
-
- - - - - - {{ 'gateway.tls-connection' | translate }} - - - - - - -
- - - {{ 'gateway.retries' | translate }} - - -
-
- - - {{ 'gateway.retries-on-empty' | translate }} - - -
-
- - - {{ 'gateway.retries-on-invalid' | translate }} - - -
-
-
gateway.poll-period
-
- - - +
+ + + + + + {{ 'gateway.tls-connection' | translate }} + + + + + +
-
-
-
gateway.connect-attempt-time
-
- - - +
+ + + {{ 'gateway.retries' | translate }} + +
-
-
-
gateway.connect-attempt-count
-
- - - +
+ + + {{ 'gateway.retries-on-empty' | translate }} + +
-
-
-
gateway.wait-after-failed-attempts
-
- - - +
+ + + {{ 'gateway.retries-on-invalid' | translate }} + + +
+
+
gateway.poll-period
+
+ + + +
+
+
+
gateway.connect-attempt-time
+
+ + + +
+
+
+
gateway.connect-attempt-count
+
+ + + +
+
+
+
gateway.wait-after-failed-attempts
+
+ + + +
-
- + +
diff --git a/ui-ngx/src/app/modules/home/components/widget/lib/gateway/connectors-configuration/modbus/modbus-slave-dialog/modbus-slave-dialog.component.ts b/ui-ngx/src/app/modules/home/components/widget/lib/gateway/connectors-configuration/modbus/modbus-slave-dialog/modbus-slave-dialog.component.ts index 40a4459221..f01bbf7618 100644 --- a/ui-ngx/src/app/modules/home/components/widget/lib/gateway/connectors-configuration/modbus/modbus-slave-dialog/modbus-slave-dialog.component.ts +++ b/ui-ngx/src/app/modules/home/components/widget/lib/gateway/connectors-configuration/modbus/modbus-slave-dialog/modbus-slave-dialog.component.ts @@ -51,6 +51,7 @@ import { Router } from '@angular/router'; import { MAT_DIALOG_DATA, MatDialogRef } from '@angular/material/dialog'; import { GatewayPortTooltipPipe } from '@home/pipes/gateway-port-tooltip.pipe'; import { takeUntil } from 'rxjs/operators'; +import { isEqual } from '@core/utils'; @Component({ selector: 'tb-modbus-slave-dialog', @@ -81,12 +82,6 @@ import { takeUntil } from 'rxjs/operators'; .slaves-config-container { width: 900px; } - .nested-expansion-header { - .mat-content { - height: 100%; - overflow: hidden; - } - } } `], }) @@ -169,10 +164,10 @@ export class ModbusSlaveDialogComponent extends DialogComponent { - return { - ...registersAcc, - [register]: this.fb.group(this.modbusValueKeys.reduce((acc, key) => ({...acc, [key]: [[], []]}), {})), - }; - }, {})); + ) {} + ngOnInit() { + this.initializeValuesFormGroup(); this.observeValuesChanges(); } - ngOnChanges(): void { - if (this.singleMode) { - this.valuesFormGroup = this.fb.group(this.modbusValueKeys.reduce((acc, key) => ({...acc, [key]: [[], []]}), {})); - this.observeValuesChanges(); - } - } - ngOnDestroy(): void { this.destroy$.next(); this.destroy$.complete(); @@ -218,6 +207,28 @@ export class ModbusValuesComponent implements ControlValueAccessor, Validator, O } } + private initializeValuesFormGroup(): void { + if (this.singleMode) { + this.valuesFormGroup = this.fb.group(this.modbusValueKeys.reduce((acc, key) => { + acc[key] = this.fb.control([[], []]); + return acc; + }, {})); + } else { + this.valuesFormGroup = this.fb.group( + this.modbusRegisterTypes.reduce((registersAcc, register) => { + + registersAcc[register] = this.fb.group(this.modbusValueKeys.reduce((acc, key) => { + acc[key] = this.fb.control([[], []]); + return acc; + }, {})); + + return registersAcc; + }, {}) + ); + } + } + + private observeValuesChanges(): void { this.valuesFormGroup.valueChanges .pipe(takeUntil(this.destroy$)) diff --git a/ui-ngx/src/app/modules/home/components/widget/lib/gateway/gateway-widget.models.ts b/ui-ngx/src/app/modules/home/components/widget/lib/gateway/gateway-widget.models.ts index 3c041cd005..2db6d9d2fe 100644 --- a/ui-ngx/src/app/modules/home/components/widget/lib/gateway/gateway-widget.models.ts +++ b/ui-ngx/src/app/modules/home/components/widget/lib/gateway/gateway-widget.models.ts @@ -823,6 +823,9 @@ export enum ModbusDataType { STRING = 'string', BYTES = 'bytes', BITS = 'bits', + INT8 = '8int', + UINT8 = '8uint', + FLOAT8 = '8float', INT16 = '16int', UINT16 = '16uint', FLOAT16 = '16float', @@ -835,6 +838,9 @@ export enum ModbusDataType { } export enum ModbusObjectCountByDataType { + '8int' = 1, + '8uint' = 1, + '8float' = 1, '16int' = 1, '16uint' = 1, '16float' = 1, @@ -907,14 +913,6 @@ export const ModbusKeysNoKeysTextTranslationsMap = new Map( - [ - [ModbusProtocolType.TCP, 'TCP/UDP'], - [ModbusProtocolType.UDP, 'TCP/UDP'], - [ModbusProtocolType.Serial, 'Serial'], - ] -); - export const ModbusFunctionCodeTranslationsMap = new Map( [ [1, 'gateway.read-coils'], From e9d58525bc26ddca5a4de4061c5b0bd09fa1d359 Mon Sep 17 00:00:00 2001 From: mpetrov Date: Wed, 24 Jul 2024 16:26:51 +0300 Subject: [PATCH 13/20] dialog word order for serial fix --- .../modbus-slave-dialog/modbus-slave-dialog.component.html | 2 +- .../modbus/modbus-slave-dialog/modbus-slave-dialog.component.ts | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/ui-ngx/src/app/modules/home/components/widget/lib/gateway/connectors-configuration/modbus/modbus-slave-dialog/modbus-slave-dialog.component.html b/ui-ngx/src/app/modules/home/components/widget/lib/gateway/connectors-configuration/modbus/modbus-slave-dialog/modbus-slave-dialog.component.html index a8aa70c878..60ef25f875 100644 --- a/ui-ngx/src/app/modules/home/components/widget/lib/gateway/connectors-configuration/modbus/modbus-slave-dialog/modbus-slave-dialog.component.html +++ b/ui-ngx/src/app/modules/home/components/widget/lib/gateway/connectors-configuration/modbus/modbus-slave-dialog/modbus-slave-dialog.component.html @@ -262,7 +262,7 @@
-
+
gateway.word-order
diff --git a/ui-ngx/src/app/modules/home/components/widget/lib/gateway/connectors-configuration/modbus/modbus-slave-dialog/modbus-slave-dialog.component.ts b/ui-ngx/src/app/modules/home/components/widget/lib/gateway/connectors-configuration/modbus/modbus-slave-dialog/modbus-slave-dialog.component.ts index f01bbf7618..8c6fb87e11 100644 --- a/ui-ngx/src/app/modules/home/components/widget/lib/gateway/connectors-configuration/modbus/modbus-slave-dialog/modbus-slave-dialog.component.ts +++ b/ui-ngx/src/app/modules/home/components/widget/lib/gateway/connectors-configuration/modbus/modbus-slave-dialog/modbus-slave-dialog.component.ts @@ -105,7 +105,7 @@ export class ModbusSlaveDialogComponent extends DialogComponent(); From 7b9d8e0a5528e07344cd829267e55c3821b144a7 Mon Sep 17 00:00:00 2001 From: mpetrov Date: Thu, 25 Jul 2024 12:09:26 +0300 Subject: [PATCH 14/20] major refactoring --- .../modbus-basic-config.component.ts | 15 +- .../modbus-data-keys-panel.component.html | 13 +- .../modbus-data-keys-panel.component.ts | 92 +++++++---- .../modbus-master-table.component.html | 10 +- .../modbus-master-table.component.scss | 21 +-- .../modbus-master-table.component.ts | 45 +++--- .../modbus-security-config.component.ts | 2 +- .../modbus-slave-config.component.html | 9 +- .../modbus-slave-config.component.scss | 5 - .../modbus-slave-config.component.ts | 151 +++++++++--------- .../modbus-slave-dialog.component.html | 6 +- .../modbus-slave-dialog.component.ts | 122 +++++++------- .../modbus-values/modbus-values.component.ts | 109 ++++++------- .../lib/gateway/gateway-widget.models.ts | 27 +--- .../widget/widget-components.module.ts | 39 ++--- .../home/pipes/gateway-port-tooltip.pipe.ts | 12 +- .../ellipsis-chip-list.directive.ts | 5 +- .../assets/locale/locale.constant-en_US.json | 1 + .../connector-default-configs/modbus.json | 1 + 19 files changed, 340 insertions(+), 345 deletions(-) diff --git a/ui-ngx/src/app/modules/home/components/widget/lib/gateway/connectors-configuration/modbus/modbus-basic-config/modbus-basic-config.component.ts b/ui-ngx/src/app/modules/home/components/widget/lib/gateway/connectors-configuration/modbus/modbus-basic-config/modbus-basic-config.component.ts index 35e0ee6e61..12143fb6db 100644 --- a/ui-ngx/src/app/modules/home/components/widget/lib/gateway/connectors-configuration/modbus/modbus-basic-config/modbus-basic-config.component.ts +++ b/ui-ngx/src/app/modules/home/components/widget/lib/gateway/connectors-configuration/modbus/modbus-basic-config/modbus-basic-config.component.ts @@ -68,12 +68,6 @@ import { ModbusMasterTableComponent } from '../modbus-master-table/modbus-master overflow: hidden !important; } } - - :host ::ng-deep { - .mat-mdc-tab-group, .mat-mdc-tab-body-wrapper { - height: 100%; - } - } `] }) @@ -83,10 +77,9 @@ export class ModbusBasicConfigComponent implements ControlValueAccessor, Validat basicFormGroup: FormGroup; - onChange: (value: string) => void; + onChange: (value: ModbusBasicConfig) => void; onTouched: () => void; - protected readonly connectorType = ConnectorType; private destroy$ = new Subject(); constructor(private fb: FormBuilder) { @@ -108,7 +101,7 @@ export class ModbusBasicConfigComponent implements ControlValueAccessor, Validat this.destroy$.complete(); } - registerOnChange(fn: (value: string) => void): void { + registerOnChange(fn: (value: ModbusBasicConfig) => void): void { this.onChange = fn; } @@ -118,8 +111,8 @@ export class ModbusBasicConfigComponent implements ControlValueAccessor, Validat writeValue(basicConfig: ModbusBasicConfig): void { const editedBase = { - slave: basicConfig.slave || {}, - master: basicConfig.master || {}, + slave: basicConfig.slave ?? {}, + master: basicConfig.master ?? {}, }; this.basicFormGroup.setValue(editedBase, {emitEvent: false}); diff --git a/ui-ngx/src/app/modules/home/components/widget/lib/gateway/connectors-configuration/modbus/modbus-data-keys-panel/modbus-data-keys-panel.component.html b/ui-ngx/src/app/modules/home/components/widget/lib/gateway/connectors-configuration/modbus/modbus-data-keys-panel/modbus-data-keys-panel.component.html index 048ee94553..5285422bd6 100644 --- a/ui-ngx/src/app/modules/home/components/widget/lib/gateway/connectors-configuration/modbus/modbus-data-keys-panel/modbus-data-keys-panel.component.html +++ b/ui-ngx/src/app/modules/home/components/widget/lib/gateway/connectors-configuration/modbus/modbus-data-keys-panel/modbus-data-keys-panel.component.html @@ -20,7 +20,7 @@
{{ panelTitle | translate }}{{' (' + keysListFormArray.controls.length + ')'}}
+ *ngFor="let keyControl of keysListFormArray.controls; trackBy: trackByControlId; let $index = index; let last = last;">
@@ -99,10 +99,19 @@
-
gateway.address
+
gateway.address
+ + warning +
diff --git a/ui-ngx/src/app/modules/home/components/widget/lib/gateway/connectors-configuration/modbus/modbus-data-keys-panel/modbus-data-keys-panel.component.ts b/ui-ngx/src/app/modules/home/components/widget/lib/gateway/connectors-configuration/modbus/modbus-data-keys-panel/modbus-data-keys-panel.component.ts index d128e3c246..d0bfcf1e76 100644 --- a/ui-ngx/src/app/modules/home/components/widget/lib/gateway/connectors-configuration/modbus/modbus-data-keys-panel/modbus-data-keys-panel.component.ts +++ b/ui-ngx/src/app/modules/home/components/widget/lib/gateway/connectors-configuration/modbus/modbus-data-keys-panel/modbus-data-keys-panel.component.ts @@ -14,14 +14,21 @@ /// limitations under the License. /// -import { Component, EventEmitter, Input, OnInit, Output } from '@angular/core'; -import { AbstractControl, FormGroup, UntypedFormArray, UntypedFormBuilder, Validators } from '@angular/forms'; +import { Component, EventEmitter, Input, OnDestroy, OnInit, Output } from '@angular/core'; +import { + AbstractControl, + FormArray, + FormGroup, + UntypedFormArray, + UntypedFormBuilder, + UntypedFormGroup, + Validators +} from '@angular/forms'; import { TbPopoverComponent } from '@shared/components/popover.component'; import { ModbusDataType, ModbusFunctionCodeTranslationsMap, ModbusObjectCountByDataType, - ModbusRegisterType, ModbusValue, ModbusValueKey, noLeadTrailSpacesRegex, @@ -31,6 +38,8 @@ import { SharedModule } from '@shared/shared.module'; import { GatewayHelpLinkPipe } from '@home/pipes/gateway-help-link.pipe'; import { generateSecret } from '@core/utils'; import { coerceBoolean } from '@shared/decorators/coercion'; +import { takeUntil } from 'rxjs/operators'; +import { Subject } from 'rxjs'; @Component({ selector: 'tb-modbus-data-keys-panel', @@ -43,7 +52,7 @@ import { coerceBoolean } from '@shared/decorators/coercion'; GatewayHelpLinkPipe, ] }) -export class ModbusDataKeysPanelComponent implements OnInit { +export class ModbusDataKeysPanelComponent implements OnInit, OnDestroy { @coerceBoolean() @Input() isMaster = false; @@ -51,15 +60,13 @@ export class ModbusDataKeysPanelComponent implements OnInit { @Input() addKeyTitle: string; @Input() deleteKeyTitle: string; @Input() noKeysText: string; - @Input() register: ModbusRegisterType; @Input() keysType: ModbusValueKey; @Input() values: ModbusValue[]; @Input() popover: TbPopoverComponent; @Output() keysDataApplied = new EventEmitter>(); - keysListFormArray: UntypedFormArray; - errorText = ''; + keysListFormArray: FormArray; modbusDataTypes = Object.values(ModbusDataType); withFunctionCode = true; functionCodesMap = new Map(); @@ -67,9 +74,12 @@ export class ModbusDataKeysPanelComponent implements OnInit { readonly editableDataTypes = [ModbusDataType.BYTES, ModbusDataType.BITS, ModbusDataType.STRING]; readonly ModbusFunctionCodeTranslationsMap = ModbusFunctionCodeTranslationsMap; - readonly defaultReadFunctionCodes = [3, 4]; - readonly defaultWriteFunctionCodes = [5, 6, 15, 16]; - readonly stringAttrUpdatesWriteFunctionCodes = [6, 16]; + + private destroy$ = new Subject(); + + private readonly defaultReadFunctionCodes = [3, 4]; + private readonly defaultWriteFunctionCodes = [5, 6, 15, 16]; + private readonly stringAttrUpdatesWriteFunctionCodes = [6, 16]; constructor(private fb: UntypedFormBuilder) {} @@ -79,8 +89,13 @@ export class ModbusDataKeysPanelComponent implements OnInit { this.defaultFunctionCodes = this.getDefaultFunctionCodes(); } - trackByKey(_: number, keyControl: AbstractControl): AbstractControl { - return keyControl; + ngOnDestroy(): void { + this.destroy$.next(); + this.destroy$.complete(); + } + + trackByControlId(_: number, keyControl: AbstractControl): string { + return keyControl.value.id; } addKey(): void { @@ -88,7 +103,7 @@ export class ModbusDataKeysPanelComponent implements OnInit { tag: ['', [Validators.required, Validators.pattern(noLeadTrailSpacesRegex)]], value: [{value: '', disabled: !this.isMaster}, [Validators.required, Validators.pattern(noLeadTrailSpacesRegex)]], type: [ModbusDataType.BYTES, [Validators.required]], - address: [0, [Validators.required]], + address: [null, [Validators.required]], objectsCount: [1, [Validators.required]], functionCode: [this.getDefaultFunctionCodes()[0]], id: [{value: generateSecret(5), disabled: true}], @@ -107,7 +122,7 @@ export class ModbusDataKeysPanelComponent implements OnInit { } cancel(): void { - this.popover?.hide(); + this.popover.hide(); } applyKeysData(): void { @@ -116,32 +131,38 @@ export class ModbusDataKeysPanelComponent implements OnInit { private prepareKeysFormArray(values: ModbusValue[]): UntypedFormArray { const keysControlGroups: Array = []; + if (values) { - values.forEach(keyData => { - const { tag, value, type, address, objectsCount, functionCode } = keyData; - const dataKeyFormGroup = this.fb.group({ - tag: [tag, [Validators.required, Validators.pattern(noLeadTrailSpacesRegex)]], - value: [{value, disabled: !this.isMaster}, [Validators.required, Validators.pattern(noLeadTrailSpacesRegex)]], - type: [type, [Validators.required]], - address: [address, [Validators.required]], - objectsCount: [objectsCount, [Validators.required]], - functionCode: [functionCode, []], - id: [{value: generateSecret(5), disabled: true}], - }); + values.forEach(value => { + const dataKeyFormGroup = this.createDataKeyFormGroup(value); this.observeKeyDataType(dataKeyFormGroup); - this.functionCodesMap.set(dataKeyFormGroup.get('id').value, this.getFunctionCodes(type)); + this.functionCodesMap.set(dataKeyFormGroup.get('id').value, this.getFunctionCodes(value.type)); keysControlGroups.push(dataKeyFormGroup); }); } + return this.fb.array(keysControlGroups); } + private createDataKeyFormGroup(modbusValue: ModbusValue): FormGroup { + const { tag, value, type, address, objectsCount, functionCode } = modbusValue; + + return this.fb.group({ + tag: [tag, [Validators.required, Validators.pattern(noLeadTrailSpacesRegex)]], + value: [{ value, disabled: !this.isMaster }, [Validators.required, Validators.pattern(noLeadTrailSpacesRegex)]], + type: [type, [Validators.required]], + address: [address, [Validators.required]], + objectsCount: [objectsCount, [Validators.required]], + functionCode: [functionCode, [Validators.required]], + id: [{ value: generateSecret(5), disabled: true }], + }); + } + private observeKeyDataType(keyFormGroup: FormGroup): void { - keyFormGroup.get('type').valueChanges.subscribe(dataType => { - const objectsCountControl = keyFormGroup.get('objectsCount'); + keyFormGroup.get('type').valueChanges.pipe(takeUntil(this.destroy$)).subscribe(dataType => { if (!this.editableDataTypes.includes(dataType)) { - objectsCountControl.patchValue(ModbusObjectCountByDataType[dataType]); + keyFormGroup.get('objectsCount').patchValue(ModbusObjectCountByDataType[dataType], {emitEvent: false}); } this.functionCodesMap.set(keyFormGroup.get('id').value, this.getFunctionCodes(dataType)); }); @@ -149,20 +170,21 @@ export class ModbusDataKeysPanelComponent implements OnInit { private getFunctionCodes(dataType: ModbusDataType): number[] { if (this.keysType === ModbusValueKey.ATTRIBUTES_UPDATES) { - if (dataType === ModbusDataType.STRING) { - return this.stringAttrUpdatesWriteFunctionCodes; - } - return this.defaultWriteFunctionCodes; + return dataType === ModbusDataType.STRING + ? this.stringAttrUpdatesWriteFunctionCodes + : this.defaultWriteFunctionCodes; } + const functionCodes = [...this.defaultReadFunctionCodes]; if (dataType === ModbusDataType.BITS) { const bitsFunctionCodes = [1, 2]; - bitsFunctionCodes.forEach(code => functionCodes.push(code)); + functionCodes.push(...bitsFunctionCodes); functionCodes.sort(); } if (this.keysType === ModbusValueKey.RPC_REQUESTS) { - this.defaultWriteFunctionCodes.forEach(code => functionCodes.push(code)); + functionCodes.push(...this.defaultWriteFunctionCodes); } + return functionCodes; } diff --git a/ui-ngx/src/app/modules/home/components/widget/lib/gateway/connectors-configuration/modbus/modbus-master-table/modbus-master-table.component.html b/ui-ngx/src/app/modules/home/components/widget/lib/gateway/connectors-configuration/modbus/modbus-master-table/modbus-master-table.component.html index 2e92d80c9f..e16aa0c51f 100644 --- a/ui-ngx/src/app/modules/home/components/widget/lib/gateway/connectors-configuration/modbus/modbus-master-table/modbus-master-table.component.html +++ b/ui-ngx/src/app/modules/home/components/widget/lib/gateway/connectors-configuration/modbus/modbus-master-table/modbus-master-table.component.html @@ -60,18 +60,18 @@
- + {{ 'gateway.name' | translate }} - + {{ mapping['name'] }} - + {{ 'gateway.client-communication-type' | translate }} - + {{ ModbusProtocolLabelsMap.get(mapping['type']) }} @@ -111,7 +111,7 @@ - +
diff --git a/ui-ngx/src/app/modules/home/components/widget/lib/gateway/connectors-configuration/modbus/modbus-master-table/modbus-master-table.component.scss b/ui-ngx/src/app/modules/home/components/widget/lib/gateway/connectors-configuration/modbus/modbus-master-table/modbus-master-table.component.scss index 82cead07a4..e9a5d3ebcd 100644 --- a/ui-ngx/src/app/modules/home/components/widget/lib/gateway/connectors-configuration/modbus/modbus-master-table/modbus-master-table.component.scss +++ b/ui-ngx/src/app/modules/home/components/widget/lib/gateway/connectors-configuration/modbus/modbus-master-table/modbus-master-table.component.scss @@ -19,19 +19,15 @@ width: 100%; height: 100%; display: block; + .tb-master-table { + .tb-master-table-content { width: 100%; height: 100%; background: #fff; overflow: hidden; - &.tb-outlined-border { - box-shadow: 0 0 0 0 rgb(0 0 0 / 20%), 0 0 0 0 rgb(0 0 0 / 14%), 0 0 0 0 rgb(0 0 0 / 12%); - border: solid 1px #e0e0e0; - border-radius: 4px; - } - .mat-toolbar-tools{ min-height: auto; } @@ -49,25 +45,17 @@ .table-container { overflow: auto; + .mat-mdc-table { table-layout: fixed; min-width: 450px; .table-value-column { padding: 0 12px; - width: 23%; - - &.request-column { - width: 38%; - } + width: 38%; } } } - - .ellipsis { - overflow: hidden; - text-overflow: ellipsis; - } } } @@ -91,6 +79,7 @@ :host ::ng-deep { mat-cell.tb-value-cell { cursor: pointer; + .mat-icon { height: 24px; width: 24px; diff --git a/ui-ngx/src/app/modules/home/components/widget/lib/gateway/connectors-configuration/modbus/modbus-master-table/modbus-master-table.component.ts b/ui-ngx/src/app/modules/home/components/widget/lib/gateway/connectors-configuration/modbus/modbus-master-table/modbus-master-table.component.ts index 10b1e5bbbe..7ca5c8a7fa 100644 --- a/ui-ngx/src/app/modules/home/components/widget/lib/gateway/connectors-configuration/modbus/modbus-master-table/modbus-master-table.component.ts +++ b/ui-ngx/src/app/modules/home/components/widget/lib/gateway/connectors-configuration/modbus/modbus-master-table/modbus-master-table.component.ts @@ -17,6 +17,7 @@ import { AfterViewInit, ChangeDetectionStrategy, + ChangeDetectorRef, Component, ElementRef, forwardRef, @@ -76,15 +77,12 @@ export class ModbusMasterTableComponent implements ControlValueAccessor, Validat textSearchMode = false; dataSource: SlavesDatasource; - hidePageSize = false; - activeValue = false; - dirtyValue = false; masterFormGroup: UntypedFormGroup; textSearch = this.fb.control('', {nonNullable: true}); readonly ModbusProtocolLabelsMap = ModbusProtocolLabelsMap; - private onChange: (value: string) => void = () => {}; + private onChange: (value: ModbusMasterConfig) => void = () => {}; private onTouched: () => void = () => {}; private destroy$ = new Subject(); @@ -93,10 +91,10 @@ export class ModbusMasterTableComponent implements ControlValueAccessor, Validat public translate: TranslateService, public dialog: MatDialog, private dialogService: DialogService, - private fb: FormBuilder + private fb: FormBuilder, + private cdr: ChangeDetectorRef, ) { - this.masterFormGroup = this.fb.group({ slaves: this.fb.array([])}); - this.dirtyValue = !this.activeValue; + this.masterFormGroup = this.fb.group({ slaves: this.fb.array([]) }); this.dataSource = new SlavesDatasource(); } @@ -124,13 +122,10 @@ export class ModbusMasterTableComponent implements ControlValueAccessor, Validat debounceTime(150), distinctUntilChanged((prev, current) => (prev ?? '') === current.trim()), takeUntil(this.destroy$) - ).subscribe((text) => { - const searchText = text.trim(); - this.updateTableData(this.slaves.value, searchText.trim()); - }); + ).subscribe(text => this.updateTableData(this.slaves.value, text.trim())); } - registerOnChange(fn: (value: string) => void): void { + registerOnChange(fn: (value: ModbusMasterConfig) => void): void { this.onChange = fn; } @@ -151,10 +146,10 @@ export class ModbusMasterTableComponent implements ControlValueAccessor, Validat enterFilterMode(): void { this.textSearchMode = true; - setTimeout(() => { - this.searchInputField.nativeElement.focus(); - this.searchInputField.nativeElement.setSelectionRange(0, 0); - }, 10); + this.cdr.detectChanges(); + const searchInput = this.searchInputField.nativeElement; + searchInput.focus(); + searchInput.setSelectionRange(0, 0); } exitFilterMode(): void { @@ -167,19 +162,20 @@ export class ModbusMasterTableComponent implements ControlValueAccessor, Validat if ($event) { $event.stopPropagation(); } - const value = isDefinedAndNotNull(index) ? this.slaves.at(index).value : {}; + const withIndex = isDefinedAndNotNull(index); + const value = withIndex ? this.slaves.at(index).value : {}; this.dialog.open(ModbusSlaveDialogComponent, { disableClose: true, panelClass: ['tb-dialog', 'tb-fullscreen-dialog'], data: { value, - buttonTitle: isUndefinedOrNull(index) ? 'action.add' : 'action.apply' + buttonTitle: withIndex ? 'action.add' : 'action.apply' } }).afterClosed() .pipe(take(1), takeUntil(this.destroy$)) .subscribe(res => { if (res) { - if (isDefinedAndNotNull(index)) { + if (withIndex) { this.slaves.at(index).patchValue(res); } else { this.slaves.push(this.fb.control(res)); @@ -199,7 +195,7 @@ export class ModbusMasterTableComponent implements ControlValueAccessor, Validat this.translate.instant('action.no'), this.translate.instant('action.yes'), true - ).subscribe((result) => { + ).pipe(take(1), takeUntil(this.destroy$)).subscribe((result) => { if (result) { this.slaves.removeAt(index); this.masterFormGroup.markAsDirty(); @@ -208,15 +204,14 @@ export class ModbusMasterTableComponent implements ControlValueAccessor, Validat } private updateTableData(data: SlaveConfig[], textSearch?: string): void { - let tableValue = data; if (textSearch) { - tableValue = tableValue.filter(value => - Object.values(value).some(val => - val.toString().toLowerCase().includes(textSearch.toLowerCase()) + data = data.filter(item => + Object.values(item).some(value => + value.toString().toLowerCase().includes(textSearch.toLowerCase()) ) ); } - this.dataSource.loadData(tableValue); + this.dataSource.loadData(data); } private pushDataAsFormArrays(slaves: SlaveConfig[]): void { diff --git a/ui-ngx/src/app/modules/home/components/widget/lib/gateway/connectors-configuration/modbus/modbus-security-config/modbus-security-config.component.ts b/ui-ngx/src/app/modules/home/components/widget/lib/gateway/connectors-configuration/modbus/modbus-security-config/modbus-security-config.component.ts index 75328d1264..bc40727b55 100644 --- a/ui-ngx/src/app/modules/home/components/widget/lib/gateway/connectors-configuration/modbus/modbus-security-config/modbus-security-config.component.ts +++ b/ui-ngx/src/app/modules/home/components/widget/lib/gateway/connectors-configuration/modbus/modbus-security-config/modbus-security-config.component.ts @@ -85,7 +85,7 @@ export class ModbusSecurityConfigComponent implements ControlValueAccessor, Vali keyfile: ['', [Validators.pattern(noLeadTrailSpacesRegex)]], password: ['', [Validators.pattern(noLeadTrailSpacesRegex)]], server_hostname: ['', [Validators.pattern(noLeadTrailSpacesRegex)]], - reqclicert: [{value: false, disabled: true}, []], + reqclicert: [{value: false, disabled: true}], }); this.observeValueChanges(); diff --git a/ui-ngx/src/app/modules/home/components/widget/lib/gateway/connectors-configuration/modbus/modbus-slave-config/modbus-slave-config.component.html b/ui-ngx/src/app/modules/home/components/widget/lib/gateway/connectors-configuration/modbus/modbus-slave-config/modbus-slave-config.component.html index 25a07ccfa5..56fc579361 100644 --- a/ui-ngx/src/app/modules/home/components/widget/lib/gateway/connectors-configuration/modbus/modbus-slave-config/modbus-slave-config.component.html +++ b/ui-ngx/src/app/modules/home/components/widget/lib/gateway/connectors-configuration/modbus/modbus-slave-config/modbus-slave-config.component.html @@ -86,11 +86,8 @@ warning @@ -188,7 +185,7 @@
gateway.advanced-connection-settings
-
+
gateway.byte-order
diff --git a/ui-ngx/src/app/modules/home/components/widget/lib/gateway/connectors-configuration/modbus/modbus-slave-config/modbus-slave-config.component.scss b/ui-ngx/src/app/modules/home/components/widget/lib/gateway/connectors-configuration/modbus/modbus-slave-config/modbus-slave-config.component.scss index f3b39e3508..0c2f9240be 100644 --- a/ui-ngx/src/app/modules/home/components/widget/lib/gateway/connectors-configuration/modbus/modbus-slave-config/modbus-slave-config.component.scss +++ b/ui-ngx/src/app/modules/home/components/widget/lib/gateway/connectors-configuration/modbus/modbus-slave-config/modbus-slave-config.component.scss @@ -16,11 +16,6 @@ $server-config-header-height: 132px; :host { - .nested-expansion-header { - ::ng-deep .mat-content { - height: 100%; - } - } .slave-content { height: calc(100% - #{$server-config-header-height}); diff --git a/ui-ngx/src/app/modules/home/components/widget/lib/gateway/connectors-configuration/modbus/modbus-slave-config/modbus-slave-config.component.ts b/ui-ngx/src/app/modules/home/components/widget/lib/gateway/connectors-configuration/modbus/modbus-slave-config/modbus-slave-config.component.ts index 189ca75762..d44272734d 100644 --- a/ui-ngx/src/app/modules/home/components/widget/lib/gateway/connectors-configuration/modbus/modbus-slave-config/modbus-slave-config.component.ts +++ b/ui-ngx/src/app/modules/home/components/widget/lib/gateway/connectors-configuration/modbus/modbus-slave-config/modbus-slave-config.component.ts @@ -18,9 +18,9 @@ import { ChangeDetectionStrategy, Component, forwardRef, OnDestroy } from '@angu import { ControlValueAccessor, FormBuilder, + FormControl, NG_VALIDATORS, NG_VALUE_ACCESSOR, - UntypedFormControl, UntypedFormGroup, ValidationErrors, Validator, @@ -78,7 +78,7 @@ import { isEqual } from '@core/utils'; export class ModbusSlaveConfigComponent implements ControlValueAccessor, Validator, OnDestroy { slaveConfigFormGroup: UntypedFormGroup; - showSecurityControl: UntypedFormControl; + showSecurityControl: FormControl; ModbusProtocolLabelsMap = ModbusProtocolLabelsMap; ModbusMethodLabelsMap = ModbusMethodLabelsMap; portLimits = PortLimits; @@ -89,8 +89,9 @@ export class ModbusSlaveConfigComponent implements ControlValueAccessor, Validat readonly modbusOrderType = Object.values(ModbusOrderType); readonly ModbusProtocolType = ModbusProtocolType; readonly modbusBaudrates = ModbusBaudrates; - readonly serialSpecificControlKeys = ['serialPort', 'baudrate']; - readonly tcpUdpSpecificControlKeys = ['port', 'security', 'host']; + + private readonly serialSpecificControlKeys = ['serialPort', 'baudrate']; + private readonly tcpUdpSpecificControlKeys = ['port', 'security', 'host']; private onChange: (value: SlaveConfig) => void; private onTouched: () => void; @@ -100,18 +101,18 @@ export class ModbusSlaveConfigComponent implements ControlValueAccessor, Validat constructor(private fb: FormBuilder) { this.showSecurityControl = this.fb.control(false); this.slaveConfigFormGroup = this.fb.group({ - type: [ModbusProtocolType.TCP, []], + type: [ModbusProtocolType.TCP], host: ['', [Validators.required, Validators.pattern(noLeadTrailSpacesRegex)]], port: [null, [Validators.required, Validators.min(PortLimits.MIN), Validators.max(PortLimits.MAX)]], serialPort: ['', [Validators.required, Validators.pattern(noLeadTrailSpacesRegex)]], - method: [ModbusMethodType.RTU, []], + method: [ModbusMethodType.SOCKET], unitId: [0, [Validators.required]], - baudrate: [this.ModbusProtocolType[0], []], + baudrate: [this.modbusBaudrates[0]], deviceName: ['', [Validators.required, Validators.pattern(noLeadTrailSpacesRegex)]], deviceType: ['', [Validators.required, Validators.pattern(noLeadTrailSpacesRegex)]], - pollPeriod: [5000, []], - sendDataToThingsBoard: [false, []], - byteOrder:[ModbusOrderType.BIG, []], + pollPeriod: [5000], + sendDataToThingsBoard: [false], + byteOrder:[ModbusOrderType.BIG], security: [], identity: this.fb.group({ vendorName: ['', [Validators.pattern(noLeadTrailSpacesRegex)]], @@ -123,22 +124,16 @@ export class ModbusSlaveConfigComponent implements ControlValueAccessor, Validat values: [], }); - this.slaveConfigFormGroup.valueChanges.pipe( - takeUntil(this.destroy$) - ).subscribe((value: SlaveConfig) => { - if (value.type === ModbusProtocolType.Serial) { - value.port = value.serialPort; - delete value.serialPort; - } - this.onChange(value); - this.onTouched(); - }); - + this.observeValueChanges(); this.observeTypeChange(); this.observeFormEnable(); this.observeShowSecurity(); } + get isSlaveEnabled(): boolean { + return this.slaveConfigFormGroup.get('sendDataToThingsBoard').value; + } + ngOnDestroy(): void { this.destroy$.next(); this.destroy$.complete(); @@ -154,7 +149,7 @@ export class ModbusSlaveConfigComponent implements ControlValueAccessor, Validat validate(): ValidationErrors | null { return this.slaveConfigFormGroup.valid ? null : { - serverConfigFormGroup: { valid: false } + slaveConfigFormGroup: { valid: false } }; } @@ -164,18 +159,29 @@ export class ModbusSlaveConfigComponent implements ControlValueAccessor, Validat this.updateFormEnableState(slaveConfig.sendDataToThingsBoard); } - private observeTypeChange(): void { - this.slaveConfigFormGroup.get('type').valueChanges.pipe(takeUntil(this.destroy$)).subscribe(type => { - this.updateFormEnableState(this.slaveConfigFormGroup.get('sendDataToThingsBoard').value); + private observeValueChanges(): void { + this.slaveConfigFormGroup.valueChanges.pipe( + takeUntil(this.destroy$) + ).subscribe((value: SlaveConfig) => { + if (value.type === ModbusProtocolType.Serial) { + value.port = value.serialPort; + delete value.serialPort; + } + this.onChange(value); + this.onTouched(); }); } + private observeTypeChange(): void { + this.slaveConfigFormGroup.get('type').valueChanges + .pipe(takeUntil(this.destroy$)) + .subscribe(() => this.updateFormEnableState(this.isSlaveEnabled)); + } + private observeFormEnable(): void { this.slaveConfigFormGroup.get('sendDataToThingsBoard').valueChanges - .pipe(startWith(this.slaveConfigFormGroup.get('sendDataToThingsBoard').value), takeUntil(this.destroy$)) - .subscribe(value => { - this.updateFormEnableState(value); - }); + .pipe(startWith(this.isSlaveEnabled), takeUntil(this.destroy$)) + .subscribe(value => this.updateFormEnableState(value)); } private updateFormEnableState(enabled: boolean): void { @@ -192,11 +198,13 @@ export class ModbusSlaveConfigComponent implements ControlValueAccessor, Validat } private observeShowSecurity(): void { - this.showSecurityControl.valueChanges.pipe(takeUntil(this.destroy$)).subscribe(value => this.updateSecurityEnable(value)); + this.showSecurityControl.valueChanges + .pipe(takeUntil(this.destroy$)) + .subscribe(value => this.updateSecurityEnable(value)); } - private updateSecurityEnable(isEnabled: boolean): void { - if (isEnabled && this.slaveConfigFormGroup.get('sendDataToThingsBoard').value) { + private updateSecurityEnable(securityEnabled: boolean): void { + if (securityEnabled && this.isSlaveEnabled) { this.slaveConfigFormGroup.get('security').enable({emitEvent: false}); } else { this.slaveConfigFormGroup.get('security').disable({emitEvent: false}); @@ -204,63 +212,58 @@ export class ModbusSlaveConfigComponent implements ControlValueAccessor, Validat } private updateEnablingByProtocol(type: ModbusProtocolType): void { - if (type === ModbusProtocolType.Serial) { - if (this.slaveConfigFormGroup.get('sendDataToThingsBoard').value) { - this.serialSpecificControlKeys.forEach(key => this.slaveConfigFormGroup.get(key)?.enable({emitEvent: false})); - } - this.tcpUdpSpecificControlKeys.forEach(key => this.slaveConfigFormGroup.get(key)?.disable({emitEvent: false})); - } else { - this.serialSpecificControlKeys.forEach(key => this.slaveConfigFormGroup.get(key)?.disable({emitEvent: false})); - if (this.slaveConfigFormGroup.get('sendDataToThingsBoard').value) { - this.tcpUdpSpecificControlKeys.forEach(key => this.slaveConfigFormGroup.get(key)?.enable({emitEvent: false})); - } + const enableKeys = type === ModbusProtocolType.Serial ? this.serialSpecificControlKeys : this.tcpUdpSpecificControlKeys; + const disableKeys = type === ModbusProtocolType.Serial ? this.tcpUdpSpecificControlKeys : this.serialSpecificControlKeys; + + if (this.isSlaveEnabled) { + enableKeys.forEach(key => this.slaveConfigFormGroup.get(key)?.enable({ emitEvent: false })); } - }; + + disableKeys.forEach(key => this.slaveConfigFormGroup.get(key)?.disable({ emitEvent: false })); + } private updateSlaveConfig(slaveConfig: ModbusSlave): void { const { + type = ModbusProtocolType.TCP, + method = ModbusMethodType.RTU, + unitId = 0, + deviceName = '', + deviceType = '', + pollPeriod = 5000, + sendDataToThingsBoard = false, + byteOrder = ModbusOrderType.BIG, + security = {}, + identity = { + vendorName: '', + productCode: '', + vendorUrl: '', + productName: '', + modelName: '', + }, + values = {} as ModbusRegisterValues, + baudrate = this.modbusBaudrates[0], + host = '', + port = null, + } = slaveConfig; + + const slaveState: ModbusSlave = { type, method, unitId, deviceName, deviceType, pollPeriod, - sendDataToThingsBoard, + sendDataToThingsBoard: !!sendDataToThingsBoard, byteOrder, security, identity, values, baudrate, - host, - port, - } = slaveConfig; - let slaveState: ModbusSlave = { - host: host ?? '', - type: type ?? ModbusProtocolType.TCP, - method: method ?? ModbusMethodType.RTU, - unitId: unitId ?? 0, - deviceName: deviceName ?? '', - deviceType: deviceType ?? '', - pollPeriod: pollPeriod ?? 5000, - sendDataToThingsBoard: !!sendDataToThingsBoard, - byteOrder: byteOrder ?? ModbusOrderType.BIG, - security: security ?? {}, - identity: identity ?? { - vendorName: '', - productCode: '', - vendorUrl: '', - productName: '', - modelName: '', - }, - values: values ?? {} as ModbusRegisterValues, - port: port ?? null, - baudrate: baudrate ?? this.modbusBaudrates[0], + host: type === ModbusProtocolType.Serial ? '' : host, + port: type === ModbusProtocolType.Serial ? null : port, + serialPort: (type === ModbusProtocolType.Serial ? port : '') as string, }; - if (slaveConfig.type === ModbusProtocolType.Serial) { - slaveState = { ...slaveState, serialPort: port, host: '', port: null } as ModbusSlave; - } else { - slaveState = { ...slaveState, serialPort: '' } as ModbusSlave; - } - this.slaveConfigFormGroup.setValue(slaveState, {emitEvent: false}); + + this.slaveConfigFormGroup.setValue(slaveState, { emitEvent: false }); } } diff --git a/ui-ngx/src/app/modules/home/components/widget/lib/gateway/connectors-configuration/modbus/modbus-slave-dialog/modbus-slave-dialog.component.html b/ui-ngx/src/app/modules/home/components/widget/lib/gateway/connectors-configuration/modbus/modbus-slave-dialog/modbus-slave-dialog.component.html index 60ef25f875..31d2202fcb 100644 --- a/ui-ngx/src/app/modules/home/components/widget/lib/gateway/connectors-configuration/modbus/modbus-slave-dialog/modbus-slave-dialog.component.html +++ b/ui-ngx/src/app/modules/home/components/widget/lib/gateway/connectors-configuration/modbus/modbus-slave-dialog/modbus-slave-dialog.component.html @@ -105,10 +105,8 @@ warning diff --git a/ui-ngx/src/app/modules/home/components/widget/lib/gateway/connectors-configuration/modbus/modbus-slave-dialog/modbus-slave-dialog.component.ts b/ui-ngx/src/app/modules/home/components/widget/lib/gateway/connectors-configuration/modbus/modbus-slave-dialog/modbus-slave-dialog.component.ts index 8c6fb87e11..aae327ee6f 100644 --- a/ui-ngx/src/app/modules/home/components/widget/lib/gateway/connectors-configuration/modbus/modbus-slave-dialog/modbus-slave-dialog.component.ts +++ b/ui-ngx/src/app/modules/home/components/widget/lib/gateway/connectors-configuration/modbus/modbus-slave-dialog/modbus-slave-dialog.component.ts @@ -17,14 +17,13 @@ import { ChangeDetectionStrategy, Component, forwardRef, Inject, OnDestroy } from '@angular/core'; import { FormBuilder, + FormControl, NG_VALIDATORS, NG_VALUE_ACCESSOR, - UntypedFormControl, UntypedFormGroup, Validators, } from '@angular/forms'; import { - MappingInfo, ModbusBaudrates, ModbusByteSizes, ModbusMethodLabelsMap, @@ -35,6 +34,7 @@ import { ModbusProtocolLabelsMap, ModbusProtocolType, ModbusSerialMethodType, + ModbusSlaveInfo, noLeadTrailSpacesRegex, PortLimits, SlaveConfig, @@ -80,15 +80,15 @@ import { isEqual } from '@core/utils'; styles: [` :host { .slaves-config-container { - width: 900px; - } + width: 80vw; + max-width: 900px; } } `], }) export class ModbusSlaveDialogComponent extends DialogComponent implements OnDestroy { slaveConfigFormGroup: UntypedFormGroup; - showSecurityControl: UntypedFormControl; + showSecurityControl: FormControl; portLimits = PortLimits; readonly modbusProtocolTypes = Object.values(ModbusProtocolType); @@ -104,8 +104,9 @@ export class ModbusSlaveDialogComponent extends DialogComponent(); @@ -113,7 +114,7 @@ export class ModbusSlaveDialogComponent extends DialogComponent, protected router: Router, - @Inject(MAT_DIALOG_DATA) public data: MappingInfo, + @Inject(MAT_DIALOG_DATA) public data: ModbusSlaveInfo, public dialogRef: MatDialogRef, ) { super(store, router, dialogRef); @@ -121,42 +122,38 @@ export class ModbusSlaveDialogComponent extends DialogComponent { - this.updateControlsEnabling(type); - }); + this.slaveConfigFormGroup.get('type').valueChanges + .pipe(takeUntil(this.destroy$)) + .subscribe(type => this.updateControlsEnabling(type)); } private updateControlsEnabling(type: ModbusProtocolType): void { - if (type === ModbusProtocolType.Serial) { - this.serialSpecificControlKeys.forEach(key => this.slaveConfigFormGroup.get(key)?.enable({emitEvent: false})); - this.tcpUdpSpecificControlKeys.forEach(key => this.slaveConfigFormGroup.get(key)?.disable({emitEvent: false})); - } else { - this.serialSpecificControlKeys.forEach(key => this.slaveConfigFormGroup.get(key)?.disable({emitEvent: false})); - this.tcpUdpSpecificControlKeys.forEach(key => this.slaveConfigFormGroup.get(key)?.enable({emitEvent: false})); - } - }; + const [enableKeys, disableKeys] = type === ModbusProtocolType.Serial + ? [this.serialSpecificControlKeys, this.tcpUdpSpecificControlKeys] + : [this.tcpUdpSpecificControlKeys, this.serialSpecificControlKeys]; + + enableKeys.forEach(key => this.slaveConfigFormGroup.get(key)?.enable({ emitEvent: false })); + disableKeys.forEach(key => this.slaveConfigFormGroup.get(key)?.disable({ emitEvent: false })); + + this.updateSecurityEnabling(this.showSecurityControl.value); + } private observeShowSecurity(): void { - this.showSecurityControl.valueChanges.pipe(takeUntil(this.destroy$)).subscribe(value => { - if (value) { - this.slaveConfigFormGroup.get('security').enable({emitEvent: false}); - } else { - this.slaveConfigFormGroup.get('security').disable({emitEvent: false}); - } - }); + this.showSecurityControl.valueChanges + .pipe(takeUntil(this.destroy$)) + .subscribe(value => this.updateSecurityEnabling(value)); + } + + private updateSecurityEnabling(isEnabled: boolean): void { + if (isEnabled) { + this.slaveConfigFormGroup.get('security').enable({emitEvent: false}); + } else { + this.slaveConfigFormGroup.get('security').disable({emitEvent: false}); + } } } diff --git a/ui-ngx/src/app/modules/home/components/widget/lib/gateway/connectors-configuration/modbus/modbus-values/modbus-values.component.ts b/ui-ngx/src/app/modules/home/components/widget/lib/gateway/connectors-configuration/modbus/modbus-values/modbus-values.component.ts index 0e57d06d85..e111fee7b1 100644 --- a/ui-ngx/src/app/modules/home/components/widget/lib/gateway/connectors-configuration/modbus/modbus-values/modbus-values.component.ts +++ b/ui-ngx/src/app/modules/home/components/widget/lib/gateway/connectors-configuration/modbus/modbus-values/modbus-values.component.ts @@ -42,6 +42,7 @@ import { ModbusRegisterTranslationsMap, ModbusRegisterType, ModbusRegisterValues, + ModbusValue, ModbusValueKey, ModbusValues, ModbusValuesState, @@ -103,7 +104,7 @@ export class ModbusValuesComponent implements ControlValueAccessor, Validator, O ModbusValueKey = ModbusValueKey; valuesFormGroup: FormGroup; - private onChange: (value: string) => void; + private onChange: (value: ModbusValuesState) => void; private onTouched: () => void; private destroy$ = new Subject(); @@ -125,7 +126,7 @@ export class ModbusValuesComponent implements ControlValueAccessor, Validator, O this.destroy$.complete(); } - registerOnChange(fn: (value: string) => void): void { + registerOnChange(fn: (value: ModbusValuesState) => void): void { this.onChange = fn; } @@ -135,15 +136,15 @@ export class ModbusValuesComponent implements ControlValueAccessor, Validator, O writeValue(values: ModbusValuesState): void { if (this.singleMode) { - this.valuesFormGroup.setValue(this.getSingleRegisterState(values as ModbusValues), {emitEvent: false}); + this.valuesFormGroup.setValue(this.getSingleRegisterState(values as ModbusValues), { emitEvent: false }); } else { - const registers = values as ModbusRegisterValues; + const { holding_registers, coils_initializer, input_registers, discrete_inputs } = values as ModbusRegisterValues; this.valuesFormGroup.setValue({ - holding_registers: this.getSingleRegisterState(registers.holding_registers), - coils_initializer: this.getSingleRegisterState(registers.coils_initializer), - input_registers: this.getSingleRegisterState(registers.input_registers), - discrete_inputs: this.getSingleRegisterState(registers.discrete_inputs), - }, {emitEvent: false}); + holding_registers: this.getSingleRegisterState(holding_registers), + coils_initializer: this.getSingleRegisterState(coils_initializer), + input_registers: this.getSingleRegisterState(input_registers), + discrete_inputs: this.getSingleRegisterState(discrete_inputs), + }, { emitEvent: false }); } this.cdr.markForCheck(); } @@ -159,69 +160,63 @@ export class ModbusValuesComponent implements ControlValueAccessor, Validator, O this.cdr.markForCheck(); } - getValueGroup(valueKey: ModbusValueKey, register?: ModbusRegisterType) { + getValueGroup(valueKey: ModbusValueKey, register?: ModbusRegisterType): FormGroup { return register ? this.valuesFormGroup.get(register).get(valueKey).value : this.valuesFormGroup.get(valueKey).value; } manageKeys($event: Event, matButton: MatButton, keysType: ModbusValueKey, register?: ModbusRegisterType): void { - if ($event) { - $event.stopPropagation(); - } + $event.stopPropagation(); const trigger = matButton._elementRef.nativeElement; if (this.popoverService.hasPopover(trigger)) { this.popoverService.hidePopover(trigger); - } else { - const group = this.valuesFormGroup; - - const keysControl = register ? group.get(register).get(keysType) : group.get(keysType); - const ctx = { - values: keysControl.value, - isMaster: !this.singleMode, - keysType, - panelTitle: ModbusKeysPanelTitleTranslationsMap.get(keysType), - addKeyTitle: ModbusKeysAddKeyTranslationsMap.get(keysType), - deleteKeyTitle: ModbusKeysDeleteKeyTranslationsMap.get(keysType), - noKeysText: ModbusKeysNoKeysTextTranslationsMap.get(keysType) - }; - const dataKeysPanelPopover = this.popoverService.displayPopover( - trigger, - this.renderer, - this.viewContainerRef, - ModbusDataKeysPanelComponent, - 'leftBottom', - false, - null, - ctx, - {}, - {}, - {}, - true - ); - dataKeysPanelPopover.tbComponentRef.instance.popover = dataKeysPanelPopover; - dataKeysPanelPopover.tbComponentRef.instance.keysDataApplied.pipe(takeUntil(this.destroy$)).subscribe((keysData) => { - dataKeysPanelPopover.hide(); - keysControl.patchValue(keysData); - keysControl.markAsDirty(); - this.cdr.markForCheck(); - }); + return; } + + const keysControl = this.getValueGroup(keysType, register); + const ctx = { + values: keysControl.value, + isMaster: !this.singleMode, + keysType, + panelTitle: ModbusKeysPanelTitleTranslationsMap.get(keysType), + addKeyTitle: ModbusKeysAddKeyTranslationsMap.get(keysType), + deleteKeyTitle: ModbusKeysDeleteKeyTranslationsMap.get(keysType), + noKeysText: ModbusKeysNoKeysTextTranslationsMap.get(keysType) + }; + const dataKeysPanelPopover = this.popoverService.displayPopover( + trigger, + this.renderer, + this.viewContainerRef, + ModbusDataKeysPanelComponent, + 'leftBottom', + false, + null, + ctx, + {}, + {}, + {}, + true + ); + dataKeysPanelPopover.tbComponentRef.instance.popover = dataKeysPanelPopover; + dataKeysPanelPopover.tbComponentRef.instance.keysDataApplied.pipe(takeUntil(this.destroy$)).subscribe((keysData: ModbusValue[]) => { + dataKeysPanelPopover.hide(); + keysControl.patchValue(keysData); + keysControl.markAsDirty(); + this.cdr.markForCheck(); + }); } private initializeValuesFormGroup(): void { + const getValuesFormGroup = () => this.fb.group(this.modbusValueKeys.reduce((acc, key) => { + acc[key] = this.fb.control([[], []]); + return acc; + }, {})); + if (this.singleMode) { - this.valuesFormGroup = this.fb.group(this.modbusValueKeys.reduce((acc, key) => { - acc[key] = this.fb.control([[], []]); - return acc; - }, {})); + this.valuesFormGroup = getValuesFormGroup(); } else { this.valuesFormGroup = this.fb.group( this.modbusRegisterTypes.reduce((registersAcc, register) => { - - registersAcc[register] = this.fb.group(this.modbusValueKeys.reduce((acc, key) => { - acc[key] = this.fb.control([[], []]); - return acc; - }, {})); - + registersAcc[register] = getValuesFormGroup(); return registersAcc; }, {}) ); diff --git a/ui-ngx/src/app/modules/home/components/widget/lib/gateway/gateway-widget.models.ts b/ui-ngx/src/app/modules/home/components/widget/lib/gateway/gateway-widget.models.ts index 2db6d9d2fe..3bdb67069a 100644 --- a/ui-ngx/src/app/modules/home/components/widget/lib/gateway/gateway-widget.models.ts +++ b/ui-ngx/src/app/modules/home/components/widget/lib/gateway/gateway-widget.models.ts @@ -485,6 +485,11 @@ export interface MappingInfo { buttonTitle: string; } +export interface ModbusSlaveInfo { + value: SlaveConfig; + buttonTitle: string; +} + export enum ConnectorConfigurationModes { BASIC = 'basic', ADVANCED = 'advanced' @@ -852,24 +857,6 @@ export enum ModbusObjectCountByDataType { '64float' = 4, } -export enum ModbusValueField { - Tag = 'tag', - Type = 'type', - ObjectsCount = 'objectsCount', - Address = 'address', - Value = 'value', -} - -export const ModbusFieldsTranslationsMap = new Map( - [ - [ModbusValueField.Tag, 'gateway.tag'], - [ModbusValueField.Type, 'gateway.type'], - [ModbusValueField.ObjectsCount, 'gateway.objects_count'], - [ModbusValueField.Address, 'gateway.address'], - [ModbusValueField.Value, 'gateway.value'] - ] -); - export enum ModbusValueKey { ATTRIBUTES = 'attributes', TIMESERIES = 'timeseries', @@ -946,7 +933,7 @@ export interface SlaveConfig { pollPeriod: number; unitId: number; deviceName: string; - deviceType?: string; + deviceType: string; sendDataOnlyOnChange: boolean; connectAttemptTimeMs: number; connectAttemptCount: number; @@ -959,7 +946,7 @@ export interface SlaveConfig { baudrate?: number; stopbits?: number; bytesize?: number; - parity?: string; + parity?: ModbusParity; strict?: boolean; } diff --git a/ui-ngx/src/app/modules/home/components/widget/widget-components.module.ts b/ui-ngx/src/app/modules/home/components/widget/widget-components.module.ts index 1e4093c22c..504f98420e 100644 --- a/ui-ngx/src/app/modules/home/components/widget/widget-components.module.ts +++ b/ui-ngx/src/app/modules/home/components/widget/widget-components.module.ts @@ -208,25 +208,26 @@ import { LabelCardWidgetComponent, LabelValueCardWidgetComponent, UnreadNotificationWidgetComponent, - NotificationTypeFilterPanelComponent], - imports: [ - CommonModule, - SharedModule, - RpcWidgetsModule, - HomePageWidgetsModule, - SharedHomeComponentsModule, - RestConnectorSecurityComponent, - GatewayHelpLinkPipe, - BrokerConfigControlComponent, - WorkersConfigControlComponent, - OpcServerConfigComponent, - MqttBasicConfigComponent, - MappingTableComponent, - OpcUaBasicConfigComponent, - KeyValueIsNotEmptyPipe, - ModbusBasicConfigComponent, - EllipsisChipListDirective, - ], + NotificationTypeFilterPanelComponent + ], + imports: [ + CommonModule, + SharedModule, + RpcWidgetsModule, + HomePageWidgetsModule, + SharedHomeComponentsModule, + RestConnectorSecurityComponent, + GatewayHelpLinkPipe, + BrokerConfigControlComponent, + WorkersConfigControlComponent, + OpcServerConfigComponent, + MqttBasicConfigComponent, + MappingTableComponent, + OpcUaBasicConfigComponent, + KeyValueIsNotEmptyPipe, + ModbusBasicConfigComponent, + EllipsisChipListDirective, + ], exports: [ EntitiesTableWidgetComponent, AlarmsTableWidgetComponent, diff --git a/ui-ngx/src/app/modules/home/pipes/gateway-port-tooltip.pipe.ts b/ui-ngx/src/app/modules/home/pipes/gateway-port-tooltip.pipe.ts index 3981499a26..fcf5766c05 100644 --- a/ui-ngx/src/app/modules/home/pipes/gateway-port-tooltip.pipe.ts +++ b/ui-ngx/src/app/modules/home/pipes/gateway-port-tooltip.pipe.ts @@ -30,12 +30,12 @@ export class GatewayPortTooltipPipe implements PipeTransform { transform(portControl: AbstractControl): string { if (portControl.hasError('required')) { return this.translate.instant('gateway.port-required'); - } else if ( - portControl.hasError('min') || - portControl.hasError('max') - ) { - return this.translate.instant('gateway.port-limits-error', - {min: PortLimits.MIN, max: PortLimits.MAX}); + } + if (portControl.hasError('min') || portControl.hasError('max')) { + return this.translate.instant('gateway.port-limits-error', { + min: PortLimits.MIN, + max: PortLimits.MAX, + }); } return ''; } diff --git a/ui-ngx/src/app/shared/directives/ellipsis-chip-list.directive.ts b/ui-ngx/src/app/shared/directives/ellipsis-chip-list.directive.ts index bb2847aeee..c320a2f78a 100644 --- a/ui-ngx/src/app/shared/directives/ellipsis-chip-list.directive.ts +++ b/ui-ngx/src/app/shared/directives/ellipsis-chip-list.directive.ts @@ -61,8 +61,11 @@ export class EllipsisChipListDirective implements OnDestroy { ).subscribe(() => { this.adjustChips(); }); + this.observeIntersection(); + } - this.intersectionObserver = new IntersectionObserver((entries) => { + private observeIntersection(): void { + this.intersectionObserver = new IntersectionObserver(entries => { entries.forEach(entry => { if (entry.isIntersecting) { this.adjustChips(); diff --git a/ui-ngx/src/assets/locale/locale.constant-en_US.json b/ui-ngx/src/assets/locale/locale.constant-en_US.json index fb8f695619..967e6a0122 100644 --- a/ui-ngx/src/assets/locale/locale.constant-en_US.json +++ b/ui-ngx/src/assets/locale/locale.constant-en_US.json @@ -2756,6 +2756,7 @@ }, "gateway": { "address": "Address", + "address-required": "Address required", "add-entry": "Add configuration", "add-attribute": "Add attribute", "add-attribute-update": "Add attribute update", diff --git a/ui-ngx/src/assets/metadata/connector-default-configs/modbus.json b/ui-ngx/src/assets/metadata/connector-default-configs/modbus.json index fee4e745d6..c84a2278b9 100644 --- a/ui-ngx/src/assets/metadata/connector-default-configs/modbus.json +++ b/ui-ngx/src/assets/metadata/connector-default-configs/modbus.json @@ -16,6 +16,7 @@ "pollPeriod": 5000, "unitId": 1, "deviceName": "Temp Sensor", + "deviceType": "default", "sendDataOnlyOnChange": true, "connectAttemptTimeMs": 5000, "connectAttemptCount": 5, From 0b8c122f0ac079a703d827d7fee02f8664896910 Mon Sep 17 00:00:00 2001 From: mpetrov Date: Thu, 25 Jul 2024 12:11:19 +0300 Subject: [PATCH 15/20] refactoring --- .../modbus-data-keys-panel.component.html | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/ui-ngx/src/app/modules/home/components/widget/lib/gateway/connectors-configuration/modbus/modbus-data-keys-panel/modbus-data-keys-panel.component.html b/ui-ngx/src/app/modules/home/components/widget/lib/gateway/connectors-configuration/modbus/modbus-data-keys-panel/modbus-data-keys-panel.component.html index 5285422bd6..02d2d1491d 100644 --- a/ui-ngx/src/app/modules/home/components/widget/lib/gateway/connectors-configuration/modbus/modbus-data-keys-panel/modbus-data-keys-panel.component.html +++ b/ui-ngx/src/app/modules/home/components/widget/lib/gateway/connectors-configuration/modbus/modbus-data-keys-panel/modbus-data-keys-panel.component.html @@ -76,7 +76,12 @@
- {{ ModbusFunctionCodeTranslationsMap.get(code) | translate }} + + {{ ModbusFunctionCodeTranslationsMap.get(code) | translate }} +
From 7624517a5e7673ef056712d0d18b7c74c9852985 Mon Sep 17 00:00:00 2001 From: mpetrov Date: Thu, 25 Jul 2024 12:17:08 +0300 Subject: [PATCH 16/20] refactoring --- .../modbus-slave-dialog.component.ts | 64 ++++++++++--------- 1 file changed, 35 insertions(+), 29 deletions(-) diff --git a/ui-ngx/src/app/modules/home/components/widget/lib/gateway/connectors-configuration/modbus/modbus-slave-dialog/modbus-slave-dialog.component.ts b/ui-ngx/src/app/modules/home/components/widget/lib/gateway/connectors-configuration/modbus/modbus-slave-dialog/modbus-slave-dialog.component.ts index aae327ee6f..44f093e06e 100644 --- a/ui-ngx/src/app/modules/home/components/widget/lib/gateway/connectors-configuration/modbus/modbus-slave-dialog/modbus-slave-dialog.component.ts +++ b/ui-ngx/src/app/modules/home/components/widget/lib/gateway/connectors-configuration/modbus/modbus-slave-dialog/modbus-slave-dialog.component.ts @@ -120,6 +120,39 @@ export class ModbusSlaveDialogComponent extends DialogComponent Date: Thu, 25 Jul 2024 12:44:57 +0300 Subject: [PATCH 17/20] minor fix --- .../modbus/modbus-slave-dialog/modbus-slave-dialog.component.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ui-ngx/src/app/modules/home/components/widget/lib/gateway/connectors-configuration/modbus/modbus-slave-dialog/modbus-slave-dialog.component.ts b/ui-ngx/src/app/modules/home/components/widget/lib/gateway/connectors-configuration/modbus/modbus-slave-dialog/modbus-slave-dialog.component.ts index 44f093e06e..0aa4174c25 100644 --- a/ui-ngx/src/app/modules/home/components/widget/lib/gateway/connectors-configuration/modbus/modbus-slave-dialog/modbus-slave-dialog.component.ts +++ b/ui-ngx/src/app/modules/home/components/widget/lib/gateway/connectors-configuration/modbus/modbus-slave-dialog/modbus-slave-dialog.component.ts @@ -143,7 +143,7 @@ export class ModbusSlaveDialogComponent extends DialogComponent Date: Thu, 25 Jul 2024 15:30:07 +0300 Subject: [PATCH 18/20] method type switch fix and data keys crash fix --- .../modbus-data-keys-panel.component.ts | 14 +++++++++++--- .../modbus-slave-config.component.ts | 16 +++++++++++++++- .../modbus-slave-dialog.component.ts | 16 +++++++++++++++- .../modbus-values/modbus-values.component.html | 16 ++++++++-------- .../modbus-values/modbus-values.component.ts | 4 +++- 5 files changed, 52 insertions(+), 14 deletions(-) diff --git a/ui-ngx/src/app/modules/home/components/widget/lib/gateway/connectors-configuration/modbus/modbus-data-keys-panel/modbus-data-keys-panel.component.ts b/ui-ngx/src/app/modules/home/components/widget/lib/gateway/connectors-configuration/modbus/modbus-data-keys-panel/modbus-data-keys-panel.component.ts index d0bfcf1e76..c68bd8086e 100644 --- a/ui-ngx/src/app/modules/home/components/widget/lib/gateway/connectors-configuration/modbus/modbus-data-keys-panel/modbus-data-keys-panel.component.ts +++ b/ui-ngx/src/app/modules/home/components/widget/lib/gateway/connectors-configuration/modbus/modbus-data-keys-panel/modbus-data-keys-panel.component.ts @@ -105,7 +105,7 @@ export class ModbusDataKeysPanelComponent implements OnInit, OnDestroy { type: [ModbusDataType.BYTES, [Validators.required]], address: [null, [Validators.required]], objectsCount: [1, [Validators.required]], - functionCode: [this.getDefaultFunctionCodes()[0]], + functionCode: [{ value: this.getDefaultFunctionCodes()[0], disabled: !this.withFunctionCode }, [Validators.required]], id: [{value: generateSecret(5), disabled: true}], }); this.observeKeyDataType(dataKeyFormGroup); @@ -154,7 +154,7 @@ export class ModbusDataKeysPanelComponent implements OnInit, OnDestroy { type: [type, [Validators.required]], address: [address, [Validators.required]], objectsCount: [objectsCount, [Validators.required]], - functionCode: [functionCode, [Validators.required]], + functionCode: [{ value: functionCode, disabled: !this.withFunctionCode }, [Validators.required]], id: [{ value: generateSecret(5), disabled: true }], }); } @@ -164,10 +164,18 @@ export class ModbusDataKeysPanelComponent implements OnInit, OnDestroy { if (!this.editableDataTypes.includes(dataType)) { keyFormGroup.get('objectsCount').patchValue(ModbusObjectCountByDataType[dataType], {emitEvent: false}); } - this.functionCodesMap.set(keyFormGroup.get('id').value, this.getFunctionCodes(dataType)); + this.updateFunctionCodes(keyFormGroup, dataType); }); } + private updateFunctionCodes(keyFormGroup: FormGroup, dataType: ModbusDataType): void { + const functionCodes = this.getFunctionCodes(dataType); + this.functionCodesMap.set(keyFormGroup.get('id').value, functionCodes); + if (!functionCodes.includes(keyFormGroup.get('functionCode').value)) { + keyFormGroup.get('functionCode').patchValue(functionCodes[0], {emitEvent: false}); + } + } + private getFunctionCodes(dataType: ModbusDataType): number[] { if (this.keysType === ModbusValueKey.ATTRIBUTES_UPDATES) { return dataType === ModbusDataType.STRING diff --git a/ui-ngx/src/app/modules/home/components/widget/lib/gateway/connectors-configuration/modbus/modbus-slave-config/modbus-slave-config.component.ts b/ui-ngx/src/app/modules/home/components/widget/lib/gateway/connectors-configuration/modbus/modbus-slave-config/modbus-slave-config.component.ts index d44272734d..3ff9e929ed 100644 --- a/ui-ngx/src/app/modules/home/components/widget/lib/gateway/connectors-configuration/modbus/modbus-slave-config/modbus-slave-config.component.ts +++ b/ui-ngx/src/app/modules/home/components/widget/lib/gateway/connectors-configuration/modbus/modbus-slave-config/modbus-slave-config.component.ts @@ -175,7 +175,21 @@ export class ModbusSlaveConfigComponent implements ControlValueAccessor, Validat private observeTypeChange(): void { this.slaveConfigFormGroup.get('type').valueChanges .pipe(takeUntil(this.destroy$)) - .subscribe(() => this.updateFormEnableState(this.isSlaveEnabled)); + .subscribe(type => { + this.updateFormEnableState(this.isSlaveEnabled); + this.updateMethodType(type); + }); + } + + private updateMethodType(type: ModbusProtocolType): void { + if (this.slaveConfigFormGroup.get('method').value !== ModbusMethodType.RTU) { + this.slaveConfigFormGroup.get('method').patchValue( + type === ModbusProtocolType.Serial + ? ModbusSerialMethodType.ASCII + : ModbusMethodType.SOCKET, + {emitEvent: false} + ); + } } private observeFormEnable(): void { diff --git a/ui-ngx/src/app/modules/home/components/widget/lib/gateway/connectors-configuration/modbus/modbus-slave-dialog/modbus-slave-dialog.component.ts b/ui-ngx/src/app/modules/home/components/widget/lib/gateway/connectors-configuration/modbus/modbus-slave-dialog/modbus-slave-dialog.component.ts index 0aa4174c25..38a77ac587 100644 --- a/ui-ngx/src/app/modules/home/components/widget/lib/gateway/connectors-configuration/modbus/modbus-slave-dialog/modbus-slave-dialog.component.ts +++ b/ui-ngx/src/app/modules/home/components/widget/lib/gateway/connectors-configuration/modbus/modbus-slave-dialog/modbus-slave-dialog.component.ts @@ -201,7 +201,21 @@ export class ModbusSlaveDialogComponent extends DialogComponent this.updateControlsEnabling(type)); + .subscribe(type => { + this.updateControlsEnabling(type); + this.updateMethodType(type); + }); + } + + private updateMethodType(type: ModbusProtocolType): void { + if (this.slaveConfigFormGroup.get('method').value !== ModbusMethodType.RTU) { + this.slaveConfigFormGroup.get('method').patchValue( + type === ModbusProtocolType.Serial + ? ModbusSerialMethodType.ASCII + : ModbusMethodType.SOCKET, + {emitEvent: false} + ); + } } private updateControlsEnabling(type: ModbusProtocolType): void { diff --git a/ui-ngx/src/app/modules/home/components/widget/lib/gateway/connectors-configuration/modbus/modbus-values/modbus-values.component.html b/ui-ngx/src/app/modules/home/components/widget/lib/gateway/connectors-configuration/modbus/modbus-values/modbus-values.component.html index 5e0b4d7649..a6b4382638 100644 --- a/ui-ngx/src/app/modules/home/components/widget/lib/gateway/connectors-configuration/modbus/modbus-values/modbus-values.component.html +++ b/ui-ngx/src/app/modules/home/components/widget/lib/gateway/connectors-configuration/modbus/modbus-values/modbus-values.component.html @@ -36,8 +36,8 @@
gateway.attributes
- - + + {{ attribute.tag }} @@ -57,8 +57,8 @@
gateway.timeseries
- - + + {{ telemetry.tag }} @@ -78,8 +78,8 @@
gateway.attribute-updates
- - + + {{ attributeUpdate.tag }} @@ -99,8 +99,8 @@
gateway.rpc-requests
- - + + {{ rpcRequest.tag }} diff --git a/ui-ngx/src/app/modules/home/components/widget/lib/gateway/connectors-configuration/modbus/modbus-values/modbus-values.component.ts b/ui-ngx/src/app/modules/home/components/widget/lib/gateway/connectors-configuration/modbus/modbus-values/modbus-values.component.ts index e111fee7b1..caff181b6a 100644 --- a/ui-ngx/src/app/modules/home/components/widget/lib/gateway/connectors-configuration/modbus/modbus-values/modbus-values.component.ts +++ b/ui-ngx/src/app/modules/home/components/widget/lib/gateway/connectors-configuration/modbus/modbus-values/modbus-values.component.ts @@ -161,7 +161,9 @@ export class ModbusValuesComponent implements ControlValueAccessor, Validator, O } getValueGroup(valueKey: ModbusValueKey, register?: ModbusRegisterType): FormGroup { - return register ? this.valuesFormGroup.get(register).get(valueKey).value : this.valuesFormGroup.get(valueKey).value; + return register + ? this.valuesFormGroup.get(register).get(valueKey) as FormGroup + : this.valuesFormGroup.get(valueKey) as FormGroup; } manageKeys($event: Event, matButton: MatButton, keysType: ModbusValueKey, register?: ModbusRegisterType): void { From 2ff3d0bafc883e4e650e2b0de4ed436c03ca3737 Mon Sep 17 00:00:00 2001 From: mpetrov Date: Thu, 25 Jul 2024 15:58:48 +0300 Subject: [PATCH 19/20] server register values fix --- .../modbus-data-keys-panel/modbus-data-keys-panel.component.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ui-ngx/src/app/modules/home/components/widget/lib/gateway/connectors-configuration/modbus/modbus-data-keys-panel/modbus-data-keys-panel.component.ts b/ui-ngx/src/app/modules/home/components/widget/lib/gateway/connectors-configuration/modbus/modbus-data-keys-panel/modbus-data-keys-panel.component.ts index c68bd8086e..f1919e7a57 100644 --- a/ui-ngx/src/app/modules/home/components/widget/lib/gateway/connectors-configuration/modbus/modbus-data-keys-panel/modbus-data-keys-panel.component.ts +++ b/ui-ngx/src/app/modules/home/components/widget/lib/gateway/connectors-configuration/modbus/modbus-data-keys-panel/modbus-data-keys-panel.component.ts @@ -84,8 +84,8 @@ export class ModbusDataKeysPanelComponent implements OnInit, OnDestroy { constructor(private fb: UntypedFormBuilder) {} ngOnInit(): void { - this.keysListFormArray = this.prepareKeysFormArray(this.values); this.withFunctionCode = !this.isMaster || (this.keysType !== ModbusValueKey.ATTRIBUTES && this.keysType !== ModbusValueKey.TIMESERIES); + this.keysListFormArray = this.prepareKeysFormArray(this.values); this.defaultFunctionCodes = this.getDefaultFunctionCodes(); } From 2837edbbd1f6a90059164324c3a8f907c8a9746a Mon Sep 17 00:00:00 2001 From: mpetrov Date: Thu, 25 Jul 2024 16:31:58 +0300 Subject: [PATCH 20/20] refactoring --- .../modbus-slave-config.component.html | 10 +++++----- .../modbus-slave-config.component.ts | 8 ++++++-- .../modbus-slave-dialog.component.html | 14 +++++++------- .../modbus-slave-dialog.component.ts | 6 +++++- .../metadata/connector-default-configs/modbus.json | 2 +- 5 files changed, 24 insertions(+), 16 deletions(-) diff --git a/ui-ngx/src/app/modules/home/components/widget/lib/gateway/connectors-configuration/modbus/modbus-slave-config/modbus-slave-config.component.html b/ui-ngx/src/app/modules/home/components/widget/lib/gateway/connectors-configuration/modbus/modbus-slave-config/modbus-slave-config.component.html index 56fc579361..d37c8cc9ee 100644 --- a/ui-ngx/src/app/modules/home/components/widget/lib/gateway/connectors-configuration/modbus/modbus-slave-config/modbus-slave-config.component.html +++ b/ui-ngx/src/app/modules/home/components/widget/lib/gateway/connectors-configuration/modbus/modbus-slave-config/modbus-slave-config.component.html @@ -34,7 +34,7 @@
-
@@ -54,7 +54,7 @@
-
@@ -102,7 +102,7 @@
- {{ ModbusMethodLabelsMap.get(method) }} @@ -168,7 +168,7 @@
-
+
gateway.baudrate
@@ -196,7 +196,7 @@
-
+
diff --git a/ui-ngx/src/app/modules/home/components/widget/lib/gateway/connectors-configuration/modbus/modbus-slave-config/modbus-slave-config.component.ts b/ui-ngx/src/app/modules/home/components/widget/lib/gateway/connectors-configuration/modbus/modbus-slave-config/modbus-slave-config.component.ts index 3ff9e929ed..05083a087d 100644 --- a/ui-ngx/src/app/modules/home/components/widget/lib/gateway/connectors-configuration/modbus/modbus-slave-config/modbus-slave-config.component.ts +++ b/ui-ngx/src/app/modules/home/components/widget/lib/gateway/connectors-configuration/modbus/modbus-slave-config/modbus-slave-config.component.ts @@ -134,6 +134,10 @@ export class ModbusSlaveConfigComponent implements ControlValueAccessor, Validat return this.slaveConfigFormGroup.get('sendDataToThingsBoard').value; } + get protocolType(): ModbusProtocolType { + return this.slaveConfigFormGroup.get('type').value; + } + ngOnDestroy(): void { this.destroy$.next(); this.destroy$.complete(); @@ -207,7 +211,7 @@ export class ModbusSlaveConfigComponent implements ControlValueAccessor, Validat this.showSecurityControl.disable({emitEvent: false}); this.slaveConfigFormGroup.get('sendDataToThingsBoard').enable({emitEvent: false}); } - this.updateEnablingByProtocol(this.slaveConfigFormGroup.get('type').value); + this.updateEnablingByProtocol(this.protocolType); this.updateSecurityEnable(this.showSecurityControl.value); } @@ -218,7 +222,7 @@ export class ModbusSlaveConfigComponent implements ControlValueAccessor, Validat } private updateSecurityEnable(securityEnabled: boolean): void { - if (securityEnabled && this.isSlaveEnabled) { + if (securityEnabled && this.isSlaveEnabled && this.protocolType !== ModbusProtocolType.Serial) { this.slaveConfigFormGroup.get('security').enable({emitEvent: false}); } else { this.slaveConfigFormGroup.get('security').disable({emitEvent: false}); diff --git a/ui-ngx/src/app/modules/home/components/widget/lib/gateway/connectors-configuration/modbus/modbus-slave-dialog/modbus-slave-dialog.component.html b/ui-ngx/src/app/modules/home/components/widget/lib/gateway/connectors-configuration/modbus/modbus-slave-dialog/modbus-slave-dialog.component.html index 31d2202fcb..1e47479f03 100644 --- a/ui-ngx/src/app/modules/home/components/widget/lib/gateway/connectors-configuration/modbus/modbus-slave-dialog/modbus-slave-dialog.component.html +++ b/ui-ngx/src/app/modules/home/components/widget/lib/gateway/connectors-configuration/modbus/modbus-slave-dialog/modbus-slave-dialog.component.html @@ -53,7 +53,7 @@
-
@@ -73,7 +73,7 @@
-
@@ -122,14 +122,14 @@
- {{ ModbusMethodLabelsMap.get(method) }}
- +
gateway.baudrate
@@ -260,7 +260,7 @@
-
+
gateway.word-order
@@ -270,7 +270,7 @@
-
+
@@ -356,7 +356,7 @@
diff --git a/ui-ngx/src/app/modules/home/components/widget/lib/gateway/connectors-configuration/modbus/modbus-slave-dialog/modbus-slave-dialog.component.ts b/ui-ngx/src/app/modules/home/components/widget/lib/gateway/connectors-configuration/modbus/modbus-slave-dialog/modbus-slave-dialog.component.ts index 38a77ac587..cbfc7cc8c2 100644 --- a/ui-ngx/src/app/modules/home/components/widget/lib/gateway/connectors-configuration/modbus/modbus-slave-dialog/modbus-slave-dialog.component.ts +++ b/ui-ngx/src/app/modules/home/components/widget/lib/gateway/connectors-configuration/modbus/modbus-slave-dialog/modbus-slave-dialog.component.ts @@ -128,6 +128,10 @@ export class ModbusSlaveDialogComponent extends DialogComponent