| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950 |
- import { fetchMonitoringPointDetail } from './detailApi.js'
- /** Read the current snapshot, not just the selected point. Never borrow a parent's position. */
- export async function loadEnhancementDevices(summary, { fetchPoint = fetchMonitoringPointDetail, alive = () => true, onProgress = () => {} } = {}) {
- const objects = summary?.objects || []
- const points = objects.filter((o) => o.kind === 'monitoring-point')
- const rows = new Map(objects.filter((o) => o.kind === 'unassociated-equipment').map((o) => [o.code, { ...o, pointCode: '' }]))
- let cursor = 0
- let failedPoints = 0
- let missingDevices = 0
- let extraDevices = 0
- let stalePoints = 0
- let missingLists = 0
- let loadedPoints = 0
- const state = (loading) => ({ rows: [...rows.values()], failedPoints, missingDevices, extraDevices, stalePoints,
- missingLists, loadedPoints, totalPoints: points.length,
- partial: loading || !['complete', 'empty'].includes(summary?.phase) || !!summary?.truncated
- || (Number.isFinite(summary?.total) && summary.total > objects.length)
- || failedPoints > 0 || missingDevices > 0 || extraDevices > 0 || missingLists > 0 || !alive(),
- relationNote: '仅使用已提供的设施关联;其余为邻近示意,非实测安装位置' })
- const publish = () => { if (alive()) onProgress(state(true)) }
- publish()
- async function worker() {
- while (cursor < points.length && alive()) {
- const point = points[cursor++]
- let result
- try { result = await fetchPoint(point.code) } catch { failedPoints += 1; loadedPoints += 1; publish(); continue }
- if (!alive()) return
- if (!['success', 'empty', 'stale'].includes(result?.state) || !Array.isArray(result?.data?.devices)
- || result.data.code !== point.code) { failedPoints += 1; loadedPoints += 1; publish(); continue }
- if (result.state === 'stale') stalePoints += 1
- const listAvailable = point.deviceCodesAvailable ?? Array.isArray(point.deviceCodes)
- if (!listAvailable) missingLists += 1
- const expected = new Set(point.deviceCodes || [])
- const found = new Set()
- for (const device of result.data.devices) {
- if (!device?.code) continue
- if (listAvailable && !expected.has(device.code)) { extraDevices += 1; continue }
- found.add(device.code)
- rows.set(device.code, { ...device, pointCode: point.code, industry: device.industry || point.industry,
- retained: result.state === 'stale' })
- }
- missingDevices += [...expected].filter((code) => !found.has(code)).length
- loadedPoints += 1
- publish()
- }
- }
- await Promise.all(Array.from({ length: Math.min(4, points.length) }, worker))
- return state(false)
- }
|