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
113 lines
2.8 KiB
import { createAsyncThunk, createSlice, PayloadAction } from '@reduxjs/toolkit';
|
|
import {
|
|
BleList,
|
|
PeripheralStatus,
|
|
SyncItemFinish,
|
|
SyncProgress,
|
|
} from '../../services/mobileWsType';
|
|
import Bridge from '../../utils/bridge';
|
|
|
|
interface ContextState {
|
|
device: PeripheralStatus['data'];
|
|
currRailTypeId: number; // 当前选择的轨型
|
|
currOrgCode: string; // 铁路局
|
|
currGWDCode: string; // 工务段
|
|
currXMCode: string; // 线名
|
|
|
|
bleList: BleList['data'];
|
|
syncProgress: SyncProgress['data'];
|
|
syncItems: Array<SyncItemFinish['data']>;
|
|
|
|
setting: {
|
|
server: string;
|
|
};
|
|
}
|
|
|
|
const orgGwdXmStr = localStorage.getItem('org_gwd_xm');
|
|
let orgGwdXm: string[] | undefined;
|
|
if (orgGwdXmStr) {
|
|
orgGwdXm = orgGwdXmStr.split(',');
|
|
}
|
|
|
|
const initialState: ContextState = {
|
|
device: {
|
|
connected: true, //是否已连接蓝牙
|
|
power: 60, //电量
|
|
inclinatorX: 0.276, //x轴倾斜
|
|
inclinatorY: 3.019, //y轴倾斜
|
|
temperature: 32.026, //温度
|
|
},
|
|
|
|
currRailTypeId: 1,
|
|
|
|
currOrgCode: orgGwdXm ? orgGwdXm[0] : '',
|
|
currGWDCode: orgGwdXm ? orgGwdXm[1] : '',
|
|
currXMCode: orgGwdXm ? orgGwdXm[2] : '',
|
|
|
|
bleList: [],
|
|
syncProgress: {
|
|
remaining: 0,
|
|
fail: 0,
|
|
total: 0,
|
|
finish: true,
|
|
},
|
|
syncItems: [],
|
|
|
|
setting: {
|
|
server: '',
|
|
},
|
|
};
|
|
|
|
export const fetchConfig = createAsyncThunk('context/fetchConfig', async () => {
|
|
const conf = await Bridge.getConfig();
|
|
return conf.success ? conf.data : null;
|
|
});
|
|
|
|
export const saveConfig = createAsyncThunk(
|
|
'context/saveConfig',
|
|
async (param: { server: string }, thunkAPI) => {
|
|
const res = await Bridge.saveConfig(param);
|
|
res.success && thunkAPI.dispatch(fetchConfig());
|
|
return res
|
|
}
|
|
);
|
|
|
|
export const contextSlice = createSlice({
|
|
name: 'context',
|
|
initialState,
|
|
reducers: {
|
|
updateOrg: (state, action: PayloadAction<string[]>) => {
|
|
state.currOrgCode = action.payload[0];
|
|
state.currGWDCode = action.payload[1];
|
|
state.currXMCode = action.payload[2];
|
|
localStorage.setItem('org_gwd_xm', action.payload.join(','));
|
|
},
|
|
|
|
updateDevice: (state, action: PayloadAction<PeripheralStatus['data']>) => {
|
|
state.device = action.payload;
|
|
},
|
|
|
|
updateRailTypeId: (state, action: PayloadAction<number>) => {
|
|
state.currRailTypeId = action.payload;
|
|
},
|
|
|
|
updateBleList: (state, action: PayloadAction<BleList['data']>) => {
|
|
state.bleList = action.payload;
|
|
},
|
|
|
|
updateSyncProgress: (state, action: PayloadAction<SyncProgress['data']>) => {
|
|
state.syncProgress = action.payload;
|
|
},
|
|
},
|
|
extraReducers: (builder) => {
|
|
builder.addCase(fetchConfig.fulfilled, (state, action) => {
|
|
if (action.payload) {
|
|
state.setting = action.payload;
|
|
}
|
|
});
|
|
},
|
|
});
|
|
|
|
export const { updateOrg, updateDevice, updateRailTypeId, updateBleList, updateSyncProgress } =
|
|
contextSlice.actions;
|
|
export default contextSlice.reducer;
|