跳到主要内容

服务端信息

概述

OPC UA 标准节点 i=2256 (Server.ServerStatus) 暴露服务端运行时信息。SDK 把 11 个子节点 (i=2257..i=2266 + i=2992 + i=2993) 一次装配为强类型 ServerStatus

对应规范段: Part 5 §6.3 (ServerStatusDataType)。

API

方法类别读写说明
ua.read_server_status()诊断返回完整 ServerStatus
ua.read_server_state()诊断返回 ServerState 枚举
ua.read_current_time()诊断返回 datetime(UTC)

相关结构:

@dataclass(frozen=True)
class ServerStatus:
start_time: datetime # 启动 UTC
current_time: datetime # 当前 UTC
state: ServerState # 当前状态
build_info: BuildInfo # 产品/版本信息
seconds_till_shutdown: int # 距关停秒数
shutdown_reason: str # 关停原因

class ServerState(IntEnum):
RUNNING = 0 # 正常
FAILED = 1 # 致命故障
NO_CONFIGURATION = 2 # 缺配置
SUSPENDED = 3 # 被运维挂起
SHUTDOWN = 4 # 关停中
TEST = 5 # 测试模式
COMMUNICATION_FAULT = 6 # 链路故障
UNKNOWN = 7 # SDK 兜底

代码示例

from opcua import DarraOpcUa, ServerState

with DarraOpcUa("opc.tcp://localhost:4840") as ua:
ua.connect()

# 1) 一次性
status = ua.read_server_status()
print(f"Start = {status.start_time}")
print(f"Now = {status.current_time}")
print(f"State = {status.state}")
print(f"Build = {status.build_info.product_name} {status.build_info.software_version}")

if status.state != ServerState.RUNNING:
print(f"⚠ 服务端非 Running, 倒计时 {status.seconds_till_shutdown}s")

# 2) 轻量心跳
if ua.read_server_state() == ServerState.RUNNING:
print("在线")

# 3) 时钟漂移
server_now = ua.read_current_time()
if server_now:
from datetime import datetime, timezone
drift = (datetime.now(timezone.utc) - server_now).total_seconds() * 1000
print(f"时钟差 = {drift:.1f} ms")

跨语言对照

C#PythonJavaC++RustC
ReadServerStatusread_server_statusreadServerStatusReadServerStatusread_server_statusDarraUa_Session_ReadServerStatus
ReadServerStateread_server_statereadServerStateReadServerStateread_server_stateDarraUa_Session_ReadServerState

下一步