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.

121 lines
3.5 KiB

5 months ago
4 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 } 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. }
  46. });
  47. return () => subscription.unsubscribe();
  48. }, [dispatch]);
  49. // 开启推送
  50. useEffect(() => {
  51. // if (appWebview) {
  52. // registerBridgeFunc();
  53. // } else {
  54. //连接websocket
  55. const wsClient = createWebSocket(sharedWsUrl);
  56. const subscription = wsClient.dataOb.subscribe((data) => {
  57. emitBridgeEvent(data);
  58. });
  59. wsClient.connect();
  60. return () => subscription.unsubscribe();
  61. // }
  62. }, []);
  63. // 初始同步数据
  64. useEffect(() => {
  65. Bridge.needSyncBaseData().then((res) => {
  66. if (!res.success) {
  67. return Toast.show(res.message);
  68. }
  69. if (res.data.needSync) {
  70. dispatch(syncBaseData());
  71. } else {
  72. dispatch(fetchOrgTree());
  73. }
  74. });
  75. }, [dispatch]);
  76. // 同步完成后,重新获取局/段/线信息
  77. useEffect(() => {
  78. if (
  79. baseState.syncBaseProgress.finish &&
  80. !baseState.syncBaseProgress.error &&
  81. baseState.syncBaseProgress.progress === 100
  82. ) {
  83. dispatch(fetchOrgTree());
  84. }
  85. }, [baseState.syncBaseProgress, dispatch]);
  86. // 预获取车站信息
  87. useEffect(() => {
  88. if (contextState.currXMCode) {
  89. dispatch(
  90. refreshStationList({
  91. tljCode: contextState.currOrgCode,
  92. gwdCode: contextState.currGWDCode,
  93. xmCode: contextState.currXMCode,
  94. })
  95. );
  96. }
  97. }, [contextState.currGWDCode, contextState.currOrgCode, contextState.currXMCode, dispatch]);
  98. return (
  99. <>
  100. <Outlet />
  101. {/* <Mask visible={baseState.syncingBaseData}>
  102. <div className="flex flex-col items-center gap-4 absolute left-1/2 top-1/2 -translate-x-1/2 -translate-y-1/2">
  103. <SpinLoading color="white" />
  104. <p className="text-white text-sm">...</p>
  105. </div>
  106. </Mask> */}
  107. </>
  108. );
  109. }
  110. export default App;