Page 1 of 1

How do I read or subscribe for data internally in the server

Posted: 24 Jul 2011, 22:16
by Lost+Found
Hello,

I want to read nodes from my code in the OPC UA server. The OPC UA server was developed with the C++ SDK from Unified Automation.

Is there an easy way to read, write or subscribe nodes in the server.

Thank you

Re:How do I read or subscribe for data internally in the ser

Posted: 24 Jul 2011, 22:58
by Support Team
Hi,

There are two ways to do this.

(1) Simple but limited option

If the node you want to access was created in the toolkit layer and is present in memory, you can get the node and access the attributes directly. But this option does not work for all nodes depending on the selected implementation for the node.

Code: Select all

	UaNodeId         nodeId(OpcUaId_ServerState);
	// Get root NodeManager
	NodeManagerRoot* pNMRoot = (NodeManagerRoot*)m_pServerManager->getNodeManagerRoot();
	// Try to find the node
	UaNode*          pNode   = pNMRoot->getNode(nodeId);
	if ( pNode )
	{
		// Get any attribute value from the node
		// Check for node class before casting to the interface like UaVariable for specific attributes 
		OpcUa_NodeClass nodeClass = pNode->nodeClass();

		// Do not forget to release the reference to the node created by getNode()
		pNode->releaseReference();
	}



(2) The option that always works.

There is an internal interface ServerManager provided by the SDK. This interface defines methods to browse, read, write, call a method or to create monitored items for data or event monitoring. This option works always independent of the way the data acquisition is implemented in the server.

The sample code for Historical Access in \"Server Getting Started\" -> \"Lesson07\" provides an example for creating an internal session and to create internal data monitored items:

Code: Select all

    UaStatus status;

    // Create internal session
    m_pSession = m_pServerManager->getServerConfig()->createSession(0, UaNodeId());
    if ( m_pSession )
    {
        status = m_pSession->open(\"InternalHistorizing\", UaByteString(), OpcUa_UInt32_Max).statusCode();
        if ( status.isGood() )
        {
            UaStringArray localeIDs;
            localeIDs.create(1);
            UaString sTemp(\"en\");
            sTemp.copyTo(&localeIDs[0]);
            status = m_pSession->activate(0, NULL, localeIDs).statusCode();
        }
    }

    // Create internal monitored items for historizing
    if ( status.isGood() )
    {
        DataMonitoredItemSpecArray dataMonitoredItems;
        OpcUa_UInt32               i = 0;
        std::map<UaNodeId, HistorizedVariable*>::iterator it;

        dataMonitoredItems.create(m_mapVariables.size());

        // Collect information for the monitored items to create
        for(it=m_mapVariables.begin(); it!=m_mapVariables.end(); it++)
        {
            HistorizedVariable* pVariable = it->second;
            pVariable->m_pVariable->nodeId().copyTo(&dataMonitoredItems[i].m_itemToMonitor.NodeId);
            dataMonitoredItems[i].m_itemToMonitor.AttributeId = OpcUa_Attributes_Value;
            dataMonitoredItems[i].m_requestedSamplingInterval = HISTORYMANAGERBA_SAMPLING_INTERVAL;
            dataMonitoredItems[i].m_pDataCallback = pVariable;
            i++;
        }

        // Create the monitored items
        status = m_pServerManager->createDataMonitoredItems(m_pSession, dataMonitoredItems);
    
        if ( status.isGood() )
        {
            i = 0;
            // Store the create results
            for(it=m_mapVariables.begin(); it!=m_mapVariables.end(); it++)
            {
                HistorizedVariable* pVariable = it->second;
                if ( dataMonitoredItems[i].m_createResult.isGood() )
                {
                    pVariable->m_isValid = OpcUa_True;
                    pVariable->m_monitoredItemId = dataMonitoredItems[i].m_monitoredItemId;
                    if ( dataMonitoredItems[i].m_isInitialValueProvided != OpcUa_False )
                    {
                        pVariable->dataChange(dataMonitoredItems[i].m_initialValue);
                    }
                }
                i++;
            }
        }
    }
Best Regards,
Unified Automation Support Team

Re:How do I read or subscribe for data internally in the server

Posted: 06 Aug 2011, 13:28
by Lost+Found
Thank you very much for your excellent support.
This was exactly what I was looking for.