91 lines
7.1 KiB
JavaScript
91 lines
7.1 KiB
JavaScript
|
|
import fs from 'node:fs'
|
|||
|
|
import assert from 'node:assert/strict'
|
|||
|
|
import crypto from 'node:crypto'
|
|||
|
|
|
|||
|
|
// Offline derivative only. Never publishes or runs a scanner, motor, AGV or arm.
|
|||
|
|
const [kind, sourcePath, outputPath, summaryPath] = process.argv.slice(2)
|
|||
|
|
if (!['fixture','frame'].includes(kind) || !sourcePath || !outputPath) throw new Error('Usage: node build_handheld_scan.mjs <fixture|frame> <source-snapshot.json> <flow.json> [summary.json]')
|
|||
|
|
const source=JSON.parse(fs.readFileSync(sourcePath,'utf8').replace(/^\uFEFF/,''))
|
|||
|
|
assert.equal(source.published.id,kind==='fixture'?'8946d6a8bfcbb1ca252b9bd26ccfcb1a':'a02ccea8e279e8c9caf0acb6d6cb1dc9','Review a different source before deriving')
|
|||
|
|
const original=JSON.parse(source.published.flowData),byId=new Map(original.nodes.map(n=>[n.id,n])),order=[]
|
|||
|
|
for(let n=original.nodes.find(n=>n.type==='start');n;){
|
|||
|
|
assert(!order.includes(n),'Unexpected source cycle');order.push(n)
|
|||
|
|
const edges=original.edges.filter(e=>e.sourceNodeId===n.id);assert(edges.length<=1,'Review source branching')
|
|||
|
|
n=edges.length?byId.get(edges[0].targetNodeId):null
|
|||
|
|
}
|
|||
|
|
let generatedId=0
|
|||
|
|
const uid=()=>crypto.createHash('sha256').update(source.published.id+':handheld:'+kind+':'+(++generatedId)).digest('hex').slice(0,32),clone=v=>structuredClone(v)
|
|||
|
|
const action=n=>(n.properties.action||'').toUpperCase(),params=n=>n.properties.nodeParams||[]
|
|||
|
|
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,...path],quoteType:path[0],required:true,disabled:true})
|
|||
|
|
const start=clone(order[0]),end=clone(order.at(-1))
|
|||
|
|
assert.equal(start.type,'start');assert.equal(end.type,'end')
|
|||
|
|
const name=kind==='fixture'?'夹具手持扫描':'车架手持扫描'
|
|||
|
|
let retained
|
|||
|
|
if(kind==='fixture'){
|
|||
|
|
retained=order.slice(1,-1).filter(n=>/^(AGV_|MOTOR_|SCANNER_)/.test(action(n))||action(n)==='SLEEP').map(clone)
|
|||
|
|
assert.equal(retained.filter(n=>action(n)==='AGV_MOVE_TO_STATION').length,1)
|
|||
|
|
assert.equal(retained.filter(n=>action(n)==='MOTOR_MOVE').length,8)
|
|||
|
|
assert.equal(retained.filter(n=>action(n)==='SCANNER_START').length,8)
|
|||
|
|
}else{
|
|||
|
|
const first=order.findIndex(n=>action(n)==='SCANNER_START')
|
|||
|
|
const stop=order.findIndex((n,i)=>i>first&&action(n)==='SCANNER_STOP')
|
|||
|
|
const mesh=order.findIndex(n=>action(n)==='SCANNER_MESH')
|
|||
|
|
assert(first>0&&stop>first&&mesh>stop)
|
|||
|
|
retained=[...order.slice(1,first).filter(n=>action(n).startsWith('SCANNER_')),order[first],order[stop],...order.slice(mesh,-1).filter(n=>action(n).startsWith('SCANNER_'))].map(clone)
|
|||
|
|
retained.find(n=>action(n)==='SCANNER_START').properties.name='开始车架手持采集'
|
|||
|
|
retained.find(n=>action(n)==='SCANNER_STOP').properties.name='结束车架手持采集并保留工程'
|
|||
|
|
assert(!retained.some(n=>/^(AGV_|ARM_|MOTOR_)/.test(action(n))))
|
|||
|
|
}
|
|||
|
|
const usedInputs=new Set()
|
|||
|
|
function inspect(v){
|
|||
|
|
if(Array.isArray(v)){v.forEach(inspect);return}
|
|||
|
|
if(!v||typeof v!=='object')return
|
|||
|
|
if(v.type==='quote'&&v.quote?.[0]===start.id&&v.quote[1]==='input')usedInputs.add(v.quote[2])
|
|||
|
|
Object.values(v).forEach(inspect)
|
|||
|
|
}
|
|||
|
|
retained.forEach(inspect)
|
|||
|
|
start.properties.inputParams=(start.properties.inputParams||[]).filter(p=>usedInputs.has(p.name))
|
|||
|
|
start.properties.inputParams.push({name:'manualScanSeconds',type:'number',input:120,children:[],required:true,desc:'每段手持采集最长等待秒数,默认120,可输入1至3600整数;只有点击结束本段才能继续,超时将停止并阻断后续步骤。'})
|
|||
|
|
start.properties.name=name+'开始'
|
|||
|
|
const options={id:uid(),type:'code',x:0,y:0,properties:{name:'检查人工采集等待时长',action:'CODE',nodeParams:[quote('seconds',start,'input','manualScanSeconds'),input('code','function handler(params){var s=params.seconds;if(typeof s!=="number"||!isFinite(s)||s!==Math.floor(s)||s<1||s>3600)throw new Error("每段人工等待时长必须为1至3600整数秒");return {timeoutMs:s*1000};}')],outputType:'json',outputParams:[{name:'result',type:'object',required:true,children:[{name:'timeoutMs',type:'number',children:[],required:true}]}]}}
|
|||
|
|
const sequence=[start,options],labels=[]
|
|||
|
|
let collecting=false,segment=''
|
|||
|
|
for(const n of retained){
|
|||
|
|
const a=action(n)
|
|||
|
|
if(a==='SCANNER_START'){assert(!collecting,'Nested capture');collecting=true;segment=n.properties.name}
|
|||
|
|
if(a==='MOTOR_MOVE'||a==='AGV_MOVE_TO_STATION')assert(!collecting,'Source moves a platform during active hand-held collection; review explicitly')
|
|||
|
|
if(a==='SCANNER_STOP'&&collecting){
|
|||
|
|
const label=kind==='frame'?'车架手持采集':'第'+(labels.length+1)+'/8段:'+segment
|
|||
|
|
labels.push(label)
|
|||
|
|
sequence.push({id:uid(),type:'serviceNode',x:0,y:0,properties:{name:'等待人工结束:'+label,action:'SCANNER_MANUAL_WAIT',nodeType:'EDGE',executionTarget:{roleKey:'scanner',deviceSlotKey:'scanner'},nodeParams:[quote('timeoutMs',options,'output','result','timeoutMs'),input('label',label)],outputType:'json',outputParams:[]}})
|
|||
|
|
collecting=false
|
|||
|
|
}
|
|||
|
|
sequence.push(n)
|
|||
|
|
}
|
|||
|
|
assert(!collecting,'Missing stop after manual collection')
|
|||
|
|
sequence.push(end)
|
|||
|
|
assert.equal(labels.length,kind==='fixture'?8:1)
|
|||
|
|
const targets=new Map()
|
|||
|
|
for(const n of sequence){const t=n.properties.executionTarget;if(t?.roleKey&&t.deviceSlotKey){if(!targets.has(t.roleKey))targets.set(t.roleKey,new Set());targets.get(t.roleKey).add(t.deviceSlotKey)}}
|
|||
|
|
start.properties.resourceRoles=(start.properties.resourceRoles||[]).filter(r=>targets.has(r.roleKey)).map(r=>({...r,deviceSlots:r.deviceSlots.filter(s=>targets.get(r.roleKey).has(s.slotKey))}))
|
|||
|
|
for(const [key,slots] of targets)for(const slot of slots)assert(start.properties.resourceRoles.find(r=>r.roleKey===key)?.deviceSlots.some(s=>s.slotKey===slot),'Resource role missing')
|
|||
|
|
const graph={...clone(original),nodes:sequence,edges:sequence.slice(0,-1).map((n,i)=>({id:uid(),type:'bezier',sourceNodeId:n.id,targetNodeId:sequence[i+1].id,sourceAnchorId:n.id+'_1',targetAnchorId:sequence[i+1].id+'_3'}))}
|
|||
|
|
sequence.forEach((n,i)=>{n.x=i*320;n.y=220;delete n.properties.parentId})
|
|||
|
|
const present=new Set(sequence.map(n=>n.id))
|
|||
|
|
function check(v){
|
|||
|
|
if(Array.isArray(v)){v.forEach(check);return}
|
|||
|
|
if(!v||typeof v!=='object')return
|
|||
|
|
if(v.type==='quote')assert(present.has(v.quote[0]),'Dangling parameter reference')
|
|||
|
|
Object.values(v).forEach(check)
|
|||
|
|
}
|
|||
|
|
sequence.forEach(check)
|
|||
|
|
assert(!sequence.some(n=>action(n).startsWith('ARM_')))
|
|||
|
|
assert(!start.properties.resourceRoles.some(r=>r.deviceSlots.some(s=>s.deviceKind===2)))
|
|||
|
|
for(const n of retained)assert.deepEqual(n.properties.nodeParams,byId.get(n.id).properties.nodeParams,'Retained device parameters changed')
|
|||
|
|
if(kind==='fixture')assert.deepEqual(retained.filter(n=>action(n).startsWith('SCANNER_')).map(n=>action(n)),order.filter(n=>action(n).startsWith('SCANNER_')).map(n=>action(n)))
|
|||
|
|
fs.writeFileSync(outputPath,JSON.stringify(graph,null,2))
|
|||
|
|
const summary={kind,name,nodes:sequence.length,manualSegments:labels.length,labels,armActions:0,agvMoves:sequence.filter(n=>action(n).startsWith('AGV_')).length,motorMoves:sequence.filter(n=>action(n)==='MOTOR_MOVE').length,inputCount:start.properties.inputParams.length,requiresManualWaitRuntime:true,published:false,deviceCommandsSent:0}
|
|||
|
|
if(summaryPath)fs.writeFileSync(summaryPath,JSON.stringify(summary,null,2))
|
|||
|
|
console.log(JSON.stringify(summary))
|