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

699 lines
23 KiB
JavaScript
Raw Normal View History

2019-07-25 10:58:17 +08:00
import Vue from 'vue';
import { TrainingMode } from '@/scripts/ConstDic';
2019-11-18 14:44:01 +08:00
import OperateHandler from '@/scripts/plugin/OperateHandler';
2019-07-30 16:47:11 +08:00
import deviceType from '../../jmap/constant/deviceType';
import LangStorage from '@/utils/lang';
import Handler from '@/scripts/cmdPlugin/Handler';
2020-07-02 17:57:48 +08:00
import { deepAssign } from '@/utils/index';
2019-11-18 18:47:02 +08:00
2019-07-25 10:58:17 +08:00
/**
* 实训状态数据
*/
2019-11-18 18:47:02 +08:00
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: [], // 实训步骤数据
order: -1, // 实训进行到第几步
orderCount: 0, // 步骤变更标识
operateErrMsg: '', // 操作错误提示信息
subscribeCount: 0, // 仿真订阅完成标识
score: 0, // 实训得分
usedTime: 0, // 实训所需时间
timeInterval: null, // 计时器
tipEvent: 0, // 提示刷新检测
operate: {}, // 操作model,
initTime: 0, // 当前系统时间
prdType: '', // 产品类型
roles: '', // 角色权限类型
group: '', // 设置全局 group
2020-07-02 17:57:48 +08:00
centerStationCode:'', // 当前居中的集中站code
memberList: [], // 综合仿真成员列表
memberData: {}, // 综合仿真成员列表
simulationUserList: [] // 综合仿真用户列表
2019-11-07 15:20:39 +08:00
},
getters: {
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;
},
2020-07-02 17:57:48 +08:00
memberList: (state) => {
return state.memberList;
},
simulationUserList: (state) => {
return state.simulationUserList;
},
2019-11-07 15:20:39 +08:00
// 视图中的列车列表
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;
2020-05-28 11:27:53 +08:00
},
commonMenuStep: (state) => {
return state.commonMenuStep;
2019-11-07 15:20:39 +08:00
}
},
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;
state.rezoomCount += 1;
2019-11-07 15:20:39 +08:00
},
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;
}
},
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;
},
2020-05-18 11:04:39 +08:00
setInitTime: (state, initTime) => {
state.initTime = initTime;
2019-11-07 15:20:39 +08:00
},
setPrdType: (state, prdType) => {
state.prdType = prdType;
},
setRoles: (state, roles) => {
state.roles = roles;
},
setGroup: (state, group) => {
state.group = group;
},
notifyGetCommandDefinition: (state, lineCode) => {
Handler.notify(lineCode);
},
setCenterStationCode:(state, centerStationCode) => {
state.centerStationCode = centerStationCode;
2020-07-02 17:57:48 +08:00
},
2020-07-06 18:00:44 +08:00
setMemberList: (state, {memberList, userId}) => {
2020-07-02 17:57:48 +08:00
state.memberList = memberList;
state.memberData = {};
if (memberList && memberList.length) {
memberList.forEach(item => {
state.memberData[item.id] = deepAssign({userId: ''}, item);
2020-07-06 18:00:44 +08:00
state.memberData[item.id].disabled = userId == item.userId;
2020-07-02 17:57:48 +08:00
});
}
},
setSimulationUserList: (state, simulationUserList) => {
state.simulationUserList = simulationUserList;
},
2020-07-23 17:57:35 +08:00
updateMemberTrust:(state, {deviceCode, trust}) => {
for (const k in state.memberData) {
if (state.memberData[k].deviceCode == deviceCode) {
state.memberData[k].trust = trust;
break;
}
2020-07-14 15:03:31 +08:00
}
},
2020-07-10 17:33:37 +08:00
updateMemberListInScript:(state, {oldMemberId, newMemberId, userId, name}) => {
2020-07-10 16:40:42 +08:00
if (oldMemberId) {
// 重置旧数据
delete state.memberData[oldMemberId].userId;
2020-07-10 17:33:37 +08:00
delete state.memberData[oldMemberId].name;
2020-07-10 16:40:42 +08:00
state.memberData[oldMemberId].disabled = false;
}
// 更新新数据
state.memberData[newMemberId].userId = userId;
2020-07-10 17:33:37 +08:00
state.memberData[newMemberId].name = name;
2020-07-10 16:40:42 +08:00
state.memberData[newMemberId].disabled = true;
},
2020-07-14 15:03:31 +08:00
2020-07-06 18:00:44 +08:00
updateMemberAndUser: (state, {simulationUserList, userId}) => {
simulationUserList.forEach(item => {
2020-07-02 17:57:48 +08:00
let memberIndex = -1;
let memberId = '';
state.simulationUserList.forEach((user, index) =>{
if (user.userId === item.userId) {
memberId = user.memberId;
user.online = item.online;
user.memberId = item.memberId;
user.role = item.role;
memberIndex = index;
}
});
try {
2020-07-03 18:42:29 +08:00
if (state.memberData && memberId !== item.memberId && memberId && state.memberData[memberId]) {
2020-07-02 17:57:48 +08:00
state.memberData[memberId].userId = '';
2020-07-06 18:00:44 +08:00
state.memberData[memberId].disabled = false;
2020-07-02 17:57:48 +08:00
}
2020-07-03 18:42:29 +08:00
if (state.memberData && memberId !== item.memberId && item.memberId && state.memberData[item.memberId]) {
2020-07-02 17:57:48 +08:00
state.memberData[item.memberId].userId = item.userId;
2020-07-06 18:00:44 +08:00
state.memberData[item.memberId].disabled = item.userId == userId;
2020-07-02 17:57:48 +08:00
}
if (item.messageType === 'ENTER' && memberIndex < 0) {
delete item.messageType;
state.simulationUserList.push(item);
} else if (item.messageType === 'KICK_OUT' && memberIndex > -1) {
state.simulationUserList.splice(memberIndex, 1);
}
} catch (e) {
console.error(e);
}
});
2019-11-07 15:20:39 +08:00
}
},
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();
2019-12-31 17:20:04 +08:00
Handler.clear(); // 两种方式
2019-11-07 15:20:39 +08:00
// 清空计时器以及得分
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-12-31 17:20:04 +08:00
/**
* 下一步
*/
nextNew: ({ commit, state }, operate) => {
return new Promise((resolve, reject) => {
commit('setOperate', operate);
if (!state.started && !state.mode) {
commit('setOperateErrMsg', { errMsg: LangStorage.getLang() == 'en' ? 'Please click start, start training!' : '请点击开始,开始实训!', color: 'red' });
return;
}
try {
2020-07-23 17:57:35 +08:00
// 处理operation
2019-12-31 17:20:04 +08:00
Handler.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 => {
console.error(error);
reject(error);
});
} catch (error) {
console.error(error);
reject(error);
}
});
},
2019-11-07 15:20:39 +08:00
/**
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;
2019-11-07 15:20:39 +08:00
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
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);
},
/** 设置当前居中的集中站code */
setCenterStationCode:({ commit }, centerStationCode) => {
commit('setCenterStationCode', centerStationCode);
2020-07-02 17:57:48 +08:00
},
/** 设置综合演练仿真成员列表 */
2020-07-06 18:00:44 +08:00
setMemberList: ({ commit }, data) => {
commit('setMemberList', data);
2020-07-02 17:57:48 +08:00
},
/** 设置综合演练仿真用户列表 */
setSimulationUserList: ({ commit }, simulationUserList) => {
commit('setSimulationUserList', simulationUserList);
},
2020-07-14 15:03:31 +08:00
updateMemberTrust:({ commit }, data) => {
commit('updateMemberTrust', data);
},
2020-07-02 17:57:48 +08:00
/** 更新综合演练仿真成员和用户列表 */
2020-07-06 18:00:44 +08:00
updateMemberAndUser: ({ commit }, data) => {
commit('updateMemberAndUser', data);
2020-07-10 16:40:42 +08:00
},
/** 更新剧本所有成员列表 */
updateMemberListInScript:({ commit }, data) => {
commit('updateMemberListInScript', data);
2019-11-07 15:20:39 +08:00
}
}
2019-07-25 10:58:17 +08:00
};
export default training;