Search in sources :

Example 1 with UShort

use of org.eclipse.milo.opcua.stack.core.types.builtin.unsigned.UShort in project FAAAST-Service by FraunhoferIOSB.

the class OpcUaAssetConnection method parseNodeId.

private NodeId parseNodeId(String nodeId) {
    Optional<String> ns = Stream.of(nodeId.split(NODE_ID_SEPARATOR)).filter(x -> x.startsWith(NS_PREFIX)).findFirst();
    int namespaceIndex = 0;
    if (ns.isPresent()) {
        String namespace = ns.get().replace(NS_PREFIX, "");
        try {
            namespaceIndex = Integer.parseUnsignedInt(namespace);
        } catch (NumberFormatException ex) {
            UShort actualNamespaceIndex = client.getNamespaceTable().getIndex(namespace);
            if (actualNamespaceIndex == null) {
                throw new RuntimeException(String.format("could not resolve namespace '%s'", namespace));
            }
            namespaceIndex = actualNamespaceIndex.intValue();
        }
    } else {
        System.out.println("no namespace provided for node. Using default (ns=0)");
    }
    return NodeId.parse(nodeId.replace(ns.get(), NS_PREFIX + namespaceIndex));
}
Also used : SubmodelElement(io.adminshell.aas.v3.model.SubmodelElement) ValueConversionException(de.fraunhofer.iosb.ilt.faaast.service.assetconnection.opcua.conversion.ValueConversionException) DataElementValue(de.fraunhofer.iosb.ilt.faaast.service.model.value.DataElementValue) VariableNode(org.eclipse.milo.opcua.sdk.core.nodes.VariableNode) LoggerFactory(org.slf4j.LoggerFactory) CallMethodResult(org.eclipse.milo.opcua.stack.core.types.structured.CallMethodResult) AddressSpace(org.eclipse.milo.opcua.sdk.client.AddressSpace) DefaultProperty(io.adminshell.aas.v3.model.impl.DefaultProperty) NewDataListener(de.fraunhofer.iosb.ilt.faaast.service.assetconnection.NewDataListener) Argument(org.eclipse.milo.opcua.stack.core.types.structured.Argument) ManagedDataItem(org.eclipse.milo.opcua.sdk.client.subscriptions.ManagedDataItem) Unsigned.uint(org.eclipse.milo.opcua.stack.core.types.builtin.unsigned.Unsigned.uint) Property(io.adminshell.aas.v3.model.Property) Map(java.util.Map) AssetConnection(de.fraunhofer.iosb.ilt.faaast.service.assetconnection.AssetConnection) UaMethodNode(org.eclipse.milo.opcua.sdk.client.nodes.UaMethodNode) ManagedSubscription(org.eclipse.milo.opcua.sdk.client.subscriptions.ManagedSubscription) AnonymousProvider(org.eclipse.milo.opcua.sdk.client.api.identity.AnonymousProvider) TimestampsToReturn(org.eclipse.milo.opcua.stack.core.types.enumerated.TimestampsToReturn) NodeId(org.eclipse.milo.opcua.stack.core.types.builtin.NodeId) BrowseDirection(org.eclipse.milo.opcua.stack.core.types.enumerated.BrowseDirection) Reference(io.adminshell.aas.v3.model.Reference) Collectors(java.util.stream.Collectors) AssetConnectionException(de.fraunhofer.iosb.ilt.faaast.service.assetconnection.AssetConnectionException) AasUtils(io.adminshell.aas.v3.dataformat.core.util.AasUtils) TypedValue(de.fraunhofer.iosb.ilt.faaast.service.model.value.primitive.TypedValue) Objects(java.util.Objects) List(java.util.List) Stream(java.util.stream.Stream) Variant(org.eclipse.milo.opcua.stack.core.types.builtin.Variant) CoreConfig(de.fraunhofer.iosb.ilt.faaast.service.config.CoreConfig) ElementValue(de.fraunhofer.iosb.ilt.faaast.service.model.value.ElementValue) UaNode(org.eclipse.milo.opcua.sdk.client.nodes.UaNode) StatusCode(org.eclipse.milo.opcua.stack.core.types.builtin.StatusCode) TypeInfo(de.fraunhofer.iosb.ilt.faaast.service.typing.TypeInfo) Optional(java.util.Optional) ElementValueTypeInfo(de.fraunhofer.iosb.ilt.faaast.service.typing.ElementValueTypeInfo) CallMethodRequest(org.eclipse.milo.opcua.stack.core.types.structured.CallMethodRequest) DefaultOperationVariable(io.adminshell.aas.v3.model.impl.DefaultOperationVariable) DataValue(org.eclipse.milo.opcua.stack.core.types.builtin.DataValue) OpcUaClient(org.eclipse.milo.opcua.sdk.client.OpcUaClient) OperationVariable(io.adminshell.aas.v3.model.OperationVariable) ServiceContext(de.fraunhofer.iosb.ilt.faaast.service.ServiceContext) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) UShort(org.eclipse.milo.opcua.stack.core.types.builtin.unsigned.UShort) SecurityPolicy(org.eclipse.milo.opcua.stack.core.security.SecurityPolicy) AssetOperationProvider(de.fraunhofer.iosb.ilt.faaast.service.assetconnection.AssetOperationProvider) StatusCodes(org.eclipse.milo.opcua.stack.core.StatusCodes) Logger(org.slf4j.Logger) ElementValueMapper(de.fraunhofer.iosb.ilt.faaast.service.model.value.mapper.ElementValueMapper) Datatype(de.fraunhofer.iosb.ilt.faaast.service.model.value.primitive.Datatype) LocalizedText(org.eclipse.milo.opcua.stack.core.types.builtin.LocalizedText) ValueConverter(de.fraunhofer.iosb.ilt.faaast.service.assetconnection.opcua.conversion.ValueConverter) AssetSubscriptionProvider(de.fraunhofer.iosb.ilt.faaast.service.assetconnection.AssetSubscriptionProvider) ExecutionException(java.util.concurrent.ExecutionException) UaException(org.eclipse.milo.opcua.stack.core.UaException) LambdaExceptionHelper(de.fraunhofer.iosb.ilt.faaast.service.util.LambdaExceptionHelper) AssetValueProvider(de.fraunhofer.iosb.ilt.faaast.service.assetconnection.AssetValueProvider) PropertyValue(de.fraunhofer.iosb.ilt.faaast.service.model.value.PropertyValue) UShort(org.eclipse.milo.opcua.stack.core.types.builtin.unsigned.UShort) Unsigned.uint(org.eclipse.milo.opcua.stack.core.types.builtin.unsigned.Unsigned.uint)

Example 2 with UShort

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

the class ExpandedNodeId method parse.

/**
 * Parse {@code s} into an {@link ExpandedNodeId}.
 *
 * @param s the String to parse.
 * @return an {@link ExpandedNodeId}.
 * @throws UaRuntimeException if parsing fails.
 */
public static ExpandedNodeId parse(String s) {
    try {
        String[] parts = s.split(";");
        NodeId nodeId = NodeId.parse(parts[parts.length - 1]);
        UInteger serverIndex = UInteger.MIN;
        UShort namespaceIndex = UShort.MIN;
        String namespaceUri = null;
        Object identifier = nodeId.getIdentifier();
        for (String part : parts) {
            String[] ss = part.split("=", 2);
            if ("svr".equals(ss[0])) {
                serverIndex = uint(Integer.parseInt(ss[1]));
            } else if ("ns".equals(ss[0])) {
                namespaceIndex = ushort(Integer.parseInt(ss[1]));
            } else if ("nsu".equals(ss[0])) {
                namespaceUri = ss[1];
            }
        }
        return new ExpandedNodeId(namespaceIndex, namespaceUri, identifier, serverIndex);
    } catch (Throwable t) {
        throw new UaRuntimeException(StatusCodes.Bad_NodeIdInvalid, t);
    }
}
Also used : UaRuntimeException(org.eclipse.milo.opcua.stack.core.UaRuntimeException) UInteger(org.eclipse.milo.opcua.stack.core.types.builtin.unsigned.UInteger) UShort(org.eclipse.milo.opcua.stack.core.types.builtin.unsigned.UShort)

Example 3 with UShort

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

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

the class UaNode method setProperty.

public <T> void setProperty(QualifiedProperty<T> property, T value) {
    UShort namespaceIndex = context.getServer().getNamespaceTable().getIndex(property.getNamespaceUri());
    if (namespaceIndex == null) {
        throw new IllegalArgumentException("property belongs to unregistered " + "namespace: " + property.getNamespaceUri());
    }
    VariableNode node = getPropertyNode(property).orElseGet(() -> {
        String browseName = property.getBrowseName();
        NodeId propertyNodeId = new NodeId(getNodeId().getNamespaceIndex(), String.format("%s.%s", getNodeId().getIdentifier().toString(), browseName));
        PropertyTypeNode propertyNode = new PropertyTypeNode(context, propertyNodeId, new QualifiedName(namespaceIndex, browseName), LocalizedText.english(browseName), LocalizedText.NULL_VALUE, uint(0), uint(0));
        NodeId dataType = property.getDataType().toNodeId(context.getNamespaceTable()).orElse(NodeId.NULL_VALUE);
        propertyNode.setDataType(dataType);
        propertyNode.setValueRank(property.getValueRank());
        propertyNode.setArrayDimensions(property.getArrayDimensions());
        propertyNode.addReference(new Reference(propertyNode.getNodeId(), Identifiers.HasTypeDefinition, Identifiers.PropertyType.expanded(), true));
        addProperty(propertyNode);
        context.getNodeManager().addNode(propertyNode);
        return propertyNode;
    });
    node.setValue(new DataValue(new Variant(value)));
}
Also used : Variant(org.eclipse.milo.opcua.stack.core.types.builtin.Variant) VariableNode(org.eclipse.milo.opcua.sdk.core.nodes.VariableNode) DataValue(org.eclipse.milo.opcua.stack.core.types.builtin.DataValue) Reference(org.eclipse.milo.opcua.sdk.core.Reference) QualifiedName(org.eclipse.milo.opcua.stack.core.types.builtin.QualifiedName) NodeId(org.eclipse.milo.opcua.stack.core.types.builtin.NodeId) ExpandedNodeId(org.eclipse.milo.opcua.stack.core.types.builtin.ExpandedNodeId) UShort(org.eclipse.milo.opcua.stack.core.types.builtin.unsigned.UShort) PropertyTypeNode(org.eclipse.milo.opcua.sdk.server.model.nodes.variables.PropertyTypeNode)

Example 5 with UShort

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

the class OpcUaXmlStreamDecoder method readDataValue.

@Override
public DataValue readDataValue(String field) throws UaSerializationException {
    if (currentNode(field)) {
        Node node = currentNode;
        Map<String, Node> children = nodeMap(currentNode.getChildNodes());
        Variant value = Variant.NULL_VALUE;
        StatusCode statusCode = StatusCode.GOOD;
        DateTime sourceTimestamp = null;
        UShort sourcePicoseconds = null;
        DateTime serverTimestamp = null;
        UShort serverPicoseconds = null;
        try {
            Node valueNode = children.get("Value");
            if (valueNode != null) {
                currentNode = valueNode;
                value = readVariant("Value");
            }
            Node statusCodeNode = children.get("StatusCode");
            if (statusCodeNode != null) {
                currentNode = statusCodeNode;
                statusCode = readStatusCode("StatusCode");
            }
            Node sourceTimestampNode = children.get("SourceTimestamp");
            if (sourceTimestampNode != null) {
                currentNode = sourceTimestampNode;
                sourceTimestamp = readDateTime("SourceTimestamp");
            }
            Node sourcePicosecondsNode = children.get("SourcePicoseconds");
            if (sourcePicosecondsNode != null) {
                currentNode = sourcePicosecondsNode;
                sourcePicoseconds = readUInt16("SourcePicoseconds");
            }
            Node serverTimestampNode = children.get("ServerTimestamp");
            if (serverTimestampNode != null) {
                currentNode = serverTimestampNode;
                serverTimestamp = readDateTime("ServerTimestamp");
            }
            Node serverPicosecondsNode = children.get("ServerPicoseconds");
            if (serverPicosecondsNode != null) {
                currentNode = serverPicosecondsNode;
                serverPicoseconds = readUInt16("ServerPicoseconds");
            }
            return new DataValue(value, statusCode, sourceTimestamp, sourcePicoseconds, serverTimestamp, serverPicoseconds);
        } finally {
            currentNode = node.getNextSibling();
        }
    } else {
        return new DataValue(Variant.NULL_VALUE);
    }
}
Also used : Variant(org.eclipse.milo.opcua.stack.core.types.builtin.Variant) DataValue(org.eclipse.milo.opcua.stack.core.types.builtin.DataValue) Node(org.w3c.dom.Node) 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)

Aggregations

UShort (org.eclipse.milo.opcua.stack.core.types.builtin.unsigned.UShort)13 DataValue (org.eclipse.milo.opcua.stack.core.types.builtin.DataValue)6 ByteString (org.eclipse.milo.opcua.stack.core.types.builtin.ByteString)5 QualifiedName (org.eclipse.milo.opcua.stack.core.types.builtin.QualifiedName)5 StatusCode (org.eclipse.milo.opcua.stack.core.types.builtin.StatusCode)5 Variant (org.eclipse.milo.opcua.stack.core.types.builtin.Variant)5 DateTime (org.eclipse.milo.opcua.stack.core.types.builtin.DateTime)4 Unsigned.uint (org.eclipse.milo.opcua.stack.core.types.builtin.unsigned.Unsigned.uint)4 UUID (java.util.UUID)3 NodeId (org.eclipse.milo.opcua.stack.core.types.builtin.NodeId)3 UInteger (org.eclipse.milo.opcua.stack.core.types.builtin.unsigned.UInteger)3 VariableNode (org.eclipse.milo.opcua.sdk.core.nodes.VariableNode)2 ExpandedNodeId (org.eclipse.milo.opcua.stack.core.types.builtin.ExpandedNodeId)2 LocalizedText (org.eclipse.milo.opcua.stack.core.types.builtin.LocalizedText)2 ImmutableList (com.google.common.collect.ImmutableList)1 JsonArray (com.google.gson.JsonArray)1 JsonElement (com.google.gson.JsonElement)1 JsonPrimitive (com.google.gson.JsonPrimitive)1 ServiceContext (de.fraunhofer.iosb.ilt.faaast.service.ServiceContext)1 AssetConnection (de.fraunhofer.iosb.ilt.faaast.service.assetconnection.AssetConnection)1