javascriptplcopc-uacodesysnode-opcua

Node-OPCUA Issue: 'Acknowledge Method Not Found' Error with Alarm Acknowledgment Functions


I am working with Node-OPCUA as an OPC UA client to interact with a Codesys PLC. I'm using node-opcua version 2.115.0 on an ASUS ZenBook, running Windows 11 Pro, version 10.0.22631 Build 22631. My goal is to acknowledge and confirm alarms from my OPC UA server.

• Current Problem:

I successfully connect to my local Codesys PLC, can see and modify values, and view alarms and their current status. However, when trying to use the acknowledgeCondition or confirmCondition functions to acknowledge an alarm, I encounter an error stating: "Error: cannot find Acknowledge Method". This is perplexing as I am passing the same values ('ConditionId' and 'EventId') that EventMonitor.on() provides me.

Expected Behavior:

The alarms should be acknowledged and reflected in my Codesys PLC. With UAExpert, I can see my alarms and acknowledge and confirm them without issues, leading me to believe the error lies within node-opcua.

Code:

import express from "express";
import { createServer } from 'node:http';
import { AttributeIds, OPCUAClient, TimestampsToReturn, constructEventFilter, ObjectIds } from "node-opcua";
                                                                                            
const app = express();
const server = createServer(app);

const URL = "opc.tcp://ADRIAN-ASUS:4840";

(async () => {
    try {
        const client = OPCUAClient.create();
        client.on("backoff", (retry, delay) => {
            console.log("Retying to connect to ", URL, " attempt ", retry);
        });
   
        console.log("connecting to ", URL);
        await client.connect(URL);
        console.log("connected to ", URL);

        const session = await client.createSession();
        console.log("session initialized");

        const subscripcion = await session.createSubscription2({
            requestedPublishingInterval: 50,
            requestedMaxKeepAliveCount: 20,
            publishingEnabled: true,
        });

        const fields = [
            "EventId",
            "AckedState",
            "AckedState.Id",
            "ConfirmedState",
            "ConfirmedState.Id",
            "ConditionId",
        ];

        const eventFilter = constructEventFilter(fields);

        const itemToMonitor = {
            nodeId: ObjectIds.Server,
            attributeId: AttributeIds.EventNotifier,
        };

        const parameters = {
            filter: eventFilter,
            discardOldest: true,
            queueSize: 100,
        };

        const EventMonitor = await subscripcion.monitor(
            itemToMonitor,
            parameters,
            TimestampsToReturn.Both
        )

        const alarmData = {
        }

        EventMonitor.on("changed", (events) => {
            for (let i = 0; i < events.length; i++) {
                alarmData[fields[i]] = events[i].value
            }
        });

        setTimeout( async () => {
            try {
                const comment = 'Comment random';

                console.log({conditionId: alarmData.ConditionId, eventId: alarmData.EventId})

                session.acknowledgeCondition(alarmData.ConditionId, alarmData.EventId, comment, (err) => {
                    console.log({ err })
                }) 
            } 
            catch (error) {
                console.log(error)
            }

        }, 5000)



        let running = true;
        process.on("SIGINT", async () => {
            if (!running) {
                return; // avoid calling shutdown twice
            }
            console.log("shutting down client");
            running = false;
            await subscripcion.terminate();
            await session.close();
            await client.disconnect();
            console.log("Done");
            process.exit(0);
        });
    } 
    catch (error) {
        console.log("ERROR: ", error.message);
        console.log(error);
    }

    server.listen(4000, () => {
        console.log('server running at http://localhost:4000');
    })
})()

• Errors and Logs:

Error when attempting to acknowledge the alarm: Error Log

Output of console.log showing the 'conditionId' and 'eventId' values: Console.log conditionId and eventId data

.Gif where I recognize an alarm using UAExpert: UAExpert recognizing the alarm

Additional Information:

• I have installed node-opcua as a package using npm. • Using Node 20.10.0 • I am attempting to connect to an OPCUA system: CODESYS Control Win V3 x64, version 3.5.19.3 (x64).

-- UPDATE --

While debugging the code, I observed that in the 'result' parameter of the callback, which is passed as an argument to this.translateBrowsePath, the StatusCode is:

statusCode: BadNodeIdUnknown (0x80340000) {
     _description: 'The node id refers to a node that does not exist in the server address space.',
     _name: 'BadNodeIdUnknown',
     _value: 2150891520,
}

Error BadNodeIdUnknown

Try downloading all the previous versions of node.


Solution

  • I successfully acknowledged the alarm by employing a different method, which I discovered in a node-opcua issue discussion: How to acknowledge alarm using Cogent DataHub #1317

    However, the standard or 'correct' method for event acknowledgment proved entirely ineffective in my case. Consequently, it's fair to conclude that this issue remains unresolved.

    This situation is particularly puzzling since I've been providing precisely the same parameters that worked with session.acknowledgeCondition.

    Here's how the code currently stands: image

    Additionally, I am eager to share the complete code for the benefit of anyone looking to implement a similar solution.

    import express from "express";
    import { createServer } from 'node:http'
    import { AttributeIds, OPCUAClient, TimestampsToReturn, constructEventFilter, ObjectIds, callConditionRefresh, MethodIds, Variant, DataType } from "node-opcua-client";
    
    const app = express();
    const server = createServer(app);
    
    const URL = "opc.tcp://ADRIAN-ASUS:4840";
    
    (async () => {
        try {
            const client = OPCUAClient.create();
    
            client.on("backoff", (retry, delay) => {
                console.log("Retying to connect to ", URL, " attempt ", retry);
            });
    
            console.log("connecting to ", URL);
            await client.connect(URL);
            console.log("connected to ", URL);
    
            const session = await client.createSession();
            console.log("session initialized");
    
            const subscripcion = await session.createSubscription2({
                requestedPublishingInterval: 50,
                requestedMaxKeepAliveCount: 20,
                publishingEnabled: true,
            });
    
            const fields = [
                "EventId",
                "AckedState",
                "AckedState.Id",
                "ConfirmedState",
                "ConfirmedState.Id",
                "ConditionId",
            ];
    
            const eventFilter = constructEventFilter(fields);
    
            const itemToMonitor = {
                nodeId: ObjectIds.Server,
                attributeId: AttributeIds.EventNotifier,
            };
    
            const parameters = {
                filter: eventFilter,
                discardOldest: true,
                queueSize: 100,
            };
    
    
            const EventMonitor = await subscripcion.monitor(
                itemToMonitor,
                parameters,
                TimestampsToReturn.Both
            )
    
            const alarmData = {
            }
    
            callConditionRefresh(subscripcion) // Give us all events!!!
    
            EventMonitor.on("changed", (events) => {
                for (let i = 0; i < events.length; i++) {
                    alarmData[fields[i]] = events[i].value
                }
                console.log(alarmData)
            });
    
            setTimeout(async () => {
                try {
    
                    /* session.acknowledgeCondition(alarmData.ConditionId, alarmData.EventId, comment, (err) => {
                        console.log({ err })
                    }) */
    
                    const methodToCall = {
                        objectId: alarmData.ConditionId,
                        methodId: MethodIds.AcknowledgeableConditionType_Acknowledge, // TODO: Replace to MethodIds
                        inputArguments: [
                            new Variant({ dataType: DataType.ByteString, value: alarmData.EventId }),
                            new Variant({ dataType: DataType.LocalizedText, value: "test comment!" })
                        ]
                    }
    
                    session.call(methodToCall, (err, result) => {
                        if (err) return console.log(err); // Currently not important
                        console.log("Method call result: " + JSON.stringify(result, null, 2));
                    })
                } catch (error) {
                    console.log(error)
                }
    
            }, 5000)
    
    
    
            let running = true;
            process.on("SIGINT", async () => {
                if (!running) {
                    return; // avoid calling shutdown twice
                }
                console.log("shutting down client");
                running = false;
                await subscripcion.terminate();
                await session.close();
                await client.disconnect();
                console.log("Done");
                process.exit(0);
            });
        } catch (error) {
            console.log("ERROR: ", error.message);
            console.log(error);
        }
    
        server.listen(4000, () => {
            console.log('server running at http://localhost:4000');
        });
    })();