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 = { -- #1
  ParentNodeId = ObjectsFolder,
  ReferenceTypeId = Organizes,
  RequestedNewNodeId = "i=1000",
  BrowseName = {Name="TestFolder", ns=0},
  NodeClass = ua.Types.NodeClass.Object,
  TypeDefinition = FolderType,
  NodeAttributes = {
    TypeId = "i=354",
    Body = {
      SpecifiedAttributes = ua.Types.ObjectAttributesMask,
      DisplayName = {Text="DisplayName"},
      Description = {Text="Description"},
      WriteMask = 0,
      UserWriteMask = 0,
      EventNotifier = 0,
    }
  }
}
local request = {
  NodesToAdd = {folderParams}
}
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 dataValue = {
  Value = {UInt32=30000}
}

local newVariable = ua.newVariableParams(res.AddedNodeId, {Name="uint32_variable"}, "UInt32", dataValue, scalarBooleanId)

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 (DataValue)

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)

The value rank for the field. It shall be Scalar (-1) or a fixed rank Array (>=1). There are next predefined values:

  • ScalarOrOneDimension = -3,

  • Any = -2,

  • Scalar = -1,

  • OneOrMoreDimensions = 0,

  • OneDimension = 1,

Greater values are number of dimensions: 2(matrix),3,4..

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.