You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@plc4x.apache.org by hu...@apache.org on 2020/12/11 12:10:44 UTC

[plc4x] branch bug/driver-opcua-read-arrays updated: Add support for datatype in address for opc ua client.

This is an automated email from the ASF dual-hosted git repository.

hutcheb pushed a commit to branch bug/driver-opcua-read-arrays
in repository https://gitbox.apache.org/repos/asf/plc4x.git


The following commit(s) were added to refs/heads/bug/driver-opcua-read-arrays by this push:
     new 1e553dd  Add support for datatype in address for opc ua client.
1e553dd is described below

commit 1e553ddeaa9f81cd9c7b20dcc20220c57ba314f1
Author: hutcheb <be...@gmail.com>
AuthorDate: Fri Dec 11 07:10:07 2020 -0500

    Add support for datatype in address for opc ua client.
    
    Have to tidy up still.
---
 .../opcua/connection/OpcuaTcpPlcConnection.java    | 173 +++++++++++++++++----
 .../plc4x/java/opcua/protocol/OpcuaField.java      |  20 ++-
 .../plc4x/java/opcua/OpcuaPlcDriverTest.java       |   2 +-
 3 files changed, 159 insertions(+), 36 deletions(-)

diff --git a/plc4j/drivers/opcua/src/main/java/org/apache/plc4x/java/opcua/connection/OpcuaTcpPlcConnection.java b/plc4j/drivers/opcua/src/main/java/org/apache/plc4x/java/opcua/connection/OpcuaTcpPlcConnection.java
index ac05854..2af4dc6 100644
--- a/plc4j/drivers/opcua/src/main/java/org/apache/plc4x/java/opcua/connection/OpcuaTcpPlcConnection.java
+++ b/plc4j/drivers/opcua/src/main/java/org/apache/plc4x/java/opcua/connection/OpcuaTcpPlcConnection.java
@@ -45,9 +45,7 @@ import org.eclipse.milo.opcua.stack.core.Identifiers;
 import org.eclipse.milo.opcua.stack.core.UaException;
 import org.eclipse.milo.opcua.stack.core.security.SecurityPolicy;
 import org.eclipse.milo.opcua.stack.core.types.builtin.*;
-import org.eclipse.milo.opcua.stack.core.types.builtin.unsigned.UInteger;
-import org.eclipse.milo.opcua.stack.core.types.builtin.unsigned.UShort;
-import org.eclipse.milo.opcua.stack.core.types.builtin.unsigned.UByte;
+import org.eclipse.milo.opcua.stack.core.types.builtin.unsigned.*;
 import org.eclipse.milo.opcua.stack.core.types.enumerated.MonitoringMode;
 import org.eclipse.milo.opcua.stack.core.types.enumerated.TimestampsToReturn;
 import org.eclipse.milo.opcua.stack.core.types.structured.*;
@@ -59,6 +57,7 @@ import java.math.BigDecimal;
 import java.util.stream.Collectors;
 import java.net.InetAddress;
 import java.time.Duration;
+import java.time.LocalDateTime;
 import java.util.*;
 import java.util.concurrent.CompletableFuture;
 import java.util.concurrent.ExecutionException;
@@ -547,34 +546,148 @@ public class OpcuaTcpPlcConnection extends BaseOpcuaPlcConnection {
                 Variant var = null;
                 if (valueObject instanceof ArrayList) {
                     List<PlcValue> plcValueList = (List<PlcValue>) valueObject;
-                    if (plcValueList.get(0) instanceof PlcBOOL) {
-                        List<Boolean> valueList = (plcValueList).stream().map(x -> ((PlcBOOL) x).getBoolean()).collect(Collectors.toList());
-                        var = new Variant(valueList.toArray(new Boolean[valueList.size()]));
-                    } else if (plcValueList.get(0) instanceof PlcSINT) {
-                        List<UByte> valueList = (List<UByte>) (plcValueList).stream().map(x -> UByte.valueOf(((PlcSINT) x).getByte())).collect(Collectors.toList());
-                        var = new Variant(valueList.toArray(new UByte[valueList.size()]));
-                    } else if (plcValueList.get(0) instanceof PlcINT) {
-                        List<Short> valueList = (plcValueList).stream().map(x -> ((PlcINT) x).getShort()).collect(Collectors.toList());
-                        var = new Variant(valueList.toArray(new Short[valueList.size()]));
-                    } else if (plcValueList.get(0) instanceof PlcDINT) {
-                        List<Integer> valueList = (plcValueList).stream().map(x -> ((PlcDINT) x).getInteger()).collect(Collectors.toList());
-                        var = new Variant(valueList.toArray(new Integer[valueList.size()]));
-                    } else if (plcValueList.get(0) instanceof PlcLINT) {
-                        List<Long> valueList = (plcValueList).stream().map(x -> ((PlcLINT) x).getLong()).collect(Collectors.toList());
-                        var = new Variant(valueList.toArray(new Long[valueList.size()]));
-                    } else if (plcValueList.get(0) instanceof PlcREAL) {
-                        List<Float> valueList = (plcValueList).stream().map(x -> ((PlcREAL) x).getFloat()).collect(Collectors.toList());
-                        var = new Variant(valueList.toArray(new Float[valueList.size()]));
-                    } else if (plcValueList.get(0) instanceof PlcLREAL) {
-                        List<Double> valueList = (plcValueList).stream().map(x -> ((PlcLREAL) x).getDouble()).collect(Collectors.toList());
-                        var = new Variant(valueList.toArray(new Double[valueList.size()]));
-                    } else if (plcValueList.get(0) instanceof PlcSTRING) {
-                        List<String> valueList = (plcValueList).stream().map(x -> ((PlcSTRING) x).getString()).collect(Collectors.toList());
-                        var = new Variant(valueList.toArray(new String[valueList.size()]));
-                    } else {
-                        logger.warn("Unsupported data type : {}", plcValueList.get(0).getClass());
+                    String dataType = uaField.getPlcDataType();
+                    if (dataType.equals("IEC61131_NULL")) {                        
+                        if (plcValueList.get(0).getObject() instanceof Boolean) {
+                            dataType = "IEC61131_BOOL";
+                        } else if (plcValueList.get(0).getObject() instanceof Byte) {
+                            dataType = "IEC61131_SINT";
+                        } else if (plcValueList.get(0).getObject() instanceof Short) {
+                            dataType = "IEC61131_INT";
+                        } else if (plcValueList.get(0).getObject() instanceof Integer) {
+                            dataType = "IEC61131_DINT";
+                        } else if (plcValueList.get(0).getObject() instanceof Long) {
+                            dataType = "IEC61131_LINT";
+                        } else if (plcValueList.get(0).getObject() instanceof Float) {
+                            dataType = "IEC61131_REAL";
+                        } else if (plcValueList.get(0).getObject() instanceof Double) {
+                            dataType = "IEC61131_LREAL";
+                        } else if (plcValueList.get(0).getObject() instanceof String) {
+                            dataType = "IEC61131_STRING";
+                        }
+                    }
+                    switch (dataType) {
+                        case "IEC61131_BOOL":
+                        case "IEC61131_BIT":
+                            List<Boolean> booleanList = (plcValueList).stream().map(
+                                    x -> ((PlcBOOL) x).getBoolean()).collect(Collectors.toList());
+                            var = new Variant(booleanList.toArray(new Boolean[booleanList.size()]));
+                            break;
+                        case "IEC61131_BYTE":
+                        case "IEC61131_BITARR8":
+                            List<UByte> byteList = (plcValueList).stream().map(
+                                    x -> UByte.valueOf(((PlcBYTE) x).getShort())).collect(Collectors.toList());
+                            var = new Variant(byteList.toArray(new UByte[byteList.size()]));
+                            break;
+                        case "IEC61131_SINT":
+                        case "IEC61131_INT8":
+                            List<Byte> sintList = (plcValueList).stream().map(
+                                    x -> ((PlcSINT) x).getByte()).collect(Collectors.toList());
+                            var = new Variant(sintList.toArray(new Byte[sintList.size()]));
+                            break;
+                        case "IEC61131_USINT":
+                        case "IEC61131_UINT8":
+                        case "IEC61131_BIT8":
+                            List<UByte> usintList = (plcValueList).stream().map(
+                                    x -> UByte.valueOf(((PlcUSINT) x).getShort())).collect(Collectors.toList());
+                            var = new Variant(usintList.toArray(new UByte[usintList.size()]));
+                            break;
+                        case "IEC61131_INT":
+                        case "IEC61131_INT16":
+                            List<Short> intList = (plcValueList).stream().map(
+                                    x -> ((PlcINT) x).getShort()).collect(Collectors.toList());
+                            var = new Variant(intList.toArray(new Short[intList.size()]));
+                            break;
+                        case "IEC61131_UINT":
+                        case "IEC61131_UINT16":
+                            List<UShort> uintList = (plcValueList).stream().map(
+                                    x -> UShort.valueOf(((PlcUINT) x).getInteger())).collect(Collectors.toList());
+                            var = new Variant(uintList.toArray(new UShort[uintList.size()]));
+                            break;
+                        case "IEC61131_WORD":
+                        case "IEC61131_BITARR16":
+                            List<UShort> wordList = (plcValueList).stream().map(
+                                    x -> UShort.valueOf(((PlcWORD) x).getInteger())).collect(Collectors.toList());
+                            var = new Variant(wordList.toArray(new UShort[wordList.size()]));
+                            break;
+                        case "IEC61131_DINT":
+                        case "IEC61131_INT32":
+                            List<Integer> dintList = (plcValueList).stream().map(
+                                    x -> ((PlcDINT) x).getInteger()).collect(Collectors.toList());
+                            var = new Variant(dintList.toArray(new Integer[dintList.size()]));
+                            break;
+                        case "IEC61131_UDINT":
+                        case "IEC61131_UINT32":
+                            List<UInteger> udintList = (plcValueList).stream().map(
+                                    x -> UInteger.valueOf(((PlcUDINT) x).getLong())).collect(Collectors.toList());
+                            var = new Variant(udintList.toArray(new UInteger[udintList.size()]));
+                            break;
+                        case "IEC61131_DWORD":
+                        case "IEC61131_BITARR32":
+                            List<UInteger> dwordList = (plcValueList).stream().map(
+                                    x -> UInteger.valueOf(((PlcDWORD) x).getLong())).collect(Collectors.toList());
+                            var = new Variant(dwordList.toArray(new UInteger[dwordList.size()]));
+                            break;
+                        case "IEC61131_LINT":
+                        case "IEC61131_INT64":
+                            List<Long> lintList = (plcValueList).stream().map(
+                                    x -> ((PlcLINT) x).getLong()).collect(Collectors.toList());
+                            var = new Variant(lintList.toArray(new Long[lintList.size()]));
+                            break;
+                        case "IEC61131_ULINT":
+                        case "IEC61131_UINT64":
+                            List<ULong> ulintList = (plcValueList).stream().map(
+                                    x -> ULong.valueOf(((PlcULINT) x).getBigInteger())).collect(Collectors.toList());
+                            var = new Variant(ulintList.toArray(new ULong[ulintList.size()]));
+                            break;
+                        case "IEC61131_LWORD":
+                        case "IEC61131_BITARR64":
+                            List<ULong> lwordList = (plcValueList).stream().map(
+                                    x -> ULong.valueOf(((PlcLWORD) x).getBigInteger())).collect(Collectors.toList());
+                            var = new Variant(lwordList.toArray(new ULong[lwordList.size()]));
+                            break;
+                        case "IEC61131_REAL":
+                        case "IEC61131_FLOAT":
+                            List<Float> realList = (plcValueList).stream().map(
+                                    x -> ((PlcREAL) x).getFloat()).collect(Collectors.toList());
+                            var = new Variant(realList.toArray(new Float[realList.size()]));
+                            break;
+                        case "IEC61131_LREAL":
+                        case "IEC61131_DOUBLE":
+                            List<Double> lrealList = (plcValueList).stream().map(
+                                    x -> (Double) ((PlcLREAL) x).getDouble()).collect(Collectors.toList());
+                            var = new Variant(lrealList.toArray(new Double[lrealList.size()]));
+                            break;
+                        case "IEC61131_CHAR":
+                            List<String> charList = (plcValueList).stream().map(
+                                    x -> ((PlcCHAR) x).getString()).collect(Collectors.toList());
+                            var = new Variant(charList.toArray(new String[charList.size()]));
+                            break;
+                        case "IEC61131_WCHAR":
+                            List<String> wcharList = (plcValueList).stream().map(
+                                    x -> ((PlcWCHAR) x).getString()).collect(Collectors.toList());
+                            var = new Variant(wcharList.toArray(new String[wcharList.size()]));
+                            break;
+                        case "IEC61131_STRING":
+                            List<String> stringList = (plcValueList).stream().map(
+                                    x -> ((PlcSTRING) x).getString()).collect(Collectors.toList());
+                            var = new Variant(stringList.toArray(new String[stringList.size()]));
+                            break;
+                        case "IEC61131_WSTRING":
+                        case "IEC61131_STRING16":
+                            List<String> wstringList = (plcValueList).stream().map(
+                                    x -> (String) ((PlcSTRING) x).getString()).collect(Collectors.toList());
+                            var = new Variant(wstringList.toArray(new String[wstringList.size()]));
+                            break;
+                        case "IEC61131_DATE_AND_TIME":
+                            List<LocalDateTime> dateTimeList = (plcValueList).stream().map(
+                                    x -> ((PlcDATE_AND_TIME) x).getDateTime()).collect(Collectors.toList());
+                            var = new Variant(dateTimeList.toArray(new LocalDateTime[dateTimeList.size()]));
+                            break;
+                        default:
+                            logger.warn("Unsupported data type : {}, {}", plcValueList.get(0).getClass(), dataType);
                     }
-                }   else {
+                } else {
                     var = new Variant(valueObject);
                 }
 
diff --git a/plc4j/drivers/opcua/src/main/java/org/apache/plc4x/java/opcua/protocol/OpcuaField.java b/plc4j/drivers/opcua/src/main/java/org/apache/plc4x/java/opcua/protocol/OpcuaField.java
index 13a4988..1d626f5 100644
--- a/plc4j/drivers/opcua/src/main/java/org/apache/plc4x/java/opcua/protocol/OpcuaField.java
+++ b/plc4j/drivers/opcua/src/main/java/org/apache/plc4x/java/opcua/protocol/OpcuaField.java
@@ -30,7 +30,7 @@ import java.util.regex.Pattern;
  */
 public class OpcuaField implements PlcField {
 
-    public static final Pattern ADDRESS_PATTERN = Pattern.compile("^ns=(?<namespace>\\d+);(?<identifierType>[isgb])=((?<identifier>.+))?");
+    public static final Pattern ADDRESS_PATTERN = Pattern.compile("^ns=(?<namespace>\\d+);(?<identifierType>[isgb])=((?<identifier>[^:]+))?(:(?<datatype>[a-zA-Z_]+))?");
 
     private final OpcuaIdentifierType identifierType;
 
@@ -38,22 +38,26 @@ public class OpcuaField implements PlcField {
 
     private final String identifier;
 
-    protected OpcuaField(int namespace, OpcuaIdentifierType identifierType, String identifier) {
+    private final String dataType;
+
+    protected OpcuaField(int namespace, OpcuaIdentifierType identifierType, String identifier, String dataType) {
         this.namespace = namespace;
         this.identifier = identifier;
         this.identifierType = identifierType;
         if (this.identifier == null || this.namespace < 0) {
             throw new IllegalArgumentException("Identifier can not be null or Namespace can not be lower then 0.");
         }
+        this.dataType = dataType != null ? dataType.toUpperCase() : null;
     }
 
-    private OpcuaField(Integer namespace, String identifier, OpcuaIdentifierType identifierType) {
+    private OpcuaField(Integer namespace, String identifier, OpcuaIdentifierType identifierType, String dataType) {
         this.identifier = Objects.requireNonNull(identifier);
         this.identifierType = Objects.requireNonNull(identifierType);
         this.namespace = namespace != null ? namespace : 0;
         if (this.namespace < 0) {
             throw new IllegalArgumentException("namespace must be greater then zero. Was " + this.namespace);
         }
+        this.dataType = dataType != null ? dataType.toUpperCase() : null;
     }
 
     public static OpcuaField of(String address) {
@@ -69,7 +73,9 @@ public class OpcuaField implements PlcField {
         String namespaceString = matcher.group("namespace");
         Integer namespace = namespaceString != null ? Integer.valueOf(namespaceString) : 0;
 
-        return new OpcuaField(namespace, identifier, identifierType);
+        String dataType = "IEC61131_" + matcher.group("datatype");
+
+        return new OpcuaField(namespace, identifier, identifierType, dataType);
     }
 
 
@@ -89,9 +95,13 @@ public class OpcuaField implements PlcField {
         return identifierType;
     }
 
+    public String getDataType() {
+        return dataType;
+    }
+
     @Override
     public String getPlcDataType() {
-        return identifierType.toString();
+        return dataType;
     }
 
     @Override
diff --git a/plc4j/drivers/opcua/src/test/java/org/apache/plc4x/java/opcua/OpcuaPlcDriverTest.java b/plc4j/drivers/opcua/src/test/java/org/apache/plc4x/java/opcua/OpcuaPlcDriverTest.java
index 8ce7f62..c273c20 100644
--- a/plc4j/drivers/opcua/src/test/java/org/apache/plc4x/java/opcua/OpcuaPlcDriverTest.java
+++ b/plc4j/drivers/opcua/src/test/java/org/apache/plc4x/java/opcua/OpcuaPlcDriverTest.java
@@ -286,7 +286,7 @@ public class OpcuaPlcDriverTest {
 
 
               builder.addItem("BooleanArray", BOOL_ARRAY_IDENTIFIER, new Boolean[] {true, true, true, true, true});
-              builder.addItem("ByteArray", BYTE_ARRAY_IDENTIFIER, new Byte[] {1, -100, 100, -123, 123});
+              builder.addItem("ByteArray", BYTE_ARRAY_IDENTIFIER + ":BYTE", new Short[] {1, 100, 100, 255, 123});
               builder.addItem("DoubleArray", DOUBLE_ARRAY_IDENTIFIER, new Double[] {1.0,2.0,3.0,4.0,5.0});
               builder.addItem("FloatArray", FLOAT_ARRAY_IDENTIFIER, new Float[] {1.0F,2.0F,3.0F,4.0F,5.0F});
               builder.addItem("Int16Array", INT16_ARRAY_IDENTIFIER, new Short[] {1,2,3,4,5});