Server API reference
Server Constructor
To create a need to call new function ‘newServer’ from ‘opcua’ module.
- ua.newServer([config])
- config:
table with configuration 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.
config = { bufSize = 16384, securePolicies ={ { securityPolicyUri = "http://opcfoundation.org/UA/SecurityPolicy#None" } } endpointUrl = "opc.tcp://[hostname|ip]:4841"" }
- return:
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 customizations: 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()
- return:
error
server:shutdown
This method stops the server and closes all connections.
- server:shutdown()
- return:
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
- return:
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 attibutes 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. Will be read all possible attributes
- parameters:
(table) Table with detailed parameters. For details see Reading Attributes
- return:
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
- return:
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 the each node node
nil id StatusCode is not nil
- error
StatusCode of operation overall or nil
server:setVariableSource
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:setVariableSource(nodeId, callback)
- nodeId:
NodeId of the variable node.
- callback:
Function to be called when the value of the variable is being read or write.
server:setWritehook
This method sets a callback function for a variable node. When 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 same XML model are added to the same namespace. For every new model new namespace is created with next index after last added namespace. Namespace index 0 reserved for default namespace ‘http://opcfoundation.org/UA/’. Namespace index 1 reserved for namespace reserved for 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.
- return:
index of the namespace.
This method is used to create a new namespace. Created namespace will have next index after last added namespace. Namespace index 0 reserved for default namespace ‘http://opcfoundation.org/UA/’. Namespace index 1 reserved for namespace reserved for current server. All new 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)