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.

182 lines
5.0 KiB

4 months ago
4 months ago
4 months ago
4 months ago
4 months ago
4 months ago
4 months ago
4 months ago
4 months ago
4 months ago
4 months ago
4 months ago
4 months ago
4 months ago
4 months ago
4 months ago
4 months ago
4 months ago
4 months ago
4 months ago
4 months ago
4 months ago
4 months ago
4 months ago
  1. import { createAsyncThunk, createSlice, PayloadAction } from '@reduxjs/toolkit';
  2. import {
  3. BleList,
  4. PeripheralStatus,
  5. SyncItemFinish,
  6. SyncProgress,
  7. } from '../../services/mobileWsType';
  8. import Bridge from '../../utils/bridge';
  9. import { ReqStatus, SettingDTO, StationLabelItem } from '../../services/apiTypes';
  10. interface ContextState {
  11. device: PeripheralStatus['data'];
  12. currRailTypeId: number; // 当前选择的轨型
  13. currOrgCode: string; // 铁路局
  14. currGWDCode: string; // 工务段
  15. currXMCode: string; // 线名
  16. stationList: StationLabelItem[];
  17. bleList: BleList['data'];
  18. syncProgress: SyncProgress['data'];
  19. syncItems: Array<SyncItemFinish['data']>;
  20. setting: SettingDTO;
  21. settingReqStatus: ReqStatus;
  22. error?: string;
  23. }
  24. const orgGwdXmStr = localStorage.getItem('org_gwd_xm');
  25. let orgGwdXm: string[] | undefined;
  26. if (orgGwdXmStr) {
  27. orgGwdXm = orgGwdXmStr.split(',');
  28. }
  29. const initialState: ContextState = {
  30. device: {
  31. flag: 0,
  32. connected: true, //是否已连接蓝牙
  33. power: 100, //电量
  34. inclinatorX: 0.276, //x轴倾斜
  35. inclinatorY: 3.019, //y轴倾斜
  36. temperature: 32.026, //温度
  37. },
  38. currRailTypeId: 1,
  39. currOrgCode: orgGwdXm ? orgGwdXm[0] : '',
  40. currGWDCode: orgGwdXm ? orgGwdXm[1] : '',
  41. currXMCode: orgGwdXm ? orgGwdXm[2] : '',
  42. stationList: [],
  43. bleList: [],
  44. syncProgress: {
  45. remaining: 0,
  46. fail: 0,
  47. total: 0,
  48. status: 'finished',
  49. },
  50. syncItems: [],
  51. setting: {
  52. server: '',
  53. standbyMinutes: 20,
  54. },
  55. settingReqStatus: 'idle',
  56. error: undefined,
  57. };
  58. export const fetchConfig = createAsyncThunk('context/fetchConfig', async (_, thunkAPI) => {
  59. const conf = await Bridge.getConfig();
  60. return conf.success ? conf.data : Promise.reject(conf.message);
  61. });
  62. export const saveConfig = createAsyncThunk(
  63. 'context/saveConfig',
  64. async (param: SettingDTO, thunkAPI) => {
  65. const res = await Bridge.saveConfig(param);
  66. // res.success && thunkAPI.dispatch(fetchConfig());
  67. return res.success ? res : Promise.reject(res.message);
  68. }
  69. );
  70. export const refreshSyncProgress = createAsyncThunk('context/refreshSync', async () => {
  71. const res = await Bridge.getSyncProcess();
  72. return res.success ? res.data : null;
  73. });
  74. export const refreshStationList = createAsyncThunk(
  75. 'context/refreshStationList',
  76. async (params: { tljCode: string; gwdCode: string; xmCode: string }) => {
  77. return await Bridge.getStationList(params);
  78. }
  79. );
  80. export const contextSlice = createSlice({
  81. name: 'context',
  82. initialState,
  83. reducers: {
  84. updateOrg: (state, action: PayloadAction<string[]>) => {
  85. state.currOrgCode = action.payload[0];
  86. state.currGWDCode = action.payload[1];
  87. state.currXMCode = action.payload[2];
  88. localStorage.setItem('org_gwd_xm', action.payload.join(','));
  89. },
  90. updateDevice: (state, action: PayloadAction<PeripheralStatus['data']>) => {
  91. state.device = action.payload;
  92. },
  93. updateRailTypeId: (state, action: PayloadAction<number>) => {
  94. state.currRailTypeId = action.payload;
  95. },
  96. updateBleList: (state, action: PayloadAction<BleList['data']>) => {
  97. state.bleList = action.payload;
  98. },
  99. updateBleLinkStatus: (state, action: PayloadAction<{ mac: string; link: boolean }>) => {
  100. state.bleList = state.bleList.map((ble) => {
  101. if (ble.mac === action.payload.mac) {
  102. ble.linked = action.payload.link;
  103. }
  104. return ble;
  105. });
  106. },
  107. updateSyncProgress: (state, action: PayloadAction<SyncProgress['data']>) => {
  108. state.syncProgress = action.payload;
  109. },
  110. resetSettingReqStatus: (state) => {
  111. state.settingReqStatus = 'idle';
  112. state.error = undefined;
  113. },
  114. },
  115. extraReducers: (builder) => {
  116. builder.addCase(fetchConfig.pending, (state) => {
  117. state.settingReqStatus = 'loading';
  118. });
  119. builder.addCase(fetchConfig.fulfilled, (state, action) => {
  120. state.setting = action.payload;
  121. state.settingReqStatus = 'succeeded';
  122. });
  123. builder.addCase(fetchConfig.rejected, (state, action) => {
  124. state.settingReqStatus = 'failed';
  125. state.error = action.error.message;
  126. });
  127. builder.addCase(saveConfig.pending, (state) => {
  128. state.settingReqStatus = 'loading';
  129. });
  130. builder.addCase(saveConfig.fulfilled, (state, action) => {
  131. state.settingReqStatus = 'succeeded';
  132. });
  133. builder.addCase(saveConfig.rejected, (state, action) => {
  134. state.settingReqStatus = 'failed';
  135. state.error = action.error.message;
  136. });
  137. builder.addCase(refreshSyncProgress.fulfilled, (state, action) => {
  138. if (action.payload) {
  139. state.syncProgress = action.payload;
  140. }
  141. });
  142. builder.addCase(refreshStationList.fulfilled, (state, action) => {
  143. if (action.payload.success) {
  144. state.stationList = action.payload.data.map((item) => ({
  145. label: item.value,
  146. value: item.key,
  147. }));
  148. }
  149. });
  150. },
  151. });
  152. export const {
  153. updateOrg,
  154. updateDevice,
  155. updateRailTypeId,
  156. updateBleList,
  157. updateBleLinkStatus,
  158. updateSyncProgress,
  159. resetSettingReqStatus,
  160. } = contextSlice.actions;
  161. export default contextSlice.reducer;