Search in sources :

Example 1 with DeviceConfiguration

use of org.apache.plc4x.java.opcuaserver.configuration.DeviceConfiguration in project plc4x by apache.

the class Plc4xNamespace method addNodes.

private void addNodes() {
    for (DeviceConfiguration c : config.getDevices()) {
        NodeId folderNodeId = newNodeId(c.getName());
        UaFolderNode folderNode = new UaFolderNode(getNodeContext(), folderNodeId, newQualifiedName(c.getName()), LocalizedText.english(c.getName()));
        getNodeManager().addNode(folderNode);
        folderNode.addReference(new Reference(folderNode.getNodeId(), Identifiers.Organizes, Identifiers.ObjectsFolder.expanded(), false));
        addConfiguredNodes(folderNode, c);
    }
}
Also used : Reference(org.eclipse.milo.opcua.sdk.core.Reference) NodeId(org.eclipse.milo.opcua.stack.core.types.builtin.NodeId) UaFolderNode(org.eclipse.milo.opcua.sdk.server.nodes.UaFolderNode) DeviceConfiguration(org.apache.plc4x.java.opcuaserver.configuration.DeviceConfiguration)

Example 2 with DeviceConfiguration

use of org.apache.plc4x.java.opcuaserver.configuration.DeviceConfiguration 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)

Aggregations

DeviceConfiguration (org.apache.plc4x.java.opcuaserver.configuration.DeviceConfiguration)2 Reference (org.eclipse.milo.opcua.sdk.core.Reference)2 UaFolderNode (org.eclipse.milo.opcua.sdk.server.nodes.UaFolderNode)2 NodeId (org.eclipse.milo.opcua.stack.core.types.builtin.NodeId)2 Array (java.lang.reflect.Array)1 Arrays (java.util.Arrays)1 List (java.util.List)1 PlcConnectionException (org.apache.plc4x.java.api.exceptions.PlcConnectionException)1 Configuration (org.apache.plc4x.java.opcuaserver.configuration.Configuration)1 Tag (org.apache.plc4x.java.opcuaserver.configuration.Tag)1 PooledPlcDriverManager (org.apache.plc4x.java.utils.connectionpool.PooledPlcDriverManager)1 AccessLevel (org.eclipse.milo.opcua.sdk.core.AccessLevel)1 ValueRank (org.eclipse.milo.opcua.sdk.core.ValueRank)1 OpcUaServer (org.eclipse.milo.opcua.sdk.server.OpcUaServer)1 DataItem (org.eclipse.milo.opcua.sdk.server.api.DataItem)1 ManagedNamespaceWithLifecycle (org.eclipse.milo.opcua.sdk.server.api.ManagedNamespaceWithLifecycle)1 MonitoredItem (org.eclipse.milo.opcua.sdk.server.api.MonitoredItem)1 DataTypeDictionaryManager (org.eclipse.milo.opcua.sdk.server.dtd.DataTypeDictionaryManager)1 UaVariableNode (org.eclipse.milo.opcua.sdk.server.nodes.UaVariableNode)1 AttributeFilters (org.eclipse.milo.opcua.sdk.server.nodes.filters.AttributeFilters)1