From 21da7e6900be74f0eb0cfc12711713849a8e17cb Mon Sep 17 00:00:00 2001 From: Matthias Strljic Date: Tue, 14 May 2019 14:33:58 +0200 Subject: [PATCH] [Fixed] JavaDoc formatting [Added] Warning messages at exception points with the consideration of adding later some run-time exceptions --- .../plc4x/java/opcua/OpcuaPlcDriver.java | 9 +- .../connection/BaseOpcuaPlcConnection.java | 9 +- .../connection/OpcuaConnectionFactory.java | 9 +- .../connection/OpcuaTcpPlcConnection.java | 175 +++++++++--------- .../plc4x/java/opcua/protocol/OpcuaField.java | 14 +- .../opcua/protocol/OpcuaPlcFieldHandler.java | 9 +- .../protocol/OpcuaSubsriptionHandle.java | 9 +- .../opcua/protocol/model/OpcuaDataTypes.java | 39 ++-- .../protocol/model/OpcuaIdentifierType.java | 33 +++- .../plc4x/java/opcua/ManualPLC4XOpcua.java | 92 +++++---- .../plc4x/java/opcua/OpcuaPlcDriverTest.java | 9 +- .../apache/plc4x/java/opcua/UtilsTest.java | 9 +- .../connection/OpcuaTcpPlcConnectionTest.java | 9 +- .../java/opcua/protocol/OpcuaFieldTest.java | 9 +- .../protocol/OpcuaPlcFieldHandlerTest.java | 9 +- .../protocol/OpcuaSubscriptionHandleTest.java | 9 +- 16 files changed, 250 insertions(+), 202 deletions(-) diff --git a/plc4j/drivers/opcua/src/main/java/org/apache/plc4x/java/opcua/OpcuaPlcDriver.java b/plc4j/drivers/opcua/src/main/java/org/apache/plc4x/java/opcua/OpcuaPlcDriver.java index ef5f9858b78..7fefae7c45a 100644 --- a/plc4j/drivers/opcua/src/main/java/org/apache/plc4x/java/opcua/OpcuaPlcDriver.java +++ b/plc4j/drivers/opcua/src/main/java/org/apache/plc4x/java/opcua/OpcuaPlcDriver.java @@ -15,10 +15,7 @@ Licensed to the Apache Software Foundation (ASF) under one 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; import org.apache.commons.lang3.StringUtils; @@ -33,9 +30,13 @@ Licensed to the Apache Software Foundation (ASF) under one 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) + * + * @author Matthias Milan Stlrljic + * Created by Matthias Milan Stlrljic on 10.05.2019 */ public class OpcuaPlcDriver implements PlcDriver { diff --git a/plc4j/drivers/opcua/src/main/java/org/apache/plc4x/java/opcua/connection/BaseOpcuaPlcConnection.java b/plc4j/drivers/opcua/src/main/java/org/apache/plc4x/java/opcua/connection/BaseOpcuaPlcConnection.java index 0585e828174..50a5bd0633c 100644 --- a/plc4j/drivers/opcua/src/main/java/org/apache/plc4x/java/opcua/connection/BaseOpcuaPlcConnection.java +++ b/plc4j/drivers/opcua/src/main/java/org/apache/plc4x/java/opcua/connection/BaseOpcuaPlcConnection.java @@ -15,9 +15,7 @@ Licensed to the Apache Software Foundation (ASF) under one 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; @@ -31,7 +29,10 @@ Licensed to the Apache Software Foundation (ASF) under one import org.slf4j.Logger; import org.slf4j.LoggerFactory; - +/** + * @author Matthias Milan Stlrljic + * Created by Matthias Milan Stlrljic on 10.05.2019 + */ public abstract class BaseOpcuaPlcConnection extends AbstractPlcConnection implements PlcReader, PlcWriter, PlcSubscriber { private static final Logger logger = LoggerFactory.getLogger(BaseOpcuaPlcConnection.class); diff --git a/plc4j/drivers/opcua/src/main/java/org/apache/plc4x/java/opcua/connection/OpcuaConnectionFactory.java b/plc4j/drivers/opcua/src/main/java/org/apache/plc4x/java/opcua/connection/OpcuaConnectionFactory.java index 84046ad2916..5c399f30843 100644 --- a/plc4j/drivers/opcua/src/main/java/org/apache/plc4x/java/opcua/connection/OpcuaConnectionFactory.java +++ b/plc4j/drivers/opcua/src/main/java/org/apache/plc4x/java/opcua/connection/OpcuaConnectionFactory.java @@ -15,14 +15,15 @@ Licensed to the Apache Software Foundation (ASF) under one 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; - +/** + * @author Matthias Milan Stlrljic + * Created by Matthias Milan Stlrljic on 10.05.2019 + */ public class OpcuaConnectionFactory { public OpcuaTcpPlcConnection opcuaTcpPlcConnectionOf(InetAddress address, Integer port, String params, int requestTimeout) { diff --git a/plc4j/drivers/opcua/src/main/java/org/apache/plc4x/java/opcua/connection/OpcuaTcpPlcConnection.java b/plc4j/drivers/opcua/src/main/java/org/apache/plc4x/java/opcua/connection/OpcuaTcpPlcConnection.java index b7de4b60268..c59c36a35ea 100644 --- a/plc4j/drivers/opcua/src/main/java/org/apache/plc4x/java/opcua/connection/OpcuaTcpPlcConnection.java +++ b/plc4j/drivers/opcua/src/main/java/org/apache/plc4x/java/opcua/connection/OpcuaTcpPlcConnection.java @@ -94,6 +94,7 @@ public OpcuaTcpPlcConnection(InetAddress address, int port, String params, int r this.address = address; this.port = port; this.params = params; + this.requestTimeout = requestTimeout; } public OpcuaTcpPlcConnection(String params) { @@ -108,7 +109,51 @@ public static OpcuaTcpPlcConnection of(InetAddress address, int port, String par return new OpcuaTcpPlcConnection(address, port, params, requestTimeout); } + public static BaseDefaultFieldItem encodeFieldItem(DataValue value){ + NodeId typeNode = value.getValue().getDataType().get(); + Object objValue = value.getValue().getValue(); + + if(typeNode.equals(Identifiers.Boolean)){ + return new DefaultBooleanFieldItem((Boolean)objValue); + }else if (typeNode.equals(Identifiers.ByteString)){ + byte[] array = ((ByteString)objValue).bytes(); + Byte[] byteArry = new Byte[array.length]; + int counter = 0; + for (byte bytie: array + ) { + byteArry[counter] = bytie; + counter++; + } + return new DefaultByteArrayFieldItem(byteArry); + }else if (typeNode.equals(Identifiers.Integer)){ + return new DefaultIntegerFieldItem((Integer)objValue); + }else if (typeNode.equals(Identifiers.Int16)){ + return new DefaultShortFieldItem((Short)objValue); + }else if (typeNode.equals(Identifiers.Int32)){ + return new DefaultIntegerFieldItem((Integer)objValue); + }else if (typeNode.equals(Identifiers.Int64)){ + return new DefaultLongFieldItem((Long)objValue); + }else if (typeNode.equals(Identifiers.UInteger)){ + return new DefaultLongFieldItem((Long)objValue); + }else if (typeNode.equals(Identifiers.UInt16)){ + return new DefaultIntegerFieldItem(((UShort)objValue).intValue()); + }else if (typeNode.equals(Identifiers.UInt32)){ + return new DefaultLongFieldItem(((UInteger)objValue).longValue()); + }else if (typeNode.equals(Identifiers.UInt64)){ + return new DefaultBigIntegerFieldItem(new BigInteger(objValue.toString())); + }else if (typeNode.equals(Identifiers.Byte)){ + return new DefaultShortFieldItem(Short.valueOf(objValue.toString())); + }else if (typeNode.equals(Identifiers.Float)){ + return new DefaultFloatFieldItem((Float)objValue); + }else if (typeNode.equals(Identifiers.Double)){ + return new DefaultDoubleFieldItem((Double)objValue); + }else if (typeNode.equals(Identifiers.SByte)){ + return new DefaultByteFieldItem((Byte)objValue); + }else { + return new DefaultStringFieldItem(objValue.toString()); + } + } public InetAddress getRemoteAddress() { return address; @@ -120,6 +165,7 @@ public void connect() throws PlcConnectionException { try { endpoints = DiscoveryClient.getEndpoints(getEndpointUrl(address, port, params)).get(); + //TODO Exception should be handeled better when the Discovery-API of Milo is stable } catch (Exception ex) { // try the explicit discovery endpoint as well String discoveryUrl = getEndpointUrl(address, port, params); @@ -144,7 +190,7 @@ public void connect() throws PlcConnectionException { .orElseThrow(() -> new PlcConnectionException("No desired endpoints from")); OpcUaClientConfig config = OpcUaClientConfig.builder() - .setApplicationName(LocalizedText.english("eclipse milo opc-ua client")) + .setApplicationName(LocalizedText.english("eclipse milo opc-ua client of the apache PLC4X:PLC4J project")) .setApplicationUri("urn:eclipse:milo:plc4x:client") .setEndpoint(endpoint) .setIdentityProvider(getIdentityProvider()) @@ -155,9 +201,13 @@ public void connect() throws PlcConnectionException { this.client = OpcUaClient.create(config); this.client.connect().get(); isConnected = true; - } catch (UaException | InterruptedException | ExecutionException e) { + } catch (UaException e) { isConnected = false; - + String message = (config == null) ? "NULL" : config.toString(); + throw new PlcConnectionException("The given input values are a not valid OPC UA connection configuration [CONFIG]: " + message); + } catch (InterruptedException | ExecutionException e) { + isConnected = false; + throw new PlcConnectionException("Error while creation of the connection because of : " + e.getMessage()); } } @@ -232,18 +282,15 @@ public CompletableFuture subscribe(PlcSubscriptionReque subHandle = subsriptionHandle; responseCode = PlcResponseCode.OK; - } catch (InterruptedException e) { - e.printStackTrace(); - } catch (ExecutionException e) { - e.printStackTrace(); + } catch (InterruptedException | ExecutionException e) { + logger.warn("Unable to subscribe Elements because of: {}", e.getMessage()); } return Pair.of(plcFieldName, Pair.of(responseCode, subHandle)); }) .collect(Collectors.toMap(Pair::getKey, Pair::getValue)); - PlcSubscriptionResponse result = new DefaultPlcSubscriptionResponse(internalPlcSubscriptionRequest, responseItems); - return result; + return (PlcSubscriptionResponse) new DefaultPlcSubscriptionResponse(internalPlcSubscriptionRequest, responseItems); }); return future; @@ -256,10 +303,8 @@ public CompletableFuture unsubscribe(PlcUnsubscriptio OpcuaSubsriptionHandle opcSubHandle = (OpcuaSubsriptionHandle) o; try { client.getSubscriptionManager().deleteSubscription(opcSubHandle.getClientHandle()).get(); - } catch (InterruptedException e) { - e.printStackTrace(); - } catch (ExecutionException e) { - e.printStackTrace(); + } catch (InterruptedException | ExecutionException e) { + logger.warn("Unable to unsubscribe Elements because of: {}", e.getMessage()); } }); @@ -295,17 +340,13 @@ public CompletableFuture read(PlcReadRequest readRequest) { List readValues = null; try { readValues = dataValueCompletableFuture.get(); - } catch (InterruptedException e) { - e.printStackTrace(); - readValues = new LinkedList<>(); - } catch (ExecutionException e) { - e.printStackTrace(); - readValues = new LinkedList<>(); + } catch (InterruptedException | ExecutionException e) { + logger.warn("Unable to read Elements because of: {}", e.getMessage()); } - for(int counter = 0; counter < readValues.size(); counter++){ + for(int counter = 0; counter < readValueIds.size(); counter++){ PlcResponseCode resultCode = PlcResponseCode.OK; BaseDefaultFieldItem stringItem = null; - if(readValues.get(counter).getStatusCode() != StatusCode.GOOD){ + if(readValues == null || readValues.size() <= counter || readValues.get(counter).getStatusCode() != StatusCode.GOOD){ resultCode = PlcResponseCode.NOT_FOUND; }else{ stringItem = encodeFieldItem(readValues.get(counter)); @@ -317,81 +358,13 @@ public CompletableFuture read(PlcReadRequest readRequest) { } InternalPlcReadRequest internalPlcReadRequest = checkInternal(readRequest, InternalPlcReadRequest.class); - PlcReadResponse response = new DefaultPlcReadResponse(internalPlcReadRequest, fields ); - return response; + return (PlcReadResponse) new DefaultPlcReadResponse(internalPlcReadRequest, fields ); }); return future; } - private NodeId generateNodeId(OpcuaField uaField){ - NodeId idNode = null; - switch (uaField.getIdentifierType()) { - case s: - idNode = new NodeId(uaField.getNamespace(), uaField.getIdentifier()); - break; - case i: - idNode = new NodeId(uaField.getNamespace(), UInteger.valueOf(uaField.getIdentifier())); - break; - case g: - idNode = new NodeId(uaField.getNamespace(), UUID.fromString(uaField.getIdentifier())); - break; - case b: - idNode = new NodeId(uaField.getNamespace(), new ByteString(uaField.getIdentifier().getBytes())); - break; - - default: idNode = new NodeId(uaField.getNamespace(), uaField.getIdentifier()); - } - - return idNode; - } - - public static BaseDefaultFieldItem encodeFieldItem(DataValue value){ - NodeId typeNode = value.getValue().getDataType().get(); - Object objValue = value.getValue().getValue(); - - if(typeNode.equals(Identifiers.Boolean)){ - return new DefaultBooleanFieldItem((Boolean)objValue); - }else if (typeNode.equals(Identifiers.ByteString)){ - byte[] array = ((ByteString)objValue).bytes(); - Byte[] byteArry = new Byte[array.length]; - int counter = 0; - for (byte bytie: array - ) { - byteArry[counter] = bytie; - counter++; - } - return new DefaultByteArrayFieldItem(byteArry); - }else if (typeNode.equals(Identifiers.Integer)){ - return new DefaultIntegerFieldItem((Integer)objValue); - }else if (typeNode.equals(Identifiers.Int16)){ - return new DefaultShortFieldItem((Short)objValue); - }else if (typeNode.equals(Identifiers.Int32)){ - return new DefaultIntegerFieldItem((Integer)objValue); - }else if (typeNode.equals(Identifiers.Int64)){ - return new DefaultLongFieldItem((Long)objValue); - }else if (typeNode.equals(Identifiers.UInteger)){ - return new DefaultLongFieldItem((Long)objValue); - }else if (typeNode.equals(Identifiers.UInt16)){ - return new DefaultIntegerFieldItem(((UShort)objValue).intValue()); - }else if (typeNode.equals(Identifiers.UInt32)){ - return new DefaultLongFieldItem(((UInteger)objValue).longValue()); - }else if (typeNode.equals(Identifiers.UInt64)){ - return new DefaultBigIntegerFieldItem(new BigInteger(objValue.toString())); - }else if (typeNode.equals(Identifiers.Byte)){ - return new DefaultShortFieldItem(Short.valueOf(objValue.toString())); - }else if (typeNode.equals(Identifiers.Float)){ - return new DefaultFloatFieldItem((Float)objValue); - }else if (typeNode.equals(Identifiers.Double)){ - return new DefaultDoubleFieldItem((Double)objValue); - }else if (typeNode.equals(Identifiers.SByte)){ - return new DefaultByteFieldItem((Byte)objValue); - }else { - return new DefaultStringFieldItem(objValue.toString()); - } - - } @Override public CompletableFuture write(PlcWriteRequest writeRequest) { @@ -451,6 +424,28 @@ public CompletableFuture write(PlcWriteRequest writeRequest) { } + private NodeId generateNodeId(OpcuaField uaField){ + NodeId idNode = null; + switch (uaField.getIdentifierType()) { + case STRING_IDENTIFIER: + idNode = new NodeId(uaField.getNamespace(), uaField.getIdentifier()); + break; + case NUMBER_IDENTIFIER: + idNode = new NodeId(uaField.getNamespace(), UInteger.valueOf(uaField.getIdentifier())); + break; + case GUID_IDENTIFIER: + idNode = new NodeId(uaField.getNamespace(), UUID.fromString(uaField.getIdentifier())); + break; + case BINARY_IDENTIFIER: + idNode = new NodeId(uaField.getNamespace(), new ByteString(uaField.getIdentifier().getBytes())); + break; + + default: idNode = new NodeId(uaField.getNamespace(), uaField.getIdentifier()); + } + + return idNode; + } + private String getEndpointUrl(InetAddress address, Integer port, String params) { return "opc.tcp://" + address.getHostAddress() +":" + port + "/" + params; } diff --git a/plc4j/drivers/opcua/src/main/java/org/apache/plc4x/java/opcua/protocol/OpcuaField.java b/plc4j/drivers/opcua/src/main/java/org/apache/plc4x/java/opcua/protocol/OpcuaField.java index cd33c755716..91ece25e682 100644 --- a/plc4j/drivers/opcua/src/main/java/org/apache/plc4x/java/opcua/protocol/OpcuaField.java +++ b/plc4j/drivers/opcua/src/main/java/org/apache/plc4x/java/opcua/protocol/OpcuaField.java @@ -15,9 +15,7 @@ Licensed to the Apache Software Foundation (ASF) under one 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.protocol; import org.apache.plc4x.java.api.exceptions.PlcInvalidFieldException; @@ -27,7 +25,10 @@ Licensed to the Apache Software Foundation (ASF) under one import java.util.Objects; import java.util.regex.Matcher; import java.util.regex.Pattern; - +/** + * @author Matthias Milan Stlrljic + * Created by Matthias Milan Stlrljic on 10.05.2019 + */ public class OpcuaField implements PlcField { public static final Pattern ADDRESS_PATTERN = Pattern.compile("^ns=(?\\d+);(?[isgb])=((?\\w+))?"); @@ -64,7 +65,7 @@ public static OpcuaField of(String address) { String identifier = matcher.group("identifier"); String identifierTypeString = matcher.group("identifierType"); - OpcuaIdentifierType identifierType = OpcuaIdentifierType.valueOf(identifierTypeString); + OpcuaIdentifierType identifierType = OpcuaIdentifierType.fromString(identifierTypeString); String namespaceString = matcher.group("namespace"); Integer namespace = namespaceString != null ? Integer.valueOf(namespaceString) : 0; @@ -98,7 +99,7 @@ public boolean equals(Object o) { return false; } OpcuaField that = (OpcuaField) o; - return namespace == that.namespace && identifier.equals(that.identifier); + return namespace == that.namespace && identifier.equals(that.identifier) && identifierType == that.identifierType; } @Override @@ -110,6 +111,7 @@ public int hashCode() { public String toString() { return "OpcuaField{" + "namespace=" + namespace + + "identifierType=" + identifierType.getText() + "identifier=" + identifier + '}'; } diff --git a/plc4j/drivers/opcua/src/main/java/org/apache/plc4x/java/opcua/protocol/OpcuaPlcFieldHandler.java b/plc4j/drivers/opcua/src/main/java/org/apache/plc4x/java/opcua/protocol/OpcuaPlcFieldHandler.java index c39a35240f5..bb44a8e9a75 100644 --- a/plc4j/drivers/opcua/src/main/java/org/apache/plc4x/java/opcua/protocol/OpcuaPlcFieldHandler.java +++ b/plc4j/drivers/opcua/src/main/java/org/apache/plc4x/java/opcua/protocol/OpcuaPlcFieldHandler.java @@ -15,9 +15,7 @@ Licensed to the Apache Software Foundation (ASF) under one 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.protocol; @@ -28,7 +26,10 @@ Licensed to the Apache Software Foundation (ASF) under one import java.math.BigInteger; import java.util.ArrayList; - +/** + * @author Matthias Milan Stlrljic + * Created by Matthias Milan Stlrljic on 10.05.2019 + */ public class OpcuaPlcFieldHandler extends DefaultPlcFieldHandler { @Override diff --git a/plc4j/drivers/opcua/src/main/java/org/apache/plc4x/java/opcua/protocol/OpcuaSubsriptionHandle.java b/plc4j/drivers/opcua/src/main/java/org/apache/plc4x/java/opcua/protocol/OpcuaSubsriptionHandle.java index 08da653de95..8080d2209c5 100644 --- a/plc4j/drivers/opcua/src/main/java/org/apache/plc4x/java/opcua/protocol/OpcuaSubsriptionHandle.java +++ b/plc4j/drivers/opcua/src/main/java/org/apache/plc4x/java/opcua/protocol/OpcuaSubsriptionHandle.java @@ -15,9 +15,7 @@ Licensed to the Apache Software Foundation (ASF) under one 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.protocol; import org.apache.commons.lang3.tuple.ImmutablePair; @@ -40,7 +38,10 @@ Licensed to the Apache Software Foundation (ASF) under one import java.util.Map; import java.util.Set; import java.util.function.Consumer; - +/** + * @author Matthias Milan Stlrljic + * Created by Matthias Milan Stlrljic on 10.05.2019 + */ public class OpcuaSubsriptionHandle implements PlcSubscriptionHandle { Set< Consumer> consumers = new HashSet<>(); String fieldName; diff --git a/plc4j/drivers/opcua/src/main/java/org/apache/plc4x/java/opcua/protocol/model/OpcuaDataTypes.java b/plc4j/drivers/opcua/src/main/java/org/apache/plc4x/java/opcua/protocol/model/OpcuaDataTypes.java index c0bfe6522dd..af951375b6e 100644 --- a/plc4j/drivers/opcua/src/main/java/org/apache/plc4x/java/opcua/protocol/model/OpcuaDataTypes.java +++ b/plc4j/drivers/opcua/src/main/java/org/apache/plc4x/java/opcua/protocol/model/OpcuaDataTypes.java @@ -15,31 +15,32 @@ Licensed to the Apache Software Foundation (ASF) under one KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. +*/ +package org.apache.plc4x.java.opcua.protocol.model; +/** * @author Matthias Milan Stlrljic * Created by Matthias Milan Stlrljic on 10.05.2019 */ -package org.apache.plc4x.java.opcua.protocol.model; - public enum OpcuaDataTypes { BOOL, SByte, Byte, - Int16, - UInt16, - Int32, - UInt32, - Int64, - Uint64, - Float, - Double, - StatusCode, - String, - DateTime, - Guid, - ByteString, - XMLElement, - NodeId, - ExpandedNodeId, - QualifiedName + INT16, + UINT16, + INT32, + UINT32, + INT64, + UINT64, + FLOAT, + DOUBLE, + STATUS_CODE, + STRING, + DATE_TIME, + GUID, + BYTE_STRING, + XML_ELEMENT, + NODE_ID, + EXPANDABLE_NODE_ID, + QUALIFIED_NAME } diff --git a/plc4j/drivers/opcua/src/main/java/org/apache/plc4x/java/opcua/protocol/model/OpcuaIdentifierType.java b/plc4j/drivers/opcua/src/main/java/org/apache/plc4x/java/opcua/protocol/model/OpcuaIdentifierType.java index 599ad059f66..3122deb11b7 100644 --- a/plc4j/drivers/opcua/src/main/java/org/apache/plc4x/java/opcua/protocol/model/OpcuaIdentifierType.java +++ b/plc4j/drivers/opcua/src/main/java/org/apache/plc4x/java/opcua/protocol/model/OpcuaIdentifierType.java @@ -15,14 +15,35 @@ Licensed to the Apache Software Foundation (ASF) under one 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.protocol.model; +/** + * @author Matthias Milan Stlrljic + * Created by Matthias Milan Stlrljic on 10.05.2019 + */ public enum OpcuaIdentifierType { - s, - i, - g, - b + STRING_IDENTIFIER("s"), + NUMBER_IDENTIFIER("i"), + GUID_IDENTIFIER("g"), + BINARY_IDENTIFIER("BINARY_IDENTIFIER"); + + private String text; + + OpcuaIdentifierType(String text) { + this.text = text; + } + + public String getText() { + return this.text; + } + + public static OpcuaIdentifierType fromString(String text) { + for (OpcuaIdentifierType type : OpcuaIdentifierType.values()) { + if (type.text.equalsIgnoreCase(text)) { + return type; + } + } + throw new IllegalArgumentException("No constant with text " + text + " found"); + } } diff --git a/plc4j/drivers/opcua/src/test/java/org/apache/plc4x/java/opcua/ManualPLC4XOpcua.java b/plc4j/drivers/opcua/src/test/java/org/apache/plc4x/java/opcua/ManualPLC4XOpcua.java index 8d12925c7dc..57d1f798a75 100644 --- a/plc4j/drivers/opcua/src/test/java/org/apache/plc4x/java/opcua/ManualPLC4XOpcua.java +++ b/plc4j/drivers/opcua/src/test/java/org/apache/plc4x/java/opcua/ManualPLC4XOpcua.java @@ -15,9 +15,7 @@ Licensed to the Apache Software Foundation (ASF) under one 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; import org.apache.plc4x.java.PlcDriverManager; @@ -39,8 +37,29 @@ Licensed to the Apache Software Foundation (ASF) under one import java.util.Collections; import java.util.LinkedHashMap; import java.util.function.Consumer; - +/** + * @author Matthias Milan Stlrljic + * Created by Matthias Milan Stlrljic on 10.05.2019 + */ public class ManualPLC4XOpcua { + private static final String BOOL_IDENTIFIER = "ns=2;i=10844"; + private static final String BYTE_STRING_IDENTIFIER = "ns=2;i=10858"; + private static final String BYTE_IDENTIFIER = "ns=2;i=10846"; + private static final String DOUBLE_IDENTIFIER = "ns=2;i=10854"; + private static final String FLOAT_IDENTIFIER = "ns=2;i=10853"; + private static final String INT16_IDENTIFIER = "ns=2;i=10847"; + private static final String INT32_IDENTIFIER = "ns=2;i=10849"; + private static final String INT64_IDENTIFIER = "ns=2;i=10851"; + private static final String INTEGER_IDENTIFIER = "ns=2;i=10869"; + private static final String SBYTE_IDENTIFIER = "ns=2;i=10845"; + private static final String STRING_IDENTIFIER = "ns=2;i=10855"; + private static final String UINT16_IDENTIFIER = "ns=2;i=10848"; + private static final String UINT32_IDENTIFIER = "ns=2;i=10850"; + private static final String UINT64_IDENTIFIER = "ns=2;i=10852"; + private static final String UINTEGER_IDENTIFIER = "ns=2;i=10870"; + private static final String DOES_NOT_EXIST_IDENTIFIER = "ns=2;i=12512623"; + + public static void main(String args[]){ @@ -56,45 +75,44 @@ public static void main(String args[]){ } try { PlcReadRequest.Builder builder = opcuaConnection.readRequestBuilder(); - //builder.addItem("String", "ns=2;i=10855"); - builder.addItem("Bool", "ns=2;i=10844"); - builder.addItem("ByteString", "ns=2;i=10858"); - builder.addItem("Byte", "ns=2;i=10846"); - builder.addItem("Double", "ns=2;i=10854"); - builder.addItem("Float", "ns=2;i=10853"); - builder.addItem("Int16", "ns=2;i=10847"); - builder.addItem("Int32", "ns=2;i=10849"); - builder.addItem("Int64", "ns=2;i=10851"); - builder.addItem("Integer", "ns=2;i=10869"); - builder.addItem("SByte", "ns=2;i=10845"); - builder.addItem("String", "ns=2;i=10855"); - builder.addItem("UInt16", "ns=2;i=10848"); - builder.addItem("UInt32", "ns=2;i=10850"); - builder.addItem("UInt64", "ns=2;i=10852"); - builder.addItem("UInteger", "ns=2;i=10870"); + builder.addItem("Bool", BOOL_IDENTIFIER); + builder.addItem("ByteString", BYTE_STRING_IDENTIFIER); + builder.addItem("Byte", BYTE_IDENTIFIER); + builder.addItem("Double", DOUBLE_IDENTIFIER); + builder.addItem("Float", FLOAT_IDENTIFIER); + builder.addItem("Int16", INT16_IDENTIFIER); + builder.addItem("Int32", INT32_IDENTIFIER); + builder.addItem("Int64", INT64_IDENTIFIER); + builder.addItem("Integer", INTEGER_IDENTIFIER); + builder.addItem("SByte", SBYTE_IDENTIFIER); + builder.addItem("String", STRING_IDENTIFIER); + builder.addItem("UInt16", UINT16_IDENTIFIER); + builder.addItem("UInt32", UINT32_IDENTIFIER); + builder.addItem("UInt64", UINT64_IDENTIFIER); + builder.addItem("UInteger", UINTEGER_IDENTIFIER); - builder.addItem("DoesNotExists", "ns=2;i=12512623"); + builder.addItem("DoesNotExists", DOES_NOT_EXIST_IDENTIFIER); PlcReadRequest request = builder.build(); PlcReadResponse response = opcuaConnection.read(request).get(); Collection coll = response.getAllStrings("String"); PlcWriteRequest.Builder wBuilder = opcuaConnection.writeRequestBuilder(); - wBuilder.addItem("w-Bool", "ns=2;i=11012", true); - wBuilder.addItem("w-ByteString", "ns=2;i=10858", "TEST".getBytes()); - wBuilder.addItem("w-Byte", "ns=2;i=10846", (byte)1); - wBuilder.addItem("w-Double", "ns=2;i=10854", (double)0.25); - wBuilder.addItem("w-Float", "ns=2;i=10853", (float)0.25); - wBuilder.addItem("w-Int16", "ns=2;i=10847", (short)12); - wBuilder.addItem("w-Int32", "ns=2;i=10849", (int)314); - wBuilder.addItem("w-Int64", "ns=2;i=10851", (long)123125); - wBuilder.addItem("w-Integer", "ns=2;i=10869", (int)314); - wBuilder.addItem("w-SByte", "ns=2;i=10845", (short)23); - wBuilder.addItem("w-String", "ns=2;i=10855", "TEST"); - wBuilder.addItem("w-UInt16", "ns=2;i=10848", (int)222); - wBuilder.addItem("w-UInt32", "ns=2;i=10850", (long)21412); - wBuilder.addItem("w-UInt64", "ns=2;i=10852", new BigInteger("1245152")); - wBuilder.addItem("w-UInteger", "ns=2;i=10870", new BigInteger("1245152")); + wBuilder.addItem("w-Bool", BOOL_IDENTIFIER, true); + wBuilder.addItem("w-ByteString", BYTE_STRING_IDENTIFIER, "TEST".getBytes()); + wBuilder.addItem("w-Byte", BYTE_IDENTIFIER, (byte)1); + wBuilder.addItem("w-Double", DOUBLE_IDENTIFIER, (double)0.25); + wBuilder.addItem("w-Float", FLOAT_IDENTIFIER, (float)0.25); + wBuilder.addItem("w-INT16", INT16_IDENTIFIER, (short)12); + wBuilder.addItem("w-Int32", INT32_IDENTIFIER, (int)314); + wBuilder.addItem("w-Int64", INT64_IDENTIFIER, (long)123125); + wBuilder.addItem("w-Integer", INTEGER_IDENTIFIER, (int)314); + wBuilder.addItem("w-SByte", SBYTE_IDENTIFIER, (short)23); + wBuilder.addItem("w-String", STRING_IDENTIFIER, "TEST"); + wBuilder.addItem("w-UInt16", UINT16_IDENTIFIER, (int)222); + wBuilder.addItem("w-UInt32", UINT32_IDENTIFIER, (long)21412); + wBuilder.addItem("w-UInt64", UINT64_IDENTIFIER, new BigInteger("1245152")); + wBuilder.addItem("w-UInteger", UINTEGER_IDENTIFIER, new BigInteger("1245152")); PlcWriteRequest writeRequest = wBuilder.build(); PlcWriteResponse wResponse = opcuaConnection.write(writeRequest).get(); @@ -102,7 +120,7 @@ public static void main(String args[]){ opcuaConnection, new LinkedHashMap<>( Collections.singletonMap("field1", - new SubscriptionPlcField(PlcSubscriptionType.CHANGE_OF_STATE, OpcuaField.of("ns=2;i=10855"), Duration.of(1, ChronoUnit.SECONDS))) + new SubscriptionPlcField(PlcSubscriptionType.CHANGE_OF_STATE, OpcuaField.of(STRING_IDENTIFIER), Duration.of(1, ChronoUnit.SECONDS))) ) )).get(); diff --git a/plc4j/drivers/opcua/src/test/java/org/apache/plc4x/java/opcua/OpcuaPlcDriverTest.java b/plc4j/drivers/opcua/src/test/java/org/apache/plc4x/java/opcua/OpcuaPlcDriverTest.java index 0e0dd01e2ae..8c502ed2fed 100644 --- a/plc4j/drivers/opcua/src/test/java/org/apache/plc4x/java/opcua/OpcuaPlcDriverTest.java +++ b/plc4j/drivers/opcua/src/test/java/org/apache/plc4x/java/opcua/OpcuaPlcDriverTest.java @@ -15,9 +15,7 @@ Licensed to the Apache Software Foundation (ASF) under one 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; import org.junit.jupiter.api.AfterEach; @@ -27,7 +25,10 @@ Licensed to the Apache Software Foundation (ASF) under one import static org.apache.plc4x.java.opcua.OpcuaPlcDriver.INET_ADDRESS_PATTERN; import static org.apache.plc4x.java.opcua.OpcuaPlcDriver.OPCUA_URI_PATTERN; import static org.apache.plc4x.java.opcua.UtilsTest.assertMatching; - +/** + * @author Matthias Milan Stlrljic + * Created by Matthias Milan Stlrljic on 10.05.2019 + */ public class OpcuaPlcDriverTest { @BeforeEach public void before() { diff --git a/plc4j/drivers/opcua/src/test/java/org/apache/plc4x/java/opcua/UtilsTest.java b/plc4j/drivers/opcua/src/test/java/org/apache/plc4x/java/opcua/UtilsTest.java index bb1851afcca..2e84a0b63a6 100644 --- a/plc4j/drivers/opcua/src/test/java/org/apache/plc4x/java/opcua/UtilsTest.java +++ b/plc4j/drivers/opcua/src/test/java/org/apache/plc4x/java/opcua/UtilsTest.java @@ -15,15 +15,16 @@ Licensed to the Apache Software Foundation (ASF) under one 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; import java.util.regex.Pattern; import static org.junit.Assert.fail; - +/** + * @author Matthias Milan Stlrljic + * Created by Matthias Milan Stlrljic on 10.05.2019 + */ public class UtilsTest { public static void assertMatching(Pattern pattern, String match) { if (!pattern.matcher(match).matches()) { diff --git a/plc4j/drivers/opcua/src/test/java/org/apache/plc4x/java/opcua/connection/OpcuaTcpPlcConnectionTest.java b/plc4j/drivers/opcua/src/test/java/org/apache/plc4x/java/opcua/connection/OpcuaTcpPlcConnectionTest.java index 0e31f613ebf..5a42c5e28ae 100644 --- a/plc4j/drivers/opcua/src/test/java/org/apache/plc4x/java/opcua/connection/OpcuaTcpPlcConnectionTest.java +++ b/plc4j/drivers/opcua/src/test/java/org/apache/plc4x/java/opcua/connection/OpcuaTcpPlcConnectionTest.java @@ -15,14 +15,15 @@ Licensed to the Apache Software Foundation (ASF) under one 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.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeEach; - +/** + * @author Matthias Milan Stlrljic + * Created by Matthias Milan Stlrljic on 10.05.2019 + */ public class OpcuaTcpPlcConnectionTest { @BeforeEach public void before() { diff --git a/plc4j/drivers/opcua/src/test/java/org/apache/plc4x/java/opcua/protocol/OpcuaFieldTest.java b/plc4j/drivers/opcua/src/test/java/org/apache/plc4x/java/opcua/protocol/OpcuaFieldTest.java index f838b45aaed..828a0e6ac80 100644 --- a/plc4j/drivers/opcua/src/test/java/org/apache/plc4x/java/opcua/protocol/OpcuaFieldTest.java +++ b/plc4j/drivers/opcua/src/test/java/org/apache/plc4x/java/opcua/protocol/OpcuaFieldTest.java @@ -15,9 +15,7 @@ Licensed to the Apache Software Foundation (ASF) under one 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.protocol; import org.junit.jupiter.api.AfterEach; @@ -26,7 +24,10 @@ Licensed to the Apache Software Foundation (ASF) under one import static org.apache.plc4x.java.opcua.UtilsTest.assertMatching; import static org.apache.plc4x.java.opcua.protocol.OpcuaField.ADDRESS_PATTERN; - +/** + * @author Matthias Milan Stlrljic + * Created by Matthias Milan Stlrljic on 10.05.2019 + */ public class OpcuaFieldTest { @BeforeEach diff --git a/plc4j/drivers/opcua/src/test/java/org/apache/plc4x/java/opcua/protocol/OpcuaPlcFieldHandlerTest.java b/plc4j/drivers/opcua/src/test/java/org/apache/plc4x/java/opcua/protocol/OpcuaPlcFieldHandlerTest.java index 315358230f8..044de340e3f 100644 --- a/plc4j/drivers/opcua/src/test/java/org/apache/plc4x/java/opcua/protocol/OpcuaPlcFieldHandlerTest.java +++ b/plc4j/drivers/opcua/src/test/java/org/apache/plc4x/java/opcua/protocol/OpcuaPlcFieldHandlerTest.java @@ -15,14 +15,15 @@ Licensed to the Apache Software Foundation (ASF) under one 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.protocol; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeEach; - +/** + * @author Matthias Milan Stlrljic + * Created by Matthias Milan Stlrljic on 10.05.2019 + */ public class OpcuaPlcFieldHandlerTest { @BeforeEach public void before() { diff --git a/plc4j/drivers/opcua/src/test/java/org/apache/plc4x/java/opcua/protocol/OpcuaSubscriptionHandleTest.java b/plc4j/drivers/opcua/src/test/java/org/apache/plc4x/java/opcua/protocol/OpcuaSubscriptionHandleTest.java index bc75d14ef76..153d4ca85dc 100644 --- a/plc4j/drivers/opcua/src/test/java/org/apache/plc4x/java/opcua/protocol/OpcuaSubscriptionHandleTest.java +++ b/plc4j/drivers/opcua/src/test/java/org/apache/plc4x/java/opcua/protocol/OpcuaSubscriptionHandleTest.java @@ -15,14 +15,15 @@ Licensed to the Apache Software Foundation (ASF) under one 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.protocol; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeEach; - +/** + * @author Matthias Milan Stlrljic + * Created by Matthias Milan Stlrljic on 10.05.2019 + */ public class OpcuaSubscriptionHandleTest { @BeforeEach public void before() {