/** * 设备状态池的**全局**数据源(维护者 2026-11-04;票 133 收敛为单一计数入口)。 * * 为什么单独成 store:状态池是全站口径——顶部状态栏在四个一级模式里都常驻,不能挂在 * 「运行监测」的面板分区上,否则不进那个模式就不刷新(这正是当初要修的缺陷)。 * * **票 133 起它只做一件事**:取 `/monitoring/overview` 的逐设备台账(含后端给的 * `tags`:ALARM / WARNING / WORK_ORDER),交给 `domain/deviceStatusStats.deviceStatusStats()` * 算计数。设备状态标签的唯一事实源是后端 `BusinessCockpitDeviceFactResolver` * (在线/报警=equipment_status、预警=未解除预警、工单=未结束工单)。 * * **不再请求 `/monitoring/situation` 与 `/events/current`,也不用报警流水二次推断设备状态** * —— CONTEXT.md「设备预警/报警统计」明令禁止那套做法,它正是本票要修掉的旧实现。 * * 语义见 `domain/deviceStatusStats.js`:标签可叠加、在线/离线互斥、缺状态单列未知。 * 数据没到时 `pools.available:false`,界面如实降级,不补 0。 */ import { computed, ref } from 'vue' import { defineStore } from 'pinia' import { fetchMonitoringOverview } from '../services/monitoringApi.js' import { deviceStatusStats } from '../domain/deviceStatusStats.js' import { useMapStore } from './mapStore.js' export const useDeviceStatusStore = defineStore('deviceStatusStore', () => { /** 监测概览分区(逐设备 monitoringState + 状态标签 tags) */ const overview = ref(null) const loading = ref(false) const loaded = ref(false) /** 请求序号:换筛选时旧的在途响应作废。 */ let token = 0 const pools = computed(() => { const devices = overview.value && overview.value.data && overview.value.data.devices return deviceStatusStats(devices) }) /** * 未关联设备(附加状态,不是业务标签):来自地图快照的对象类别桶。 * 它不是设备业务状态,所以不进 `counts`;只用于「独立设备」图例行的筛选。 */ const unassociatedCodes = computed(() => { const mapStore = useMapStore() const set = new Set() for (const object of (mapStore.summary && mapStore.summary.objects) || []) { if (object && object.kind === 'unassociated-equipment' && object.code) { set.add(String(object.code).trim()) } } return set }) async function refresh(options = {}) { const mine = ++token loading.value = true try { const query = { industry: options.industry, scope: options.scope, deviceType: options.deviceType, config: options.config, fetchImpl: options.fetchImpl, } const overviewSection = await fetchMonitoringOverview(query) if (mine !== token) return pools.value overview.value = overviewSection loaded.value = true } finally { if (mine === token) loading.value = false } return pools.value } /** 仅测试/重置用。 */ function reset() { token += 1 overview.value = null loading.value = false loaded.value = false } /** * 按选中的状态行筛选设备(与图例同一份台账/标签)。 * 空集合=不筛;「无设备监测点」不参与设备筛选(它是点位特殊状态)。 * @param {string[]} keys 选中的图例键 * @returns {Set|{codes:Set, emptyPoints:boolean}|null} 命中的设备编码集;null=不筛 */ function filterDeviceCodes(keys) { const all = Array.isArray(keys) ? keys.filter(Boolean) : [] if (!all.length) return null // 「无设备监测点」是点位特殊状态:只筛没有关联设备的空白点位,不参与设备标签匹配。 const emptyPoints = all.includes('EMPTY') const selected = all.filter((key) => key !== 'EMPTY') if (!selected.length) return { codes: new Set(), emptyPoints } const current = pools.value if (!current.available) return new Set() const unassociated = unassociatedCodes.value const codes = new Set() for (const device of current.devices || []) { const hit = selected.some((key) => { if (key === 'NORMAL' || key === 'OFFLINE') return device.base === key if (key === 'UNASSOCIATED') return unassociated.has(device.deviceCode) return device.tags.includes(key) }) if (hit) codes.add(device.deviceCode) } return { codes, emptyPoints } } return { overview, pools, filterDeviceCodes, loading, loaded, refresh, reset } })