Search in sources :

Example 1 with ULong

use of org.eclipse.milo.opcua.stack.core.types.builtin.unsigned.ULong in project milo by eclipse.

the class JsonStructureCodec method opcUaToMemberTypeScalar.

@Override
protected JsonElement opcUaToMemberTypeScalar(String name, Object value, String typeName) {
    if (value == null) {
        return JsonNull.INSTANCE;
    } else if (value instanceof Number) {
        if (value instanceof UByte) {
            return new JsonPrimitive(((UByte) value).shortValue());
        } else if (value instanceof UShort) {
            return new JsonPrimitive(((UShort) value).intValue());
        } else if (value instanceof UInteger) {
            return new JsonPrimitive(((UInteger) value).longValue());
        } else if (value instanceof ULong) {
            return new JsonPrimitive(((ULong) value).toBigInteger());
        } else {
            return new JsonPrimitive((Number) value);
        }
    } else if (value instanceof Boolean) {
        return new JsonPrimitive((Boolean) value);
    } else if (value instanceof String) {
        return new JsonPrimitive((String) value);
    } else if (value instanceof Character) {
        return new JsonPrimitive((Character) value);
    } else if (value instanceof JsonElement) {
        return (JsonElement) value;
    } else if (value instanceof DateTime) {
        return new JsonPrimitive(((DateTime) value).getUtcTime());
    } else if (value instanceof UUID) {
        return new JsonPrimitive(value.toString());
    } else if (value instanceof LocalizedText) {
        return gson.toJsonTree(value);
    } else if (value instanceof QualifiedName) {
        return gson.toJsonTree(value);
    } else if (value instanceof ByteString) {
        ByteString byteString = (ByteString) value;
        byte[] bs = byteString.bytesOrEmpty();
        JsonArray array = new JsonArray();
        for (Byte b : bs) {
            array.add(new JsonPrimitive(b));
        }
        return array;
    } else if (value instanceof XmlElement) {
        String fragment = ((XmlElement) value).getFragment();
        return fragment != null ? new JsonPrimitive(fragment) : JsonNull.INSTANCE;
    } else if (value instanceof NodeId) {
        String nodeId = ((NodeId) value).toParseableString();
        return new JsonPrimitive(nodeId);
    } else if (value instanceof ExpandedNodeId) {
        String xNodeId = ((ExpandedNodeId) value).toParseableString();
        return new JsonPrimitive(xNodeId);
    } else if (value instanceof StatusCode) {
        long code = ((StatusCode) value).getValue();
        return new JsonPrimitive(code);
    } else {
        throw new RuntimeException("could not create JsonElement for value: " + value);
    }
}
Also used : ULong(org.eclipse.milo.opcua.stack.core.types.builtin.unsigned.ULong) JsonPrimitive(com.google.gson.JsonPrimitive) ByteString(org.eclipse.milo.opcua.stack.core.types.builtin.ByteString) QualifiedName(org.eclipse.milo.opcua.stack.core.types.builtin.QualifiedName) UShort(org.eclipse.milo.opcua.stack.core.types.builtin.unsigned.UShort) ByteString(org.eclipse.milo.opcua.stack.core.types.builtin.ByteString) StatusCode(org.eclipse.milo.opcua.stack.core.types.builtin.StatusCode) DateTime(org.eclipse.milo.opcua.stack.core.types.builtin.DateTime) LocalizedText(org.eclipse.milo.opcua.stack.core.types.builtin.LocalizedText) Unsigned.ulong(org.eclipse.milo.opcua.stack.core.types.builtin.unsigned.Unsigned.ulong) UByte(org.eclipse.milo.opcua.stack.core.types.builtin.unsigned.UByte) JsonArray(com.google.gson.JsonArray) ExpandedNodeId(org.eclipse.milo.opcua.stack.core.types.builtin.ExpandedNodeId) JsonElement(com.google.gson.JsonElement) UByte(org.eclipse.milo.opcua.stack.core.types.builtin.unsigned.UByte) UInteger(org.eclipse.milo.opcua.stack.core.types.builtin.unsigned.UInteger) NodeId(org.eclipse.milo.opcua.stack.core.types.builtin.NodeId) ExpandedNodeId(org.eclipse.milo.opcua.stack.core.types.builtin.ExpandedNodeId) XmlElement(org.eclipse.milo.opcua.stack.core.types.builtin.XmlElement) UUID(java.util.UUID)

Example 2 with ULong

use of org.eclipse.milo.opcua.stack.core.types.builtin.unsigned.ULong in project milo by eclipse.

the class SetPositionMethod method invoke.

@Override
protected Variant[] invoke(AbstractMethodInvocationHandler.InvocationContext context, Variant[] inputValues) throws UaException {
    UInteger fileHandle = (UInteger) inputValues[0].getValue();
    ULong position = (ULong) inputValues[1].getValue();
    invoke(context, fileHandle, position);
    return new Variant[] {};
}
Also used : Variant(org.eclipse.milo.opcua.stack.core.types.builtin.Variant) ULong(org.eclipse.milo.opcua.stack.core.types.builtin.unsigned.ULong) UInteger(org.eclipse.milo.opcua.stack.core.types.builtin.unsigned.UInteger)

Example 3 with ULong

use of org.eclipse.milo.opcua.stack.core.types.builtin.unsigned.ULong in project milo by eclipse.

the class OpcUaBinaryStreamDecoder method readUInt64Array.

@Override
public ULong[] readUInt64Array(String field) throws UaSerializationException {
    int length = readInt32();
    if (length == -1) {
        return null;
    } else {
        checkArrayLength(length);
        ULong[] values = new ULong[length];
        for (int i = 0; i < length; i++) {
            values[i] = readUInt64(field);
        }
        return values;
    }
}
Also used : ULong(org.eclipse.milo.opcua.stack.core.types.builtin.unsigned.ULong) Unsigned.uint(org.eclipse.milo.opcua.stack.core.types.builtin.unsigned.Unsigned.uint)

Example 4 with ULong

use of org.eclipse.milo.opcua.stack.core.types.builtin.unsigned.ULong in project milo by eclipse.

the class GetPositionMethod method invoke.

@Override
protected Variant[] invoke(AbstractMethodInvocationHandler.InvocationContext context, Variant[] inputValues) throws UaException {
    UInteger fileHandle = (UInteger) inputValues[0].getValue();
    Out<ULong> position = new Out<ULong>();
    invoke(context, fileHandle, position);
    return new Variant[] { new Variant(position.get()) };
}
Also used : Variant(org.eclipse.milo.opcua.stack.core.types.builtin.Variant) ULong(org.eclipse.milo.opcua.stack.core.types.builtin.unsigned.ULong) UInteger(org.eclipse.milo.opcua.stack.core.types.builtin.unsigned.UInteger) Out(org.eclipse.milo.opcua.sdk.server.api.methods.Out)

Example 5 with ULong

use of org.eclipse.milo.opcua.stack.core.types.builtin.unsigned.ULong in project plc4x by apache.

the class Plc4xCommunication method getValue.

public DataValue getValue(AttributeFilterContext.GetAttributeContext ctx, String tag, String connectionString) {
    PlcConnection connection = null;
    try {
        // Check if we just polled the connection and it failed. Wait for the backoff counter to expire before we try again.
        if (failedConnectionList.containsKey(connectionString)) {
            if (System.currentTimeMillis() > failedConnectionList.get(connectionString) + DEFAULT_RETRY_BACKOFF) {
                failedConnectionList.remove(connectionString);
            } else {
                logger.debug("Waiting for back off timer - " + ((failedConnectionList.get(connectionString) + DEFAULT_RETRY_BACKOFF) - System.currentTimeMillis()) + " ms left");
                return BAD_RESPONSE;
            }
        }
        // Try to connect to PLC
        try {
            connection = driverManager.getConnection(connectionString);
            logger.debug(connectionString + " Connected");
        } catch (PlcConnectionException e) {
            logger.error("Failed to connect to device, error raised - " + e);
            failedConnectionList.put(connectionString, System.currentTimeMillis());
            return BAD_RESPONSE;
        }
        if (!connection.getMetadata().canRead()) {
            logger.error("This connection doesn't support reading.");
            try {
                connection.close();
            } catch (Exception exception) {
                logger.warn("Closing connection failed with error - " + exception);
            }
            return BAD_RESPONSE;
        }
        long timeout = DEFAULT_TIMEOUT;
        if (monitoredList.containsKey(ctx.getNode().getNodeId())) {
            timeout = (long) monitoredList.get(ctx.getNode().getNodeId()).getSamplingInterval() * 1000;
        }
        // Create a new read request:
        // - Give the single item requested an alias name
        PlcReadRequest.Builder builder = connection.readRequestBuilder();
        builder.addItem("value-1", tag);
        PlcReadRequest readRequest = builder.build();
        PlcReadResponse response = null;
        try {
            response = readRequest.execute().get(timeout, TimeUnit.MICROSECONDS);
        } catch (InterruptedException | ExecutionException | TimeoutException e) {
            logger.warn(e + " Occurred while reading value, using timeout of " + timeout / 1000 + "ms");
            try {
                connection.close();
            } catch (Exception exception) {
                logger.warn("Closing connection failed with error - " + exception);
            }
            return BAD_RESPONSE;
        }
        DataValue resp = BAD_RESPONSE;
        for (String fieldName : response.getFieldNames()) {
            if (response.getResponseCode(fieldName) == PlcResponseCode.OK) {
                int numValues = response.getNumberOfValues(fieldName);
                if (numValues == 1) {
                    if (response.getObject(fieldName) instanceof BigInteger) {
                        resp = new DataValue(new Variant(ulong((BigInteger) response.getObject(fieldName))), StatusCode.GOOD);
                    } else {
                        resp = new DataValue(new Variant(response.getObject(fieldName)), StatusCode.GOOD);
                    }
                } else {
                    Object array = null;
                    if (response.getObject(fieldName, 0) instanceof BigInteger) {
                        array = Array.newInstance(ULong.class, numValues);
                    } else {
                        array = Array.newInstance(response.getObject(fieldName, 0).getClass(), numValues);
                    }
                    for (int i = 0; i < numValues; i++) {
                        if (response.getObject(fieldName, i) instanceof BigInteger) {
                            Array.set(array, i, ulong((BigInteger) response.getObject(fieldName, i)));
                        } else {
                            Array.set(array, i, response.getObject(fieldName, i));
                        }
                    }
                    resp = new DataValue(new Variant(array), StatusCode.GOOD);
                }
            }
        }
        try {
            connection.close();
        } catch (Exception e) {
            failedConnectionList.put(connectionString, System.currentTimeMillis());
            logger.warn("Closing connection failed with error " + e);
        }
        return resp;
    } catch (Exception e) {
        logger.warn("General error reading value " + e.getStackTrace()[0].toString());
        if (connection != null) {
            try {
                connection.close();
            } catch (Exception ex) {
            // Do Nothing
            }
        }
        return BAD_RESPONSE;
    }
}
Also used : ULong(org.eclipse.milo.opcua.stack.core.types.builtin.unsigned.ULong) DataValue(org.eclipse.milo.opcua.stack.core.types.builtin.DataValue) PlcConnection(org.apache.plc4x.java.api.PlcConnection) TimeoutException(java.util.concurrent.TimeoutException) PlcConnectionException(org.apache.plc4x.java.api.exceptions.PlcConnectionException) ExecutionException(java.util.concurrent.ExecutionException) Unsigned.ulong(org.eclipse.milo.opcua.stack.core.types.builtin.unsigned.Unsigned.ulong) Variant(org.eclipse.milo.opcua.stack.core.types.builtin.Variant) PlcReadResponse(org.apache.plc4x.java.api.messages.PlcReadResponse) BigInteger(java.math.BigInteger) PlcConnectionException(org.apache.plc4x.java.api.exceptions.PlcConnectionException) PlcReadRequest(org.apache.plc4x.java.api.messages.PlcReadRequest) ExecutionException(java.util.concurrent.ExecutionException) TimeoutException(java.util.concurrent.TimeoutException)

Aggregations

ULong (org.eclipse.milo.opcua.stack.core.types.builtin.unsigned.ULong)5 Variant (org.eclipse.milo.opcua.stack.core.types.builtin.Variant)3 UInteger (org.eclipse.milo.opcua.stack.core.types.builtin.unsigned.UInteger)3 Unsigned.ulong (org.eclipse.milo.opcua.stack.core.types.builtin.unsigned.Unsigned.ulong)2 JsonArray (com.google.gson.JsonArray)1 JsonElement (com.google.gson.JsonElement)1 JsonPrimitive (com.google.gson.JsonPrimitive)1 BigInteger (java.math.BigInteger)1 UUID (java.util.UUID)1 ExecutionException (java.util.concurrent.ExecutionException)1 TimeoutException (java.util.concurrent.TimeoutException)1 PlcConnection (org.apache.plc4x.java.api.PlcConnection)1 PlcConnectionException (org.apache.plc4x.java.api.exceptions.PlcConnectionException)1 PlcReadRequest (org.apache.plc4x.java.api.messages.PlcReadRequest)1 PlcReadResponse (org.apache.plc4x.java.api.messages.PlcReadResponse)1 Out (org.eclipse.milo.opcua.sdk.server.api.methods.Out)1 ByteString (org.eclipse.milo.opcua.stack.core.types.builtin.ByteString)1 DataValue (org.eclipse.milo.opcua.stack.core.types.builtin.DataValue)1 DateTime (org.eclipse.milo.opcua.stack.core.types.builtin.DateTime)1 ExpandedNodeId (org.eclipse.milo.opcua.stack.core.types.builtin.ExpandedNodeId)1