Adding Nodes

Adding Folder Node

UA Folder in the OPC-UA Address space is of the type Folder object and is based on the Object NodeClass. The purpose with this object is to organize nodes into a tree structure.

To create a Folder node, specify the following attributes:

-- Insert a folder into the address space
local folderParams = {
  parentNodeId = nodeIds.ObjectsFolder,
  referenceTypeId = nodeIds.Organizes,
  requestedNewNodeId = ua.NodeId.Null,
  browseName = {name="TestFolder", ns=3},
  nodeClass = ua.Types.NodeClass.Object,
  displayName = {text="Folder"},
  description = {text="Folder Example"},
  writeMask = 0,
  userWriteMask = 0,
  eventNotifier = 0,
  typeDefinition = nodeIds.FolderType
}

local request = {
  nodesToAdd = {folderParams}
}

local resp, err = client:addNodes(request)
for i,res in ipairs(resp.results) do
  if res.statusCode ~= 0 then
    trace(string.format("Adding folder node failed: 0x%X", res.statusCode))
  else
    trace(string.format("Added new folder node with Id: '%s'", res.addedNodeId))
  end
end

Full source

Adding Variables

A Variable in OPC-UA is a node in the address space with a set of predefined attributes:

local newVariable = {
  parentNodeId = nodeIds.ObjectsFolder,
  referenceTypeId = nodeIds.Organizes,
  requestedNewNodeId = ua.NodeId.Null,
  browseName = {name="uint32_variable", ns=0},
  nodeClass = ua.Types.NodeClass.Variable,
  typeDefinition = nodeIds.BaseDataVariableType,
  displayName = {text="UInt32"},
  description = {text="UInt32 scalar variable example"},
  writeMask = 0,
  userWriteMask = 0,
  value = {uint32=30000},
  dataType = nodeIds.UInt32,
  valueRank = ua.Types.ValueRank.Scalar,
  arrayDimensions = nil,
  accessLevel = 0,
  userAccessLevel = 0,
  minimumSamplingInterval = 1000,
  historizing = 0
}

local request = {
  nodesToAdd = {newVariable}
}

local resp, err = client:addNodes(request)
for i,res in ipairs(resp.results) do
  if res.statusCode ~= 0 then
    trace(string.format("Adding variable node failed: 0x%X", res.statusCode))
  else
    trace(string.format("Added new variable with NodeId: '%s'", res.addedNodeId))
  end
end

Full source

Variable Node Attributes

To add a new variable node, you need to set the following parameters:

parentNodeId (type NodeID)
Parent’s node identifier.
referenceTypeId (type NodeID)
Parent’s reference type identifier.
requestedNewNodeId (type NodeID)
NodeID for the new node. If NodeId equals to ua.NodeId.Null, then the actual identifier will be automatically assigned by server.
browseName (type QualifiedName)
Internal name for the node. This name is used in the TranslateBrowsePathsToNodeIdsservice to resolve NodeId by path from some nodes.
nodeClass (value from table ua.Types.NodeClass)
Node class: Variable, Object, DataType. Only Variable node class is currently supported.
displayName (type LocalizedText)
Clients use this attribute if they want to display the name of the node to the user. Can be localized.
description (type LocalizedText)
The optional description attribute explains the purpose with the node using localised text.
writeMask (type UInt32)
Bit mask. Every bit defines ability to change corresponding attribute.
userWriteMask (type UInt32)
Bit mask. Every bit defines ability to change corresponding attribute for current user.
value (type Variant)
Initial value of a variable node.
dataType (type NodeID)
Identifier of a node of NodeClass DataType that describes structue of a value. For example encoding/decoding in binary, json, xml formats. This may be an ID of a node of simple type (Int32, UInt,Float, etc), and complex structures (for example certificate, ApplicationDescription, BuildInfo).
valueRank (type Int32)
Possible values: scalar, array. More information in section ValueRank
arrayDimensions (array of UInt32)
Array dimensions of the value. For example: {2,2} describes square matrix with size 2*2.
accessLevel (type Byte)
The AccessLevel Attribute is used to indicate how the Value of a Variable can be accessed (read/write) and if it contains current and/or historic data.
userAccessLevel (type Byte)
Write mask of current user

Object node attributes

To add a new Folder node, you need to set the following parameters:

parentNodeId (type NodeID)
Parent’s node identifier.
referenceTypeId (type NodeID)
Parent’s reference type identifier.
requestedNewNodeId (type NodeID)
NodeID for the new node. The identifier will be automatically assigned by server if NodeId equals ua.NodeId.Null.
browseName (type QualifiedName)
Internal name for the node. This name is used in the TranslateBrowsePathsToNodeIdsservice to resolve NodeId by path from some nodes.
nodeClass (value from table ua.Types.NodeClass)
Node class: Variable, Object, DataType. Only Variable node class is currently supported.
displayName (type LocalizedText)
Clients use this attribute if they want to show the name of the node to the user. Can be localized.
description (type LocalizedText)
The optional description attribute explains the purpose with the node using localised text.
writeMask (type UInt32)
Bit mask. Makes it possible for a client to write the Attributes of the Node.
eventNotifier (type Byte)
Event notifier.

Results

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 current node and Identifier of added node.

Every element of the array contains a table with the following fields:

statusCode
Status code of adding corresponding node.
addedNodeId
Identifier of added node. Server automatically assign an identifier for the node if not included. In other case will be returned identifier passed in parameters. Nil in case of error.