primaryArea.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. /**
  2. * 主场景区计划(票 36):把配置里的 `map.primaryArea` 翻译成「限位与自建雾能不能开、开在哪个矩形」。
  3. *
  4. * 纯函数、不 import cesium,和 domain/basemap.js、domain/mapLayers.js 同一模式:
  5. * 逻辑脚本因此能脱浏览器把四类输入(合法 / 顺序写反 / 缺字段 / 数值个数不对)都测到。
  6. *
  7. * 契约来源 —— 决策票 `cockpit-primary-area-guardrails/issues/01` 的 Answer(Q1/Q2/Q3),
  8. * 本模块只消费,不重新解释:
  9. * · `map.primaryArea.bounds` = `[west, south, east, north]`(度),与 `map.overlays[].bounds` 同口径;
  10. * · **部署配置自己写好归一化后的顺序**(甲方给的 `[W, N, E, S]` 在准备配置时转换),
  11. * 运行时代码**绝不自动翻转**,校验要求 `west < east`、`south < north`;
  12. * · 未配置 → 关闭依赖主场景区的区域限位与自建雾(票 37 / 38);顺序非法同样走关闭路径,
  13. * 原因进配置问题清单、也进浮标与 `data-*` 读数。
  14. *
  15. * 为什么「顺序写反」必须走关闭而不是兜底翻转:翻对了没人发现配置是错的,翻错了
  16. * (经纬度真的写乱)就是拿一段假范围去锁相机。两条路都会让部署问题在运行期隐身。
  17. */
  18. /** 主场景区三态(写进宿主 `data-map-primary-area`,也是 getLifecycleStats().primaryArea 的取值)。 */
  19. export const PRIMARY_AREA_STATE = {
  20. READY: 'ready',
  21. MISSING: 'missing',
  22. INVALID: 'invalid',
  23. }
  24. /** 浮标与提示语里用的短名字(CONTEXT.md「主场景区」)。 */
  25. export const PRIMARY_AREA_LABEL = '主场景区'
  26. /** 关闭路径的统一后半句:让人一眼看出「不只是没配,是两个能力一起没了」。 */
  27. export const PRIMARY_AREA_OFF_SUFFIX = '限位与雾气关闭'
  28. /**
  29. * 关闭原因的唯一措辞:配置问题清单(`map.primaryArea.bounds …,已按关闭处理`)与引擎浮标
  30. * (`主场景区:… · 限位与雾气关闭`)都从这一份取,两边不会各写各的,原因也不会在链路上丢。
  31. */
  32. export const PRIMARY_AREA_REASON = {
  33. MISSING: '未配置',
  34. SHAPE: 'bounds 应为四个有限数字',
  35. ORDER: '顺序应为 [西,南,东,北],不自动翻转',
  36. }
  37. /** 度 → 公里的粗略换算(只用于浮标上的人读尺寸,不参与任何判定)。 */
  38. const KM_PER_DEG = 111.32
  39. /** 四个有限数字才算形状合法;顺序另判(见下)。 */
  40. function isBoundsShape(value) {
  41. return Array.isArray(value) && value.length === 4 && value.every((n) => typeof n === 'number' && Number.isFinite(n))
  42. }
  43. function withState(state, extra) {
  44. return {
  45. state,
  46. enabled: state === PRIMARY_AREA_STATE.READY,
  47. bounds: null,
  48. reason: '',
  49. sizeText: '',
  50. ...extra,
  51. }
  52. }
  53. /**
  54. * 主场景区计划:**顺序合法性的唯一判据**(配置归一化也调它,不写第二份)。
  55. * @param {object} mapConfig 归一化后的 `config.map`
  56. * @returns {{state: string, enabled: boolean, bounds: number[]|null, reason: string, sizeText: string}}
  57. */
  58. export function planPrimaryArea(mapConfig = {}) {
  59. const area = (mapConfig && mapConfig.primaryArea) || {}
  60. const raw = area.bounds
  61. // 归一化层给一个显式 `configured`,区分「部署没写这个字段」与「写了但里面是空的」:
  62. // 前者是「未配置」,后者是**写错了**,必须报出来而不是当成没配(否则 `primaryArea: {}`
  63. // 会被读成一句无害的省略)。直接调用本函数(逻辑脚本)时按「有没有给 bounds」判断。
  64. const configured = typeof area.configured === 'boolean' ? area.configured : raw !== undefined && raw !== null
  65. if (!configured) {
  66. return withState(PRIMARY_AREA_STATE.MISSING, { reason: PRIMARY_AREA_REASON.MISSING })
  67. }
  68. if (!isBoundsShape(raw)) {
  69. return withState(PRIMARY_AREA_STATE.INVALID, { reason: PRIMARY_AREA_REASON.SHAPE })
  70. }
  71. const [west, south, east, north] = raw
  72. if (!(west < east) || !(south < north)) {
  73. return withState(PRIMARY_AREA_STATE.INVALID, { reason: PRIMARY_AREA_REASON.ORDER })
  74. }
  75. return withState(PRIMARY_AREA_STATE.READY, {
  76. bounds: raw.slice(),
  77. sizeText: sizeTextOf(west, south, east, north),
  78. })
  79. }
  80. /** 矩形的近似尺寸(东西按中纬度收窄)。仅供人读。 */
  81. function sizeTextOf(west, south, east, north) {
  82. const midLat = (south + north) / 2
  83. const widthKm = (east - west) * KM_PER_DEG * Math.cos((midLat * Math.PI) / 180)
  84. const heightKm = (north - south) * KM_PER_DEG
  85. return `${widthKm.toFixed(1)}×${heightKm.toFixed(1)} km`
  86. }
  87. /**
  88. * 引擎浮标上的主场景区措辞(票 36 验收第 4 条:生效与关闭都要人读得到)。
  89. * 生效说尺寸;关闭说原因 + 「限位与雾气关闭」,不把「没配」写成一句无害的省略。
  90. * @param {object} plan planPrimaryArea() 的结果
  91. */
  92. export function primaryAreaStatusText(plan) {
  93. const current = plan || planPrimaryArea({})
  94. if (current.enabled) return `${PRIMARY_AREA_LABEL}:${current.sizeText}`
  95. // 未配置时不再复述一遍原因("未配置(未配置)"是把同一个词说了两次);
  96. // 配错时原因要留着 —— 个数错与顺序错是两种修法,排查时不能只看到一句"无效"。
  97. const head =
  98. current.state === PRIMARY_AREA_STATE.INVALID ? `配置无效(${current.reason})` : PRIMARY_AREA_REASON.MISSING
  99. return `${PRIMARY_AREA_LABEL}:${head} · ${PRIMARY_AREA_OFF_SUFFIX}`
  100. }