Skip to content

Commit

Permalink
[Fixed] JavaDoc formatting
Browse files Browse the repository at this point in the history
[Added] Warning messages at exception points with the consideration of adding later some run-time exceptions
  • Loading branch information
nalim2 authored and JulianFeinauer committed May 19, 2019
1 parent 2b98afa commit 21da7e6
Show file tree
Hide file tree
Showing 16 changed files with 250 additions and 202 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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;
Expand All @@ -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);
Expand All @@ -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())
Expand All @@ -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());
}
}

Expand Down Expand Up @@ -232,18 +282,15 @@ public CompletableFuture<PlcSubscriptionResponse> 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;
Expand All @@ -256,10 +303,8 @@ public CompletableFuture<PlcUnsubscriptionResponse> 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());
}
});

Expand Down Expand Up @@ -295,17 +340,13 @@ public CompletableFuture<PlcReadResponse> read(PlcReadRequest readRequest) {
List<DataValue> 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));
Expand All @@ -317,81 +358,13 @@ public CompletableFuture<PlcReadResponse> 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<PlcWriteResponse> write(PlcWriteRequest writeRequest) {
Expand Down Expand Up @@ -451,6 +424,28 @@ public CompletableFuture<PlcWriteResponse> 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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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=(?<namespace>\\d+);(?<identifierType>[isgb])=((?<identifier>\\w+))?");
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand All @@ -110,6 +111,7 @@ public int hashCode() {
public String toString() {
return "OpcuaField{" +
"namespace=" + namespace +
"identifierType=" + identifierType.getText() +
"identifier=" + identifier +
'}';
}
Expand Down
Loading

0 comments on commit 21da7e6

Please sign in to comment.