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

bossenti pushed a commit to branch dev
in repository 
https://gitbox.apache.org/repos/asf/incubator-streampipes-extensions.git

commit 359461ff275e87e7c98e325e74e73ba96aacdca5
Author: bossenti <[email protected]>
AuthorDate: Tue Feb 23 17:37:06 2021 +0100

    [STREAMPIPES-301] replace getConfiguration with OpcUa.from()
---
 .../streampipes/connect/adapters/opcua/OpcUa.java  | 61 ++++++++++++++++
 .../connect/adapters/opcua/OpcUaAdapter.java       | 81 ++--------------------
 2 files changed, 66 insertions(+), 76 deletions(-)

diff --git 
a/streampipes-connect-adapters/src/main/java/org/apache/streampipes/connect/adapters/opcua/OpcUa.java
 
b/streampipes-connect-adapters/src/main/java/org/apache/streampipes/connect/adapters/opcua/OpcUa.java
index 8bfdc07..303ef3e 100644
--- 
a/streampipes-connect-adapters/src/main/java/org/apache/streampipes/connect/adapters/opcua/OpcUa.java
+++ 
b/streampipes-connect-adapters/src/main/java/org/apache/streampipes/connect/adapters/opcua/OpcUa.java
@@ -23,6 +23,11 @@ import static 
org.eclipse.milo.opcua.stack.core.types.builtin.unsigned.Unsigned.
 import static org.eclipse.milo.opcua.stack.core.util.ConversionUtil.toList;
 
 import org.apache.streampipes.connect.adapter.exception.AdapterException;
+import org.apache.streampipes.connect.adapters.opcua.utils.OpcUaConnect;
+import org.apache.streampipes.connect.adapters.opcua.utils.OpcUaNodeVariants;
+import org.apache.streampipes.connect.adapters.opcua.utils.OpcUaTypes;
+import org.apache.streampipes.model.connect.adapter.AdapterDescription;
+import org.apache.streampipes.sdk.extractor.StaticPropertyExtractor;
 import org.apache.streampipes.sdk.utils.Datatypes;
 import org.eclipse.milo.opcua.sdk.client.OpcUaClient;
 import org.eclipse.milo.opcua.sdk.client.api.config.OpcUaClientConfig;
@@ -66,6 +71,7 @@ public class OpcUa {
     private String user;
     private String password;
     private List<Map<String, Integer>> unitIDs = new ArrayList<>();
+    private List<String> selectedNodeNames;
 
     private static final AtomicLong clientHandles = new AtomicLong(1L);
 
@@ -106,6 +112,61 @@ public class OpcUa {
         this.password = password;
     }
 
+    public static OpcUa from(StaticPropertyExtractor extractor) {
+
+        String selectedAlternativeConnection = 
extractor.selectedAlternativeInternalId(OpcUaConnect.getOpcHostOrUrl());
+        String selectedAlternativeAuthentication = 
extractor.selectedAlternativeInternalId(OpcUaConnect.getAccessMode());
+
+        int namespaceIndex = 
extractor.singleValueParameter(OpcUaConnect.getNamespaceIndex(), int.class);
+        String nodeId = 
extractor.singleValueParameter(OpcUaConnect.getNodeId(), String.class);
+
+        boolean useURL = 
selectedAlternativeConnection.equals(OpcUaConnect.getOpcUrl());
+        boolean unauthenticated =  
selectedAlternativeAuthentication.equals(OpcUaConnect.getUnauthenticated());
+
+        List<String> selectedNodeNames = 
extractor.selectedMultiValues(OpcUaConnect.getAvailableNodes(), String.class);
+
+        if (useURL && unauthenticated){
+
+            String serverAddress = 
extractor.singleValueParameter(OpcUaConnect.getOpcServerUrl(), String.class);
+            serverAddress = OpcUaConnect.formatServerAddress(serverAddress);
+
+            return new OpcUa(serverAddress, namespaceIndex, nodeId, 
selectedNodeNames);
+
+        } else if(!useURL && unauthenticated){
+            String serverAddress = 
extractor.singleValueParameter(OpcUaConnect.getOpcServerHost(), String.class);
+            serverAddress = OpcUaConnect.formatServerAddress(serverAddress);
+            int port = 
extractor.singleValueParameter(OpcUaConnect.getOpcServerPort(), int.class);
+
+            return new OpcUa(serverAddress, port, namespaceIndex, nodeId, 
selectedNodeNames);
+        } else {
+
+            String username = 
extractor.singleValueParameter(OpcUaConnect.getUsername(), String.class);
+            String password = 
extractor.secretValue(OpcUaConnect.getPassword());
+
+            if (useURL) {
+                String serverAddress = 
extractor.singleValueParameter(OpcUaConnect.getOpcServerUrl(), String.class);
+                serverAddress = 
OpcUaConnect.formatServerAddress(serverAddress);
+
+                return new OpcUa(serverAddress, namespaceIndex, nodeId, 
username, password, selectedNodeNames);
+            } else {
+                String serverAddress = 
extractor.singleValueParameter(OpcUaConnect.getOpcServerHost(), String.class);
+                serverAddress = 
OpcUaConnect.formatServerAddress(serverAddress);
+                int port = 
extractor.singleValueParameter(OpcUaConnect.getOpcServerPort(), int.class);
+
+                return new OpcUa(serverAddress, port, namespaceIndex, nodeId, 
username, password, selectedNodeNames);
+            }
+        }
+
+    }
+
+    public static OpcUa from(AdapterDescription adapterDescription){
+
+        StaticPropertyExtractor extractor =
+                StaticPropertyExtractor.from(adapterDescription.getConfig(), 
new ArrayList<>());
+
+        return from(extractor);
+    }
+
     public void connect() throws Exception {
 
         List<EndpointDescription> endpoints = 
DiscoveryClient.getEndpoints(this.opcServerURL).get();
diff --git 
a/streampipes-connect-adapters/src/main/java/org/apache/streampipes/connect/adapters/opcua/OpcUaAdapter.java
 
b/streampipes-connect-adapters/src/main/java/org/apache/streampipes/connect/adapters/opcua/OpcUaAdapter.java
index 6a44b60..cae6778 100644
--- 
a/streampipes-connect-adapters/src/main/java/org/apache/streampipes/connect/adapters/opcua/OpcUaAdapter.java
+++ 
b/streampipes-connect-adapters/src/main/java/org/apache/streampipes/connect/adapters/opcua/OpcUaAdapter.java
@@ -61,14 +61,6 @@ public class OpcUaAdapter extends SpecificDataStreamAdapter 
implements ResolvesC
     private static final String UNAUTHENTICATED = "UNAUTHENTICATED";
     private static final String AVAILABLE_NODES = "AVAILABLE_NODES";
 
-    private String opcUaServer;
-    private String namespaceIndex;
-    private String nodeId;
-    private String port;
-    private boolean selectedURL;
-    private boolean unauthenticated;
-    private String username;
-    private String password;
 
     private Map<String, Object> event;
     private List<OpcNode> allNodes;
@@ -84,7 +76,7 @@ public class OpcUaAdapter extends SpecificDataStreamAdapter 
implements ResolvesC
     public OpcUaAdapter(SpecificAdapterStreamDescription adapterDescription) {
         super(adapterDescription);
 
-        getConfigurations(adapterDescription);
+        this.opcUa = OpcUa.from(this.adapterDescription);
 
         this.event = new HashMap<>();
         this.numberProperties = 0;
@@ -146,15 +138,8 @@ public class OpcUaAdapter extends 
SpecificDataStreamAdapter implements ResolvesC
 
     @Override
     public void startAdapter() throws AdapterException {
-        if (this.selectedURL && this.unauthenticated) {
-            this.opcUa = new OpcUa(opcUaServer, 
Integer.parseInt(namespaceIndex), nodeId);
-        } else if (!this.selectedURL && this.unauthenticated){
-            this.opcUa = new OpcUa(opcUaServer, Integer.parseInt(port), 
Integer.parseInt(namespaceIndex), nodeId);
-        } else if (this.selectedURL) {
-            this.opcUa = new OpcUa(opcUaServer, 
Integer.parseInt(namespaceIndex), nodeId, username, password);
-        } else {
-            this.opcUa = new OpcUa(opcUaServer, Integer.parseInt(port), 
Integer.parseInt(namespaceIndex), nodeId, username, password);
-        }
+
+        this.opcUa = OpcUa.from(this.adapterDescription);
 
         try {
             this.opcUa.connect();
@@ -170,8 +155,7 @@ public class OpcUaAdapter extends SpecificDataStreamAdapter 
implements ResolvesC
             this.numberProperties = nodeIds.size();
             this.opcUa.createListSubscription(nodeIds, this);
         } catch (Exception e) {
-            throw new AdapterException("Could not connect to OPC-UA server! 
Server: " + opcUaServer + " Port: " + port +
-                    " NamespaceIndex: " + namespaceIndex + " NodeId: " + 
nodeId);
+            throw new AdapterException("Could not connect to OPC-UA server! 
Server: " + this.opcUa.getOpcServerURL());
         }
     }
 
@@ -193,18 +177,7 @@ public class OpcUaAdapter extends 
SpecificDataStreamAdapter implements ResolvesC
         EventSchema eventSchema = new EventSchema();
         List<EventProperty> allProperties = new ArrayList<>();
 
-
-        getConfigurations(adapterDescription);
-        OpcUa opc;
-        if (this.selectedURL && this.unauthenticated) {
-            opc = new OpcUa(opcUaServer, Integer.parseInt(namespaceIndex), 
nodeId);
-        } else if (!this.selectedURL && this.unauthenticated){
-            opc = new OpcUa(opcUaServer, Integer.parseInt(port), 
Integer.parseInt(namespaceIndex), nodeId);
-        } else if (this.selectedURL) {
-            opc = new OpcUa(opcUaServer, Integer.parseInt(namespaceIndex), 
nodeId, username, password);
-        } else {
-            opc = new OpcUa(opcUaServer, Integer.parseInt(port), 
Integer.parseInt(namespaceIndex), nodeId, username, password);
-        }
+        OpcUa opc = OpcUa.from(this.adapterDescription);
 
         try {
             opc.connect();
@@ -245,48 +218,6 @@ public class OpcUaAdapter extends 
SpecificDataStreamAdapter implements ResolvesC
         return ID;
     }
 
-    private void getConfigurations(SpecificAdapterStreamDescription 
adapterDescription) {
-        StaticPropertyExtractor extractor =
-                StaticPropertyExtractor.from(adapterDescription.getConfig(), 
new ArrayList<>());
-
-        String selectedAlternativeConnection = 
extractor.selectedAlternativeInternalId(OPC_HOST_OR_URL);
-        String selectedAlternativeAuthentication = 
extractor.selectedAlternativeInternalId(ACCESS_MODE);
-
-
-
-        if (selectedAlternativeConnection.equals(OPC_URL)) {
-
-            String serverAddress = 
extractor.singleValueParameter(OPC_SERVER_URL, String.class);
-
-            if (!serverAddress.startsWith("opc.tcp://")) {
-                serverAddress = "opc.tcp://" + serverAddress;
-            }
-
-            this.opcUaServer = serverAddress;
-            this.selectedURL = true;
-        } else {
-
-            String serverAddress = 
extractor.singleValueParameter(OPC_SERVER_HOST, String.class);
-
-            if (!serverAddress.startsWith("opc.tcp://")) {
-                serverAddress = "opc.tcp://" + serverAddress;
-            }
-
-            this.opcUaServer = serverAddress;
-            this.port = extractor.singleValueParameter(OPC_SERVER_PORT, 
String.class);
-            this.selectedURL = false;
-        }
-        this.unauthenticated = 
selectedAlternativeAuthentication.equals(UNAUTHENTICATED);
-        if (!this.unauthenticated) {
-            this.username = extractor.singleValueParameter(USERNAME, 
String.class);
-            this.password = extractor.secretValue(PASSWORD);
-        }
-
-        this.namespaceIndex = extractor.singleValueParameter(NAMESPACE_INDEX, 
String.class);
-        this.nodeId = extractor.singleValueParameter(NODE_ID, String.class);
-    }
-
-
     @Override
     public List<Option> resolveOptions(String requestId, 
StaticPropertyExtractor parameterExtractor) {
 
@@ -315,8 +246,6 @@ public class OpcUaAdapter extends SpecificDataStreamAdapter 
implements ResolvesC
         return nodes;
     }
 
-
-
     private String getRuntimeNameOfNode(NodeId nodeId) {
         String[] keys = nodeId.getIdentifier().toString().split("\\.");
         String key;

Reply via email to