Server API
Server Constructor
To create a server, you need to call the function ‘newServer’ from the ‘opcua’ module.
- ua.newServer([config][, model])
- Config:
table with Configuration table options.
If not provided, default configuration will be used. in this case host name for endpoint will be automatically detected. Server will listen on port 4841.
- Model:
- Address Space API to use.
If nil then
ua.baseModel()will be initialized.
config = { bufSize = 16384, securePolicies ={ { securityPolicyUri = "http://opcfoundation.org/UA/SecurityPolicy#None" } } endpointUrl = "opc.tcp://[hostname|ip]:4841" }
- Returns:
server object
Server default configuration
-- Load the OPCUA API module
local ua = require("opcua.api")
-- Create new OPC UA server instance.
-- Pass configuration table to server.
local server = ua.newServer()
Server custom configuration
local ua = require("opcua.api")
local config = {
endpoints = {
{
endpointUrl = "opc.tcp://localhost:4845",
}
},
securePolicies = {
{ -- #1
securityPolicyUri = ua.SecurityPolicy.None,
},
},
}
local server = ua.newServer(config)
server:initialize
- server:initialize()
This method initializes internal server structures and becomes ready for customization: adding nodes, setting variable sources, writing values, etc.
- return:
nil
server:run
This method opens a server socket and starts listening for incoming connections.
- server:run()
- Returns:
error
server:shutdown
This method stops the server and closes all connections.
- server:shutdown()
- Returns:
nil
server:addNodes
Add new nodes to server address space.
- server:addNodes(parameters)
- Parameters:
A table with a field NodesToAdd. NodesToAdd is an array of tables with the parameters of new nodes. For details see Adding Nodes
- Returns:
The result from an OPC UA call will be an array. Every element of the array will be a table with two fields: Status code for the current node and identifier of the added node.
Every element of the array contains a table with the following fields:
- StatusCode
The status code from adding the corresponding node.
- AddedNodeId
Identifier of added node. The server automatically assigns an identifier for the node if not included. The identifier will be nil in case of error.
server:browse
Browse nodes in the server address space.
- server:browse(<NodeId | NodeId[] | parameters>)
- NodeId:
Browse one node by NodeId.
- NodeId[]:
Array of NodeIds to browse.
- Parameters:
Table with detailed parameters. For details see Detailed Parameters
server:read
Read attribute values of nodes.
- server:read(<NodeId | NodeId[] | parameters>)
- NodeId:
(NodeID) Read possible attributes of one node by NodeId.
- NodeId[]:
(NodeID) Array of NodeIds to read. All possible attributes will be read
- Parameters:
(table) Table with detailed parameters. For details see Reading Attributes
- Returns:
The result from an OPC UA call will be an array. Every element of the array will be a table with two fields: Status code for the current node and the value of the attribute.
Every element of the array contains a table with the following fields:
- StatusCode
The status code from reading the corresponding node.
- Value (DataValue)
The value of the attribute. The value will be nil in case of error.
server:write
- server:write(parameters)
- Parameters:
A table with a field NodesToWrite. NodesToWrite is an array of tables with the parameters of nodes to write. For details see Writing Attributes
- Returns:
response, error
- response
array if error is nil The result from an OPC UA call will be an array. Every element of the array is Status code for each node
nil if StatusCode is not nil
- error
StatusCode of operation overall or nil
server:setValueCallback
This method sets a callback function for a variable node. The callback function will be called when the value of the variable is reading or writing. For more details, see Exporting Device Data
- server:setValueCallback(nodeId, callback)
- NodeId:
NodeId of the variable node.
- Callback:
Function to be called when the value of the variable is being read or written.
server:setWritehook
This method sets a callback function for a variable node. When the value of the variable has been changed, the callback function will be called.
- services:setWriteHook(nodeId, writeHook)
- NodeId:
NodeId for which need to set a hook
- WriteHook:
Function to be called when the value of the variable has been changed.
- writeHook(nodeId, attributeId, value)
- NodeId:
NodeId which attribute has changed
- AttributeId:
AttributeId which has changed
- Value:
New value of the attribute
server:loadXmlModels
Load XML models to server address space.
- server:loadXmlModels(modelPaths[])
- ModelPaths[]:
Array of paths to XML models.
All nodes from the same XML model are added to the same namespace. For every new model, a new namespace is created with the next index after the last added namespace. Namespace index 0 is reserved for the default namespace ‘http://opcfoundation.org/UA/’. Namespace index 1 is reserved for the namespace reserved for the current server. All new loaded models will be mapped to namespaces with indexes starting from 2.
-- Load the required modules
local ua = require("opcua.api")
local server = ua.newServer()
server:initialize()
-- current file path
local rootPath = debug.getinfo(1, "S").short_src:match("..(/.*/)")
rootPath = ba.openio("home"):realpath(rootPath)
server:loadXmlModels({
rootPath.."euromap83_1_03/Opc.Ua.Di.NodeSet2.xml",
rootPath.."euromap83_1_03/Opc.Ua.PlasticsRubber.GeneralTypes.NodeSet2.xml"
})
server:run()
server:shutdown()
server:createNamespace
Create a new namespace.
- server:createNamespace(namespaceUri)
- NamespaceUri:
URI of the namespace.
- Returns:
index of the namespace.
This method is used to create a new namespace. The created namespace will have the next index after the last added namespace. Namespace index 0 is reserved for the default namespace ‘http://opcfoundation.org/UA/’. Namespace index 1 is reserved for the namespace reserved for the current server. All newly created namespaces will be mapped to namespaces with indexes starting from 2.
local server = ua.newServer()
server:initialize()
local nsIndex = server:createNamespace("http://test.com")
print(nsIndex)
server:exportXmlModels
Export XML models to the file.
- server:exportXmlModels(output, namespaceUris)
- Output:
File path or callback function to export XML models.
- NamespaceUris:
Array of namespace URIs to export.
-- Load XML models from the OPCUA Foundation github repository
local baseUrl = "https://raw.githubusercontent.com/OPCFoundation/UA-Nodeset/refs/heads/latest"
server:loadXmlModels({
baseUrl.."/DI/Opc.Ua.Di.NodeSet2.xml",
baseUrl.."/PlasticsRubber/GeneralTypes/1.03/Opc.Ua.PlasticsRubber.GeneralTypes.NodeSet2.xml"
})
-- Method 1: Export all models to a file
local outputFile = "exported_models.xml"
server:exportXmlModels(outputFile)
print("Exported all models to: " .. outputFile)