You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

135 lines
4.0 KiB

5 months ago
2 months ago
2 months ago
2 months ago
2 months ago
2 months ago
  1. import React, { useEffect } from 'react';
  2. import { Outlet } from 'react-router';
  3. import { Toast } from 'antd-mobile';
  4. import Bridge, { bridgeOb, emitBridgeEvent } from './utils/bridge';
  5. import { useAppDispatch, useAppSelector } from './utils/hooks';
  6. import { addNewPoint, updateTaskState, updateVersion, updateMeasureFinish } from './store/features/measureSlice';
  7. import {
  8. refreshStationList,
  9. updateBleList,
  10. updateDevice,
  11. updateSyncProgress,
  12. } from './store/features/contextSlice';
  13. import { createWebSocket, sharedWsUrl } from './services/socket';
  14. import {
  15. fetchOrgTree,
  16. fetchRailTypes,
  17. syncBaseData,
  18. updateSyncBaseProgress,
  19. } from './store/features/baseData';
  20. function App() {
  21. const dispatch = useAppDispatch();
  22. const baseState = useAppSelector((state) => state.baseData);
  23. const contextState = useAppSelector((state) => state.context);
  24. // 获取轨型数据
  25. useEffect(() => {
  26. if (baseState.railTypes.length === 0) {
  27. dispatch(fetchRailTypes());
  28. }
  29. }, [baseState.railTypes.length, dispatch]);
  30. // 监听推送消息,更新store
  31. useEffect(() => {
  32. const subscription = bridgeOb.subscribe((datagram) => {
  33. if (datagram.type === 'measure-event') {
  34. dispatch(updateTaskState(datagram.data));
  35. } else if (datagram.type === 'measure-point') {
  36. dispatch(addNewPoint(datagram.data));
  37. } else if (datagram.type === 'peripheral-status') {
  38. dispatch(updateDevice(datagram.data));
  39. } else if (datagram.type === 'ble-list') {
  40. dispatch(updateBleList(datagram.data));
  41. } else if (datagram.type === 'sync-progress') {
  42. dispatch(updateSyncProgress(datagram.data));
  43. } else if (datagram.type === 'update-base-progress') {
  44. dispatch(updateSyncBaseProgress(datagram.data));
  45. }else if (datagram.type === 'measure-finished') {
  46. dispatch(updateMeasureFinish(datagram.data));
  47. }else if (datagram.type === 'msg-toast') {
  48. // @ts-ignore
  49. Toast.show({
  50. content: datagram.data,
  51. position: 'bottom',
  52. duration: 5000
  53. })
  54. }
  55. });
  56. return () => subscription.unsubscribe();
  57. }, [dispatch]);
  58. // 开启推送
  59. useEffect(() => {
  60. Bridge.getVersion().then((res) => {
  61. if (res.success) {
  62. dispatch(updateVersion(res.data))
  63. }
  64. });
  65. // if (appWebview) {
  66. // registerBridgeFunc();
  67. // } else {
  68. //连接websocket
  69. const wsClient = createWebSocket(sharedWsUrl);
  70. const subscription = wsClient.dataOb.subscribe((data) => {
  71. emitBridgeEvent(data);
  72. });
  73. wsClient.connect();
  74. return () => subscription.unsubscribe();
  75. // }
  76. }, []);
  77. // 初始同步数据
  78. useEffect(() => {
  79. Bridge.needSyncBaseData().then((res) => {
  80. if (!res.success) {
  81. return Toast.show(res.message);
  82. }
  83. if (res.data.needSync) {
  84. dispatch(syncBaseData());
  85. } else {
  86. dispatch(fetchOrgTree());
  87. }
  88. });
  89. }, [dispatch]);
  90. // 同步完成后,重新获取局/段/线信息
  91. useEffect(() => {
  92. if (
  93. baseState.syncBaseProgress.finish &&
  94. !baseState.syncBaseProgress.error &&
  95. baseState.syncBaseProgress.progress === 100
  96. ) {
  97. dispatch(fetchOrgTree());
  98. }
  99. }, [baseState.syncBaseProgress, dispatch]);
  100. // 预获取车站信息
  101. useEffect(() => {
  102. if (contextState.currXMCode) {
  103. dispatch(
  104. refreshStationList({
  105. tljCode: contextState.currOrgCode,
  106. gwdCode: contextState.currGWDCode,
  107. xmCode: contextState.currXMCode,
  108. })
  109. );
  110. }
  111. }, [contextState.currGWDCode, contextState.currOrgCode, contextState.currXMCode, dispatch]);
  112. return (
  113. <>
  114. <Outlet />
  115. {/* <Mask visible={baseState.syncingBaseData}>
  116. <div className="flex flex-col items-center gap-4 absolute left-1/2 top-1/2 -translate-x-1/2 -translate-y-1/2">
  117. <SpinLoading color="white" />
  118. <p className="text-white text-sm">...</p>
  119. </div>
  120. </Mask> */}
  121. </>
  122. );
  123. }
  124. export default App;