You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@plc4x.apache.org by sr...@apache.org on 2018/03/08 16:01:41 UTC
[incubator-plc4x] 03/03: integrate serial connection into driver
This is an automated email from the ASF dual-hosted git repository.
sruehl pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-plc4x.git
commit 89dfb6a03d8bff4498b6378992bf4d71f52ac0a7
Author: Sebastian Rühl <sr...@apache.org>
AuthorDate: Thu Mar 8 17:01:33 2018 +0100
integrate serial connection into driver
---
.../org/apache/plc4x/java/ads/AdsPlcDriver.java | 40 ++++++++++++++++++----
.../apache/plc4x/java/ads/AdsPlcDriverTest.java | 35 +++++++++++++++++--
2 files changed, 65 insertions(+), 10 deletions(-)
diff --git a/plc4j/protocols/ads/src/main/java/org/apache/plc4x/java/ads/AdsPlcDriver.java b/plc4j/protocols/ads/src/main/java/org/apache/plc4x/java/ads/AdsPlcDriver.java
index 35d11f8..bab0506 100644
--- a/plc4j/protocols/ads/src/main/java/org/apache/plc4x/java/ads/AdsPlcDriver.java
+++ b/plc4j/protocols/ads/src/main/java/org/apache/plc4x/java/ads/AdsPlcDriver.java
@@ -21,6 +21,7 @@ package org.apache.plc4x.java.ads;
import org.apache.commons.lang3.StringUtils;
import org.apache.plc4x.java.ads.api.generic.types.AmsNetId;
import org.apache.plc4x.java.ads.api.generic.types.AmsPort;
+import org.apache.plc4x.java.ads.connection.AdsSerialPlcConnection;
import org.apache.plc4x.java.ads.connection.AdsTcpPlcConnection;
import org.apache.plc4x.java.api.PlcDriver;
import org.apache.plc4x.java.api.authentication.PlcAuthentication;
@@ -39,12 +40,14 @@ import java.util.regex.Pattern;
*/
public class AdsPlcDriver implements PlcDriver {
- private static final Pattern ADS_ADDRESS_PATTERN =
+ public static final Pattern ADS_ADDRESS_PATTERN =
Pattern.compile("(?<targetAmsNetId>" + AmsNetId.AMS_NET_ID_PATTERN + "):(?<targetAmsPort>" + AmsPort.AMS_PORT_PATTERN + ")"
+ "(/"
+ "(?<sourceAmsNetId>" + AmsNetId.AMS_NET_ID_PATTERN + "):(?<sourceAmsPort>" + AmsPort.AMS_PORT_PATTERN + ")"
+ ")?");
- private static final Pattern ADS_URI_PATTERN = Pattern.compile("^ads://(?<host>\\w+)(:(?<port>\\d*))?/" + ADS_ADDRESS_PATTERN);
+ public static final Pattern INET_ADDRESS_PATTERN = Pattern.compile("(?<host>[\\w.]+)(:(?<port>\\d*))?");
+ public static final Pattern SERIAL_PATTERN = Pattern.compile("(?<serialDefinition>serial:.*)");
+ public static final Pattern ADS_URI_PATTERN = Pattern.compile("^ads:/?/?(" + INET_ADDRESS_PATTERN + "|" + SERIAL_PATTERN + ")/" + ADS_ADDRESS_PATTERN);
@Override
public String getProtocolCode() {
@@ -61,9 +64,10 @@ public class AdsPlcDriver implements PlcDriver {
Matcher matcher = ADS_URI_PATTERN.matcher(url);
if (!matcher.matches()) {
throw new PlcConnectionException(
- "Connection url " + url + " doesn't match 'ads://{host|ip}/{targetAmsNetId}:{targetAmsPort}/{sourceAmsNetId}:{sourceAmsPort}' RAW:" + ADS_URI_PATTERN);
+ "Connection url " + url + " doesn't match 'ads://{{host|ip}|serial:definition}/{targetAmsNetId}:{targetAmsPort}/{sourceAmsNetId}:{sourceAmsPort}' RAW:" + ADS_URI_PATTERN);
}
String host = matcher.group("host");
+ String serialDefinition = matcher.group("serialDefinition");
String portString = matcher.group("port");
Integer port = StringUtils.isNotBlank(portString) ? Integer.parseInt(portString) : null;
AmsNetId targetAmsNetId = AmsNetId.of(matcher.group("targetAmsNetId"));
@@ -72,10 +76,32 @@ public class AdsPlcDriver implements PlcDriver {
AmsNetId sourceAmsNetId = StringUtils.isNotBlank(sourceAmsNetIdString) ? AmsNetId.of(sourceAmsNetIdString) : null;
String sourceAmsPortString = matcher.group("sourceAmsPort");
AmsPort sourceAmsPort = StringUtils.isNotBlank(sourceAmsPortString) ? AmsPort.of(sourceAmsPortString) : null;
- try {
- return new AdsTcpPlcConnection(InetAddress.getByName(host), port, targetAmsNetId, targetAmsPort, sourceAmsNetId, sourceAmsPort);
- } catch (UnknownHostException e) {
- throw new PlcConnectionException(e);
+
+ if (serialDefinition != null) {
+ String serialPort = serialDefinition.substring(serialDefinition.indexOf(':'));
+ if (sourceAmsNetId == null || sourceAmsPort == null) {
+ return new AdsSerialPlcConnection(serialPort, targetAmsNetId, targetAmsPort);
+ } else {
+ return new AdsSerialPlcConnection(serialPort, targetAmsNetId, targetAmsPort, sourceAmsNetId, sourceAmsPort);
+ }
+ } else {
+ try {
+ if (sourceAmsNetId == null || sourceAmsPort == null) {
+ if (port == null) {
+ return new AdsTcpPlcConnection(InetAddress.getByName(host), targetAmsNetId, targetAmsPort);
+ } else {
+ return new AdsTcpPlcConnection(InetAddress.getByName(host), port, targetAmsNetId, targetAmsPort);
+ }
+ } else {
+ if (port == null) {
+ return new AdsTcpPlcConnection(InetAddress.getByName(host), targetAmsNetId, targetAmsPort, sourceAmsNetId, sourceAmsPort);
+ } else {
+ return new AdsTcpPlcConnection(InetAddress.getByName(host), port, targetAmsNetId, targetAmsPort, sourceAmsNetId, sourceAmsPort);
+ }
+ }
+ } catch (UnknownHostException e) {
+ throw new PlcConnectionException(e);
+ }
}
}
diff --git a/plc4j/protocols/ads/src/test/java/org/apache/plc4x/java/ads/AdsPlcDriverTest.java b/plc4j/protocols/ads/src/test/java/org/apache/plc4x/java/ads/AdsPlcDriverTest.java
index 98965fe..99e47c7 100644
--- a/plc4j/protocols/ads/src/test/java/org/apache/plc4x/java/ads/AdsPlcDriverTest.java
+++ b/plc4j/protocols/ads/src/test/java/org/apache/plc4x/java/ads/AdsPlcDriverTest.java
@@ -28,9 +28,11 @@ import org.apache.plc4x.java.api.exceptions.PlcException;
import org.junit.Rule;
import org.junit.Test;
+import java.util.regex.Pattern;
+
+import static org.apache.plc4x.java.ads.AdsPlcDriver.*;
import static org.hamcrest.Matchers.is;
-import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertThat;
+import static org.junit.Assert.*;
public class AdsPlcDriverTest {
@@ -38,6 +40,33 @@ public class AdsPlcDriverTest {
public TcpHexDumper tcpHexDumper = new TcpHexDumper(0, 2);
@Test
+ public void testAdsAddressPattern() throws Exception {
+ assertMatching(ADS_ADDRESS_PATTERN, "0.0.0.0.0.0:13");
+ assertMatching(ADS_ADDRESS_PATTERN, "0.0.0.0.0.0:13/0.0.0.0.0.0:13");
+
+ assertMatching(INET_ADDRESS_PATTERN, "localhost");
+ assertMatching(INET_ADDRESS_PATTERN, "localhost:3131");
+ assertMatching(INET_ADDRESS_PATTERN, "www.google.de");
+ assertMatching(INET_ADDRESS_PATTERN, "www.google.de:443");
+
+ assertMatching(SERIAL_PATTERN, "serial:/dev/com1");
+ assertMatching(SERIAL_PATTERN, "serial:COM1");
+ assertMatching(SERIAL_PATTERN, "serial:/dev/ttyUSB0");
+
+ assertMatching(ADS_URI_PATTERN, "ads://www.google.de/0.0.0.0.0.0:13");
+ assertMatching(ADS_URI_PATTERN, "ads://www.google.de:443/0.0.0.0.0.0:13");
+ assertMatching(ADS_URI_PATTERN, "ads://serial:/dev/com1/0.0.0.0.0.0:13");
+ assertMatching(ADS_URI_PATTERN, "ads://serial:COM1/0.0.0.0.0.0:13");
+ assertMatching(ADS_URI_PATTERN, "ads://serial:/dev/ttyUSB0/0.0.0.0.0.0:13");
+ }
+
+ private void assertMatching(Pattern pattern, String match) {
+ if (!pattern.matcher(match).matches()) {
+ fail(pattern + "doesn't match " + match);
+ }
+ }
+
+ @Test
public void getConnection() throws Exception {
AdsTcpPlcConnection adsConnection = (AdsTcpPlcConnection)
new PlcDriverManager().getConnection("ads://localhost:" + tcpHexDumper.getPort() + "/0.0.0.0.0.0:13");
@@ -73,7 +102,7 @@ public class AdsPlcDriverTest {
}
@Test
- public void getProtocol() {
+ public void getProtocol() {
AdsPlcDriver driver = new AdsPlcDriver();
assertThat(driver.getProtocolCode(), is("ads"));
assertThat(driver.getProtocolName(), is("Beckhoff Twincat ADS"));
--
To stop receiving notification emails like this one, please contact
sruehl@apache.org.