[ { "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 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": [] } ]