Informações do servidor
Visão geral
O nó padrão da OPC UA i=2256 (Server.ServerStatus) expõe informações de tempo de execução do servidor. O SDK não interpreta o ExtensionObject ServerStatusDataType; em vez disso, lê separadamente os 11 subnós padrão e então fornece o tipo forte ServerStatus (11 leituras de RPC no total: i=2257 / i=2258 / i=2259 / i=2261..i=2266 / i=2992 / i=2993).
Seção correspondente da norma: Parte 5, §6.3 (ServerStatusDataType).
API
| Método | Categoria | Leitura e escrita | Explicação |
|---|---|---|---|
| ua.read_server_status() | Diagnóstico | Ler | Retorna ServerStatus (11 visualizações RPC) |
| ua.read_server_state() | Diagnóstico | Ler | Retorna ServerState Enumeração (única RPC) |
| ua.read_current_time() | Diagnóstico | Ler | Retornar Optional[datetime] (UTC, único RPC; None em caso de falha na leitura) |
Assinatura autêntica:
def read_server_status(self) -> ServerStatus: ...
def read_server_state(self) -> ServerState: ...
def read_current_time(self) -> Optional[datetime]: ...
@dataclass(frozen=True)
class BuildInfo:
product_uri: Optional[str] = None
manufacturer_name: Optional[str] = None
product_name: Optional[str] = None
software_version: Optional[str] = None
build_number: Optional[str] = None
build_date: Optional[datetime] = None
@dataclass(frozen=True)
class ServerStatus:
start_time: Optional[datetime] = None # i=2257
current_time: Optional[datetime] = None # i=2258
state: ServerState = ServerState.UNKNOWN # i=2259
build: Optional[BuildInfo] = None # i=2261..i=2266 Montagem
seconds_till_shutdown: int = 0 # i=2992
shutdown_reason: Optional[str] = None # i=2993
class ServerState(IntEnum):
RUNNING = 0 # Normal
FAILED = 1 # Falha fatal
NO_CONFIGURATION = 2 # Falta configuração
SUSPENDED = 3 # Suspenso pela equipe de operações e manutenção
SHUTDOWN = 4 # Em processo de desativação
TEST = 5 # Modo de teste
COMMUNICATION_FAULT = 6 # Falha na conexão
UNKNOWN = 7 # SDK Tratamento de exceções (em caso de falha / valor fora do intervalo 0..6)
Se a leitura de qualquer subcampo falhar, será utilizado o valor padrão (None / 0 / UNKNOWN), sem que seja lançada uma exceção.
Exemplo de código
from darra_opcua import DarraOpcUa, ServerState # Nome do módulo darra_opcua
with DarraOpcUa("opc.tcp://localhost:4840") as ua:
ua.connect()
# 1) Descartável
status = ua.read_server_status()
print(f"Start = {status.start_time}")
print(f"Now = {status.current_time}")
print(f"State = {status.state.name}")
if status.build is not None:
print(f"Build = {status.build.product_name} {status.build.software_version}")
if status.state != ServerState.RUNNING:
print(f"服务端非 Running, 距关停 {status.seconds_till_shutdown}s")
# 2) Verificação de estado leve
if ua.read_server_state() == ServerState.RUNNING:
print("在线")
# 3) Desvio do relógio
server_now = ua.read_current_time()
if server_now is not None:
from datetime import datetime, timezone
drift = (datetime.now(timezone.utc) - server_now).total_seconds() * 1000
print(f"时钟差 = {drift:.1f} ms")
Comparação entre idiomas
| 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 |