opcua
The opcua module provides an OPC UA client API for JSH applications.
Client
OPC UA client object.
Creation
new Client(options)- Returns:
Client - Throws
missing argumentsifoptionsis omitted.
Options
| Option | Type | Default | Description |
|---|---|---|---|
| endpoint | string | "" | OPC UA endpoint (opc.tcp://host:port) |
| readRetryInterval | number | 100 (clamped to 100 if lower) | Retry interval for read() in milliseconds |
| messageSecurityMode | number | MessageSecurityMode.None | Security mode. See MessageSecurityMode |
Usage example
| |
close()
Closes the client connection.
Syntax
close()- Returns:
undefined
read()
Reads values from the given node list.
Syntax
read(readRequest)Parameters
readRequest(object): ReadRequest
Return value
object[]: array of ReadResult
Error behavior:
- Throws
missing argumentif argument count is not exactly one. - Throws
missing nodeswhennodesis empty.
write()
Writes one or more node values.
Syntax
write(...writeRequest)Parameters
writeRequest(object, variadic): WriteRequest
Return value
object: WriteResult
Error behavior:
- Throws
missing argumentwhen no argument is provided.
Usage example
| |
ReadRequest
| Property | Type | Default | Description |
|---|---|---|---|
| nodes | string[] | List of OPC UA node IDs to read | |
| maxAge | number | 0 | Maximum acceptable cache age in milliseconds |
| timestampsToReturn | number | TimestampsToReturn.Neither | Timestamp return policy |
ReadResult
| Property | Type | Description |
|---|---|---|
| status | number | OPC UA status code (uint32) |
| statusText | string | Status text |
| statusCode | string | Status code name (for example, StatusGood) |
| value | any | Read value |
| type | string | Value type name (for example, Boolean, Int32, Double) |
| sourceTimestamp | number | Unix epoch timestamp in milliseconds |
| serverTimestamp | number | Unix epoch timestamp in milliseconds |
WriteRequest
| Property | Type | Description |
|---|---|---|
| node | string | Target node ID |
| value | any | Value to write |
WriteResult
| Property | Type | Description |
|---|---|---|
| error | Error|null | Write error |
| timestamp | number | Response timestamp (Unix epoch milliseconds) |
| requestHandle | number | OPC UA request handle |
| serviceResult | number | OPC UA service result code |
| stringTable | string[] | OPC UA string table |
| results | number[] | Per-node status code array |
MessageSecurityMode
MessageSecurityMode.NoneMessageSecurityMode.SignMessageSecurityMode.SignAndEncryptMessageSecurityMode.Invalid
TimestampsToReturn
TimestampsToReturn.SourceTimestampsToReturn.ServerTimestampsToReturn.BothTimestampsToReturn.NeitherTimestampsToReturn.Invalid
OPCUA Client
This example implements a collector that connects to an OPC UA server, reads system metrics, and stores them in the database.
Flow
- OPC UA integration: use the
opcuamodule to connect toopc.tcp://localhost:4840and read node values such assys_cpu,sys_mem, andload1. - Periodic collection: use
setInterval()to read data every 10 seconds. - Data ingestion: store collected values in the
EXAMPLEtable (name,time,value).
Data collector
Save the script as opcua-client.js, then run it in the background from the JSH terminal.
jsh / > opcua-client
jsh / > ps
┌──────┬──────┬──────┬──────────────────┬────────┐
│ PID │ PPID │ USER │ NAME │ UPTIME │
├──────┼──────┼──────┼──────────────────┼────────┤
│ 1044 │ 1 │ sys │ /opcua-client.js │ 13s │
│ 1045 │ 1025 │ sys │ ps │ 0s │
└──────┴──────┴──────┴──────────────────┴────────┘ opcua-client.js
| |
Simulator server
To test opcua-client.js, you need an OPC UA server that provides system metric nodes.
If you do not have a real environment, use the simulator from the repository below.
It provides sample data such as sys_cpu, sys_mem, load1, load5, and load15
to validate collection and visualization flows.
Follow the instructions in the repository to set it up.
https://github.com/machbase/neo-server/tree/main/jsh/native/opcua/test_server
After starting the simulator, run opcua-client.js and the OPC UA client will connect
and collect data.