You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@streampipes.apache.org by ze...@apache.org on 2024/02/27 07:40:27 UTC

(streampipes) 01/01: refactor: Remove unused code and minor refactoring

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

zehnder pushed a commit to branch refactor-opc-ua-remove-unused-code
in repository https://gitbox.apache.org/repos/asf/streampipes.git

commit 87f21d846ee4d8a0c1b5c8ff6bd6ce0c03f26b46
Author: Philipp Zehnder <te...@users.noreply.github.com>
AuthorDate: Tue Feb 27 08:40:18 2024 +0100

    refactor: Remove unused code and minor refactoring
---
 .../opcua/config/SpOpcUaConfigExtractor.java       |  4 +-
 .../connectors/opcua/utils/OpcUaUtil.java          | 95 ++++++++--------------
 .../connectors/opcua/utils/OpcUaUtilTest.java      | 40 +++++++++
 3 files changed, 74 insertions(+), 65 deletions(-)

diff --git a/streampipes-extensions/streampipes-connectors-opcua/src/main/java/org/apache/streampipes/extensions/connectors/opcua/config/SpOpcUaConfigExtractor.java b/streampipes-extensions/streampipes-connectors-opcua/src/main/java/org/apache/streampipes/extensions/connectors/opcua/config/SpOpcUaConfigExtractor.java
index 612b3e4905..1145cb3501 100644
--- a/streampipes-extensions/streampipes-connectors-opcua/src/main/java/org/apache/streampipes/extensions/connectors/opcua/config/SpOpcUaConfigExtractor.java
+++ b/streampipes-extensions/streampipes-connectors-opcua/src/main/java/org/apache/streampipes/extensions/connectors/opcua/config/SpOpcUaConfigExtractor.java
@@ -84,9 +84,9 @@ public class SpOpcUaConfigExtractor {
     if (useURL) {
       String serverAddress =
           extractor.singleValueParameter(OPC_SERVER_URL.name(), String.class);
-      config.setOpcServerURL(OpcUaUtil.formatServerAddress(serverAddress));
+      config.setOpcServerURL(OpcUaUtil.addOpcPrefixIfNotExists(serverAddress));
     } else {
-      String serverAddress = OpcUaUtil.formatServerAddress(
+      String serverAddress = OpcUaUtil.addOpcPrefixIfNotExists(
           extractor.singleValueParameter(OPC_SERVER_HOST.name(), String.class)
       );
       int port = extractor.singleValueParameter(OPC_SERVER_PORT.name(), int.class);
diff --git a/streampipes-extensions/streampipes-connectors-opcua/src/main/java/org/apache/streampipes/extensions/connectors/opcua/utils/OpcUaUtil.java b/streampipes-extensions/streampipes-connectors-opcua/src/main/java/org/apache/streampipes/extensions/connectors/opcua/utils/OpcUaUtil.java
index 19e8d7c1c6..f00fa61731 100644
--- a/streampipes-extensions/streampipes-connectors-opcua/src/main/java/org/apache/streampipes/extensions/connectors/opcua/utils/OpcUaUtil.java
+++ b/streampipes-extensions/streampipes-connectors-opcua/src/main/java/org/apache/streampipes/extensions/connectors/opcua/utils/OpcUaUtil.java
@@ -37,12 +37,9 @@ import org.apache.streampipes.model.staticproperty.RuntimeResolvableTreeInputSta
 import org.apache.streampipes.sdk.builder.PrimitivePropertyBuilder;
 import org.apache.streampipes.sdk.builder.adapter.GuessSchemaBuilder;
 
-import org.eclipse.milo.opcua.sdk.client.OpcUaClient;
 import org.eclipse.milo.opcua.stack.core.UaException;
 import org.eclipse.milo.opcua.stack.core.types.builtin.DataValue;
-import org.eclipse.milo.opcua.stack.core.types.builtin.NodeId;
 import org.eclipse.milo.opcua.stack.core.types.builtin.StatusCode;
-import org.eclipse.milo.opcua.stack.core.types.builtin.unsigned.UInteger;
 import org.eclipse.milo.opcua.stack.core.types.enumerated.TimestampsToReturn;
 
 import java.net.URI;
@@ -60,18 +57,15 @@ import java.util.stream.Collectors;
  */
 public class OpcUaUtil {
 
+  private static final String OPC_TCP_PREFIX = "opc.tcp://";
+
   /***
    * Ensures server address starts with {@code opc.tcp://}
    * @param serverAddress server address as given by user
    * @return correctly formated server address
    */
-  public static String formatServerAddress(String serverAddress) {
-
-    if (!serverAddress.startsWith("opc.tcp://")) {
-      serverAddress = "opc.tcp://" + serverAddress;
-    }
-
-    return serverAddress;
+  public static String addOpcPrefixIfNotExists(String serverAddress) {
+    return serverAddress.startsWith(OPC_TCP_PREFIX) ? serverAddress : OPC_TCP_PREFIX + serverAddress;
   }
 
   /***
@@ -103,21 +97,24 @@ public class OpcUaUtil {
         for (OpcNode opcNode : selectedNodes) {
           if (opcNode.hasUnitId()) {
             allProperties.add(PrimitivePropertyBuilder
-                .create(opcNode.getType(), opcNode.getLabel())
-                .label(opcNode.getLabel())
-                .measurementUnit(new URI(opcNode.getQudtURI()))
-                .build());
+                                  .create(opcNode.getType(), opcNode.getLabel())
+                                  .label(opcNode.getLabel())
+                                  .measurementUnit(new URI(opcNode.getQudtURI()))
+                                  .build());
           } else {
             allProperties.add(PrimitivePropertyBuilder
-                .create(opcNode.getType(), opcNode.getLabel())
-                .label(opcNode.getLabel())
-                .build());
+                                  .create(opcNode.getType(), opcNode.getLabel())
+                                  .label(opcNode.getLabel())
+                                  .build());
           }
         }
       }
 
-      var nodeIds = selectedNodes.stream().map(OpcNode::getNodeId).collect(Collectors.toList());
-      var response = spOpcUaClient.getClient().readValues(0, TimestampsToReturn.Both, nodeIds);
+      var nodeIds = selectedNodes.stream()
+                                 .map(OpcNode::getNodeId)
+                                 .collect(Collectors.toList());
+      var response = spOpcUaClient.getClient()
+                                  .readValues(0, TimestampsToReturn.Both, nodeIds);
 
       var returnValues = response.get();
 
@@ -138,20 +135,25 @@ public class OpcUaUtil {
     return builder.build();
   }
 
-  private static void makeEventPreview(List<OpcNode> selectedNodes,
-                                       Map<String, Object> eventPreview,
-                                       Map<String, FieldStatusInfo> fieldStatusInfos,
-                                       List<DataValue> dataValues) {
+  private static void makeEventPreview(
+      List<OpcNode> selectedNodes,
+      Map<String, Object> eventPreview,
+      Map<String, FieldStatusInfo> fieldStatusInfos,
+      List<DataValue> dataValues
+  ) {
 
     for (int i = 0; i < dataValues.size(); i++) {
       var dv = dataValues.get(i);
-      String label = selectedNodes.get(i).getLabel();
+      String label = selectedNodes.get(i)
+                                  .getLabel();
       if (StatusCode.GOOD.equals(dv.getStatusCode())) {
-        var value = dv.getValue().getValue();
+        var value = dv.getValue()
+                      .getValue();
         eventPreview.put(label, value);
         fieldStatusInfos.put(label, FieldStatusInfo.good());
       } else {
-        String additionalInfo = dv.getStatusCode() != null ? dv.getStatusCode().toString() : "Status code is null";
+        String additionalInfo = dv.getStatusCode() != null ? dv.getStatusCode()
+                                                               .toString() : "Status code is null";
         fieldStatusInfos.put(label, FieldStatusInfo.bad(additionalInfo, false));
       }
     }
@@ -165,9 +167,10 @@ public class OpcUaUtil {
    * @param parameterExtractor to extract parameters from the OPC UA config
    * @return {@code List<Option>} with available node names for the given OPC UA configuration
    */
-  public static RuntimeResolvableTreeInputStaticProperty
-      resolveConfiguration(String internalName,
-                           IStaticPropertyExtractor parameterExtractor)
+  public static RuntimeResolvableTreeInputStaticProperty resolveConfiguration(
+      String internalName,
+      IStaticPropertyExtractor parameterExtractor
+  )
       throws SpConfigurationException {
 
     RuntimeResolvableTreeInputStaticProperty config = parameterExtractor
@@ -209,38 +212,4 @@ public class OpcUaUtil {
     }
   }
 
-  public static String getRuntimeNameOfNode(NodeId nodeId) {
-    String[] keys = nodeId.getIdentifier().toString().split("\\.");
-    String key;
-
-    if (keys.length > 0) {
-      key = keys[keys.length - 1];
-    } else {
-      key = nodeId.getIdentifier().toString();
-    }
-
-    return key;
-  }
-
-  /**
-   * connects to each node individually and updates the data type in accordance to the data from the server.
-   *
-   * @param opcNodes List of opcNodes where the data type is not determined appropriately
-   */
-  public static void retrieveDataTypesFromServer(OpcUaClient client, List<OpcNode> opcNodes) throws AdapterException {
-
-    for (OpcNode opcNode : opcNodes) {
-      try {
-        UInteger dataTypeId =
-            (UInteger) client.getAddressSpace().getVariableNode(opcNode.getNodeId()).getDataType()
-                .getIdentifier();
-        OpcUaTypes.getType(dataTypeId);
-        opcNode.setType(OpcUaTypes.getType(dataTypeId));
-      } catch (UaException e) {
-        throw new AdapterException("Could not guess schema for opc node! " + e.getMessage());
-      }
-    }
-  }
-
-
 }
diff --git a/streampipes-extensions/streampipes-connectors-opcua/src/test/java/org/apache/streampipes/extensions/connectors/opcua/utils/OpcUaUtilTest.java b/streampipes-extensions/streampipes-connectors-opcua/src/test/java/org/apache/streampipes/extensions/connectors/opcua/utils/OpcUaUtilTest.java
new file mode 100644
index 0000000000..5dda95b53e
--- /dev/null
+++ b/streampipes-extensions/streampipes-connectors-opcua/src/test/java/org/apache/streampipes/extensions/connectors/opcua/utils/OpcUaUtilTest.java
@@ -0,0 +1,40 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
+
+package org.apache.streampipes.extensions.connectors.opcua.utils;
+
+import org.junit.Test;
+
+import static org.junit.Assert.assertEquals;
+
+public class OpcUaUtilTest {
+
+  private static final String SERVER_ADDRESS_WITH_OPC_PREFIX = "opc.tcp://example.com";
+
+  @Test
+  public void testAddOpcPrefixIfNotExistsWithPrefix() {
+    var result = OpcUaUtil.addOpcPrefixIfNotExists(SERVER_ADDRESS_WITH_OPC_PREFIX);
+    assertEquals(SERVER_ADDRESS_WITH_OPC_PREFIX, result);
+  }
+
+  @Test
+  public void testAddOpcPrefixIfNotExistsNoPrefix() {
+    var result = OpcUaUtil.addOpcPrefixIfNotExists("example.com");
+    assertEquals(SERVER_ADDRESS_WITH_OPC_PREFIX, result);
+  }
+}
\ No newline at end of file