index.ts 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. import { InjectionKey } from 'vue'
  2. import { ActionTree, createStore, GetterTree, MutationTree, Store, StoreOptions, useStore } from 'vuex'
  3. import { EDeviceTypeName } from '../types'
  4. import { Device, DeviceHms, DeviceOsd, DeviceStatus, DockOsd, GatewayOsd, OSDVisible } from '../types/device'
  5. import { getLayers } from '/@/api/layer'
  6. import { LayerType } from '/@/types/mapLayer'
  7. import { WaylineFile } from '/@/types/wayline'
  8. import { DevicesCmdExecuteInfo } from '/@/types/device-cmd'
  9. const initStateFunc = () => ({
  10. trajectoryList: [],// 轨迹列表
  11. realTimeTrajectory: {// 实时轨迹
  12. sn: '',
  13. host: []
  14. },
  15. deviceReason: {} as {
  16. [sn: string]: any,
  17. },
  18. mapClickElement: {
  19. id: '',
  20. type: '',
  21. },
  22. Layers: [
  23. // {
  24. // name: '默认',
  25. // id: '',
  26. // is_distributed: true,
  27. // elements: [],
  28. // is_check: false,
  29. // is_select: false,
  30. // type: 1
  31. // },
  32. // {
  33. // name: '分享',
  34. // id: '',
  35. // is_distributed: true,
  36. // elements: [],
  37. // is_check: false,
  38. // is_select: false,
  39. // type: 2
  40. // }
  41. ],
  42. layerBaseInfo: {} as {
  43. [key: string]: string
  44. },
  45. livestreamOthersVisible: false,
  46. livestreamAgoraVisible: false,
  47. coverMap: {} as {
  48. [key: string]: any[]
  49. },
  50. wsEvent: {
  51. mapElementCreat: {},
  52. mapElementUpdate: {},
  53. mapElementDelete: {}
  54. },
  55. deviceStatusEvent: {
  56. deviceOnline: {} as DeviceStatus,
  57. deviceOffline: {}
  58. },
  59. markerInfo: {
  60. coverMap: {} as {
  61. [sn: string]: any
  62. },
  63. pathMap: {} as {
  64. [sn: string]: any[]
  65. }
  66. },
  67. deviceState: {
  68. // remote controller, dock
  69. gatewayInfo: {} as {
  70. [sn: string]: GatewayOsd
  71. },
  72. // drone
  73. deviceInfo: {} as {
  74. [sn: string]: DeviceOsd
  75. },
  76. dockInfo: {} as {
  77. [sn: string]: DockOsd
  78. },
  79. currentSn: '',
  80. currentType: -1
  81. },
  82. osdVisible: { // osd 显示设备相关信息
  83. sn: '',
  84. callsign: '',
  85. model: '',
  86. visible: false,
  87. gateway_sn: '',
  88. is_dock: false,
  89. payloads: null
  90. } as OSDVisible,
  91. waylineInfo: {
  92. } as WaylineFile,
  93. dockInfo: {
  94. } as Device,
  95. hmsInfo: {} as {
  96. [sn: string]: DeviceHms[]
  97. },
  98. // 机场指令执行状态信息
  99. devicesCmdExecuteInfo: {
  100. } as DevicesCmdExecuteInfo,
  101. mqttState: null as any, // mqtt 实例
  102. clientId: '', // mqtt 连接 唯一客户端id
  103. })
  104. export type RootStateType = ReturnType<typeof initStateFunc>
  105. const getters: GetterTree<RootStateType, RootStateType> = {
  106. }
  107. const mutations: MutationTree<RootStateType> = {
  108. SET_TRAJECTORY_LIST(state, list) {
  109. state.trajectoryList = list
  110. },
  111. SET_REAL_TIME_TRAJECTORY(state, info) {
  112. state.realTimeTrajectory = info
  113. },
  114. SET_DEVICE_REASON(state, info) {
  115. state.deviceReason[info.sn] = info
  116. },
  117. SET_MAP_CLICK_ELEMENT(state, info) {
  118. state.mapClickElement = info
  119. },
  120. SET_LAYER_INFO(state, info) {
  121. state.Layers = info
  122. },
  123. SET_DEVICE_INFO(state, info) {
  124. state.deviceState.deviceInfo[info.sn] = info.host
  125. state.deviceState.currentSn = info.sn
  126. state.deviceState.currentType = EDeviceTypeName.Aircraft
  127. },
  128. SET_GATEWAY_INFO(state, info) {
  129. state.deviceState.gatewayInfo[info.sn] = info.host
  130. state.deviceState.currentSn = info.sn
  131. state.deviceState.currentType = EDeviceTypeName.Gateway
  132. },
  133. SET_DOCK_INFO(state, info) {
  134. if (Object.keys(info.host).length === 0) {
  135. return
  136. }
  137. if (!state.deviceState.dockInfo[info.sn]) {
  138. state.deviceState.dockInfo[info.sn] = {} as DockOsd
  139. }
  140. state.deviceState.currentSn = info.sn
  141. state.deviceState.currentType = EDeviceTypeName.Dock
  142. const dock = state.deviceState.dockInfo[info.sn]
  143. if (info.host.mode_code !== undefined) {
  144. dock.basic_osd = info.host
  145. return
  146. }
  147. if (info.host.wireless_link) {
  148. dock.link_osd = info.host
  149. return
  150. }
  151. if (info.host.job_number !== undefined) {
  152. dock.work_osd = info.host
  153. }
  154. },
  155. SET_LIVESTREAM_OTHERS_VISIBLE(state, bool) {
  156. state.livestreamOthersVisible = bool
  157. },
  158. SET_LIVESTREAM_AGORA_VISIBLE(state, bool) {
  159. state.livestreamAgoraVisible = bool
  160. },
  161. SET_MAP_ELEMENT_CREATE(state, info) {
  162. state.wsEvent.mapElementCreat = info
  163. },
  164. SET_MAP_ELEMENT_UPDATE(state, info) {
  165. state.wsEvent.mapElementUpdate = info
  166. },
  167. SET_MAP_ELEMENT_DELETE(state, info) {
  168. state.wsEvent.mapElementDelete = info
  169. },
  170. SET_DEVICE_ONLINE(state, info) {
  171. state.deviceStatusEvent.deviceOnline = info
  172. },
  173. SET_DEVICE_OFFLINE(state, info) {
  174. state.deviceStatusEvent.deviceOffline = info
  175. delete state.deviceState.gatewayInfo[info.sn]
  176. delete state.deviceState.deviceInfo[info.sn]
  177. delete state.deviceState.dockInfo[info.sn]
  178. delete state.hmsInfo[info.sn]
  179. // delete state.markerInfo.coverMap[info.sn]
  180. // delete state.markerInfo.pathMap[info.sn]
  181. },
  182. SET_OSD_VISIBLE_INFO(state, info) {
  183. state.osdVisible = info
  184. },
  185. SET_SELECT_WAYLINE_INFO(state, info) {
  186. state.waylineInfo = info
  187. },
  188. SET_SELECT_DOCK_INFO(state, info) {
  189. state.dockInfo = info
  190. },
  191. SET_DEVICE_HMS_INFO(state, info) {
  192. const hmsList: Array<DeviceHms> = state.hmsInfo[info.sn]
  193. state.hmsInfo[info.sn] = info.host.concat(hmsList ?? [])
  194. },
  195. SET_DEVICES_CMD_EXECUTE_INFO(state, info) { // 保存设备指令ws消息推送
  196. if (!info.sn) {
  197. return
  198. }
  199. if (state.devicesCmdExecuteInfo[info.sn]) {
  200. const index = state.devicesCmdExecuteInfo[info.sn].findIndex(cmdExecuteInfo => cmdExecuteInfo.biz_code === info.biz_code)
  201. if (index >= 0) {
  202. // 丢弃前面的消息
  203. if (state.devicesCmdExecuteInfo[info.sn][index].timestamp > info.timestamp) {
  204. return
  205. }
  206. state.devicesCmdExecuteInfo[info.sn][index] = info
  207. } else {
  208. state.devicesCmdExecuteInfo[info.sn].push(info)
  209. }
  210. } else {
  211. state.devicesCmdExecuteInfo[info.sn] = [info]
  212. }
  213. },
  214. SET_MQTT_STATE(state, mqttState) {
  215. state.mqttState = mqttState
  216. },
  217. SET_CLIENT_ID(state, clientId) {
  218. state.clientId = clientId
  219. },
  220. }
  221. const actions: ActionTree<RootStateType, RootStateType> = {
  222. async getAllElement({ commit }) {
  223. const result = await getLayers({
  224. groupId: '',
  225. isDistributed: true
  226. })
  227. commit('SET_LAYER_INFO', result.data?.list)
  228. },
  229. updateElement({ state }, content: { type: 'is_check' | 'is_select', id: string, bool: boolean }) {
  230. const key = content.id.replaceAll('resource__', '')
  231. const type = content.type
  232. const layers = state.Layers
  233. const layer = layers.find(item => item.id === key)
  234. if (layer) {
  235. layer[type] = content.bool
  236. }
  237. },
  238. setLayerInfo({ state }, layers) {
  239. // const layers = state.Layers
  240. const obj: {
  241. [key: string]: string
  242. } = {}
  243. layers.forEach(layer => {
  244. if (layer.type === LayerType.Default) {
  245. obj.default = layer.id
  246. } else {
  247. if (layer.type === LayerType.Share) {
  248. obj.share = layer.id
  249. }
  250. }
  251. })
  252. state.layerBaseInfo = obj
  253. },
  254. getLayerInfo({ state }, id: string) {
  255. return state.layerBaseInfo[id]
  256. }
  257. }
  258. const storeOptions: StoreOptions<RootStateType> = {
  259. state: initStateFunc,
  260. getters,
  261. mutations,
  262. actions
  263. }
  264. const rootStore = createStore(storeOptions)
  265. export default rootStore
  266. export const storeKey: InjectionKey<Store<RootStateType>> = Symbol('')
  267. type AllStateStoreTypes = RootStateType & {
  268. // moduleName: moduleType
  269. }
  270. export function useMyStore<T = AllStateStoreTypes>() {
  271. return useStore<T>(storeKey)
  272. }