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
182 lines
5.0 KiB
import { createAsyncThunk, createSlice, PayloadAction } from '@reduxjs/toolkit';
|
|
import {
|
|
BleList,
|
|
PeripheralStatus,
|
|
SyncItemFinish,
|
|
SyncProgress,
|
|
} from '../../services/mobileWsType';
|
|
import Bridge from '../../utils/bridge';
|
|
import { ReqStatus, SettingDTO, StationLabelItem } from '../../services/apiTypes';
|
|
|
|
interface ContextState {
|
|
device: PeripheralStatus['data'];
|
|
currRailTypeId: number; // 当前选择的轨型
|
|
currOrgCode: string; // 铁路局
|
|
currGWDCode: string; // 工务段
|
|
currXMCode: string; // 线名
|
|
stationList: StationLabelItem[];
|
|
|
|
bleList: BleList['data'];
|
|
syncProgress: SyncProgress['data'];
|
|
syncItems: Array<SyncItemFinish['data']>;
|
|
|
|
setting: SettingDTO;
|
|
settingReqStatus: ReqStatus;
|
|
error?: string;
|
|
}
|
|
|
|
const orgGwdXmStr = localStorage.getItem('org_gwd_xm');
|
|
let orgGwdXm: string[] | undefined;
|
|
if (orgGwdXmStr) {
|
|
orgGwdXm = orgGwdXmStr.split(',');
|
|
}
|
|
|
|
const initialState: ContextState = {
|
|
device: {
|
|
flag: 0,
|
|
connected: true, //是否已连接蓝牙
|
|
power: 100, //电量
|
|
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] : '',
|
|
stationList: [],
|
|
|
|
bleList: [],
|
|
syncProgress: {
|
|
remaining: 0,
|
|
fail: 0,
|
|
total: 0,
|
|
status: 'finished',
|
|
},
|
|
syncItems: [],
|
|
|
|
setting: {
|
|
server: '',
|
|
standbyMinutes: 20,
|
|
},
|
|
settingReqStatus: 'idle',
|
|
error: undefined,
|
|
};
|
|
|
|
export const fetchConfig = createAsyncThunk('context/fetchConfig', async (_, thunkAPI) => {
|
|
const conf = await Bridge.getConfig();
|
|
return conf.success ? conf.data : Promise.reject(conf.message);
|
|
});
|
|
|
|
export const saveConfig = createAsyncThunk(
|
|
'context/saveConfig',
|
|
async (param: SettingDTO, thunkAPI) => {
|
|
const res = await Bridge.saveConfig(param);
|
|
// res.success && thunkAPI.dispatch(fetchConfig());
|
|
return res.success ? res : Promise.reject(res.message);
|
|
}
|
|
);
|
|
|
|
export const refreshSyncProgress = createAsyncThunk('context/refreshSync', async () => {
|
|
const res = await Bridge.getSyncProcess();
|
|
return res.success ? res.data : null;
|
|
});
|
|
|
|
export const refreshStationList = createAsyncThunk(
|
|
'context/refreshStationList',
|
|
async (params: { tljCode: string; gwdCode: string; xmCode: string }) => {
|
|
return await Bridge.getStationList(params);
|
|
}
|
|
);
|
|
|
|
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;
|
|
},
|
|
|
|
updateBleLinkStatus: (state, action: PayloadAction<{ mac: string; link: boolean }>) => {
|
|
state.bleList = state.bleList.map((ble) => {
|
|
if (ble.mac === action.payload.mac) {
|
|
ble.linked = action.payload.link;
|
|
}
|
|
return ble;
|
|
});
|
|
},
|
|
|
|
updateSyncProgress: (state, action: PayloadAction<SyncProgress['data']>) => {
|
|
state.syncProgress = action.payload;
|
|
},
|
|
|
|
resetSettingReqStatus: (state) => {
|
|
state.settingReqStatus = 'idle';
|
|
state.error = undefined;
|
|
},
|
|
},
|
|
extraReducers: (builder) => {
|
|
builder.addCase(fetchConfig.pending, (state) => {
|
|
state.settingReqStatus = 'loading';
|
|
});
|
|
builder.addCase(fetchConfig.fulfilled, (state, action) => {
|
|
state.setting = action.payload;
|
|
state.settingReqStatus = 'succeeded';
|
|
});
|
|
builder.addCase(fetchConfig.rejected, (state, action) => {
|
|
state.settingReqStatus = 'failed';
|
|
state.error = action.error.message;
|
|
});
|
|
builder.addCase(saveConfig.pending, (state) => {
|
|
state.settingReqStatus = 'loading';
|
|
});
|
|
builder.addCase(saveConfig.fulfilled, (state, action) => {
|
|
state.settingReqStatus = 'succeeded';
|
|
});
|
|
builder.addCase(saveConfig.rejected, (state, action) => {
|
|
state.settingReqStatus = 'failed';
|
|
state.error = action.error.message;
|
|
});
|
|
builder.addCase(refreshSyncProgress.fulfilled, (state, action) => {
|
|
if (action.payload) {
|
|
state.syncProgress = action.payload;
|
|
}
|
|
});
|
|
builder.addCase(refreshStationList.fulfilled, (state, action) => {
|
|
if (action.payload.success) {
|
|
state.stationList = action.payload.data.map((item) => ({
|
|
label: item.value,
|
|
value: item.key,
|
|
}));
|
|
}
|
|
});
|
|
},
|
|
});
|
|
|
|
export const {
|
|
updateOrg,
|
|
updateDevice,
|
|
updateRailTypeId,
|
|
updateBleList,
|
|
updateBleLinkStatus,
|
|
updateSyncProgress,
|
|
resetSettingReqStatus,
|
|
} = contextSlice.actions;
|
|
export default contextSlice.reducer;
|