Session 会话 — OpcUaSession
OpcUaSession 是 Client 主类, 一个实例对应一个 OPC UA Session. 所有访问 (read / write / browse / subscribe / call / history) 都从这个对象出发.
DarraOpcUa是OpcUaSession的兼容别名 (DarraOpcUa = OpcUaSession), 二者等价.
子页跳转
- 构造与连接参数请参考 构造函数.
- 连接 / 断开请参考 连接管理.
- 单点 / 批量读写请参考 read / write.
- 内部事件统一通道请参考 Events.
- 命名空间反查请参考 Namespaces.
- 心跳与会话保活请参考 KeepAlive.
公共属性
| 类别 | 属性 | 类型 | 访问 | 说明 |
|---|---|---|---|---|
| 连接 | endpoint_url | str | R | 构造时指定的 endpoint URL |
security_mode | MessageSecurityMode | R | 安全模式 (NONE / Sign / SignAndEncrypt) | |
| 状态 | state | SessionState | R | 当前会话状态 (Disconnected / Connecting / Connected / Reconnecting / Closing / Failed) |
is_connected | bool | R | 是否已连接 (= state == SessionState.Connected) | |
handle | int | R | C 层 native session handle (internal) | |
| 子对象 | nodes | NodeCollection | R | Lazy Load 节点访问器 |
namespaces | NamespaceCollection | R | 命名空间 URI ↔ Index 互转 | |
| 事件 | events | OpcUaEvents | R | 会话级事件统一通道 (connected / disconnected / data_change / server_event / ...) |
| 配置 | keep_alive_interval_ms | int | RW | KeepAlive 心跳周期 (默认 10000 ms, 0 = 禁用) |
auto_publish | bool | RW | 启停后台 Publish 线程 (默认 True, 由 connect() 自动开) |
公共方法
连接管理
| 方法 | 说明 |
|---|---|
connect() | 建立连接 (Hello → SecureChannel → Session → Activate) |
disconnect() | 主动断开, 实例可重连 |
close() / dispose() | 释放资源, 自动 disconnect (with 自动调用) |
心跳与发布
| 方法 | 说明 |
|---|---|
publish(timeout_ms = 2000) | 手动触发一次 Publish (一般无需调用) |
读 / 写
| 方法 | 说明 |
|---|---|
read(node_id, attribute = AttributeId.Value) | 读单节点指定 Attribute |
read_many(node_ids, attribute) | 批量读 (单 Attribute) |
write(node_id, value, attribute = AttributeId.Value) | 写单节点 |
浏览
| 方法 | 说明 |
|---|---|
browse(node_id, filter = NodeClass.Unspecified) | 单层浏览 |
browse_many(node_ids, filter) | 批量浏览 |
browse_with_paging(node_id, filter) | 含 ContinuationPoint 翻页支持 |
browse_next(continuation_point, release = False) | 续翻分页 |
translate_browse_paths(paths) | 路径批量解析为 NodeId |
resolve(path, start_node_id = None) | 单条路径解析 |
register_nodes(node_ids) | 注册临时高效 NodeId |
unregister_nodes(registered_node_ids) | 注销临时 NodeId |
订阅
| 方法 | 说明 |
|---|---|
create_subscription(publishing_interval_ms = 500.0) | 创建数据订阅 |
subscribe_events(node_id = None, existing_subscription = None) | 订阅事件 (报警 / 条件) |
transfer_subscriptions(subscription_ids, send_initial_values = True) | 跨 Session 迁移订阅 |
方法调用
| 方法 | 说明 |
|---|---|
call(object_node_id, method_node_id, inputs = None) | 调用 OPC UA Method 节点 |
历史
| 方法 | 说明 |
|---|---|
read_history(node_id, start_utc, end_utc, max_values = 0) | 读原始历史 (简化入口) |
read_history_modified(...) | 读含修改记录的历史 |
read_history_at_time(...) | 读指定时间点的内插值 |
read_history_processed(...) | 聚合 (Avg / Min / Max / TimeAverage / ...) |
read_history_events(...) | 读历史事件 |
update_history(...) | Insert / Replace / Update 历史 |
delete_history_range(...) | 按时间段删除历史 |
控制
| 方法 | 说明 |
|---|---|
cancel(request_handle) | 取消未完成请求 |
完整示例
from darra_opcua import OpcUaSession, OpcUaVariant
import time
with OpcUaSession("opc.tcp://localhost:4840") as ua:
ua.events.connected.append(
lambda e: print(f"Connected to {e.endpoint_url}"))
ua.events.disconnected.append(
lambda e: print(f"Disconnected: {e.message}"))
ua.connect()
# 读
with ua.read("ns=2;s=Temperature") as dv:
print(f"T = {dv.value} °C")
# 写
st = ua.write("ns=2;s=Setpoint", OpcUaVariant().set_double(42.0))
print(f"Write status = {st.name}")
# 订阅
with ua.create_subscription(500) as sub:
sub.data_changed.append(
lambda e: print(f"{e.node_id} = {e.value_string} ({e.status.name})"))
sub.add_node("ns=2;s=Counter")
# 方法
outputs = ua.call(
"ns=2;s=Calc", "ns=2;s=Calc.Add",
[OpcUaVariant().set_int32(3), OpcUaVariant().set_int32(4)])
for v in outputs:
with v:
print(f"sum = {v.as_int32}")
time.sleep(60)