Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/opcua #66

Merged
merged 9 commits into from
May 19, 2019
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions NOTICE
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,6 @@ The Apache Software Foundation (http://www.apache.org/).

This product includes software developed at
The Netty project (https://netty.io/).

This product includes software developed at
The Milo project (https://github.com/eclipse/milo).
86 changes: 86 additions & 0 deletions plc4j/drivers/opcua/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
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.
-->
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>org.apache.plc4x</groupId>
<artifactId>plc4j-drivers</artifactId>
<version>0.4.0-SNAPSHOT</version>
</parent>

<artifactId>plc4j-driver-opcua</artifactId>
<name>PLC4J: Driver: OPC UA</name>
<description>Implementation of a PLC4X driver able to speak with devices using the OPC UA protocol.</description>

<dependencies>
<dependency>
<groupId>org.apache.plc4x</groupId>
<artifactId>plc4j-api</artifactId>
<version>0.4.0-SNAPSHOT</version>
</dependency>

<dependency>
<groupId>org.apache.plc4x</groupId>
<artifactId>plc4j-protocol-driver-base</artifactId>
<version>0.4.0-SNAPSHOT</version>
</dependency>

<dependency>
<groupId>org.eclipse.milo</groupId>
<artifactId>sdk-client</artifactId>
<version>0.3.0-M1</version>
</dependency>
<dependency>
<groupId>org.eclipse.milo</groupId>
<artifactId>stack-core</artifactId>
<version>0.3.0-M1</version>
</dependency>
<dependency>
<groupId>org.eclipse.milo</groupId>
<artifactId>stack-client</artifactId>
<version>0.3.0-M1</version>
</dependency>

<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
</dependency>

</dependencies>


<!--
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<configuration>
<usedDependencies combine.children="append">
<usedDependency>org.eclipse.milo:sdk-client</usedDependency>
</usedDependencies>
</configuration>
</plugin>
</plugins>
</build>
-->
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
/*
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.

* @author Matthias Milan Stlrljic
nalim2 marked this conversation as resolved.
Show resolved Hide resolved
* Created by Matthias Milan Stlrljic on 10.05.2019
*/
package org.apache.plc4x.java.opcua;

import org.apache.commons.lang3.StringUtils;
import org.apache.plc4x.java.api.PlcConnection;
import org.apache.plc4x.java.api.authentication.PlcAuthentication;
import org.apache.plc4x.java.api.exceptions.PlcConnectionException;
import org.apache.plc4x.java.opcua.connection.OpcuaConnectionFactory;
import org.apache.plc4x.java.spi.PlcDriver;

import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
* Implementation of the OPC UA protocol, based on:
* - Eclipse Milo (https://github.com/eclipse/milo)
*/
public class OpcuaPlcDriver implements PlcDriver {



public static final Pattern INET_ADDRESS_PATTERN = Pattern.compile("tcp://(?<host>[\\w.-]+)(:(?<port>\\d*))?");
public static final Pattern OPCUA_URI_PATTERN = Pattern.compile("^opcua:(" + INET_ADDRESS_PATTERN + ")?" + "(?<params>/[\\w/]+)?");
private static final int requestTimeout = 10000;
private OpcuaConnectionFactory opcuaConnectionFactory;

public OpcuaPlcDriver() {
this.opcuaConnectionFactory = new OpcuaConnectionFactory();
}

public OpcuaPlcDriver(OpcuaConnectionFactory opcuaConnectionFactory) {
this.opcuaConnectionFactory = opcuaConnectionFactory;
}

@Override
public String getProtocolCode() {
return "opcua";
}

@Override
public String getProtocolName() {
return "OPC UA (TCP)";
}

@Override
public PlcConnection connect(String url) throws PlcConnectionException {
Matcher matcher = OPCUA_URI_PATTERN.matcher(url);

if (!matcher.matches()) {
throw new PlcConnectionException(
"Connection url doesn't match the format 'opcua:{type}//{port|host}'");
}

String host = matcher.group("host");
String portString = matcher.group("port");
Integer port = StringUtils.isNotBlank(portString) ? Integer.parseInt(portString) : null;
String params = matcher.group("params") != null ? matcher.group("params").substring(1) : null;

try {
return opcuaConnectionFactory.opcuaTcpPlcConnectionOf(InetAddress.getByName(host), port, params, requestTimeout);
} catch (UnknownHostException e) {
throw new PlcConnectionException(e);
}
}

@Override
public PlcConnection connect(String url, PlcAuthentication authentication) throws PlcConnectionException {
throw new PlcConnectionException("opcua does not support Auth at this state");
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
/*
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.
* @author Matthias Milan Stlrljic
* Created by Matthias Milan Stlrljic on 10.05.2019
*/
package org.apache.plc4x.java.opcua.connection;

import org.apache.commons.lang3.StringUtils;
import org.apache.plc4x.java.api.messages.PlcReadRequest;
import org.apache.plc4x.java.api.messages.PlcSubscriptionRequest;
import org.apache.plc4x.java.api.messages.PlcUnsubscriptionRequest;
import org.apache.plc4x.java.api.messages.PlcWriteRequest;
import org.apache.plc4x.java.base.connection.AbstractPlcConnection;
import org.apache.plc4x.java.base.messages.*;
import org.apache.plc4x.java.opcua.protocol.OpcuaPlcFieldHandler;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


public abstract class BaseOpcuaPlcConnection extends AbstractPlcConnection implements PlcReader, PlcWriter, PlcSubscriber {

private static final Logger logger = LoggerFactory.getLogger(BaseOpcuaPlcConnection.class);

BaseOpcuaPlcConnection(String params) {

if (!StringUtils.isEmpty(params)) {
for (String param : params.split("&")) {
String[] paramElements = param.split("=");
String paramName = paramElements[0];
if (paramElements.length == 2) {
String paramValue = paramElements[1];
switch (paramName) {
default:
logger.debug("Unknown parameter {} with value {}", paramName, paramValue);
}
} else {
logger.debug("Unknown no-value parameter {}", paramName);
}
}
}
}

@Override
public boolean canRead() {
return true;
}

@Override
public boolean canWrite() {
return true;
}

@Override
public PlcReadRequest.Builder readRequestBuilder() {
return new DefaultPlcReadRequest.Builder(this, new OpcuaPlcFieldHandler());
}

@Override
public PlcWriteRequest.Builder writeRequestBuilder() {
return new DefaultPlcWriteRequest.Builder(this, new OpcuaPlcFieldHandler());
}

@Override
public boolean canSubscribe() {
return true;
}

@Override
public PlcSubscriptionRequest.Builder subscriptionRequestBuilder() {
return new DefaultPlcSubscriptionRequest.Builder(this, new OpcuaPlcFieldHandler());
}

@Override
public PlcUnsubscriptionRequest.Builder unsubscriptionRequestBuilder() {
return new DefaultPlcUnsubscriptionRequest.Builder(this);
}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
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.
* @author Matthias Milan Stlrljic
* Created by Matthias Milan Stlrljic on 10.05.2019
*/
package org.apache.plc4x.java.opcua.connection;

import java.net.InetAddress;
import java.util.Objects;

public class OpcuaConnectionFactory {

public OpcuaTcpPlcConnection opcuaTcpPlcConnectionOf(InetAddress address, Integer port, String params, int requestTimeout) {
Objects.requireNonNull(address);

if (port == null) {
return OpcuaTcpPlcConnection.of(address, params, requestTimeout);
} else {
return OpcuaTcpPlcConnection.of(address, port, params, requestTimeout);
}
}

}
Loading