|
|
@@ -0,0 +1,165 @@
|
|
|
+/**
|
|
|
+ * 坐标点定位的唯一入口(#131)。
|
|
|
+ *
|
|
|
+ * 为什么收成一个 store:设备、监测点、设施、事件回联原本各自实现「解析 → 飞相机 →
|
|
|
+ * 开详情 → 反馈」,同一件事四份代码、四种手感(有的只换中心、有的不开详情、有的不复用
|
|
|
+ * #127 的揭示)。本 store 只做**编排**:解析交给既有 domain 纯函数,相机/高亮交给
|
|
|
+ * mapService,详情交给 detailStore / facilityPointStore;调用方只传目标身份。
|
|
|
+ *
|
|
|
+ * 目标形状:
|
|
|
+ * - `{ kind: 'map-object', code }`:监测点、未关联设备或关联设备编码(后者由
|
|
|
+ * `planLocate` 回联宿主点位);设备与监测点共用同一条地形感知完整视角。
|
|
|
+ * - `{ kind: 'event', event }`:事件详情卡回联;用 `planEventLocate` 解析,飞完整视角 + 高亮,
|
|
|
+ * 不替换已打开的事件详情卡。
|
|
|
+ * - `{ kind: 'facility', code, category?, facility?, prefill? }`:设施点;设施专用详情流
|
|
|
+ * 仍由 `facilityPointStore.select` 承担,本 store 只统一入口与反馈。
|
|
|
+ */
|
|
|
+import { ref } from 'vue'
|
|
|
+import { defineStore } from 'pinia'
|
|
|
+import { useMapStore } from './mapStore.js'
|
|
|
+import { useDetailStore } from './detailStore.js'
|
|
|
+import { useFacilityPointStore } from './facilityPointStore.js'
|
|
|
+import {
|
|
|
+ getAssociatedDeviceState,
|
|
|
+ getCameraState,
|
|
|
+ highlightMapObject,
|
|
|
+ locateMonitoringPoint,
|
|
|
+} from '../services/mapService.js'
|
|
|
+import { LOCATE_RESULT, locateFailureText, planLocate } from '../domain/mapLocate.js'
|
|
|
+import { EVENT_DETAIL_RESULT, planEventLocate } from '../domain/eventDetail.js'
|
|
|
+import { TERRAIN_LOCATE_REASON } from '../domain/pointLocate.js'
|
|
|
+import { MAP_OBJECT_KIND } from '../domain/mapSummary.js'
|
|
|
+
|
|
|
+const EMPTY_RESULT = { result: '', code: '', pointCode: '', deviceCode: '', reasonText: '', at: 0 }
|
|
|
+
|
|
|
+export const usePointLocateStore = defineStore('pointLocateStore', () => {
|
|
|
+ const mapStore = useMapStore()
|
|
|
+ const detailStore = useDetailStore()
|
|
|
+ const facilityPointStore = useFacilityPointStore()
|
|
|
+
|
|
|
+ /** 最近一次定位的统一反馈(成功/失败都写;外壳与各面板读它)。 */
|
|
|
+ const locate = ref({ ...EMPTY_RESULT })
|
|
|
+
|
|
|
+ function setLocate(next = {}) {
|
|
|
+ locate.value = { ...EMPTY_RESULT, ...next, at: Date.now() }
|
|
|
+ return locate.value
|
|
|
+ }
|
|
|
+
|
|
|
+ function clearLocate() {
|
|
|
+ locate.value = { ...EMPTY_RESULT }
|
|
|
+ }
|
|
|
+
|
|
|
+ /** 事件回联的可用目标:已上图的关联设备 + 地图摘要对象(与 eventStore 旧口径一致)。 */
|
|
|
+ function eventObjects() {
|
|
|
+ const associated = getAssociatedDeviceState().plan
|
|
|
+ .filter((row) => row && row.placement === 'drawn')
|
|
|
+ .map((row) => ({ ...row, kind: 'monitoring-device' }))
|
|
|
+ return [...associated, ...(mapStore.summary.objects || [])]
|
|
|
+ }
|
|
|
+
|
|
|
+ async function locateMapObject(code, options = {}) {
|
|
|
+ const objects = options.objects || mapStore.summary.objects
|
|
|
+ const mapReady = options.mapReady === undefined ? !!getCameraState() : !!options.mapReady
|
|
|
+ const flyPoint = options.flyPoint || locateMonitoringPoint
|
|
|
+ const openPoint = options.openPoint || ((value, opts) => detailStore.openPoint(value, opts))
|
|
|
+ const openDevice = options.openDevice || ((value, opts) => detailStore.openDevice(value, opts))
|
|
|
+ const selectDevice = options.selectDevice || ((value, opts) => detailStore.selectDevice(value, opts))
|
|
|
+
|
|
|
+ const plan = planLocate({ objects, pointCode: code, mapReady })
|
|
|
+ const base = {
|
|
|
+ result: plan.result,
|
|
|
+ code,
|
|
|
+ pointCode: plan.pointCode,
|
|
|
+ deviceCode: plan.deviceCode || '',
|
|
|
+ reasonText: plan.reasonText,
|
|
|
+ }
|
|
|
+ if (plan.result !== LOCATE_RESULT.OK) return setLocate(base)
|
|
|
+
|
|
|
+ let failure = TERRAIN_LOCATE_REASON.MAP
|
|
|
+ const applied = await flyPoint(plan.object, { onFailure: (reason) => { failure = reason } })
|
|
|
+ if (applied !== true) {
|
|
|
+ return setLocate({ ...base, result: LOCATE_RESULT.UNAVAILABLE, reasonText: locateFailureText(failure) })
|
|
|
+ }
|
|
|
+
|
|
|
+ // 揭示目标 = 实际飞到的地图对象(关联设备是宿主点位),由 #127 的模式计划临时放行。
|
|
|
+ const reveal = { code: plan.object.code, deviceCode: plan.deviceCode || '' }
|
|
|
+ if (plan.object.kind === MAP_OBJECT_KIND.MONITORING_POINT) {
|
|
|
+ if (plan.deviceCode) {
|
|
|
+ await openPoint(plan.object.code)
|
|
|
+ await selectDevice(plan.deviceCode, { locateTarget: reveal })
|
|
|
+ } else {
|
|
|
+ await openPoint(plan.object.code, { locateTarget: reveal })
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ await openDevice(plan.pointCode, { locateTarget: reveal })
|
|
|
+ }
|
|
|
+ return setLocate({ ...base, result: LOCATE_RESULT.OK, reasonText: '' })
|
|
|
+ }
|
|
|
+
|
|
|
+ async function locateEvent(event, options = {}) {
|
|
|
+ const objects = options.objects || eventObjects()
|
|
|
+ const mapReady = options.mapReady === undefined ? !!getCameraState() : !!options.mapReady
|
|
|
+ const flyPoint = options.flyPoint || locateMonitoringPoint
|
|
|
+ const focus = options.focus // 仅单测注入:直接接收 plan.target(旧 eventStore 的接缝)
|
|
|
+ const highlight = options.highlight || highlightMapObject
|
|
|
+
|
|
|
+ const plan = planEventLocate(event, objects, mapReady)
|
|
|
+ const base = {
|
|
|
+ result: plan.result,
|
|
|
+ code: (event && event.code) || '',
|
|
|
+ pointCode: plan.pointCode || '',
|
|
|
+ deviceCode: plan.deviceCode || '',
|
|
|
+ reasonText: plan.reasonText,
|
|
|
+ }
|
|
|
+ if (plan.result !== EVENT_DETAIL_RESULT.OK) return setLocate(base)
|
|
|
+
|
|
|
+ let failure = TERRAIN_LOCATE_REASON.MAP
|
|
|
+ const applied = typeof focus === 'function'
|
|
|
+ ? await focus(plan.target)
|
|
|
+ : await flyPoint(plan.object, { onFailure: (reason) => { failure = reason } })
|
|
|
+ if (applied !== true) {
|
|
|
+ return setLocate({ ...base, result: LOCATE_RESULT.UNAVAILABLE, reasonText: locateFailureText(failure) })
|
|
|
+ }
|
|
|
+ // 事件不新建标记;高亮设备与关联管线,一个都没命中 = 目标还没上图,如实说。
|
|
|
+ const matched = highlight(plan.codes)
|
|
|
+ if (!matched) {
|
|
|
+ return setLocate({ ...base, result: LOCATE_RESULT.UNAVAILABLE, reasonText: '关联设备尚未出现在地图上' })
|
|
|
+ }
|
|
|
+ return setLocate({ ...base, result: LOCATE_RESULT.OK, reasonText: '' })
|
|
|
+ }
|
|
|
+
|
|
|
+ async function locateFacility(target, options = {}) {
|
|
|
+ const facility = target.facility || {
|
|
|
+ kind: 'facility',
|
|
|
+ id: target.code,
|
|
|
+ category: target.category || '',
|
|
|
+ }
|
|
|
+ const result = await facilityPointStore.select(facility, target.prefill || null, {
|
|
|
+ ...options,
|
|
|
+ side: target.side,
|
|
|
+ fly: options.flyPoint || options.fly,
|
|
|
+ })
|
|
|
+ if (!result) {
|
|
|
+ return setLocate({ result: LOCATE_RESULT.UNAVAILABLE, code: target.code, reasonText: '设施定位未完成' })
|
|
|
+ }
|
|
|
+ return setLocate({
|
|
|
+ result: result.result,
|
|
|
+ code: target.code,
|
|
|
+ pointCode: result.pointCode || target.code,
|
|
|
+ reasonText: result.reasonText || '',
|
|
|
+ })
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 唯一入口:按目标身份定位。`options` 只给测试/特殊链路注入(objects / flyPoint / openPoint…),
|
|
|
+ * 业务调用方不传。
|
|
|
+ */
|
|
|
+ async function locateTarget(target = {}, options = {}) {
|
|
|
+ const kind = target.kind || 'map-object'
|
|
|
+ if (kind === 'facility') return locateFacility(target, options)
|
|
|
+ if (kind === 'event') return locateEvent(target.event, options)
|
|
|
+ return locateMapObject(target.code, options)
|
|
|
+ }
|
|
|
+
|
|
|
+ return { locate, locateTarget, clearLocate }
|
|
|
+})
|