rt-sim-training-client/src/store/modules/training.js

560 lines
16 KiB
JavaScript
Raw Normal View History

2019-07-25 10:58:17 +08:00
import Vue from 'vue';
import { TrainingMode } from '@/scripts/ConstDic';
2019-07-30 09:35:00 +08:00
import OperateHandler from '@/scripts/plugin/trainingOperateHandler';
2019-07-30 16:47:11 +08:00
import deviceType from '../../jmap/constant/deviceType';
import LangStorage from '@/utils/lang';
2019-09-30 16:13:59 +08:00
// const lang = LangStorage.getLang();
2019-07-25 10:58:17 +08:00
/**
* 实训状态数据
*/
const training = {
2019-11-07 15:20:39 +08:00
namespaced: true,
state: {
mode: null, // 模式
operatemode: null, // 操作模式 管理员/普通
started: false, // 是否开始
switchcount: 0, // 开关标识
basicInfo: {}, // 实训基本信息数据
offsetStationCode: '', // 偏移到车站
rezoomCount: 0, // 车站变更标识
steps: [], // 实训步骤数据
tempStep: {}, // 临时步骤数据(编辑模式)
order: -1, // 实训进行到第几步
orderCount: 0, // 步骤变更标识
operateErrMsg: '', // 操作错误提示信息
subscribeCount: 0, // 仿真订阅完成标识
score: 0, // 实训得分
usedTime: 0, // 实训所需时间
timeInterval: null, // 计时器
tipEvent: 0, // 提示刷新检测
operate: {}, // 操作model,
initTime: 0, // 当前系统时间
prdType: '', // 产品类型
roles: '', // 角色权限类型
group: '' // 设置全局 group
},
getters: {
tempStep: (state) => {
return state.tempStep;
},
steps: (state) => {
return state.steps;
},
order: (state) => {
return state.order;
},
offsetStationCode: (state) => {
return state.offsetStationCode;
},
basicInfo: (state) => {
return state.basicInfo;
},
mode: (state) => {
return state.mode;
},
operatemode: (state) => {
return state.operatemode;
},
started: (state) => {
return state.started;
},
score: (state) => {
return state.score;
},
usedTime: (state) => {
return state.usedTime;
},
tipEvent: (state) => {
return state.tipEvent;
},
initTime: (state) => {
return state.initTime;
},
prdType: (state) => {
return state.prdType;
},
roles: (state) => {
return state.roles;
},
// 视图中的列车列表
viewTrainList: (state) => () =>{
const trainList = [];
const mapDevice = Vue.prototype.$jlmap.mapDevice;
Object.values(mapDevice).forEach(device => {
if (device && device._type === deviceType.Train) {
trainList.push(device);
}
});
return trainList;
}
},
mutations: {
changeMode: (state, mode) => {
state.mode = mode;
},
changeOperateMode: (state, mode) => {
state.operatemode = mode;
},
start: (state) => {
state.started = true;
state.switchcount += 1;
},
over: (state) => {
state.started = false;
state.switchcount += 1;
},
updateMapState: (state, deviceStatus) => {
Vue.prototype.$jlmap && Vue.prototype.$jlmap.update(deviceStatus);
},
setMapDefaultState: (state) =>{
Vue.prototype.$jlmap && Vue.prototype.$jlmap.setDefaultState();
},
setBasicInfo: (state, basicInfo) => {
state.basicInfo = basicInfo;
},
setOffsetStationCode: (state, offsetStationCode) => {
state.offsetStationCode = offsetStationCode || null;
if (state % 100 === 0) {
state.rezoomCount = 0;
} else {
state.rezoomCount += 1;
}
},
setSteps: (state, steps) => {
state.steps = steps;
},
addStep: (state, step) => {
state.steps.push(step);
},
next: (state) => {
state.order += 1;
state.orderCount += 1;
},
orderCountIncrement: (state) => {
state.orderCount += 1;
},
resetOrder: (state) => {
state.order = -1;
},
backOrder: (state, stepNum) => {
if (state.order > stepNum) {
state.order -= stepNum;
state.orderCount += 1;
}
},
setTempStep: (state, step) => {
state.tempStep = step;
},
setOperateErrMsg: (state, errMsg) => {
state.operateErrMsg = errMsg;
},
setHasSubscribed: (state) => {
state.subscribeCount++;
},
setScore: (state, score) => {
state.score = score;
},
resetScore: (state) => {
state.score = '';
},
tipEventIncrement: (state) => {
state.tipEvent++;
},
resetUsedTime: (state) => {
state.usedTime = 0;
},
countUsedTime: (state) => {
if (state.timeInterval) {
clearInterval(state.timeInterval);
state.timeInterval = null;
}
state.timeInterval = setInterval(() => {
state.usedTime++;
state.initTime += 1000;
}, 1000);
},
stopCountTime: (state) => {
if (state.timeInterval) {
clearInterval(state.timeInterval);
state.timeInterval = null;
}
},
setOperate: (state, operate) => {
state.operate = operate;
},
setInitTime: (state, operate) => {
state.initTime = operate;
},
setPrdType: (state, prdType) => {
state.prdType = prdType;
},
setRoles: (state, roles) => {
state.roles = roles;
},
setGroup: (state, group) => {
state.group = group;
}
},
actions: {
/**
2019-07-25 10:58:17 +08:00
* 清除仿真所在组
*/
2019-11-07 15:20:39 +08:00
clearSimulationGroup: ({ commit }, type) => {
commit('clearSimulationGroup', type);
},
/**
2019-07-25 10:58:17 +08:00
* 设置socke已经连接
*/
2019-11-07 15:20:39 +08:00
setHasSubscribed: ({ commit }) => {
commit('setHasSubscribed');
},
/**
2019-07-25 10:58:17 +08:00
* 模式变更
*/
2019-11-07 15:20:39 +08:00
changeMode: ({ commit }, opts) => {
commit('changeMode', opts.mode);
},
2019-07-25 10:58:17 +08:00
2019-11-07 15:20:39 +08:00
/**
2019-07-25 10:58:17 +08:00
* 操作模式变更
*/
2019-11-07 15:20:39 +08:00
changeOperateMode: ({ commit }, opts) => {
commit('changeOperateMode', opts.mode);
},
2019-07-25 10:58:17 +08:00
2019-11-07 15:20:39 +08:00
/**
2019-07-25 10:58:17 +08:00
* 重置实训状态
*/
2019-11-07 15:20:39 +08:00
reset: ({ commit }) => {
commit('over');
commit('resetOrder');
commit('stopCountTime');
// 清空计时器以及得分
commit('resetUsedTime');
commit('resetScore');
// 设置其他属性状态
commit('setOffsetStationCode', null);
},
/**
2019-07-25 10:58:17 +08:00
* 开始
*/
2019-11-07 15:20:39 +08:00
start: ({ commit }) => {
// 清空操作组
OperateHandler.cleanOperates();
// 清空计时器以及得分
commit('stopCountTime');
commit('resetUsedTime');
commit('resetScore');
commit('start');
},
/**
2019-07-25 10:58:17 +08:00
* 结束
*/
2019-11-07 15:20:39 +08:00
over: ({ commit }) => {
commit('over');
commit('resetOrder');
commit('stopCountTime');
},
2019-07-25 10:58:17 +08:00
2019-11-07 15:20:39 +08:00
/**
2019-07-25 10:58:17 +08:00
* step步骤是否结束
*/
2019-11-07 15:20:39 +08:00
isStepOver: ({ state }) => {
if (state.order >= state.steps.length) {
return true;
}
},
2019-07-25 10:58:17 +08:00
2019-11-07 15:20:39 +08:00
/**
2019-07-25 10:58:17 +08:00
* 停止计时
*/
2019-11-07 15:20:39 +08:00
setStopCountTime: ({ commit }) => {
commit('stopCountTime');
},
2019-07-25 10:58:17 +08:00
2019-11-07 15:20:39 +08:00
/**
2019-07-25 10:58:17 +08:00
* 主动判断是否结束
*/
2019-11-07 15:20:39 +08:00
judgeFinish: ({ dispatch, commit, state }, rtn) => {
if (state.started) {
if (state.order >= state.steps.length) {
if (rtn && rtn.valid) {
commit('next');
commit('setOperateErrMsg', { errMsg: LangStorage.getLang() == 'en' ? 'Correct operation! Training is over!' : '操作正确!实训结束!', color: 'green' });
} else {
commit('setOperateErrMsg', { errMsg: LangStorage.getLang() == 'en' ? 'Operation error! Training is over!' : '操作错误!实训结束!', color: 'red' });
}
dispatch('over');
dispatch('changeMode', { mode: null });
if (rtn.hasOwnProperty('score')) {
commit('setScore', rtn.score || 0);
}
} else {
rtn && rtn.valid && commit('next');
}
}
},
/**
2019-07-25 10:58:17 +08:00
* 开始实训模式变更和开始的混合操作
*/
2019-11-07 15:20:39 +08:00
startTraining: ({ commit }, opts) => {
commit('resetOrder');
commit('changeMode', opts.mode);
if (opts.start) {
// 清空计时器以及得分
commit('stopCountTime');
commit('resetUsedTime');
commit('resetScore');
// 开始实训
commit('start');
commit('next');
// 开始计时
commit('countUsedTime');
}
},
/**
2019-07-25 10:58:17 +08:00
* 开始实训模式变更和开始的混合操作
*/
2019-11-07 15:20:39 +08:00
backSteps: ({ commit }, stepNum) => {
if (Number.isInteger) {
commit('backOrder', stepNum);
}
},
2019-07-25 10:58:17 +08:00
2019-11-07 15:20:39 +08:00
/**
2019-07-25 10:58:17 +08:00
* 下一步
*/
2019-11-07 15:20:39 +08:00
next: ({ commit, state }, operate) => {
commit('setOperate', operate);
return new Promise((resolve, reject) => {
if (!state.started && !state.mode) {
commit('setOperateErrMsg', { errMsg: LangStorage.getLang() == 'en' ? 'Please click start, start training!' : '请点击开始,开始实训!', color: 'red' });
return;
}
// 处理operation
OperateHandler.handle(operate).then(rtn => {
if (state.started) {
// 教学和联系模式需要给出过程步骤提示
if (TrainingMode.TEACH === state.mode || TrainingMode.PRACTICE === state.mode) {
if (rtn && rtn.valid) {
commit('next');
commit('setOperateErrMsg', { errMsg: LangStorage.getLang() == 'en' ? "Correct operation! That's great!" : '操作正确!真棒!', color: 'green' });
commit('tipEventIncrement');
} else {
if (!operate.repeat) {
commit('setOperateErrMsg', { errMsg: LangStorage.getLang() == 'en' ? 'operation mistake!' : '操作错误!', color: 'red' });
}
}
} else if (TrainingMode.EXAM === state.mode || TrainingMode.TEST === state.mode) {
// 测试和考试不给提示
rtn && rtn.valid && commit('next');
}
}
resolve(rtn);
}).catch(error => {
reject(error);
});
});
},
/**
2019-07-25 10:58:17 +08:00
* 提示消息重新加载
*/
2019-11-07 15:20:39 +08:00
tipReload: ({ commit }) => {
commit('orderCountIncrement');
},
2019-07-25 10:58:17 +08:00
2019-11-07 15:20:39 +08:00
/**
2019-07-25 10:58:17 +08:00
* 更新偏移位置车站
*/
2019-11-07 15:20:39 +08:00
updateOffsetStationCode: ({ commit }, payLoad) => {
commit('setOffsetStationCode', payLoad.offsetStationCode);
},
2019-07-25 10:58:17 +08:00
2019-11-07 15:20:39 +08:00
/**
2019-07-25 10:58:17 +08:00
* 设置实训数据
*/
2019-11-07 15:20:39 +08:00
setTrainingData: ({ commit }, trainingData) => {
const basicInfo = {
id: trainingData.id,
name: trainingData.name,
remarks: trainingData.remarks,
prdType: trainingData.prdType,
minDuration: trainingData.minDuration,
maxDuration: trainingData.maxDuration
};
commit('setBasicInfo', basicInfo);
const steps = trainingData.steps;
commit('setSteps', steps);
const offsetStationCode = trainingData.locateDeviceCode;
commit('setOffsetStationCode', offsetStationCode);
commit('setMapDefaultState');
},
/**
2019-07-25 10:58:17 +08:00
* 设置步骤数据
*/
2019-11-07 15:20:39 +08:00
setSteps: ({ commit }, steps) => {
commit('setSteps', steps);
},
2019-07-25 10:58:17 +08:00
2019-11-07 15:20:39 +08:00
/**
2019-07-25 10:58:17 +08:00
* 添加步骤数据
*/
2019-11-07 15:20:39 +08:00
addStep: ({ state, commit }, step) => {
return new Promise((resolve, reject) => {
try {
var valid = true;
const steps = state.steps;
if (steps && steps.length > 0) {
const last = steps.length - 1;
if (steps[last].type === step.type &&
2019-07-25 10:58:17 +08:00
steps[last].code === step.code &&
steps[last].operation === step.operation) {
2019-11-07 15:20:39 +08:00
steps.splice(last, 1);
step.order = step.order - 1;
valid = false;
}
}
commit('addStep', step);
resolve(valid);
} catch (error) {
reject(error);
}
});
},
/**
2019-07-25 10:58:17 +08:00
* 设置地图默认状态
*/
2019-11-07 15:20:39 +08:00
setMapDefaultState: ({ commit }) => {
commit('setMapDefaultState');
},
2019-07-25 10:58:17 +08:00
2019-11-07 15:20:39 +08:00
/**
2019-07-25 10:58:17 +08:00
* 更新地图设备状态数据
*/
2019-11-07 15:20:39 +08:00
updateMapState: ({ commit }, deviceStatus) => {
commit('updateMapState', deviceStatus);
},
2019-07-25 10:58:17 +08:00
2019-11-07 15:20:39 +08:00
/**
2019-07-25 10:58:17 +08:00
* 设置临时步骤数据
*/
2019-11-07 15:20:39 +08:00
setTempStep: ({ commit }, step) => {
commit('setTempStep', step);
},
2019-07-25 10:58:17 +08:00
2019-11-07 15:20:39 +08:00
/**
2019-07-25 10:58:17 +08:00
* 开始教学模式
*/
2019-11-07 15:20:39 +08:00
teachModeStart: ({ dispatch }, mode) => {
const payLoad = { start: true, mode: mode };
dispatch('startTraining', payLoad);
},
2019-07-25 10:58:17 +08:00
2019-11-07 15:20:39 +08:00
/**
2019-07-25 10:58:17 +08:00
* 开始考试模式
*/
2019-11-07 15:20:39 +08:00
examModeStart: ({ dispatch }) => {
const payLoad = { start: true, mode: TrainingMode.EXAM };
dispatch('startTraining', payLoad);
},
2019-07-25 10:58:17 +08:00
2019-11-07 15:20:39 +08:00
/**
2019-07-25 10:58:17 +08:00
* 仿真和大屏和计划模式
*/
2019-11-07 15:20:39 +08:00
simulationStart: ({ dispatch }) => {
const payLoad = { start: true, mode: TrainingMode.NORMAL };
dispatch('startTraining', payLoad);
},
2019-07-25 10:58:17 +08:00
2019-11-07 15:20:39 +08:00
/**
2019-07-25 10:58:17 +08:00
* 结束模式
*/
2019-11-07 15:20:39 +08:00
end: ({ commit }, mode) => {
commit('over');
commit('resetOrder');
commit('stopCountTime');
commit('changeMode', mode);
},
/**
2019-07-25 10:58:17 +08:00
* 是否教学模式
*/
2019-11-07 15:20:39 +08:00
isTeachMode: ({ state }) => {
return new Promise((resolve, reject) => {
if (state.mode === TrainingMode.TEACH) {
resolve();
} else {
reject(new Error('not teach mode'));
}
});
},
/**
2019-07-25 10:58:17 +08:00
* 设置用户得分
*/
2019-11-07 15:20:39 +08:00
setScore: ({ commit }, score) => {
commit('setScore', score);
},
2019-07-25 10:58:17 +08:00
2019-11-07 15:20:39 +08:00
/**
2019-07-25 10:58:17 +08:00
* 设置tip变化事件
*/
2019-11-07 15:20:39 +08:00
emitTipFresh: ({ commit }) => {
commit('tipEventIncrement');
},
2019-07-25 10:58:17 +08:00
2019-11-07 15:20:39 +08:00
/**
2019-07-25 10:58:17 +08:00
* 设置WebSocket链接状态
*/
2019-11-07 15:20:39 +08:00
setConnected: ({ commit }, isConnected) => {
commit('setConnected', isConnected);
},
2019-07-25 10:58:17 +08:00
2019-11-07 15:20:39 +08:00
/**
2019-07-25 10:58:17 +08:00
* 设置系统时间
*/
2019-11-07 15:20:39 +08:00
setInitTime: ({ commit }, initTime) => {
commit('setInitTime', initTime);
},
2019-07-25 10:58:17 +08:00
2019-11-07 15:20:39 +08:00
/**
2019-07-25 10:58:17 +08:00
* 设置产品类型
*/
2019-11-07 15:20:39 +08:00
setPrdType: ({ commit }, prdType) => {
commit('setPrdType', prdType);
},
2019-07-25 10:58:17 +08:00
2019-11-07 15:20:39 +08:00
/**
2019-07-25 10:58:17 +08:00
* 设置角色列表
*/
2019-11-07 15:20:39 +08:00
setRoles: ({ commit }, roles) => {
commit('setRoles', roles);
},
2019-07-25 10:58:17 +08:00
2019-11-07 15:20:39 +08:00
/**
2019-07-25 10:58:17 +08:00
* 设置仿真组
*/
2019-11-07 15:20:39 +08:00
setGroup: ({ commit }, group) => {
commit('setGroup', group);
}
}
2019-07-25 10:58:17 +08:00
};
export default training;