deviceStatusStore.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. /**
  2. * 设备状态池的**全局**数据源(维护者 2026-11-04;票 133 收敛为单一计数入口)。
  3. *
  4. * 为什么单独成 store:状态池是全站口径——顶部状态栏在四个一级模式里都常驻,不能挂在
  5. * 「运行监测」的面板分区上,否则不进那个模式就不刷新(这正是当初要修的缺陷)。
  6. *
  7. * **票 133 起它只做一件事**:取 `/monitoring/overview` 的逐设备台账(含后端给的
  8. * `tags`:ALARM / WARNING / WORK_ORDER),交给 `domain/deviceStatusStats.deviceStatusStats()`
  9. * 算计数。设备状态标签的唯一事实源是后端 `BusinessCockpitDeviceFactResolver`
  10. * (在线/报警=equipment_status、预警=未解除预警、工单=未结束工单)。
  11. *
  12. * **不再请求 `/monitoring/situation` 与 `/events/current`,也不用报警流水二次推断设备状态**
  13. * —— CONTEXT.md「设备预警/报警统计」明令禁止那套做法,它正是本票要修掉的旧实现。
  14. *
  15. * 语义见 `domain/deviceStatusStats.js`:标签可叠加、在线/离线互斥、缺状态单列未知。
  16. * 数据没到时 `pools.available:false`,界面如实降级,不补 0。
  17. */
  18. import { computed, ref } from 'vue'
  19. import { defineStore } from 'pinia'
  20. import { fetchMonitoringOverview } from '../services/monitoringApi.js'
  21. import { deviceStatusStats } from '../domain/deviceStatusStats.js'
  22. import { useMapStore } from './mapStore.js'
  23. export const useDeviceStatusStore = defineStore('deviceStatusStore', () => {
  24. /** 监测概览分区(逐设备 monitoringState + 状态标签 tags) */
  25. const overview = ref(null)
  26. const loading = ref(false)
  27. const loaded = ref(false)
  28. /** 请求序号:换筛选时旧的在途响应作废。 */
  29. let token = 0
  30. const pools = computed(() => {
  31. const devices = overview.value && overview.value.data && overview.value.data.devices
  32. return deviceStatusStats(devices)
  33. })
  34. /**
  35. * 未关联设备(附加状态,不是业务标签):来自地图快照的对象类别桶。
  36. * 它不是设备业务状态,所以不进 `counts`;只用于「独立设备」图例行的筛选。
  37. */
  38. const unassociatedCodes = computed(() => {
  39. const mapStore = useMapStore()
  40. const set = new Set()
  41. for (const object of (mapStore.summary && mapStore.summary.objects) || []) {
  42. if (object && object.kind === 'unassociated-equipment' && object.code) {
  43. set.add(String(object.code).trim())
  44. }
  45. }
  46. return set
  47. })
  48. async function refresh(options = {}) {
  49. const mine = ++token
  50. loading.value = true
  51. try {
  52. const query = {
  53. industry: options.industry,
  54. scope: options.scope,
  55. deviceType: options.deviceType,
  56. config: options.config,
  57. fetchImpl: options.fetchImpl,
  58. }
  59. const overviewSection = await fetchMonitoringOverview(query)
  60. if (mine !== token) return pools.value
  61. overview.value = overviewSection
  62. loaded.value = true
  63. } finally {
  64. if (mine === token) loading.value = false
  65. }
  66. return pools.value
  67. }
  68. /** 仅测试/重置用。 */
  69. function reset() {
  70. token += 1
  71. overview.value = null
  72. loading.value = false
  73. loaded.value = false
  74. }
  75. /**
  76. * 按选中的状态行筛选设备(与图例同一份台账/标签)。
  77. * 空集合=不筛;「无设备监测点」不参与设备筛选(它是点位特殊状态)。
  78. * @param {string[]} keys 选中的图例键
  79. * @returns {Set<string>|{codes:Set<string>, emptyPoints:boolean}|null} 命中的设备编码集;null=不筛
  80. */
  81. function filterDeviceCodes(keys) {
  82. const all = Array.isArray(keys) ? keys.filter(Boolean) : []
  83. if (!all.length) return null
  84. // 「无设备监测点」是点位特殊状态:只筛没有关联设备的空白点位,不参与设备标签匹配。
  85. const emptyPoints = all.includes('EMPTY')
  86. const selected = all.filter((key) => key !== 'EMPTY')
  87. if (!selected.length) return { codes: new Set(), emptyPoints }
  88. const current = pools.value
  89. if (!current.available) return new Set()
  90. const unassociated = unassociatedCodes.value
  91. const codes = new Set()
  92. for (const device of current.devices || []) {
  93. const hit = selected.some((key) => {
  94. if (key === 'NORMAL' || key === 'OFFLINE') return device.base === key
  95. if (key === 'UNASSOCIATED') return unassociated.has(device.deviceCode)
  96. return device.tags.includes(key)
  97. })
  98. if (hit) codes.add(device.deviceCode)
  99. }
  100. return { codes, emptyPoints }
  101. }
  102. return { overview, pools, filterDeviceCodes, loading, loaded, refresh, reset }
  103. })