Spaces:
Sleeping
Sleeping
File size: 6,177 Bytes
aec7019 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 | """OPC UA 仿真服务器(测试接口)。
现场数据已汇聚到一台标准 PC 的 OPC UA 服务器上,我方软件作 UA 客户端去读。
本模块提供一个**可运行的 OPC UA 仿真服务器**,把泵站点位模拟出来(含实时变化的
液位/各泵频率电流功率状态/总流量,以及可写的频率设定/写使能/心跳),
供 UaExpert / Node-RED / 我方客户端连它联调,无需真实现场。
运行:
python -m app.opcua_sim_server # 默认 opc.tcp://0.0.0.0:4840/biditech/
python -m app.opcua_sim_server --port 4841
点位命名对齐文档 4.1:ns=<idx>;s=PumpStation1.Pump<i>.<Freq|Current|Power|Status>
"""
from __future__ import annotations
import argparse
import asyncio
import math
import time
from asyncua import Server, ua
from .models.curve_fit import power_from_current
from .plant import Plant
NS_URI = "http://biditech.pump/opcua/"
async def run(endpoint: str, interval: float = 1.0):
plant = Plant()
st = plant.station
line_v = st.get("line_voltage", 394.0)
level = st.get("level_target", 5.5)
area = st.get("wet_well_area", 57.2)
ids = list(plant.pumps.keys())
server = Server()
await server.init()
server.set_endpoint(endpoint)
server.set_server_name("Biditech Pump Station OPC UA Sim")
idx = await server.register_namespace(NS_URI)
objs = server.nodes.objects
station = await objs.add_object(ua.NodeId("PumpStation1", idx), "PumpStation1")
def sid(name):
return ua.NodeId(name, idx)
# 站级只读点
n_level = await station.add_variable(sid("PumpStation1.Level"), "Level", float(level))
n_flow = await station.add_variable(sid("PumpStation1.TotalFlow"), "TotalFlow", 0.0)
n_power = await station.add_variable(sid("PumpStation1.TotalPower"), "TotalPower", 0.0)
n_pressure = await station.add_variable(sid("PumpStation1.Pressure"), "Pressure", 0.0)
# 站级可写点(回写/联锁)
n_wen = await station.add_variable(sid("PumpStation1.WriteEnable"), "WriteEnable", False)
n_hb = await station.add_variable(sid("PumpStation1.Heartbeat"), "Heartbeat", 0)
await n_wen.set_writable()
await n_hb.set_writable()
# 各泵点位
pumps = {}
for pid in ids:
p = await station.add_object(sid(f"PumpStation1.Pump{pid}"), f"Pump{pid}")
vs = {
"Freq": await p.add_variable(sid(f"PumpStation1.Pump{pid}.Freq"), "Freq", 0.0),
"Current": await p.add_variable(sid(f"PumpStation1.Pump{pid}.Current"), "Current", 0.0),
"Power": await p.add_variable(sid(f"PumpStation1.Pump{pid}.Power"), "Power", 0.0),
"Status": await p.add_variable(sid(f"PumpStation1.Pump{pid}.Status"), "Status", 0),
"RunHours": await p.add_variable(sid(f"PumpStation1.Pump{pid}.RunHours"), "RunHours", 4700.0 + pid * 180),
# 可写:频率设定 + 启停指令
"FreqSet": await p.add_variable(sid(f"PumpStation1.Pump{pid}.FreqSet"), "FreqSet", 0.0),
"Cmd": await p.add_variable(sid(f"PumpStation1.Pump{pid}.Cmd"), "Cmd", 0),
}
await vs["FreqSet"].set_writable()
await vs["Cmd"].set_writable()
pumps[pid] = vs
# 初始运行:前两台变频泵
var_ids = [pid for pid in ids if plant.variable_speed.get(pid, True)]
running = set(var_ids[:2])
print(f"[opcua-sim] endpoint: {endpoint}")
print(f"[opcua-sim] namespace ns={idx} ({NS_URI})")
print(f"[opcua-sim] pumps: {ids} 初始在运: {sorted(running)}")
print("[opcua-sim] 用 UaExpert / Node-RED 连接以上端点浏览 PumpStation1.*")
async with server:
t0 = time.time()
while True:
t = time.time() - t0
active = {}
for pid in running:
base = 49.0 if plant.variable_speed.get(pid, True) else 50.0
jit = math.sin(t / 7 + pid) * 1.0 if plant.variable_speed.get(pid, True) else 0.0
active[pid] = max(20.0, min(50.0, base + jit))
op = plant.group.solve(active, level=level)
total_flow = op.Q_total
total_power = op.P_total
for pid in ids:
vs = pumps[pid]
if pid in running:
info = op.per_pump.get(pid, {})
q = info.get("Q", 0.0)
pw = info.get("P", 0.0)
cur = power_from_current # noqa: reuse name
amps = (pw * 1000.0) / (math.sqrt(3) * line_v * 0.85) if pw > 0 else 0.0
await vs["Freq"].write_value(round(active[pid], 2))
await vs["Power"].write_value(round(pw, 2))
await vs["Current"].write_value(round(amps, 1))
await vs["Status"].write_value(1)
else:
await vs["Freq"].write_value(0.0)
await vs["Power"].write_value(0.0)
await vs["Current"].write_value(0.0)
await vs["Status"].write_value(0)
# 液位物料平衡:入流波动,出流=总抽升
q_in = 1400.0 + 500.0 * math.sin(t / 60.0)
level += (q_in - total_flow) / 3600.0 * interval / area
level = max(3.0, min(9.0, level))
await n_level.write_value(round(level, 3))
await n_flow.write_value(round(total_flow, 1))
await n_power.write_value(round(total_power, 1))
await n_pressure.write_value(round(op.H / 102.0, 4)) # m→MPa 近似
await asyncio.sleep(interval)
def main():
ap = argparse.ArgumentParser(description="Biditech 泵站 OPC UA 仿真服务器")
ap.add_argument("--host", default="0.0.0.0")
ap.add_argument("--port", type=int, default=4840)
ap.add_argument("--path", default="/biditech/server/")
ap.add_argument("--interval", type=float, default=1.0)
args = ap.parse_args()
endpoint = f"opc.tcp://{args.host}:{args.port}{args.path}"
try:
asyncio.run(run(endpoint, args.interval))
except KeyboardInterrupt:
print("\n[opcua-sim] stopped")
if __name__ == "__main__":
main()
|