服务端信息
概述
OPC UA 标准节点 i=2256 (Server.ServerStatus) 暴露服务端运行时信息。SDK 把 11 个子节点 (i=2257..i=2266 + i=2992 + i=2993) 一次装配为强类型 ServerStatus。
对应规范段: Part 5 §6.3。
API
| 方法 | 类别 | 读写 | 说明 |
|---|---|---|---|
| ua.ReadServerStatus() | 诊断 | 读 | 返回完整 ServerStatus |
| ua.ReadServerState() | 诊断 | 读 | 返回 ServerState 枚举 |
| ua.ReadCurrentTime() | 诊断 | 读 | 返回 std::optional<system_clock::time_point> |
相关结构 (ServerStatus 字段):
- start_time (time_point) — 启动 UTC
- current_time (time_point) — 当前 UTC
- state (ServerState) — 当前状态
- build_info (BuildInfo) — 产品/版本
- seconds_till_shutdown (uint32_t) — 距关停秒数
- shutdown_reason (std::string) — 关停原因
相关结构 (ServerState 枚举):
- Running (0) — 正常
- Failed (1) — 致命故障
- NoConfiguration (2) — 缺配置
- Suspended (3) — 被运维挂起
- Shutdown (4) — 关停中
- Test (5) — 测试模式
- CommunicationFault (6) — 链路故障
- Unknown (7) — SDK 兜底
代码示例
#include <darra/opcua.h>
using namespace darra::opcua;
DarraOpcUa ua("opc.tcp://localhost:4840");
ua.Connect();
// 1) 一次性
auto status = ua.ReadServerStatus();
std::cout << "Start = " << status.start_time << "\n";
std::cout << "State = " << status.state << "\n";
std::cout << "Build = " << status.build_info.product_name << " "
<< status.build_info.software_version << "\n";
if (status.state != ServerState::Running)
std::cout << "⚠ 非 Running, 倒计时 " << status.seconds_till_shutdown << "s\n";
// 2) 轻量心跳
if (ua.ReadServerState() == ServerState::Running)
statusBar->setText("在线");
// 3) 时钟漂移
auto server_now = ua.ReadCurrentTime();
if (server_now) {
auto drift = std::chrono::system_clock::now() - *server_now;
std::cout << "时钟差 = " << std::chrono::duration_cast<std::chrono::milliseconds>(drift).count() << " ms\n";
}
跨语言对照
| C# | Python | Java | C++ | Rust | C |
|---|---|---|---|---|---|
| ReadServerStatus | read_server_status | readServerStatus | ReadServerStatus | read_server_status | DarraUa_Session_ReadServerStatus |
| ReadServerState | read_server_state | readServerState | ReadServerState | read_server_state | DarraUa_Session_ReadServerState |