Skip to content

Commit

Permalink
refactor(plc4j/profinet): Added some comments and made the tests use …
Browse files Browse the repository at this point in the history
…the classloader to load the test-data instead of a fixed file-reference.
  • Loading branch information
chrisdutz committed Jun 15, 2023
1 parent 23eee0a commit 3bf83a2
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 17 deletions.
Expand Up @@ -370,6 +370,7 @@ public void setGsdFile(ProfinetISO15745Profile gsdFile) throws PlcConnectionExce

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;
Expand All @@ -379,6 +380,9 @@ private void extractGSDFileInfo(ProfinetISO15745Profile gsdFile) throws PlcConne
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());
Expand All @@ -393,9 +397,11 @@ private void extractGSDFileInfo(ProfinetISO15745Profile gsdFile) throws PlcConne
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();
Expand Down
Expand Up @@ -20,10 +20,7 @@

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;
Expand All @@ -45,6 +42,11 @@ public static void main(String[] args) throws Exception {

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
Expand Down
Expand Up @@ -29,17 +29,12 @@
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.*;

Expand Down Expand Up @@ -166,7 +161,7 @@ public void setCorrectSubModule() {
}

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
Expand All @@ -190,7 +185,7 @@ public void setCorrectSubModuleCaseInsensitive() {
}

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
Expand Down Expand Up @@ -300,7 +295,7 @@ public void parseAllowedModuleSingleSlot() {
}
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));
Expand Down
Expand Up @@ -24,8 +24,9 @@
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;

Expand All @@ -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;
}
Expand All @@ -51,7 +52,7 @@ public void readGsdmlFile() {

@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");
}

Expand Down

0 comments on commit 3bf83a2

Please sign in to comment.