High Performance OPC UA Server SDK  1.1.0.158
History Example Server

This example demonstrates how to implement UA HistoryReadRaw using a simple in-memory ring buffer for storing the history. As a data source we implement a real UDP protocol and don't use a simulation as in other examples. So this example also demonstrates how to use the SDK's network API to implement proprietary protocols and process them in the same single-threaded process.

history_example.png
Example Overview

This example makes use of the InternalProvider implementation and a custom value store for accessing the data from the ring buffer.

The address space is created by loading binary information model (cpumodel.bin), which contains the types for this example. This model has been created with UaModeler. The instances are created programmatically using the function ua_object_create_instance(). Then these instances get connected with the data provided by the custom value store.

The data source uses a simple UDP based protocol to gather information about the CPU temperature. This can be done on Linux using the provided monitor.sh shell script. But the same can be achieved also on other systems using simple python scripts or native programs. E.g. it would be possible to integrate existing Nagios sensors in the same way (see https://www.nagios.org).

UDP Protocol

START<n>\n: Starts historizing in the server.
 <n>: is the CPU index (zero based)
STOP<n>\n: Stops historzing in the server.
CPU<n>:<temp>\n: Adds a temperature sample in [°C*1000]. e.g. 47000 = 47°C

Example messages

START0
START1
CPU0:47500
CPU1:48500
CPU0:47000
CPU1:48000
...
STOP0
STOP1

Usage

First you need to start the OPC UA server. This will create one CPU instance with 4 CPU core instances. The temperature variables of the CPU cores will have set the historizing attribute to false, indicating that no history is currently recorded. The temperature variable of the first CPU core will be pre-filled with a sine wave for demonstration purpose.

uaexpert1.png
CPU Model in UaExpert

Then you can optionally start the monitor.sh shell script on a Linux shell. (This script makes use of netcat to send UDP packages, so you may need to install this package if it is not already installed: sudo apt-get install netcat) This will gather temperature information for all thermal zones of the CPU and send this to the OPC UA server using the simple UDP protocol described above. Alternatively you can use the provided simulator.py Python script to simulate the data, which works on all platforms including Windows.

monitor.png
Monitor Script

This will enable the historizing attribute of the according temperature variables and the ring buffer gets populated with real data. When stopping the script using CTRL-C the historizing attribute will be disabled again.

uaexpert2.png
Historizing change to true

Now you can also read the historian using the History Plugin in UaExpert.

uaexpert3.png
History Trend View in UaExpert

Files

The following table describes the purpose of the different source files.

Source File Description
args.[h,c] Helper files for command line args parsing
cpumodel.* UaModeler project files
cpu_protocol.[h,c] The UDP protocol implementation
history_store.[h,c] The custom value store implementation
history_ringbuffer.[h,c] Ringbuffer for storing historian values
provider_history.[h,c] The history data provider implementation
server_main.c The main application