Spaces:
Sleeping
Sleeping
File size: 12,287 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 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 | [
{
"id": "tab_integration_v2",
"type": "tab",
"label": "泵站数据接入(配置中心)",
"disabled": false,
"info": "由后台配置中心驱动。Node-RED 只负责 OPC UA 连接、采集、数据标准化和实时发布。"
},
{
"id": "cmt_cfg_v2",
"type": "comment",
"z": "tab_integration_v2",
"name": "① 每 10 秒读取后台接入配置(保存后自动生效)",
"info": "配置来源:FastAPI /api/integration/runtime。密码仅在设置内部令牌后向 Node-RED 返回。",
"x": 300,
"y": 40,
"wires": []
},
{
"id": "cfg_tick_v2",
"type": "inject",
"z": "tab_integration_v2",
"name": "加载/刷新配置",
"props": [
{
"p": "payload"
}
],
"repeat": "10",
"crontab": "",
"once": true,
"onceDelay": "1",
"topic": "",
"payload": "",
"payloadType": "date",
"x": 130,
"y": 90,
"wires": [
[
"cfg_headers_v2"
]
]
},
{
"id": "cfg_headers_v2",
"type": "function",
"z": "tab_integration_v2",
"name": "附加内部令牌",
"func": "const token = env.get('INTEGRATION_INTERNAL_TOKEN') || '';\nmsg.headers = {'X-Integration-Token': token};\nreturn msg;",
"outputs": 1,
"noerr": 0,
"initialize": "",
"finalize": "",
"libs": [],
"x": 330,
"y": 90,
"wires": [
[
"cfg_http_v2"
]
]
},
{
"id": "cfg_http_v2",
"type": "http request",
"z": "tab_integration_v2",
"name": "读取后台配置",
"method": "GET",
"ret": "obj",
"paytoqs": "ignore",
"url": "http://backend:8077/api/integration/runtime",
"tls": "",
"persist": false,
"proxy": "",
"insecureHTTPParser": false,
"authType": "",
"senderr": false,
"headers": [],
"x": 530,
"y": 90,
"wires": [
[
"cfg_apply_v2"
]
]
},
{
"id": "cfg_apply_v2",
"type": "function",
"z": "tab_integration_v2",
"name": "应用配置并切换端点",
"func": "let cfg = msg.payload;\nif (typeof cfg === 'string') { try { cfg = JSON.parse(cfg); } catch (e) { node.error('配置不是有效 JSON'); return null; } }\nif (!cfg || !cfg.connection || !Array.isArray(cfg.tags)) {\n node.status({fill:'red',shape:'ring',text:'配置不可用'});\n node.error('后台接入配置不可用:' + JSON.stringify(cfg));\n return null;\n}\nconst previousRevision = flow.get('integration_revision');\nflow.set('integration_config', cfg);\nflow.set('integration_revision', cfg.revision);\nconst readTags = cfg.tags.filter(t => t.enabled && t.node_id && !t.writable);\nflow.set('opc_read_tags', readTags);\nflow.set('source_mode', cfg.source_mode);\nnode.status({fill: cfg.source_mode === 'opcua' ? 'green' : 'yellow', shape:'dot', text:(cfg.source_mode === 'opcua' ? 'OPC · ' : '仿真 · ') + 'r' + cfg.revision});\nif (cfg.source_mode !== 'opcua' || previousRevision === cfg.revision) return null;\nconst c = cfg.connection;\nmsg.action = 'reconnect';\nmsg.topic = 'clearitems';\nmsg.payload = '';\nmsg.OpcUaEndpoint = {\n endpoint: c.endpoint,\n securityPolicy: c.security_policy || 'None',\n securityMode: c.security_mode || 'None',\n login: !!c.username,\n user: c.username || '',\n password: c.password || ''\n};\nreturn msg;",
"outputs": 1,
"noerr": 0,
"initialize": "",
"finalize": "",
"libs": [],
"x": 760,
"y": 90,
"wires": [
[
"opc_client_v2"
]
]
},
{
"id": "cmt_opc_v2",
"type": "comment",
"z": "tab_integration_v2",
"name": "② 通用 OPC UA 读取(端点与点位均由配置中心提供)",
"x": 300,
"y": 160,
"wires": []
},
{
"id": "opc_poll_v2",
"type": "inject",
"z": "tab_integration_v2",
"name": "采集调度 200ms",
"props": [
{
"p": "payload"
}
],
"repeat": "0.2",
"crontab": "",
"once": false,
"onceDelay": "2",
"topic": "",
"payload": "",
"payloadType": "date",
"x": 140,
"y": 210,
"wires": [
[
"opc_build_v2"
]
]
},
{
"id": "opc_build_v2",
"type": "function",
"z": "tab_integration_v2",
"name": "按配置构建读取列表",
"func": "const cfg = flow.get('integration_config');\nif (!cfg || cfg.source_mode !== 'opcua') return null;\nconst interval = Math.max(200, Number(cfg.connection.sampling_interval_ms || 1000));\nconst now = Date.now();\nconst last = flow.get('opc_last_poll') || 0;\nif (now - last < interval - 20) return null;\nflow.set('opc_last_poll', now);\nconst tags = flow.get('opc_read_tags') || [];\nif (!tags.length) return null;\nmsg.action = 'readmultiple';\nmsg.topic = 'readmultiple';\nmsg.payload = tags.map(t => t.node_id);\nreturn msg;",
"outputs": 1,
"noerr": 0,
"initialize": "",
"finalize": "",
"libs": [],
"x": 360,
"y": 210,
"wires": [
[
"opc_client_v2"
]
]
},
{
"id": "opc_endpoint_v2",
"type": "OpcUa-Endpoint",
"endpoint": "opc.tcp://opcua-sim:4840/biditech/server/",
"secpol": "None",
"secmode": "None",
"none": true,
"login": false,
"usercert": false
},
{
"id": "opc_client_v2",
"type": "OpcUa-Client",
"z": "tab_integration_v2",
"endpoint": "opc_endpoint_v2",
"action": "readmultiple",
"deadbandtype": "a",
"deadbandvalue": 1,
"time": 1,
"timeUnit": "s",
"certificate": "n",
"localfile": "",
"localkeyfile": "",
"securitymode": "None",
"securitypolicy": "None",
"useTransport": false,
"maxChunkCount": 1,
"maxMessageSize": 8192,
"receiveBufferSize": 8192,
"sendBufferSize": 8192,
"setstatusandtime": false,
"keepsessionalive": true,
"name": "动态 OPC UA Client",
"x": 600,
"y": 210,
"wires": [
[
"opc_shape_v2"
],
[
"opc_error_v2"
],
[]
]
},
{
"id": "opc_shape_v2",
"type": "function",
"z": "tab_integration_v2",
"name": "点表 → 标准快照",
"func": "const tags = flow.get('opc_read_tags') || [];\nconst values = flow.get('opc_values') || {};\nif ((msg.topic === 'readmultiple' || msg.topic === 'ALL') && Array.isArray(msg.payload)) {\n for (let i=0; i<Math.min(tags.length, msg.payload.length); i++) values[tags[i].node_id] = msg.payload[i];\n} else {\n const tag = tags.find(t => t.node_id === msg.topic);\n if (!tag) return null;\n values[tag.node_id] = msg.payload;\n}\nflow.set('opc_values', values);\nconst snap = {station:'胶州水厂二期提升泵站', pumps:[], source:'opcua', ts:Date.now()};\nconst valueOf = 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' && Object.prototype.hasOwnProperty.call(x,'value')) ? x.value : x;\n};\nfor (const tag of tags) {\n if (!Object.prototype.hasOwnProperty.call(values, tag.node_id)) continue;\n let value = valueOf(values[tag.node_id]);\n if (Array.isArray(value) && value.length === 2 && value.every(Number.isFinite)) value = value[0] * 4294967296 + value[1];\n if (typeof value === 'number' && typeof tag.scale === 'number') value = value * tag.scale + Number(tag.offset || 0);\n const match = tag.key.match(/^pump(\\d+)\\.(.+)$/);\n if (match) {\n const pid = Number(match[1]);\n let pump = snap.pumps.find(p => p.id === pid);\n if (!pump) { pump = {id:pid}; snap.pumps.push(pump); }\n pump[match[2]] = value;\n } else { snap[tag.key] = value; }\n}\nsnap.pumps.sort((a,b) => a.id-b.id);\nsnap.pumps.forEach(p => { if (p.status === undefined) p.status = 0; });\nif (typeof snap.totalPower === 'number' && typeof snap.totalFlow === 'number' && snap.totalFlow > 0) snap.sec = +(snap.totalPower/snap.totalFlow).toFixed(5);\nflow.set('snapshot', snap);\nmsg.payload = snap;\nreturn msg;",
"outputs": 1,
"noerr": 0,
"initialize": "",
"finalize": "",
"libs": [],
"x": 840,
"y": 210,
"wires": [
[
"ws_out_v2"
]
]
},
{
"id": "opc_error_v2",
"type": "debug",
"z": "tab_integration_v2",
"name": "OPC 连接/读取错误",
"active": true,
"tosidebar": true,
"console": false,
"tostatus": false,
"complete": "true",
"targetType": "full",
"statusVal": "",
"statusType": "auto",
"x": 850,
"y": 260,
"wires": []
},
{
"id": "cmt_sim_v2",
"type": "comment",
"z": "tab_integration_v2",
"name": "③ 仿真数据(后台选择“仿真”时自动启用)",
"x": 250,
"y": 320,
"wires": []
},
{
"id": "sim_tick_v2",
"type": "inject",
"z": "tab_integration_v2",
"name": "仿真 1s",
"props": [
{
"p": "payload"
}
],
"repeat": "1",
"crontab": "",
"once": true,
"onceDelay": "1.5",
"topic": "",
"payload": "",
"payloadType": "date",
"x": 120,
"y": 370,
"wires": [
[
"sim_gen_v2"
]
]
},
{
"id": "sim_gen_v2",
"type": "function",
"z": "tab_integration_v2",
"name": "生成标准仿真快照",
"func": "const cfg = flow.get('integration_config');\nif (cfg && cfg.source_mode !== 'simulation') return null;\nconst t = Date.now()/1000;\nconst pumps = [1,2,3,4,5].map((id,idx) => {\n const running = id === 1 || id === 2;\n const variable = id === 1 || id === 2 || id === 5;\n const freq = running ? (variable ? 46 + 1.5*Math.sin(t/8+idx) : 50) : 0;\n const flow = running ? 760*(freq/50)*(1+0.03*Math.sin(t/5+idx)) : 0;\n const power = running ? 38*Math.pow(freq/50,3)*(1+0.02*Math.sin(t/7+idx)) : 0;\n return {id, status:running?1:0, freq:(id===2&&running)?null:+freq.toFixed(2), flow:+flow.toFixed(1), power:+power.toFixed(2), current:+(power*1000/(1.732*394*0.85)).toFixed(1), runHours:4700+id*180};\n});\nconst totalFlow = pumps.reduce((s,p)=>s+p.flow,0);\nconst totalPower = pumps.reduce((s,p)=>s+p.power,0);\nconst snap = {station:'胶州水厂二期提升泵站', source:'simulation', level:+(5.5+0.7*Math.sin(t/50)).toFixed(2), pressure:+(0.145+0.006*Math.sin(t/40)).toFixed(3), totalFlow:+totalFlow.toFixed(1), totalPower:+totalPower.toFixed(2), sec:+(totalPower/totalFlow).toFixed(5), pumps, ts:Date.now()};\nflow.set('snapshot', snap); msg.payload=snap; return msg;",
"outputs": 1,
"noerr": 0,
"initialize": "",
"finalize": "",
"libs": [],
"x": 350,
"y": 370,
"wires": [
[
"ws_out_v2"
]
]
},
{
"id": "ws_listener_v2",
"type": "websocket-listener",
"path": "/ws/realtime",
"wholemsg": "false"
},
{
"id": "ws_out_v2",
"type": "websocket out",
"z": "tab_integration_v2",
"name": "实时推送 /ws/realtime",
"server": "ws_listener_v2",
"client": "",
"x": 650,
"y": 370,
"wires": []
},
{
"id": "cmt_api_v2",
"type": "comment",
"z": "tab_integration_v2",
"name": "④ REST 最新快照 /api/realtime",
"x": 230,
"y": 440,
"wires": []
},
{
"id": "http_rt_in_v2",
"type": "http in",
"z": "tab_integration_v2",
"name": "",
"url": "/api/realtime",
"method": "get",
"upload": false,
"swaggerDoc": "",
"x": 140,
"y": 490,
"wires": [
[
"http_rt_fn_v2"
]
]
},
{
"id": "http_rt_fn_v2",
"type": "function",
"z": "tab_integration_v2",
"name": "返回快照与采集状态",
"func": "const cfg = flow.get('integration_config');\nmsg.payload = flow.get('snapshot') || {error:'尚无实时数据', source_mode:cfg && cfg.source_mode};\nmsg.headers = {'Content-Type':'application/json','Cache-Control':'no-store','Access-Control-Allow-Origin':'*'};\nreturn msg;",
"outputs": 1,
"noerr": 0,
"initialize": "",
"finalize": "",
"libs": [],
"x": 370,
"y": 490,
"wires": [
[
"http_rt_resp_v2"
]
]
},
{
"id": "http_rt_resp_v2",
"type": "http response",
"z": "tab_integration_v2",
"name": "",
"statusCode": "",
"headers": {},
"x": 600,
"y": 490,
"wires": []
}
]
|