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.

113 lines
2.8 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
  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. interface ContextState {
  10. device: PeripheralStatus['data'];
  11. currRailTypeId: number; // 当前选择的轨型
  12. currOrgCode: string; // 铁路局
  13. currGWDCode: string; // 工务段
  14. currXMCode: string; // 线名
  15. bleList: BleList['data'];
  16. syncProgress: SyncProgress['data'];
  17. syncItems: Array<SyncItemFinish['data']>;
  18. setting: {
  19. server: string;
  20. };
  21. }
  22. const orgGwdXmStr = localStorage.getItem('org_gwd_xm');
  23. let orgGwdXm: string[] | undefined;
  24. if (orgGwdXmStr) {
  25. orgGwdXm = orgGwdXmStr.split(',');
  26. }
  27. const initialState: ContextState = {
  28. device: {
  29. connected: true, //是否已连接蓝牙
  30. power: 60, //电量
  31. inclinatorX: 0.276, //x轴倾斜
  32. inclinatorY: 3.019, //y轴倾斜
  33. temperature: 32.026, //温度
  34. },
  35. currRailTypeId: 1,
  36. currOrgCode: orgGwdXm ? orgGwdXm[0] : '',
  37. currGWDCode: orgGwdXm ? orgGwdXm[1] : '',
  38. currXMCode: orgGwdXm ? orgGwdXm[2] : '',
  39. bleList: [],
  40. syncProgress: {
  41. remaining: 0,
  42. fail: 0,
  43. total: 0,
  44. finish: true,
  45. },
  46. syncItems: [],
  47. setting: {
  48. server: '',
  49. },
  50. };
  51. export const fetchConfig = createAsyncThunk('context/fetchConfig', async () => {
  52. const conf = await Bridge.getConfig();
  53. return conf.success ? conf.data : null;
  54. });
  55. export const saveConfig = createAsyncThunk(
  56. 'context/saveConfig',
  57. async (param: { server: string }, thunkAPI) => {
  58. const res = await Bridge.saveConfig(param);
  59. res.success && thunkAPI.dispatch(fetchConfig());
  60. return res
  61. }
  62. );
  63. export const contextSlice = createSlice({
  64. name: 'context',
  65. initialState,
  66. reducers: {
  67. updateOrg: (state, action: PayloadAction<string[]>) => {
  68. state.currOrgCode = action.payload[0];
  69. state.currGWDCode = action.payload[1];
  70. state.currXMCode = action.payload[2];
  71. localStorage.setItem('org_gwd_xm', action.payload.join(','));
  72. },
  73. updateDevice: (state, action: PayloadAction<PeripheralStatus['data']>) => {
  74. state.device = action.payload;
  75. },
  76. updateRailTypeId: (state, action: PayloadAction<number>) => {
  77. state.currRailTypeId = action.payload;
  78. },
  79. updateBleList: (state, action: PayloadAction<BleList['data']>) => {
  80. state.bleList = action.payload;
  81. },
  82. updateSyncProgress: (state, action: PayloadAction<SyncProgress['data']>) => {
  83. state.syncProgress = action.payload;
  84. },
  85. },
  86. extraReducers: (builder) => {
  87. builder.addCase(fetchConfig.fulfilled, (state, action) => {
  88. if (action.payload) {
  89. state.setting = action.payload;
  90. }
  91. });
  92. },
  93. });
  94. export const { updateOrg, updateDevice, updateRailTypeId, updateBleList, updateSyncProgress } =
  95. contextSlice.actions;
  96. export default contextSlice.reducer;