File size: 8,062 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
[
  {
    "id": "tab_pump_opc",
    "type": "tab",
    "label": "泵站 OPC 采集与发布",
    "disabled": false,
    "info": "独立 OPC 采集层:读现场标准 PC 的 OPC UA 服务器(或本仓 app.opcua_sim_server 仿真服务器),\n整形为统一快照 → 发布 REST(/api/realtime)+WebSocket(/ws/realtime) 供 HTML 展示页消费。不依赖力控原生 OPC。\n默认跑仿真分支①;接真实/仿真 OPC 时启用分支③、禁用①的 inject。"
  },
  {
    "id": "ws_realtime",
    "type": "websocket-listener",
    "path": "/ws/realtime",
    "wholemsg": "false"
  },

  {
    "id": "cmt_sim",
    "type": "comment",
    "z": "tab_pump_opc",
    "name": "① 仿真数据分支(原型默认启用;接 OPC 后禁用此 inject)",
    "x": 240, "y": 60, "wires": []
  },
  {
    "id": "sim_tick",
    "type": "inject",
    "z": "tab_pump_opc",
    "name": "1s tick",
    "props": [{ "p": "payload" }],
    "repeat": "1", "crontab": "", "once": true, "onceDelay": "0.5",
    "topic": "", "payload": "", "payloadType": "date",
    "x": 120, "y": 110, "wires": [["sim_gen"]]
  },
  {
    "id": "sim_gen",
    "type": "function",
    "z": "tab_pump_opc",
    "name": "生成仿真快照",
    "func": "// 相似定律 Q∝f,H∝f²,P∝f³;海斯特尺度(单泵 ~850 m³/h)\nconst PUMP={Qbep:850,H0:18.5,k:5.5e-6,etaMax:0.78};\nfunction headAt(Q,f){const r=f/50,Qe=Q/r;return (PUMP.H0-PUMP.k*Qe*Qe)*r*r;}\nfunction effAt(Q,f){const r=f/50,Qb=PUMP.Qbep*r,sp=400*r;let e=PUMP.etaMax*(1-Math.pow((Q-Qb)/sp,2));return Math.max(0,e*(0.92+0.08*r));}\nfunction powerAt(Q,f){const H=headAt(Q,f),e=effAt(Q,f);if(e<0.05)return 0;return (1000*9.81*(Q/3600)*H)/(e*1000)/0.95;}\nconst t=Date.now()/1000;\n// 变频 1/2/5,工频 3/4;默认 1、2 在运\nconst base=[{id:1,vfd:true,freq:49,run:1},{id:2,vfd:true,freq:47.5,run:1},{id:3,vfd:false,freq:0,run:0},{id:4,vfd:false,freq:0,run:0},{id:5,vfd:true,freq:0,run:0}];\nconst pumps=base.map((b,i)=>{const jit=b.vfd?Math.sin(t/7+i)*1.0:0;const freq=b.run?Math.max(20,Math.min(50,b.freq+jit)):0;const r=freq/50;const flow=b.run?PUMP.Qbep*r*(1+0.04*Math.sin(t/5+i)):0;const head=b.run?headAt(flow,freq):0;const power=b.run?powerAt(flow,freq):0;const current=power>0?power*1000/(1.732*394*0.85):0;const sec=flow>0?power/flow:0;return{id:b.id,freq:(b.id===2&&b.run)?null:+freq.toFixed(2),current:+current.toFixed(1),power:+power.toFixed(1),status:b.run,flow:+flow.toFixed(1),head:+head.toFixed(2),runHours:4700+i*180,sec:+sec.toFixed(4)};});\nconst totalFlow=pumps.reduce((s,p)=>s+p.flow,0);const totalPower=pumps.reduce((s,p)=>s+p.power,0);const run=pumps.filter(p=>p.status===1);const head=run.length?run.reduce((s,p)=>s+p.head,0)/run.length:0;\nconst snap={station:'海斯特污水提升泵站',level:+(5.5+1.2*Math.sin(t/60)).toFixed(2),pressure:+(head/102).toFixed(3),head:+head.toFixed(2),totalFlow:+totalFlow.toFixed(1),totalPower:+totalPower.toFixed(1),sec:totalFlow>0?+(totalPower/totalFlow).toFixed(4):0,pumps,ts:Date.now()};\nflow.set('snapshot',snap);msg.payload=snap;return msg;",
    "outputs": 1, "noerr": 0,
    "x": 300, "y": 110, "wires": [["ws_out"]]
  },
  {
    "id": "ws_out",
    "type": "websocket out",
    "z": "tab_pump_opc",
    "name": "→ /ws/realtime",
    "server": "ws_realtime", "client": "",
    "x": 520, "y": 130, "wires": []
  },

  {
    "id": "cmt_rest",
    "type": "comment",
    "z": "tab_pump_opc",
    "name": "② REST 快照 GET /api/realtime(含 CORS)",
    "x": 210, "y": 190, "wires": []
  },
  {
    "id": "http_rt_in",
    "type": "http in",
    "z": "tab_pump_opc",
    "name": "", "url": "/api/realtime", "method": "get", "upload": false, "swaggerDoc": "",
    "x": 120, "y": 240, "wires": [["http_cors"]]
  },
  {
    "id": "http_cors",
    "type": "function",
    "z": "tab_pump_opc",
    "name": "取快照 + CORS 头",
    "func": "msg.payload = flow.get('snapshot') || { error:'no data yet' };\nmsg.headers = {\n  'Content-Type':'application/json',\n  'Access-Control-Allow-Origin':'*',\n  'Access-Control-Allow-Methods':'GET,OPTIONS',\n  'Access-Control-Allow-Headers':'Content-Type'\n};\nreturn msg;",
    "outputs": 1, "noerr": 0,
    "x": 320, "y": 240, "wires": [["http_rt_resp"]]
  },
  {
    "id": "http_rt_resp",
    "type": "http response",
    "z": "tab_pump_opc",
    "name": "", "statusCode": "200", "headers": {},
    "x": 520, "y": 240, "wires": []
  },

  {
    "id": "cmt_opc",
    "type": "comment",
    "z": "tab_pump_opc",
    "name": "③ OPC UA 采集分支(连标准PC/仿真服务器;启用后禁用①的 inject)",
    "info": "1) 安装:cd ~/.node-red && npm i node-red-contrib-opcua\n2) 双击 OpcUa-Endpoint 填端点:本仓仿真服务器 opc.tcp://<主机>:4840/biditech/server/,或现场标准 PC 的 OPC UA 服务器\n3) 用 UaExpert 确认命名空间索引 ns(仿真服务器默认 ns=2),改 opc_build 里的 NS\n4) 启用本分支各节点(右键 Enable),禁用①的 sim_tick\n点位命名对齐文档 4.1:ns=<idx>;s=PumpStation1.<...>",
    "x": 300, "y": 330, "wires": []
  },
  {
    "id": "opc_endpoint",
    "type": "OpcUa-Endpoint",
    "endpoint": "opc.tcp://127.0.0.1:4840/biditech/server/",
    "secpol": "None", "secmode": "None", "none": true, "login": false, "usercert": false
  },
  {
    "id": "opc_tick",
    "type": "inject",
    "z": "tab_pump_opc",
    "d": true,
    "name": "1s 读取",
    "props": [{ "p": "payload" }],
    "repeat": "1", "crontab": "", "once": false, "onceDelay": "1",
    "topic": "", "payload": "", "payloadType": "date",
    "x": 120, "y": 390, "wires": [["opc_build"]]
  },
  {
    "id": "opc_build",
    "type": "function",
    "z": "tab_pump_opc",
    "d": true,
    "name": "构建 NodeId 列表",
    "func": "// 用 UaExpert 确认命名空间索引;仿真服务器默认 ns=2\nconst NS = 2;\nconst S = n => `ns=${NS};s=PumpStation1.${n}`;\nconst names = ['Level','TotalFlow','TotalPower','Pressure'];\nfor (let i=1;i<=5;i++){ names.push(`Pump${i}.Freq`,`Pump${i}.Power`,`Pump${i}.Status`,`Pump${i}.Current`); }\nflow.set('opc_names', names);          // 供整形按序对应\nmsg.topic = 'readmultiple';\nmsg.payload = names.map(n => ({ nodeId: S(n) }));\nreturn msg;",
    "outputs": 1, "noerr": 0,
    "x": 320, "y": 390, "wires": [["opc_client"]]
  },
  {
    "id": "opc_client",
    "type": "OpcUa-Client",
    "z": "tab_pump_opc",
    "d": true,
    "endpoint": "opc_endpoint",
    "action": "readmultiple",
    "deadbandtype": "a", "deadbandvalue": 1, "time": 1, "timeUnit": "s",
    "certificate": "n", "localfile": "", "localkeyfile": "",
    "securitymode": "None", "securitypolicy": "None",
    "name": "OPC UA Client (readmultiple)",
    "x": 560, "y": 390, "wires": [["opc_shape"]]
  },
  {
    "id": "opc_shape",
    "type": "function",
    "z": "tab_pump_opc",
    "d": true,
    "name": "OPC→统一快照",
    "func": "// readmultiple 结果与请求同序;每项可能是原值或 {value:..}\nconst names = flow.get('opc_names') || [];\nconst arr = Array.isArray(msg.payload) ? msg.payload : [];\nconst val = x => {\n  if (x && typeof x === 'object' && x.statusCode !== undefined) {\n    const quality = String(x.statusCode);\n    if (!quality.includes('Good') && quality !== '0') return null;\n  }\n  return (x && typeof x==='object' && 'value' in x) ? x.value : x;\n};\nconst snap = { station:'海斯特污水提升泵站', pumps:[] };\narr.forEach((it, idx) => {\n  const nm = names[idx]; if(!nm) return; const v = val(it);\n  const m = nm.match(/Pump(\\d+)\\.(\\w+)/);\n  if (m){ const j=+m[1]-1; snap.pumps[j]=snap.pumps[j]||{id:+m[1]}; snap.pumps[j][m[2].toLowerCase()]=v; }\n  else if (nm==='Level') snap.level=v;\n  else if (nm==='TotalFlow') snap.totalFlow=v;\n  else if (nm==='TotalPower') snap.totalPower=v;\n  else if (nm==='Pressure') snap.pressure=v;\n});\nsnap.ts = Date.now();\nflow.set('snapshot', snap);\nmsg.payload = snap;\nreturn msg;   // → ws_out 实时推送;REST 复用 flow.snapshot",
    "outputs": 1, "noerr": 0,
    "x": 800, "y": 390, "wires": [["ws_out"]]
  }
]