Handling Complex Data Types

Complex data types may be encountered when dealing with certain OPC UA I/O items. They are data types that are structured, containing multiple pieces of data in the same data type (for example, an address can contain country, state , city , street and house number data). The structure of the complex data type varies from server to server and vendor to vendor. As such, OPC UA clients normally require prior knowledge of the data structure to effectively handle complex data types. The AspenTech Inmation UA client converts the complex data from a UA I/O item into a JSON document while still retaining the data structure from the OPC UA server. The following example shows how you can use Lua to access selected portions of the complex data type (for example an updating real time value) so it can then be visualized in a trend display or processed otherwise. Although the JSON document conversion removes some uncertainty, it is still; advantageous to know how the OPC UA server in your system structures complex data types, to handle the specific examples you may encounter and extract the desired portion of data. This example use elements of this JSON library module. This library can be housed in a Script Library Item in the System Model and accessed using the require"library-name" command. In the example the timestamp and value are extracted from the JSON using the dot notation. The complex data structure will dictate how this sub-data is named.

local json = require("json")
local newitempath = "/IO-Home/"
local obj, pos, err = json.decode (syslib.getvalue("complexdataproducingitem"), 1, nil)
if err then
  return "Error: " .. err
else local cditem = syslib.getobject(newitempath .. obj.Name) if cditem == nil then
  cditem = syslib.createobject(newitempath .. obj.Name, "MODEL_CLASS_DATAHOLDER")
end
  -- Use Javascript dot Notation to access nested Elements in the JSON document "obj"
  local ts = syslib.gettime(obj.Timestamp)
  syslib.setvalue("value_path", obj.Value, 0, ts)
  return ""
end