You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@plc4x.apache.org by cd...@apache.org on 2023/06/08 13:55:23 UTC

[plc4x] 01/02: refactor(plc4j/profinet): Added some comments and made the tests use the classloader to load the test-data instead of a fixed file-reference.

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

cdutz pushed a commit to branch chore/profinet-phase-3
in repository https://gitbox.apache.org/repos/asf/plc4x.git

commit 6588e9a1e7e444c16e104c781c6dc9f51b8f4219
Author: Christofer Dutz <cd...@apache.org>
AuthorDate: Thu Jun 8 15:53:07 2023 +0200

    refactor(plc4j/profinet): Added some comments and made the tests use the classloader to load the test-data instead of a fixed file-reference.
---
 .../java/profinet/context/ProfinetDeviceContext.java      |  6 ++++++
 .../apache/plc4x/java/profinet/ManualProfinetIoTest.java  | 10 ++++++----
 .../java/profinet/gsdml/ProfinetConfigurationTests.java   | 15 +++++----------
 .../plc4x/java/profinet/gsdml/ProfinetGSDMLParseTest.java |  7 ++++---
 4 files changed, 21 insertions(+), 17 deletions(-)

diff --git a/plc4j/drivers/profinet/src/main/java/org/apache/plc4x/java/profinet/context/ProfinetDeviceContext.java b/plc4j/drivers/profinet/src/main/java/org/apache/plc4x/java/profinet/context/ProfinetDeviceContext.java
index 26364bc1b3..599a421938 100644
--- a/plc4j/drivers/profinet/src/main/java/org/apache/plc4x/java/profinet/context/ProfinetDeviceContext.java
+++ b/plc4j/drivers/profinet/src/main/java/org/apache/plc4x/java/profinet/context/ProfinetDeviceContext.java
@@ -370,6 +370,7 @@ public class ProfinetDeviceContext implements DriverContext, HasConfiguration<Pr
 
     private void extractGSDFileInfo(ProfinetISO15745Profile gsdFile) throws PlcConnectionException {
 
+        // Find the DeviceAccessPoint specified by the "deviceAccess" parameter
         for (ProfinetDeviceAccessPointItem deviceAccessItem : gsdFile.getProfileBody().getApplicationProcess().getDeviceAccessPointList()) {
             if (deviceAccess.equals(deviceAccessItem.getId())) {
                 this.deviceAccessItem = deviceAccessItem;
@@ -379,6 +380,9 @@ public class ProfinetDeviceContext implements DriverContext, HasConfiguration<Pr
             throw new PlcConnectionException("Unable to find Device Access Item - " + this.deviceAccess);
         }
 
+        // The DAP itself is always slot 0 (Defined by "FixedInSlots").
+        // The PhysicalSlots therefore should always be in a format "0..x" format
+        // (Except, if the device wouldn't have any modules, which wouldn't make sense)
         Matcher matcher = RANGE_PATTERN.matcher(deviceAccessItem.getPhysicalSlots());
         if (!matcher.matches()) {
             throw new PlcConnectionException("Physical Slots Range is not in the correct format " + deviceAccessItem.getPhysicalSlots());
@@ -393,9 +397,11 @@ public class ProfinetDeviceContext implements DriverContext, HasConfiguration<Pr
         this.modules[deviceAccessItem.getFixedInSlots()] = new ProfinetModuleImpl(deviceAccessItem, 0, 0, deviceAccessItem.getFixedInSlots());
 
         List<ProfinetModuleItemRef> usableSubModules = this.deviceAccessItem.getUseableModules();
+        // The first slot is always 0 which is the DAP slot, so in general we'll always start with 1
         int currentSlot = deviceAccessItem.getFixedInSlots() + 1;
         int inputOffset = this.modules[deviceAccessItem.getFixedInSlots()].getInputIoPsSize();
         int outputOffset = this.modules[deviceAccessItem.getFixedInSlots()].getOutputIoCsSize();
+        // Iterate over each module.
         for (String subModule : this.subModules) {
             if (subModule.equals("")) {
                 this.modules[currentSlot] = new ProfinetEmptyModule();
diff --git a/plc4j/drivers/profinet/src/test/java/org/apache/plc4x/java/profinet/ManualProfinetIoTest.java b/plc4j/drivers/profinet/src/test/java/org/apache/plc4x/java/profinet/ManualProfinetIoTest.java
index b7e7c24833..bb5b854553 100644
--- a/plc4j/drivers/profinet/src/test/java/org/apache/plc4x/java/profinet/ManualProfinetIoTest.java
+++ b/plc4j/drivers/profinet/src/test/java/org/apache/plc4x/java/profinet/ManualProfinetIoTest.java
@@ -20,10 +20,7 @@ package org.apache.plc4x.java.profinet;
 
 import org.apache.plc4x.java.DefaultPlcDriverManager;
 import org.apache.plc4x.java.api.PlcConnection;
-import org.apache.plc4x.java.api.messages.PlcBrowseRequest;
-import org.apache.plc4x.java.api.messages.PlcBrowseResponse;
-import org.apache.plc4x.java.api.messages.PlcSubscriptionRequest;
-import org.apache.plc4x.java.api.messages.PlcSubscriptionResponse;
+import org.apache.plc4x.java.api.messages.*;
 import org.apache.plc4x.java.api.types.PlcResponseCode;
 import org.apache.plc4x.java.profinet.device.ProfinetSubscriptionHandle;
 import org.apache.plc4x.java.profinet.tag.ProfinetTag;
@@ -45,6 +42,11 @@ public class ManualProfinetIoTest {
 
         PlcBrowseRequest browseRequest = connection.browseRequestBuilder().addQuery("all", "*").build();
         PlcBrowseResponse plcBrowseResponse = browseRequest.execute().get(4000, TimeUnit.MILLISECONDS);
+        for (String queryName : plcBrowseResponse.getQueryNames()) {
+            for (PlcBrowseItem value : plcBrowseResponse.getValues(queryName)) {
+                System.out.println(value.getTag().getAddressString());
+            }
+        }
         System.out.println(plcBrowseResponse);
         // Wireshark filters:
         // - S7 1200: eth.addr == 001c0605bcdc
diff --git a/plc4j/drivers/profinet/src/test/java/org/apache/plc4x/java/profinet/gsdml/ProfinetConfigurationTests.java b/plc4j/drivers/profinet/src/test/java/org/apache/plc4x/java/profinet/gsdml/ProfinetConfigurationTests.java
index 16321d3134..26589e1bb0 100644
--- a/plc4j/drivers/profinet/src/test/java/org/apache/plc4x/java/profinet/gsdml/ProfinetConfigurationTests.java
+++ b/plc4j/drivers/profinet/src/test/java/org/apache/plc4x/java/profinet/gsdml/ProfinetConfigurationTests.java
@@ -29,17 +29,12 @@ import org.apache.plc4x.java.profinet.device.*;
 import org.apache.plc4x.java.spi.configuration.ConfigurationFactory;
 import org.junit.jupiter.api.Test;
 import org.junit.jupiter.api.TestInstance;
-import org.pcap4j.core.PcapNativeException;
-import org.pcap4j.core.PcapNetworkInterface;
-import org.pcap4j.core.Pcaps;
-import org.pcap4j.util.Inet4NetworkAddress;
 
 import java.io.File;
-import java.net.Inet4Address;
-import java.net.InetAddress;
-import java.net.UnknownHostException;
+import java.io.InputStreamReader;
 import java.util.HashMap;
 import java.util.Map;
+import java.util.Objects;
 
 import static org.junit.jupiter.api.Assertions.*;
 
@@ -166,7 +161,7 @@ public class ProfinetConfigurationTests {
         }
 
         XmlMapper xmlMapper = new XmlMapper();
-        assertDoesNotThrow(() -> devices.get("DEVICE_NAME_1").getDeviceContext().setGsdFile(xmlMapper.readValue(new File("src/test/resources/gsdml.xml"), ProfinetISO15745Profile.class)));
+        assertDoesNotThrow(() -> devices.get("DEVICE_NAME_1").getDeviceContext().setGsdFile(xmlMapper.readValue(new InputStreamReader(Objects.requireNonNull(getClass().getClassLoader().getResourceAsStream("gsdml.xml"))), ProfinetISO15745Profile.class)));
     }
 
     @Test
@@ -190,7 +185,7 @@ public class ProfinetConfigurationTests {
         }
 
         XmlMapper xmlMapper = new XmlMapper();
-        assertDoesNotThrow(() -> devices.get("DEVICE_NAME_1").getDeviceContext().setGsdFile(xmlMapper.readValue(new File("src/test/resources/gsdml.xml"), ProfinetISO15745Profile.class)));
+        assertDoesNotThrow(() -> devices.get("DEVICE_NAME_1").getDeviceContext().setGsdFile(xmlMapper.readValue(new InputStreamReader(Objects.requireNonNull(getClass().getClassLoader().getResourceAsStream("gsdml.xml"))), ProfinetISO15745Profile.class)));
     }
 
     @Test
@@ -300,7 +295,7 @@ public class ProfinetConfigurationTests {
         }
         XmlMapper xmlMapper = new XmlMapper();
 
-        assertDoesNotThrow(() -> devices.get("DEVICE NAME 1").getDeviceContext().setGsdFile(xmlMapper.readValue(new File("src/test/resources/gsdml.xml"), ProfinetISO15745Profile.class)));
+        assertDoesNotThrow(() -> devices.get("DEVICE NAME 1").getDeviceContext().setGsdFile(xmlMapper.readValue(new InputStreamReader(Objects.requireNonNull(getClass().getClassLoader().getResourceAsStream("gsdml.xml"))), ProfinetISO15745Profile.class)));
 
         for (String deviceName : deviceNames) {
             assert(devices.containsKey(deviceName));
diff --git a/plc4j/drivers/profinet/src/test/java/org/apache/plc4x/java/profinet/gsdml/ProfinetGSDMLParseTest.java b/plc4j/drivers/profinet/src/test/java/org/apache/plc4x/java/profinet/gsdml/ProfinetGSDMLParseTest.java
index 7224910da8..1edf264474 100644
--- a/plc4j/drivers/profinet/src/test/java/org/apache/plc4x/java/profinet/gsdml/ProfinetGSDMLParseTest.java
+++ b/plc4j/drivers/profinet/src/test/java/org/apache/plc4x/java/profinet/gsdml/ProfinetGSDMLParseTest.java
@@ -24,8 +24,9 @@ import org.junit.jupiter.api.BeforeAll;
 import org.junit.jupiter.api.Test;
 import org.junit.jupiter.api.TestInstance;
 
-import java.io.File;
 import java.io.IOException;
+import java.io.InputStreamReader;
+import java.util.Objects;
 
 import static org.junit.jupiter.api.Assertions.assertEquals;
 
@@ -38,7 +39,7 @@ public class ProfinetGSDMLParseTest {
     public void setUp() {
         try {
             XmlMapper xmlMapper = new XmlMapper();
-            this.gsdml = xmlMapper.readValue(new File("src/test/resources/gsdml.xml"), ProfinetISO15745Profile.class);
+            this.gsdml = xmlMapper.readValue(new InputStreamReader(Objects.requireNonNull(getClass().getClassLoader().getResourceAsStream("gsdml.xml"))), ProfinetISO15745Profile.class);
         } catch(IOException e) {
             assert false;
         }
@@ -51,7 +52,7 @@ public class ProfinetGSDMLParseTest {
 
     @Test
     public void readGsdmlFileStartupMode()  {
-        ProfinetInterfaceSubmoduleItem interfaceModule = (ProfinetInterfaceSubmoduleItem) this.gsdml.getProfileBody().getApplicationProcess().getDeviceAccessPointList().get(0).getSystemDefinedSubmoduleList().getInterfaceSubmodules().get(0);
+        ProfinetInterfaceSubmoduleItem interfaceModule = this.gsdml.getProfileBody().getApplicationProcess().getDeviceAccessPointList().get(0).getSystemDefinedSubmoduleList().getInterfaceSubmodules().get(0);
         assertEquals(interfaceModule.getApplicationRelations().getStartupMode(), "Advanced");
     }