import fs from 'node:fs' import assert from 'node:assert/strict' import crypto from 'node:crypto' // Requires loop resultNodeId support in the installed platform; never deploys or moves equipment. const [sourcePath,outputPath,summaryPath]=process.argv.slice(2) if(!sourcePath||!outputPath)throw new Error('Usage: node build_settle_update.mjs [summary.json]') const source=JSON.parse(fs.readFileSync(sourcePath,'utf8').replace(/^\uFEFF/,'')) const before=JSON.parse(source.published?.flowData||source.flowData||JSON.stringify(source)),graph=structuredClone(before) assert.equal(graph.nodes.length,315,'Review the source before applying a different layout') const byId=new Map(graph.nodes.map(node=>[node.id,node])),order=[] for(let n=graph.nodes.find(n=>n.type==='start');n;){assert(!order.includes(n));order.push(n);const edges=graph.edges.filter(e=>e.sourceNodeId===n.id);assert(edges.length<=1);n=edges.length?byId.get(edges[0].targetNodeId):null} const params=node=>node.properties.nodeParams||[],mode=node=>params(node).find(p=>p.name==='mode')?.input const input=(name,value)=>({name,type:'input',input:value,required:true,disabled:true}) const quote=(name,node,...path)=>({name,type:'quote',input:'',quote:[node.id,'output',...path],quoteType:'output',required:true,disabled:true}) const field=(name,type,children=[])=>({name,type,children,required:true}) function set(node,name,value){const index=params(node).findIndex(p=>p.name===name);if(index<0)node.properties.nodeParams.push(value);else node.properties.nodeParams[index]=value} const helper=fs.readFileSync(new URL('./settle-probe.js',import.meta.url),'utf8'),removed=new Set(),gates=[] const smooth=fs.readFileSync(new URL('./tool-z-sweep-smooth-planner.js',import.meta.url),'utf8') assert(smooth.includes('// TOOL_Z_RIGHT_MULTIPLY'),'Tool-local Z implementation required') for(const planner of order.filter(node=>mode(node)==='scan')){ const index=order.indexOf(planner),loop=order[index-4],extra=order.slice(index-3,index) assert(loop.type==='loop'&&loop.properties.name.includes('连续停稳')) assert.deepEqual(extra.map(node=>node.type),['serviceNode','sleep','serviceNode']) const probe=loop.children.map(id=>byId.get(id)).find(node=>node.type==='code') assert(probe&¶ms(probe).some(p=>p.name==='previousState')&¶ms(probe).some(p=>p.name==='state')) const first=byId.get(params(probe).find(p=>p.name==='previousState').quote[0]) const stateDef=structuredClone(first.properties.outputParams.find(p=>p.name==='state')) const exportedFields=[field('ready','boolean'),field('reason','string'),{...stateDef,name:'previousState'},{...structuredClone(stateDef),name:'state'},field('deadlineMs','number'),field('observedAt','number')] set(probe,'code',input('code',helper)) probe.properties.outputParams=[field('result','object',exportedFields)] set(loop,'resultNodeId',input('resultNodeId',probe.id)) loop.properties.outputParams=loop.properties.outputParams.filter(p=>p.name!=='result').concat(field('result','object',[field('result','object',structuredClone(exportedFields))])) loop.properties.name=loop.properties.name.replace('等待连续停稳','等待并交付同一对连续停稳回读') set(planner,'previousState',quote('previousState',loop,'result','result','previousState')) set(planner,'state',quote('state',loop,'result','result','state')) extra.forEach(node=>removed.add(node.id)) gates.push({loopId:loop.id,probeId:probe.id,plannerId:planner.id}) } assert.equal(gates.length,8) graph.nodes=graph.nodes.filter(node=>!removed.has(node.id)) for(const node of graph.nodes)if(mode(node))set(node,'code',input('code',smooth)) const main=order.filter(node=>!removed.has(node.id)),mainIds=new Set(order.map(node=>node.id)) graph.edges=graph.edges.filter(edge=>!mainIds.has(edge.sourceNodeId)&&!mainIds.has(edge.targetNodeId)) graph.edges.push(...main.slice(0,-1).map((node,i)=>({id:crypto.randomUUID().replaceAll('-',''),type:'bezier',sourceNodeId:node.id,targetNodeId:main[i+1].id,sourceAnchorId:node.id+'_1',targetAnchorId:main[i+1].id+'_3'}))) main.forEach((node,index)=>{node.x=index*320;node.y=220}) for(const node of graph.nodes)for(const p of params(node))if(p.quote)assert(!removed.has(p.quote[0]),'Removed sample node is still referenced') assert.equal(graph.nodes.filter(node=>node.properties.action==='SCANNER_START').length,8) fs.writeFileSync(outputPath,JSON.stringify(graph,null,2)) const summary={nodes:graph.nodes.length,removedDuplicateSampleNodes:removed.size,gates,strictPositionDriftMetres:0.00002,strictOrientationDriftRadians:0.0001,maxToolAngleStepDegrees:45,requiresLoopResultExport:true,deviceCommandsSent:0} if(summaryPath)fs.writeFileSync(summaryPath,JSON.stringify(summary,null,2)) console.log(JSON.stringify({...summary,gates:gates.length}))