Search in sources :

Example 1 with UByte

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

the class AddressSpace method newViewNode.

private UaViewNode newViewNode(NodeId nodeId, List<DataValue> attributeValues) throws UaException {
    DataValue nodeIdDataValue = attributeValues.get(0);
    StatusCode nodeIdStatusCode = nodeIdDataValue.getStatusCode();
    if (nodeIdStatusCode != null && nodeIdStatusCode.isBad()) {
        throw new UaException(nodeIdStatusCode);
    }
    try {
        NodeClass nodeClass = NodeClass.from((Integer) attributeValues.get(1).getValue().getValue());
        Preconditions.checkArgument(nodeClass == NodeClass.View, "expected NodeClass.View, got NodeClass." + nodeClass);
        QualifiedName browseName = (QualifiedName) attributeValues.get(2).getValue().getValue();
        LocalizedText displayName = (LocalizedText) attributeValues.get(3).getValue().getValue();
        LocalizedText description = getAttributeOrNull(attributeValues.get(4), LocalizedText.class);
        UInteger writeMask = getAttributeOrNull(attributeValues.get(5), UInteger.class);
        UInteger userWriteMask = getAttributeOrNull(attributeValues.get(6), UInteger.class);
        Boolean containsNoLoops = (Boolean) attributeValues.get(7).getValue().getValue();
        UByte eventNotifier = (UByte) attributeValues.get(8).getValue().getValue();
        return new UaViewNode(client, nodeId, nodeClass, browseName, displayName, description, writeMask, userWriteMask, containsNoLoops, eventNotifier);
    } catch (Throwable t) {
        throw UaException.extract(t).orElse(new UaException(StatusCodes.Bad_UnexpectedError, t));
    }
}
Also used : UByte(org.eclipse.milo.opcua.stack.core.types.builtin.unsigned.UByte) NodeClass(org.eclipse.milo.opcua.stack.core.types.enumerated.NodeClass) DataValue(org.eclipse.milo.opcua.stack.core.types.builtin.DataValue) UaException(org.eclipse.milo.opcua.stack.core.UaException) QualifiedName(org.eclipse.milo.opcua.stack.core.types.builtin.QualifiedName) UInteger(org.eclipse.milo.opcua.stack.core.types.builtin.unsigned.UInteger) UaViewNode(org.eclipse.milo.opcua.sdk.client.nodes.UaViewNode) StatusCode(org.eclipse.milo.opcua.stack.core.types.builtin.StatusCode) LocalizedText(org.eclipse.milo.opcua.stack.core.types.builtin.LocalizedText)

Example 2 with UByte

use of org.eclipse.milo.opcua.stack.core.types.builtin.unsigned.UByte 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 3 with UByte

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

the class SubscriptionManager method createMonitoredItem.

private BaseMonitoredItem<?> createMonitoredItem(MonitoredItemCreateRequest request, Subscription subscription, TimestampsToReturn timestamps, Map<NodeId, AttributeGroup> attributeGroups) throws UaException {
    NodeId nodeId = request.getItemToMonitor().getNodeId();
    UInteger attributeId = request.getItemToMonitor().getAttributeId();
    QualifiedName dataEncoding = request.getItemToMonitor().getDataEncoding();
    if (!AttributeId.isValid(attributeId)) {
        throw new UaException(StatusCodes.Bad_AttributeIdInvalid);
    }
    if (dataEncoding.isNotNull()) {
        if (!AttributeId.Value.isEqual(attributeId)) {
            throw new UaException(StatusCodes.Bad_DataEncodingInvalid);
        }
        if (!dataEncoding.equals(DEFAULT_BINARY_ENCODING) && !dataEncoding.equals(DEFAULT_XML_ENCODING)) {
            throw new UaException(StatusCodes.Bad_DataEncodingUnsupported);
        }
    }
    AttributeGroup attributeGroup = attributeGroups.get(nodeId);
    if (attributeId.equals(AttributeId.EventNotifier.uid())) {
        UByte eventNotifier = attributeGroup.getEventNotifier();
        // Verify that the SubscribeToEvents bit is set
        if (eventNotifier == null || (eventNotifier.intValue() & 1) == 0) {
            throw new UaException(StatusCodes.Bad_AttributeIdInvalid);
        }
        Object filterObject = request.getRequestedParameters().getFilter().decode(server.getSerializationContext());
        MonitoringFilter filter = validateEventItemFilter(filterObject, attributeGroup);
        UInteger requestedQueueSize = request.getRequestedParameters().getQueueSize();
        AtomicReference<UInteger> revisedQueueSize = new AtomicReference<>(requestedQueueSize);
        try {
            server.getAddressSpaceManager().onCreateEventItem(request.getItemToMonitor(), requestedQueueSize, revisedQueueSize::set);
        } catch (Throwable t) {
            throw new UaException(StatusCodes.Bad_InternalError, t);
        }
        MonitoredEventItem monitoredEventItem = new MonitoredEventItem(server, session, uint(subscription.nextItemId()), subscription.getId(), request.getItemToMonitor(), request.getMonitoringMode(), timestamps, request.getRequestedParameters().getClientHandle(), 0.0, revisedQueueSize.get(), request.getRequestedParameters().getDiscardOldest());
        monitoredEventItem.installFilter(filter);
        return monitoredEventItem;
    } else {
        if (attributeId.equals(AttributeId.Value.uid())) {
            UByte accessLevel = attributeGroup.getAccessLevel();
            if (accessLevel == null)
                accessLevel = ubyte(0);
            UByte userAccessLevel = attributeGroup.getUserAccessLevel();
            if (userAccessLevel == null)
                userAccessLevel = ubyte(0);
            EnumSet<AccessLevel> accessLevels = AccessLevel.fromValue(accessLevel);
            EnumSet<AccessLevel> userAccessLevels = AccessLevel.fromValue(userAccessLevel);
            if (!accessLevels.contains(AccessLevel.CurrentRead)) {
                throw new UaException(StatusCodes.Bad_NotReadable);
            }
            if (!userAccessLevels.contains(AccessLevel.CurrentRead)) {
                throw new UaException(StatusCodes.Bad_UserAccessDenied);
            }
        }
        // Validate the requested index range by parsing it.
        String indexRange = request.getItemToMonitor().getIndexRange();
        if (indexRange != null)
            NumericRange.parse(indexRange);
        Double minimumSamplingInterval = -1.0;
        try {
            minimumSamplingInterval = attributeGroup.getMinimumSamplingInterval();
            if (minimumSamplingInterval == null) {
                minimumSamplingInterval = server.getConfig().getLimits().getMinSupportedSampleRate();
            }
        } catch (UaException e) {
            if (e.getStatusCode().getValue() != StatusCodes.Bad_AttributeIdInvalid) {
                throw e;
            }
        }
        MonitoringFilter filter = MonitoredDataItem.DEFAULT_FILTER;
        try {
            ExtensionObject filterXo = request.getRequestedParameters().getFilter();
            if (filterXo != null && !filterXo.isNull()) {
                Object filterObject = filterXo.decode(server.getSerializationContext());
                filter = validateDataItemFilter(filterObject, attributeId, attributeGroup);
            }
        } catch (UaSerializationException e) {
            logger.debug("error decoding MonitoringFilter", e);
            throw new UaException(StatusCodes.Bad_MonitoredItemFilterInvalid, e);
        }
        double requestedSamplingInterval = getSamplingInterval(subscription, minimumSamplingInterval, request.getRequestedParameters().getSamplingInterval());
        UInteger requestedQueueSize = request.getRequestedParameters().getQueueSize();
        AtomicReference<Double> revisedSamplingInterval = new AtomicReference<>(requestedSamplingInterval);
        AtomicReference<UInteger> revisedQueueSize = new AtomicReference<>(requestedQueueSize);
        try {
            server.getAddressSpaceManager().onCreateDataItem(request.getItemToMonitor(), requestedSamplingInterval, requestedQueueSize, (rsi, rqs) -> {
                revisedSamplingInterval.set(rsi);
                revisedQueueSize.set(rqs);
            });
        } catch (Throwable t) {
            throw new UaException(StatusCodes.Bad_InternalError, t);
        }
        MonitoredDataItem monitoredDataItem = new MonitoredDataItem(server, session, uint(subscription.nextItemId()), subscription.getId(), request.getItemToMonitor(), request.getMonitoringMode(), timestamps, request.getRequestedParameters().getClientHandle(), revisedSamplingInterval.get(), revisedQueueSize.get(), request.getRequestedParameters().getDiscardOldest());
        monitoredDataItem.installFilter(filter);
        return monitoredDataItem;
    }
}
Also used : UaSerializationException(org.eclipse.milo.opcua.stack.core.UaSerializationException) MonitoredEventItem(org.eclipse.milo.opcua.sdk.server.items.MonitoredEventItem) UaException(org.eclipse.milo.opcua.stack.core.UaException) ExtensionObject(org.eclipse.milo.opcua.stack.core.types.builtin.ExtensionObject) QualifiedName(org.eclipse.milo.opcua.stack.core.types.builtin.QualifiedName) AtomicReference(java.util.concurrent.atomic.AtomicReference) MonitoringFilter(org.eclipse.milo.opcua.stack.core.types.structured.MonitoringFilter) AccessLevel(org.eclipse.milo.opcua.sdk.core.AccessLevel) UByte(org.eclipse.milo.opcua.stack.core.types.builtin.unsigned.UByte) MonitoredDataItem(org.eclipse.milo.opcua.sdk.server.items.MonitoredDataItem) NodeId(org.eclipse.milo.opcua.stack.core.types.builtin.NodeId) UInteger(org.eclipse.milo.opcua.stack.core.types.builtin.unsigned.UInteger) ExtensionObject(org.eclipse.milo.opcua.stack.core.types.builtin.ExtensionObject)

Example 4 with UByte

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

the class AttributeWriter method validateDataType.

private static DataValue validateDataType(OpcUaServer server, NodeId dataType, DataValue value) throws UaException {
    Variant variant = value.getValue();
    if (variant == null)
        return value;
    Object o = variant.getValue();
    if (o == null)
        throw new UaException(StatusCodes.Bad_TypeMismatch);
    Class<?> valueClass = o.getClass().isArray() ? ArrayUtil.getType(o) : o.getClass();
    Class<?> expectedClass = getExpectedClass(server, dataType, valueClass);
    if (expectedClass != null) {
        LOGGER.debug("dataTypeId={}, valueClass={}, expectedClass={}", dataType, valueClass.getSimpleName(), expectedClass.getSimpleName());
        if (!expectedClass.isAssignableFrom(valueClass)) {
            // Writing a ByteString to a UByte[] is explicitly allowed by the spec.
            if (o instanceof ByteString && expectedClass == UByte.class) {
                ByteString byteString = (ByteString) o;
                return new DataValue(new Variant(byteString.uBytes()), value.getStatusCode(), value.getSourceTime(), value.getServerTime());
            } else if (expectedClass == Variant.class) {
                // Allow writing anything to a Variant
                return value;
            } else {
                throw new UaException(StatusCodes.Bad_TypeMismatch);
            }
        }
    } else {
        throw new UaException(StatusCodes.Bad_TypeMismatch);
    }
    return value;
}
Also used : Variant(org.eclipse.milo.opcua.stack.core.types.builtin.Variant) UByte(org.eclipse.milo.opcua.stack.core.types.builtin.unsigned.UByte) DataValue(org.eclipse.milo.opcua.stack.core.types.builtin.DataValue) UaException(org.eclipse.milo.opcua.stack.core.UaException) ByteString(org.eclipse.milo.opcua.stack.core.types.builtin.ByteString) ExtensionObject(org.eclipse.milo.opcua.stack.core.types.builtin.ExtensionObject)

Example 5 with UByte

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

the class ByteConversionsTest method testConversions.

@Test
public void testConversions() {
    for (Object[] conversion : CONVERSIONS) {
        UByte b = (UByte) conversion[0];
        Object expected = conversion[1];
        BuiltinDataType targetType = BuiltinDataType.fromBackingClass(expected.getClass());
        assertNotNull(targetType);
        assertEquals(explicitConversion(b, targetType), expected);
    }
}
Also used : UByte(org.eclipse.milo.opcua.stack.core.types.builtin.unsigned.UByte) BuiltinDataType(org.eclipse.milo.opcua.stack.core.BuiltinDataType) Test(org.testng.annotations.Test)

Aggregations

UByte (org.eclipse.milo.opcua.stack.core.types.builtin.unsigned.UByte)16 UaException (org.eclipse.milo.opcua.stack.core.UaException)9 DataValue (org.eclipse.milo.opcua.stack.core.types.builtin.DataValue)9 StatusCode (org.eclipse.milo.opcua.stack.core.types.builtin.StatusCode)8 UInteger (org.eclipse.milo.opcua.stack.core.types.builtin.unsigned.UInteger)7 QualifiedName (org.eclipse.milo.opcua.stack.core.types.builtin.QualifiedName)5 LocalizedText (org.eclipse.milo.opcua.stack.core.types.builtin.LocalizedText)4 NodeId (org.eclipse.milo.opcua.stack.core.types.builtin.NodeId)4 ExtensionObject (org.eclipse.milo.opcua.stack.core.types.builtin.ExtensionObject)3 Variant (org.eclipse.milo.opcua.stack.core.types.builtin.Variant)3 OpcUaServer (org.eclipse.milo.opcua.sdk.server.OpcUaServer)2 ByteString (org.eclipse.milo.opcua.stack.core.types.builtin.ByteString)2 ExpandedNodeId (org.eclipse.milo.opcua.stack.core.types.builtin.ExpandedNodeId)2 NodeClass (org.eclipse.milo.opcua.stack.core.types.enumerated.NodeClass)2 JsonArray (com.google.gson.JsonArray)1 JsonElement (com.google.gson.JsonElement)1 JsonPrimitive (com.google.gson.JsonPrimitive)1 NoSuchElementException (java.util.NoSuchElementException)1 UUID (java.util.UUID)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1