1

我遇到了 node-opcua 库的问题。我们使用该库来设置在 Kepware Server (v6) 上匿名连接的客户端。

应用程序在各种服务器上的调试和生产中运行良好,但似乎无法在我们尝试安装它的特定服务器上创建会话。

让我认为这是一个互操作性问题的原因是,当我们第一次在服务器上安装应用程序时,它使另一个应用程序(Cimplicity)导致 Kepware 服务器崩溃。

该应用程序是使用 node-opcua 最新的电子应用程序。“有问题的服务器”是带有 Microsoft Server 2019 的 VM。

这是我的 opcua 服务代码

const opcua = require("node-opcua");
const path = require("path");
let conf = global.conf;
const { ipcMain } = require("electron");
const log = require("electron-log");

const options = {
  applicationName: "Ganex OPC UA Server",
  connectionStrategy: opcua.connectionStrategy,
  securityMode: opcua.MessageSecurityMode.None,
  securityPolicy: opcua.SecurityPolicy.None,
  endpoint_must_exist: false,
};

class OPCUAService {
  eventEmitter;

  static setEventEmitter(eventEmitterObj) {
    this.eventEmitter = eventEmitterObj;

    this.eventEmitter.on("updatedAuthentication", () => {
      // config = require("../config/configUrl");//in theory should get the new config from file if its updated
      console.log("Updated Conf?", conf.get("auth:opcUrl"));
    });
  }

  static async readData(addressArray) {
    const client = opcua.OPCUAClient.create();
    const endpointUrl = conf.get("auth:opcUrl");
    const opcUserName = conf.get("auth:opcServerUserName");
    const opcPassword = conf.get("auth:opcServerPassword");

    client.on("backoff", () => {
      console.log("backoff");
      globalThis.connected = false;
      log.info("Error connecting to OPC Server", "Can't access the OPC Server");
    });

    client.on("connected", () => (globalThis.connected = true));

    if (globalThis.connected) {
      log.info("OPC Server is accessible? ", globalThis.connected);
    }

    try {
      console.log("Start!");
      log.info("Connecting to OPC");
      //var userIdentityInfo = new usr
      await client.connect(endpointUrl);
      //const session = await client.createSession({});
      //const session = await client.createSession({userName: opcUserName, password: opcPassword});
      /* const session = await client.createSession({}, (err) => {
        if (err) {
          log.info("Error at session creation", err);
        } else {
          log.info("Session successfully created", err);
        }
      }); */

      const session = await client.createSession({});
      log.info("Session created? ", session? "true" : "false");
      log.info ("Session Content", addressArray);
      console.log("Session Content", addressArray);

      for (let count = 0; count < addressArray.length; count++) {
        const readResult = await session.read({
          nodeId: addressArray[count].address,
          attributeId: opcua.AttributeIds.Value,
        });
        addressArray[count].value = parseFloat(
          readResult.value.toString().replace(/[^\d.-]/g, "")
        ).toFixed(addressArray[count].precision);
      }

      await session.close();
      log.info("Disconnected from OPC");

      await client.disconnect();

      return addressArray;
    } catch (err) {
      log.info("Error connecting to OPC Server", err);
      log.error(err.toString());
      console.log("Err =", err);
    }
  }
}

module.exports = OPCUAService;

似乎问题出在会话创建级别。这是来自工作服务器的日志摘录:

[2021-12-20 08:03:00.004] [info] @@@ running processSolarFacilityMetData Task every 1 minute(s)
[2021-12-20 08:03:00.111] [info] OPC Server is accessible?  true
[2021-12-20 08:03:00.144] [info] Connecting to OPC
[2021-12-20 08:03:00.174] [info] @@@ running processPowerData Task every 1 minute(s)
[2021-12-20 08:03:00.191] [info] OPC Server is accessible?  true
[2021-12-20 08:03:00.236] [info] Connecting to OPC
[2021-12-20 08:03:00.605] [info] Session created?  true
[2021-12-20 08:03:00.608] [info] Session Content [object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
[2021-12-20 08:03:00.627] [info] Session created?  true
[2021-12-20 08:03:00.637] [info] Session Content [object Object],[object Object]
[2021-12-20 08:03:00.765] [info] Disconnected from OPC
[2021-12-20 08:03:01.381] [info] Disconnected from OPC

一个来自有问题的服务器

Starting up application @ C:\Program Files\Forecast Compliance...
[2021-12-20 06:31:12.280] [info]
[2021-12-20 06:31:14.814] [info] solarFacilityData: undefined
[2021-12-20 06:32:00.001] [info] @@@ running processPowerData Task every 1 minute(s)
[2021-12-20 06:32:00.011] [info] Connecting to OPC
[2021-12-20 06:32:00.020] [info] @@@ running processSolarFacilityMetData Task every 1 minute(s)
[2021-12-20 06:32:00.022] [info] Connecting to OPC
[2021-12-20 06:33:00.006] [info] @@@ running processPowerData Task every 1 minute(s)
[2021-12-20 06:33:00.013] [info] Connecting to OPC
[2021-12-20 06:33:00.017] [info] @@@ running processSolarFacilityMetData Task every 1 minute(s)
[2021-12-20 06:33:00.019] [info] Connecting to OPC
[2021-12-20 06:34:00.002] [info] @@@ running processSolarFacilityMetData Task every 1 minute(s)
[2021-12-20 06:34:00.003] [info] Connecting to OPC
[2021-12-20 06:34:00.005] [info] @@@ running processPowerData Task every 1 minute(s)

有没有人知道如何解决这类问题?

提前致谢!

4

1 回答 1

0

看起来连接方法没有返回

await client.connect(endpointUrl);

在服务器无法访问的情况下,我们希望定期引发退避事件,以指示连接函数正在重试失败的连接。我们没有这个。

此外,您有一个 try/catch 块并且没有错误消息,这让我认为套接字或服务器端正在发生某些事情。

这种情况需要线下进行更深入的调查。我建议联系 node-opcua 支持团队(有关更多信息,请参阅网站)。

免责声明,我是 node-opcua 库的作者。

于 2021-12-20T17:52:09.467 回答