Overview
OPC UA Integration allows you to stream data from the OPC UA server to NexAI and converts the device payloads to the NexAI format.
OPC-UA Integration Tutorial
In this tutorial, we will configure the integration between NexAI and OPC-UA to get the Airconditioners data from the OPC UA C++ Demo Server and allow the user to switch on/off any Airconditioner using the Integration downlink feature.
Prerequisites
- Download and install the OPC UA C++ Demo Server;
- After installation, launch the UA Admin Dialog;
- Make sure that the hostname/IP address where the OPC-UA server is hosted is set correctly, and remember the values for the hostname/IP and port. These values will be needed when configuring the OPC-UA integration.
- Launch the UaCPPServer. The console dialog will open showing the server endpoints URLs
NexAI setup
Uplink Data Converter
First, we need to create the Uplink Data converter that will be used for receiving the messages from the OPC UA server. The converter should transform the incoming payload into the required message format. The message must contain the deviceName and deviceType. These fields are used to submit the data to the correct device. If a device cannot not be found, a new device will be created. Here is how the payload from the OPC UA integration will look like:
Payload:
{
"temperature": "72.15819999999641"
}
Metadata:
{
"opcUaNode_namespaceIndex": "3",
"opcUaNode_name": "AirConditioner_1",
"integrationName": "OPC-UA Airconditioners",
"opcUaNode_identifier": "AirConditioner_1",
"opcUaNode_fqn": "Objects.BuildingAutomation.AirConditioner_1"
}
We will take the opcUaNode_name metadata value and map it to the deviceName and set the deviceType as airconditioner.
However, you can use another mapping in your specific use cases.
Also, we will retrieve the values of the temperature, humidity and powerConsumption fields and use them as device telemetries.
Go to the Integrations center section -> Data converters page and create a new uplink converter
One can use either NAEL (NexAI expression language) or JavaScript to develop user defined functions. We recommend utilizing NAEL as it’s execution in NexAI is much more efficient compared to JS.
var data = decodeToJson(payload); var deviceName = metadata['opcUaNode_name']; var deviceType = 'airconditioner'; var result = { deviceName: deviceName, deviceType: deviceType, telemetry: { }, attributes: { } }; if (data.temperature != null) { result.telemetry.temperature = toFixed(data.temperature, 2); } if (data.humidity != null) { result.telemetry.humidity = toFixed(data.humidity, 2); } if (data.powerConsumption != null) { result.telemetry.powerConsumption = toFixed(data.powerConsumption, 2); } if (data.state != null) { result.attributes.state = data.state == '1' ? true : false; } /** Helper functions 'decodeToString' and 'decodeToJson' are already built-in **/ return result; |
Downlink Data Converter
For sending Downlink messages from the NexAI to the OPC UA node, we need to define a downlink Converter.
In general, the output from a Downlink converter should have the following structure:
[{
"contentType": "JSON",
"data": "{\"writeValues\":[],\"callMethods\":[{\"objectId\":\"ns=3;s=AirConditioner_1\",\"methodId\":\"ns=3;s=AirConditioner_1.Stop\",\"args\":[]}]}",
"metadata": {}
}]
- contentType – defines how data will be encoded {TEXT | JSON | BINARY}. In case of OPC UA Integration, JSON is used by default.
- data – the actual data that will be processed by OPC UA Integration and sent to the target OPC UA nodes:
- writeValues – array of write values methods:
- nodeId – target node in OPC UA NodeId format (
ns=<namespaceIndex>;<identifiertype>=<identifier>) - value – value to write
- nodeId – target node in OPC UA NodeId format (
- callMethods – array of call methods:
- objectId – target object in OPC UA NodeId format
- methodId – target method in OPC UA NodeId format
- args – array of method input values
- writeValues – array of write values methods:
- metadata – not used in case of OPC UA Integration and can be empty.
Go to the Integrations center section -> Data converters page and create a new downlink converter.
One can use either NAEL (NexAI expression language) or JavaScript to develop user defined functions. We recommend utilizing NAEL as it’s execution in NexAI is much more efficient compared to JS.
var data = { writeValues: [], callMethods: [] }; if (msgType === 'RPC_CALL_FROM_SERVER_TO_DEVICE') { if (msg.method === 'setState') { var targetMethod = msg.params === 'true' ? 'Start' : 'Stop'; var callMethod = { objectId: 'ns=3;s=' + metadata['deviceName'], methodId: 'ns=3;s=' +metadata['deviceName']+'.'+targetMethod, args: [] }; data.callMethods.push(callMethod); } } var result = { contentType: "JSON", data: JSON.stringify(data), metadata: {} }; return result; |
This converter will process the RPC command to the device using the method setState and a boolean params value to call the ‘Start’ or ‘Stop’ method of the airconditioner.
Destination node is detected using the deviceName field of the incoming message metadata.