Injecting standard companion specifications (RFID example)
Goal
Load a standard companion specification (RFID) into your scripted server and instantiate a complex object type.
Prerequisites
- You have the basic scripted server working (see Creating an OPC UA server).
- You have the RFID companion nodeset file available (XML).
Step 1 — Add the RFID companion nodeset
Download the RFID companion nodeset and place it in your Node-RED user directory, for example:
# ~/.node-red/nodesets/Opc.Ua.Rfid.NodeSet2.xml
Then extend the server options with the nodeset file:
const server = new opcua.OPCUAServer({
port: msg.port || 4840,
nodeset_filename: [
opcua.nodesets.standard,
"./nodesets/Opc.Ua.Rfid.NodeSet2.xml"
],
serverInfo: {
applicationUri: `urn:${msg.endpoint}`,
productUri: "MyOPCUAServer",
applicationName: { text: "MyOPCUAServer", locale: "en-US" },
isOnline: true
}
});
Step 2 — Instantiate an RFID device object
After the server has initialized its address space, create an instance of the RFID device type. Place this right after the namespace creation in the Function node, and after the address space is ready:
const addressSpace = server.engine.addressSpace;
const namespace = addressSpace.getOwnNamespace();
// Find the RFID namespace (by URI from the companion spec)
const rfidNamespace = addressSpace.getNamespace("http://opcfoundation.org/UA/RFID/");
// Get the RFID DeviceType from the companion nodeset
const rfidDeviceType = rfidNamespace.findObjectType("RfidDeviceType");
// Create a folder to organize devices
const devicesFolder = namespace.addFolder("ObjectsFolder", { browseName: "Devices" });
// Instantiate a complex RFID device
const rfidDevice = rfidDeviceType.instantiate({
organizedBy: devicesFolder,
browseName: "RFID_Reader_1",
nodeId: "s=RFID_Reader_1"
});
// Example: update a variable exposed by the RFID device
const deviceStatus = rfidDevice.getComponentByName("DeviceState");
if (deviceStatus) {
deviceStatus.setValueFromSource({
dataType: opcua.DataType.Int32,
value: msg.deviceState || 0
});
}
Step 3 — Update values dynamically
Send new messages with msg.deviceState (and any other values you map) to update the RFID device values on the server.
Tips
- The RFID nodeset defines many optional components. Use
getComponentByName()to discover and set values you need. - If you don’t see the namespace URI, confirm that the XML nodeset path is correct.
- Keep your
nodeIdstable for client compatibility.
Further reading
For more tips and examples, see the Sterfive book node-opcua by example.