Import/Export
- model:loadModels(modelSources)
Load XML or packed models into the address space. The format is detected from the model content.
- ModelSources:
Array of file paths, HTTP/HTTPS URLs, XML document strings, packed blob strings, readable objects, or chunk-producing functions.
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/’. If model with same URI already exists, it will be merged with the new one.
XML specifications
- model:loadXml(source)
Import one UANodeSet XML model into the writable address-space provider.
- Source:
XML document string, readable object, or chunk-producing function.
- Returns:
nil
- Throws:
Error if the XML model cannot be parsed or imported.
- model:exportXml(output[, namespaceURIs])
Export XML models to a filename, open file handle, or output callback. A callback is called with each generated XML chunk. If
namespaceURIsis not provided, all namespaces are exported.- Output:
Filename, writable file handle, or callback function that receives XML string chunks. A caller-provided file handle is not closed.
- NamespaceURIs:
Array of namespace URIs to export.
- Returns:
nil
- Throws:
Error if the model cannot be exported.
Memory optimized packed format
OPC UA adress space can be exported to a packed binary format that is optimized for memory usage and loading speed. Packed format is ready for use without any additional parsing or preprocessing.
- model:exportPacked(output[, namespaceURIs])
Export a packed readonly address-space blob to a filename, open file handle, or output callback. A callback is called with each generated binary chunk. If
namespaceURIsis not provided, all namespaces are exported.- Output:
Filename, writable file handle, or callback function that receives binary string chunks. A caller-provided file handle is not closed.
- NamespaceURIs:
Array of namespace URIs to export.
- Returns:
nil
- Throws:
Error if the model cannot be packed.
The following example shows how to export packed XML models to the file:
local model = ua.baseModel(config)
local editor = model:edit()
local objectsFolder = editor:objectsFolder()
local sensor = editor:addObject("Sensor")
sensor:addVariable("Temperature", {Type=ua.VariantType.Double, Value=20})
sensor:addVariable("Humidity", {Type=ua.VariantType.Double, Value=50})
sensor:addVariable("Pressure", {Type=ua.VariantType.Double, Value=1013.25})
sensor:addVariable("Voltage", {Type=ua.VariantType.Double, Value=220})
sensor:addVariable("Current", {Type=ua.VariantType.Double, Value=10})
sensor:addVariable("Power", {Type=ua.VariantType.Double, Value=2200})
sensor:addVariable("Frequency", {Type=ua.VariantType.Double, Value=50})
local configurationObject = sensor:addObject("Configuration")
configurationObject:addProperty("Address", {Type=ua.VariantType.String, Value="1"})
configurationObject:addProperty("Port", {Type=ua.VariantType.UInt16, Value=1234})
configurationObject:addProperty("Enabled", {Type=ua.VariantType.Boolean, Value=true})
editor:save()
model:exportPacked("packed_model.uapb", {config.applicationUri})
- model:loadPacked(source)
Load a packed blob string as a readonly address-space provider.
- Source:
Packed blob string.
- Returns:
Loaded readonly provider.
- Throws:
Error if the packed blob is invalid or cannot be linked.
Here is the example of loading packed XML models from the file:
local ua = require("opcua.api")
local config = ua.serverConfig({
endpointUrl = "opc.tcp://localhost:4841",
applicationUri = "urn:realtimelogic.com:server",
productUri = "urn:realtimelogic.com:server",
applicationName = "Example Server",
applicationType = ua.ApplicationType.Server,
})
local server = ua.newServer(config)
server:initialize()
server:loadModels({"packed_model.uapb"})
-- server:run()
server:shutdown()