/* 多主站真实 TCP 回环:两个端口各有独立 Slave,共用同一份运行参数。 * 验证每轮Type70先于全站总召,两个端口分别完成启动;三种类型共享值、STOP及重连互不影响。 * WAIT 后确认快照不刷新等内部时序由 C 夹具测试覆盖,此处只从主站报文观察。 */ 'use strict'; const assert = require('node:assert/strict'); const net = require('node:net'); const { spawn } = require('node:child_process'); const { once } = require('node:events'); const { setTimeout: delay } = require('node:timers/promises'); const { Master, parameterRequest } = require('./slave_example_loopback_test.cjs'); const executable = process.argv[2]; if (!executable) throw new Error('Usage: node parameter_multi_master_loopback_test.cjs '); async function run() { // Five startup GIs plus the final GI each require 32 data frames and two replies. // Leave time for serial endpoint polling; individual APDU waits retain their 1500ms bound. const child = spawn(executable, ['--port', '0', '--port2', '0', '--run-ms', '10000'], { windowsHide: true, stdio: ['ignore', 'pipe', 'pipe'] }); let output = '', errors = ''; let stage = 'listen'; const startupRounds = [0, 0]; const started = Date.now(); const sockets = []; child.stdout.on('data', data => { output += data; }); child.stderr.on('data', data => { errors += data; }); const exit = once(child, 'exit'); const deadline = setTimeout(() => child.kill(), 15000); try { let ports; for (let i = 0; i < 100; ++i) { const matches = [...output.matchAll(/LISTEN ([12]) 127\.0\.0\.1:(\d+) CA=1/g)]; if (matches.length === 2) { ports = matches.map(match => Number(match[2])); break; } await delay(20); } assert(ports && ports[0] !== ports[1], `${output}\n${errors}`); const connect = async port => { stage = `connect port=${port}`; const socket = net.connect({ host: '127.0.0.1', port }); sockets.push(socket); const master = new Master(socket); await once(socket, 'connect'); await master.control(7, 0x0b); return master; }; const reply = async (master, type, ioa, value, kind, cause = 6, running = value) => { stage = `parameter type=${type} ioa=${ioa.toString(16)} cause=${cause}`; const request = parameterRequest(type, ioa, value, kind, cause, 1); await master.send(request); const expected = Buffer.from(request); expected[2] = (cause & 0x80) | 7; if (type === 112) expected.writeFloatLE(running, 9); else expected.writeInt16LE(running, 9); assert.deepEqual(await master.readASDU(), expected); }; const giRequest = [100, 1, 6, 0x55, 1, 0, 0, 0, 0, 20]; const readGi = async (master, label = 'GI') => { // 32 行参数各占一帧;类型、COT、唯一 IOA 和结束确认共同验证游标完整推进。 stage = `${label}: ACT_CON`; assert.deepEqual([...(await master.readASDU())], [100, 1, 7, 0x55, 1, 0, 0, 0, 0, 20]); const points = new Map(); for (let i = 0; i < 32; ++i) { stage = `${label}: data ${i + 1}/32`; const asdu = await master.readASDU(); assert([110, 111, 112].includes(asdu[0])); assert.equal(asdu[1], 1); assert.equal(asdu[2], 20); const ioa = asdu.readUIntLE(6, 3); assert(!points.has(ioa)); points.set(ioa, asdu); } stage = `${label}: ACT_TERM`; assert.deepEqual([...(await master.readASDU())], [100, 1, 10, 0x55, 1, 0, 0, 0, 0, 20]); return points; }; const gi = async master => { await master.send(giRequest); return readGi(master); }; const startup = async (master, endpoint) => { // 总召可以先到;即使Type70已经成帧,线上仍须先见70再见GI的CON/DATA/TERM。 // 初始化帧可能在GI请求前分配N(R),仅此阶段允许确认位置落后于刚发出的请求。 master.asynchronous = true; const label = `startup endpoint=${endpoint + 1} round=${startupRounds[endpoint] + 1}`; stage = `${label}: Type70`; await master.send(giRequest); assert.deepEqual([...(await master.readASDU())], [70, 1, 4, 0, 1, 0, 0, 0, 0, 0]); const points = await readGi(master, label); master.asynchronous = false; startupRounds[endpoint]++; return points; }; let first = await connect(ports[0]); const second = await connect(ports[1]); await startup(first, 0); // B尚未完成初始化总召,不得阻止A完成启动或处理参数;两个服务不能共享启动门禁。 await reply(first, 111, 0x8601, 321, 1); await reply(first, 110, 0x8605, 8192, 1); await reply(first, 112, 0x8631, 2.5, 2); const initial = await startup(second, 1); assert.equal(initial.size, 32); assert.equal(initial.get(0x8601).readInt16LE(9), 321); assert.equal(initial.get(0x8605).readInt16LE(9), 8192); assert.equal(initial.get(0x8631).readFloatLE(9), 2.5); // B 的 TEST 只报告 A 已装载的运行值,不能把请求中的 999 写入共享数组。 await reply(second, 111, 0x8601, 999, 1, 0x86, 321); // 依次在两侧结束会话,再用另一侧装载和重连后的 TEST 证明运行值不随连接清除。 await first.control(0x13, 0x23); await reply(second, 111, 0x8601, 456, 1); // A 的 STOP 不停止 B 的参数服务。 await first.control(7, 0x0b); assert.equal((await startup(first, 0)).get(0x8601).readInt16LE(9), 456); await reply(first, 111, 0x8601, 0, 1, 0x86, 456); await first.close(); await reply(second, 111, 0x8601, 789, 1); first = await connect(ports[0]); assert.equal((await startup(first, 0)).get(0x8601).readInt16LE(9), 789); await reply(first, 111, 0x8601, 0, 1, 0x86, 789); await second.control(0x13, 0x23); await reply(first, 111, 0x8602, 123, 1); await second.control(7, 0x0b); assert.equal((await startup(second, 1)).get(0x8602).readInt16LE(9), 123); await reply(second, 111, 0x8602, 0, 1, 0x86, 123); await second.close(); const final = await gi(first); assert.equal(final.get(0x8601).readInt16LE(9), 789); assert.equal(final.get(0x8602).readInt16LE(9), 123); assert.deepEqual(startupRounds, [3, 2]); stage = 'close and wait for server exit'; await first.close(); const [code] = await exit; assert.equal(code, 0, `${output}\n${errors}`); assert.equal(errors, ''); assert.match(output, /STOP multi_master=ok/); console.log('Parameter multi-master loopback: passed (independent Type70/GI32 startup rounds 3/2, shared values, STOP/START/disconnect)'); } catch (error) { error.message += `\nstage=${stage}; completed startup rounds=${startupRounds.join('/')}; elapsed=${Date.now() - started}ms` + `\nserver stdout:\n${output}\nserver stderr:\n${errors}`; throw error; } finally { clearTimeout(deadline); for (const socket of sockets) socket.destroy(); if (child.exitCode === null) child.kill(); } } run().catch(error => { console.error(error); process.exitCode = 1; });