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 2022/09/07 14:07:40 UTC

[plc4x] branch plc4j/opcua-browse updated: Working on getting child items

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

hutcheb pushed a commit to branch plc4j/opcua-browse
in repository https://gitbox.apache.org/repos/asf/plc4x.git


The following commit(s) were added to refs/heads/plc4j/opcua-browse by this push:
     new bc931f45c Working on getting child items
bc931f45c is described below

commit bc931f45c163e37bcfc441dbcd60e8c3a2ed202d
Author: Ben Hutcheson <be...@gmail.com>
AuthorDate: Wed Sep 7 08:07:25 2022 -0600

    Working on getting child items
---
 .../java/opcua/protocol/OpcuaProtocolLogic.java    | 86 +++++++++++++++++-----
 1 file changed, 69 insertions(+), 17 deletions(-)

diff --git a/plc4j/drivers/opcua/src/main/java/org/apache/plc4x/java/opcua/protocol/OpcuaProtocolLogic.java b/plc4j/drivers/opcua/src/main/java/org/apache/plc4x/java/opcua/protocol/OpcuaProtocolLogic.java
index 1208c8fb6..5b3c75358 100644
--- a/plc4j/drivers/opcua/src/main/java/org/apache/plc4x/java/opcua/protocol/OpcuaProtocolLogic.java
+++ b/plc4j/drivers/opcua/src/main/java/org/apache/plc4x/java/opcua/protocol/OpcuaProtocolLogic.java
@@ -50,6 +50,7 @@ import java.time.LocalDateTime;
 import java.time.ZoneOffset;
 import java.util.*;
 import java.util.concurrent.CompletableFuture;
+import java.util.concurrent.ExecutionException;
 import java.util.concurrent.TimeUnit;
 import java.util.concurrent.TimeoutException;
 import java.util.concurrent.atomic.AtomicBoolean;
@@ -127,9 +128,8 @@ public class OpcuaProtocolLogic extends Plc4xProtocolBase<OpcuaAPU> implements H
         channel.onDiscover(context);
     }
 
-    @Override
-    public CompletableFuture<PlcBrowseResponse> browse(PlcBrowseRequest browseRequest) {
-        CompletableFuture<PlcBrowseResponse> future = new CompletableFuture<>();
+    private CompletableFuture<List<PlcBrowseItem>> browseNode(NodeId nodeId) {
+        CompletableFuture<List<PlcBrowseItem>> future = new CompletableFuture<>();
         RequestHeader requestHeader = new RequestHeader(channel.getAuthenticationToken(),
             SecureChannel.getCurrentDateTime(),
             channel.getRequestHandle(),
@@ -140,7 +140,7 @@ public class OpcuaProtocolLogic extends Plc4xProtocolBase<OpcuaAPU> implements H
 
         List<ExtensionObjectDefinition> requestedValues = new ArrayList<>(1);
         requestedValues.add(new BrowseDescription(
-            new NodeId(new NodeIdTwoByte((short) 85)),
+            nodeId,
             BrowseDirection.browseDirectionForward,
             new NodeId(new NodeIdTwoByte((short) 33)),
             true,
@@ -149,7 +149,6 @@ public class OpcuaProtocolLogic extends Plc4xProtocolBase<OpcuaAPU> implements H
         ));
 
         ViewDescription viewDescription = new ViewDescription(NULL_NODEID,SecureChannel.getCurrentDateTime(),1);
-
         BrowseRequest request = new BrowseRequest(
             requestHeader,
             viewDescription,
@@ -157,8 +156,6 @@ public class OpcuaProtocolLogic extends Plc4xProtocolBase<OpcuaAPU> implements H
             1,
             requestedValues);
 
-
-
         ExpandedNodeId expandedNodeId = new ExpandedNodeId(false,           //Namespace Uri Specified
             false,            //Server Index Specified
             new NodeIdFourByte((short) 0, Integer.parseInt(request.getIdentifier())),
@@ -186,16 +183,39 @@ public class OpcuaProtocolLogic extends Plc4xProtocolBase<OpcuaAPU> implements H
 
                         for (ExtensionObjectDefinition result : castResult.getReferences()) {
                             ReferenceDescription referenceResult = (ReferenceDescription) result;
-
-                            values.add(new DefaultPlcBrowseItem(
-                                referenceResult.getBrowseName().getName().getStringValue(),
-                                referenceResult.getDisplayName().getText().getStringValue(),
-                                PlcValueType.Struct,
-                                true,
-                                true,
-                                true, new ArrayList<PlcBrowseItem>(0), null));
+                            String typeDefinition = referenceResult.getTypeDefinition().getIdentifier();
+                            PlcValueType plcValue = null;
+                            if (OpcuaDataType.isDefined(typeDefinition)) {
+                                OpcuaDataType plcType = OpcuaDataType.enumForValue(typeDefinition);
+                                plcValue = PlcValueType.enumForValue(plcType.getVariantType());
+                            } else {
+                                plcValue = PlcValueType.Struct;
+                            }
+                            ExpandedNodeId tempNodeId = ((ReferenceDescription) result).getNodeId();
+                            NodeId tempNode = new NodeId(
+                                new NodeIdString(tempNodeId tempNodeId.getIdentifier());
+                            )
+                            CompletableFuture<List<PlcBrowseItem>> childFuture = browseNode();
+                            List<PlcBrowseItem> list = childFuture.get(5000L, TimeUnit.SECONDS);
+                            if (list != null) {
+                                values.add(new DefaultPlcBrowseItem(
+                                    referenceResult.getBrowseName().getName().getStringValue(),
+                                    referenceResult.getDisplayName().getText().getStringValue(),
+                                    plcValue,
+                                    true,
+                                    true,
+                                    true, list, null));
+                            } else {
+                                values.add(new DefaultPlcBrowseItem(
+                                    referenceResult.getBrowseName().getName().getStringValue(),
+                                    referenceResult.getDisplayName().getText().getStringValue(),
+                                    plcValue,
+                                    true,
+                                    true,
+                                    true, new ArrayList<PlcBrowseItem>(0), null));
+                            }
                         }
-                        future.complete(new DefaultPlcBrowseResponse(browseRequest, PlcResponseCode.OK, values));
+                        future.complete(values);
                     } else {
                         List<PlcBrowseItem> values = new ArrayList<>(0);
                         if (reply instanceof ServiceFault) {
@@ -205,11 +225,17 @@ public class OpcuaProtocolLogic extends Plc4xProtocolBase<OpcuaAPU> implements H
                             LOGGER.error("Remote party returned an error '{}'", reply);
                         }
 
-                        future.complete(new DefaultPlcBrowseResponse(browseRequest, PlcResponseCode.INTERNAL_ERROR, values));
+                        future.complete(values);
                         return;
                     }
                 } catch (ParseException e) {
                     future.completeExceptionally(new PlcRuntimeException(e));
+                } catch (ExecutionException e) {
+                    throw new RuntimeException(e);
+                } catch (InterruptedException e) {
+                    throw new RuntimeException(e);
+                } catch (TimeoutException e) {
+                    throw new RuntimeException(e);
                 }
             };
 
@@ -233,6 +259,32 @@ public class OpcuaProtocolLogic extends Plc4xProtocolBase<OpcuaAPU> implements H
         return future;
     }
 
+    @Override
+    public CompletableFuture<PlcBrowseResponse> browse(PlcBrowseRequest browseRequest) {
+        CompletableFuture<PlcBrowseResponse> future = new CompletableFuture<>();
+
+        CompletableFuture<List<PlcBrowseItem>> childFuture = browseNode(new ExpandedNodeId(
+            false,
+            false,
+            new NodeIdTwoByte((short) 85),
+            null,
+            0L));
+
+        PlcBrowseResponse response = null;
+        try {
+            response = new DefaultPlcBrowseResponse(browseRequest, PlcResponseCode.OK, childFuture.get(1000L, TimeUnit.SECONDS));
+            future.complete(response);
+        } catch (InterruptedException e) {
+            throw new RuntimeException(e);
+        } catch (ExecutionException e) {
+            throw new RuntimeException(e);
+        } catch (TimeoutException e) {
+            throw new RuntimeException(e);
+        }
+        //new DefaultPlcBrowseResponse(browseRequest, PlcResponseCode.INTERNAL_ERROR, values)
+        return future;
+    }
+
 
     @Override
     public CompletableFuture<PlcReadResponse> read(PlcReadRequest readRequest) {