Search in sources :

Example 1 with DataValue

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

the class Plc4xNamespace method addConfiguredNodes.

private void addConfiguredNodes(UaFolderNode rootNode, DeviceConfiguration c) {
    final List<Tag> tags = c.getTags();
    final String connectionString = c.getConnectionString();
    for (int i = 0; i < tags.size(); i++) {
        logger.info("Adding Tag " + tags.get(i).getAlias() + " - " + tags.get(i).getAddress());
        String name = tags.get(i).getAlias();
        final String tag = tags.get(i).getAddress();
        Class datatype = null;
        NodeId typeId = Identifiers.String;
        UaVariableNode node = null;
        Variant variant = null;
        try {
            datatype = plc4xServer.getField(tag, connectionString).getDefaultJavaType();
            final int length = plc4xServer.getField(tag, connectionString).getNumberOfElements();
            typeId = Plc4xCommunication.getNodeId(plc4xServer.getField(tag, connectionString).getPlcDataType());
            if (length > 1) {
                node = new UaVariableNode.UaVariableNodeBuilder(getNodeContext()).setNodeId(newNodeId(name)).setAccessLevel(AccessLevel.READ_WRITE).setUserAccessLevel(AccessLevel.READ_WRITE).setBrowseName(newQualifiedName(name)).setDisplayName(LocalizedText.english(name)).setDataType(typeId).setTypeDefinition(Identifiers.BaseDataVariableType).setValueRank(ValueRank.OneDimension.getValue()).setArrayDimensions(new UInteger[] { uint(length) }).build();
                Object array = Array.newInstance(datatype, length);
                for (int j = 0; j < length; j++) {
                    Array.set(array, j, false);
                }
                variant = new Variant(array);
            } else {
                node = new UaVariableNode.UaVariableNodeBuilder(getNodeContext()).setNodeId(newNodeId(name)).setAccessLevel(AccessLevel.READ_WRITE).setUserAccessLevel(AccessLevel.READ_WRITE).setBrowseName(newQualifiedName(name)).setDisplayName(LocalizedText.english(name)).setDataType(typeId).setTypeDefinition(Identifiers.BaseDataVariableType).build();
                variant = new Variant(0);
            }
            node.setValue(new DataValue(variant));
            node.getFilterChain().addLast(AttributeFilters.getValue(ctx -> plc4xServer.getValue(ctx, tag, connectionString)));
            node.getFilterChain().addLast(AttributeFilters.setValue((ctx, value) -> {
                if (length > 1) {
                    plc4xServer.setValue(tag, Arrays.toString((Object[]) value.getValue().getValue()), connectionString);
                } else {
                    plc4xServer.setValue(tag, value.getValue().getValue().toString(), connectionString);
                }
            }));
        } catch (PlcConnectionException e) {
            logger.info("Couldn't find data type");
            System.exit(1);
        }
        getNodeManager().addNode(node);
        rootNode.addOrganizes(node);
    }
}
Also used : Arrays(java.util.Arrays) Array(java.lang.reflect.Array) DataValue(org.eclipse.milo.opcua.stack.core.types.builtin.DataValue) DataItem(org.eclipse.milo.opcua.sdk.server.api.DataItem) LoggerFactory(org.slf4j.LoggerFactory) ManagedNamespaceWithLifecycle(org.eclipse.milo.opcua.sdk.server.api.ManagedNamespaceWithLifecycle) UaFolderNode(org.eclipse.milo.opcua.sdk.server.nodes.UaFolderNode) ValueRank(org.eclipse.milo.opcua.sdk.core.ValueRank) AccessLevel(org.eclipse.milo.opcua.sdk.core.AccessLevel) Unsigned.uint(org.eclipse.milo.opcua.stack.core.types.builtin.unsigned.Unsigned.uint) Tag(org.apache.plc4x.java.opcuaserver.configuration.Tag) PlcConnectionException(org.apache.plc4x.java.api.exceptions.PlcConnectionException) AttributeFilters(org.eclipse.milo.opcua.sdk.server.nodes.filters.AttributeFilters) PooledPlcDriverManager(org.apache.plc4x.java.utils.connectionpool.PooledPlcDriverManager) Reference(org.eclipse.milo.opcua.sdk.core.Reference) NodeId(org.eclipse.milo.opcua.stack.core.types.builtin.NodeId) Logger(org.slf4j.Logger) UaVariableNode(org.eclipse.milo.opcua.sdk.server.nodes.UaVariableNode) UInteger(org.eclipse.milo.opcua.stack.core.types.builtin.unsigned.UInteger) LocalizedText(org.eclipse.milo.opcua.stack.core.types.builtin.LocalizedText) SubscriptionModel(org.eclipse.milo.opcua.sdk.server.util.SubscriptionModel) OpcUaServer(org.eclipse.milo.opcua.sdk.server.OpcUaServer) MonitoredItem(org.eclipse.milo.opcua.sdk.server.api.MonitoredItem) List(java.util.List) Configuration(org.apache.plc4x.java.opcuaserver.configuration.Configuration) Variant(org.eclipse.milo.opcua.stack.core.types.builtin.Variant) DeviceConfiguration(org.apache.plc4x.java.opcuaserver.configuration.DeviceConfiguration) DataTypeDictionaryManager(org.eclipse.milo.opcua.sdk.server.dtd.DataTypeDictionaryManager) Identifiers(org.eclipse.milo.opcua.stack.core.Identifiers) DataValue(org.eclipse.milo.opcua.stack.core.types.builtin.DataValue) Unsigned.uint(org.eclipse.milo.opcua.stack.core.types.builtin.unsigned.Unsigned.uint) Variant(org.eclipse.milo.opcua.stack.core.types.builtin.Variant) UaVariableNode(org.eclipse.milo.opcua.sdk.server.nodes.UaVariableNode) NodeId(org.eclipse.milo.opcua.stack.core.types.builtin.NodeId) Tag(org.apache.plc4x.java.opcuaserver.configuration.Tag) PlcConnectionException(org.apache.plc4x.java.api.exceptions.PlcConnectionException)

Example 2 with DataValue

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

the class ExampleNamespace method addDataAccessNodes.

private void addDataAccessNodes(UaFolderNode rootNode) {
    // DataAccess folder
    UaFolderNode dataAccessFolder = new UaFolderNode(getNodeContext(), newNodeId("HelloWorld/DataAccess"), newQualifiedName("DataAccess"), LocalizedText.english("DataAccess"));
    getNodeManager().addNode(dataAccessFolder);
    rootNode.addOrganizes(dataAccessFolder);
    try {
        AnalogItemTypeNode node = (AnalogItemTypeNode) getNodeFactory().createNode(newNodeId("HelloWorld/DataAccess/AnalogValue"), Identifiers.AnalogItemType, new NodeFactory.InstantiationCallback() {

            @Override
            public boolean includeOptionalNode(NodeId typeDefinitionId, QualifiedName browseName) {
                return true;
            }
        });
        node.setBrowseName(newQualifiedName("AnalogValue"));
        node.setDisplayName(LocalizedText.english("AnalogValue"));
        node.setDataType(Identifiers.Double);
        node.setValue(new DataValue(new Variant(3.14d)));
        node.setEURange(new Range(0.0, 100.0));
        getNodeManager().addNode(node);
        dataAccessFolder.addOrganizes(node);
    } catch (UaException e) {
        logger.error("Error creating AnalogItemType instance: {}", e.getMessage(), e);
    }
}
Also used : Variant(org.eclipse.milo.opcua.stack.core.types.builtin.Variant) AnalogItemTypeNode(org.eclipse.milo.opcua.sdk.server.model.nodes.variables.AnalogItemTypeNode) 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) NodeId(org.eclipse.milo.opcua.stack.core.types.builtin.NodeId) UaFolderNode(org.eclipse.milo.opcua.sdk.server.nodes.UaFolderNode) Range(org.eclipse.milo.opcua.stack.core.types.structured.Range)

Example 3 with DataValue

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

the class ExampleNamespace method addDynamicNodes.

private void addDynamicNodes(UaFolderNode rootNode) {
    UaFolderNode dynamicFolder = new UaFolderNode(getNodeContext(), newNodeId("HelloWorld/Dynamic"), newQualifiedName("Dynamic"), LocalizedText.english("Dynamic"));
    getNodeManager().addNode(dynamicFolder);
    rootNode.addOrganizes(dynamicFolder);
    // Dynamic Boolean
    {
        String name = "Boolean";
        NodeId typeId = Identifiers.Boolean;
        Variant variant = new Variant(false);
        UaVariableNode node = new UaVariableNode.UaVariableNodeBuilder(getNodeContext()).setNodeId(newNodeId("HelloWorld/Dynamic/" + name)).setAccessLevel(AccessLevel.READ_WRITE).setBrowseName(newQualifiedName(name)).setDisplayName(LocalizedText.english(name)).setDataType(typeId).setTypeDefinition(Identifiers.BaseDataVariableType).build();
        node.setValue(new DataValue(variant));
        getNodeManager().addNode(node);
        dynamicFolder.addOrganizes(node);
    }
    // Dynamic Int32
    {
        String name = "Int32";
        NodeId typeId = Identifiers.Int32;
        Variant variant = new Variant(0);
        UaVariableNode node = new UaVariableNode.UaVariableNodeBuilder(getNodeContext()).setNodeId(newNodeId("HelloWorld/Dynamic/" + name)).setAccessLevel(AccessLevel.READ_WRITE).setBrowseName(newQualifiedName(name)).setDisplayName(LocalizedText.english(name)).setDataType(typeId).setTypeDefinition(Identifiers.BaseDataVariableType).build();
        node.setValue(new DataValue(variant));
        getNodeManager().addNode(node);
        dynamicFolder.addOrganizes(node);
    }
    // Dynamic Double
    {
        String name = "Double";
        NodeId typeId = Identifiers.Double;
        Variant variant = new Variant(0.0);
        UaVariableNode node = new UaVariableNode.UaVariableNodeBuilder(getNodeContext()).setNodeId(newNodeId("HelloWorld/Dynamic/" + name)).setAccessLevel(AccessLevel.READ_WRITE).setBrowseName(newQualifiedName(name)).setDisplayName(LocalizedText.english(name)).setDataType(typeId).setTypeDefinition(Identifiers.BaseDataVariableType).build();
        node.setValue(new DataValue(variant));
        getNodeManager().addNode(node);
        dynamicFolder.addOrganizes(node);
    }
}
Also used : Variant(org.eclipse.milo.opcua.stack.core.types.builtin.Variant) UaVariableNode(org.eclipse.milo.opcua.sdk.server.nodes.UaVariableNode) DataValue(org.eclipse.milo.opcua.stack.core.types.builtin.DataValue) NodeId(org.eclipse.milo.opcua.stack.core.types.builtin.NodeId) UaFolderNode(org.eclipse.milo.opcua.sdk.server.nodes.UaFolderNode) ByteString(org.eclipse.milo.opcua.stack.core.types.builtin.ByteString)

Example 4 with DataValue

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

the class ExampleNamespace method addAdminWritableNodes.

private void addAdminWritableNodes(UaFolderNode rootNode) {
    UaFolderNode adminFolder = new UaFolderNode(getNodeContext(), newNodeId("HelloWorld/OnlyAdminCanWrite"), newQualifiedName("OnlyAdminCanWrite"), LocalizedText.english("OnlyAdminCanWrite"));
    getNodeManager().addNode(adminFolder);
    rootNode.addOrganizes(adminFolder);
    String name = "String";
    UaVariableNode node = new UaVariableNode.UaVariableNodeBuilder(getNodeContext()).setNodeId(newNodeId("HelloWorld/OnlyAdminCanWrite/" + name)).setAccessLevel(AccessLevel.READ_WRITE).setBrowseName(newQualifiedName(name)).setDisplayName(LocalizedText.english(name)).setDataType(Identifiers.String).setTypeDefinition(Identifiers.BaseDataVariableType).build();
    node.setValue(new DataValue(new Variant("admin was here")));
    getNodeManager().addNode(node);
    adminFolder.addOrganizes(node);
}
Also used : Variant(org.eclipse.milo.opcua.stack.core.types.builtin.Variant) UaVariableNode(org.eclipse.milo.opcua.sdk.server.nodes.UaVariableNode) DataValue(org.eclipse.milo.opcua.stack.core.types.builtin.DataValue) UaFolderNode(org.eclipse.milo.opcua.sdk.server.nodes.UaFolderNode) ByteString(org.eclipse.milo.opcua.stack.core.types.builtin.ByteString)

Example 5 with DataValue

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

the class DataTypeDictionaryManager method registerStructureDescription.

public void registerStructureDescription(StructureDescription description, NodeId binaryEncodingId) {
    structureDescriptions.put(description.getDataTypeId(), description);
    // Add a DataTypeDescriptionTypeNode with a ComponentOf reference to
    // dictionaryNode.
    DataTypeDescriptionTypeNode descriptionNode = new DataTypeDescriptionTypeNode(getNodeContext(), newNodeId(String.format("%s.Description", description.getName())), newQualifiedName(description.getName().getName()), LocalizedText.english(description.getName().getName()), LocalizedText.NULL_VALUE, uint(0), uint(0));
    descriptionNode.setValue(new DataValue(new Variant(description.getName().getName())));
    descriptionNode.setDataType(Identifiers.String);
    descriptionNode.addReference(new Reference(descriptionNode.getNodeId(), Identifiers.HasTypeDefinition, Identifiers.DataTypeDescriptionType.expanded(), Direction.FORWARD));
    descriptionNode.addReference(new Reference(descriptionNode.getNodeId(), Identifiers.HasComponent, dictionaryNode.getNodeId().expanded(), Direction.INVERSE));
    addNode(descriptionNode);
    // Add a DataTypeEncodingTypeNode with a HasDescription reference to
    // descriptionNode and an EncodingOf reference to the DataTypeNode.
    DataTypeEncodingTypeNode dataTypeEncodingNode = new DataTypeEncodingTypeNode(getNodeContext(), binaryEncodingId, new QualifiedName(0, "Default Binary"), LocalizedText.english("Default Binary"), LocalizedText.NULL_VALUE, uint(0), uint(0));
    dataTypeEncodingNode.addReference(new Reference(dataTypeEncodingNode.getNodeId(), Identifiers.HasTypeDefinition, Identifiers.DataTypeEncodingType.expanded(), Direction.FORWARD));
    dataTypeEncodingNode.addReference(new Reference(dataTypeEncodingNode.getNodeId(), Identifiers.HasDescription, descriptionNode.getNodeId().expanded(), Direction.FORWARD));
    dataTypeEncodingNode.addReference(new Reference(dataTypeEncodingNode.getNodeId(), Identifiers.HasEncoding, description.getDataTypeId().expanded(), Direction.INVERSE));
    addNode(dataTypeEncodingNode);
    dictionaryFile.reset();
}
Also used : Variant(org.eclipse.milo.opcua.stack.core.types.builtin.Variant) DataTypeDescriptionTypeNode(org.eclipse.milo.opcua.sdk.server.model.nodes.variables.DataTypeDescriptionTypeNode) 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) DataTypeEncodingTypeNode(org.eclipse.milo.opcua.sdk.server.model.nodes.objects.DataTypeEncodingTypeNode)

Aggregations

DataValue (org.eclipse.milo.opcua.stack.core.types.builtin.DataValue)1099 QualifiedName (org.eclipse.milo.opcua.stack.core.types.builtin.QualifiedName)889 LocalizedText (org.eclipse.milo.opcua.stack.core.types.builtin.LocalizedText)883 Reference (org.eclipse.milo.opcua.sdk.core.Reference)879 PropertyTypeNode (org.eclipse.milo.opcua.sdk.server.model.nodes.variables.PropertyTypeNode)477 Variant (org.eclipse.milo.opcua.stack.core.types.builtin.Variant)419 StringReader (java.io.StringReader)243 OpcUaXmlStreamDecoder (org.eclipse.milo.opcua.stack.core.serialization.OpcUaXmlStreamDecoder)243 BaseDataVariableTypeNode (org.eclipse.milo.opcua.sdk.server.model.nodes.variables.BaseDataVariableTypeNode)227 ExtensionObject (org.eclipse.milo.opcua.stack.core.types.builtin.ExtensionObject)110 UaException (org.eclipse.milo.opcua.stack.core.UaException)102 StatusCode (org.eclipse.milo.opcua.stack.core.types.builtin.StatusCode)79 DataTypeDescriptionTypeNode (org.eclipse.milo.opcua.sdk.server.model.nodes.variables.DataTypeDescriptionTypeNode)75 NodeId (org.eclipse.milo.opcua.stack.core.types.builtin.NodeId)60 UaVariableTypeNode (org.eclipse.milo.opcua.sdk.server.nodes.UaVariableTypeNode)37 UaVariableNode (org.eclipse.milo.opcua.sdk.server.nodes.UaVariableNode)34 UInteger (org.eclipse.milo.opcua.stack.core.types.builtin.unsigned.UInteger)33 AttributeId (org.eclipse.milo.opcua.stack.core.AttributeId)32 ByteString (org.eclipse.milo.opcua.stack.core.types.builtin.ByteString)28 Identifiers (org.eclipse.milo.opcua.stack.core.Identifiers)26