Redis
The gateway can read the value of a node from a Redis database. Another program, for example a Node-RED flow, writes the values to Redis. The gateway only reads from Redis.
Words on this page
| Word | Meaning |
|---|---|
| Redis connection | One entry of redis:. It has a name: and an endpoint:. |
| Redis key | The name of a value in the Redis database. |
| DataValue | An OPC UA value with a status code and a timestamp. |
Before you start
- Run a Redis database that the gateway can reach.
- Know the host and the port of the Redis database.
How to read a Redis key in a node
- Add
redis:at the top level of the configuration file.redis:is a top-level key. It is not inbrownfieldDevices:. - Add one entry for each Redis database. Set
name:andendpoint:. - In
mapping:, add a mapping entry for the node. - Set
value:to a$redisGetor$redisGetRawexpression with the connection name and the Redis key.
redis:
- name: r1
endpoint: redis://localhost:6379
The mapping entries use the connection r1:
- node: /di:DeviceSet/own:MyObject/own:MyInt32
value: $redisGet("r1","MyInt32")
- node: /di:DeviceSet/own:MyObject/own:MyFloat2
value: |
(
$mb := $redisGetRaw("r1","MyFloat2");
{
"value": $mb.value / 10.0 + 3.14,
"statusCode": $mb.statusCode
};
)
Which function to use
| Function | The Redis key holds | The result |
|---|---|---|
$redisGet(connection, key) | A DataValue in OPC UA JSON format. | The value and the status code of the DataValue. |
$redisGetRaw(connection, key) | A JSON value, for example 3.14 or [3.14, 2.71, 1.41]. | The value, with the timestamp of the key <key>:timestamp if it exists. |
For $redisGetRaw, the key <key>:timestamp holds a time in milliseconds since 1970.
When this key does not exist, the gateway uses the current time.
If the connection is missing or not connected, or the key is empty, the node gets a bad status code.
How to write test values
-
Open
redis-cli. -
Write a raw value for
$redisGetRaw:SET MyFloat2 "3.14"SET MyFloatArray "[3.14, 2.71, 1.41]" -
Write an OPC UA JSON DataValue for
$redisGet:SET MyInt32 "{\"UaType\": 6,\"Value\": 3000}"
For the format, see OPC UA Part 6, JSON data encoding.
The package @opcua/omni-edge-for-node-red can help to make the OPC UA JSON.
See Node-RED.
How to open redis-cli
To use the Redis container of the Docker Compose bundle, run:
docker exec -it redis-server redis-cli
To use a separate container and a Redis server on the host at port 16379, run:
docker run -it --rm --network host redis:latest redis-cli -h localhost -p 16379
How to check that it works
- Start the gateway with the configuration file.
- Write a value for the key in Redis.
- Read the node with an OPC UA client, for example UaExpert. The node shows the value of the key.
See the Redis connection for the configuration.