1import api from '@/store/api'; 2import i18n from '@/i18n'; 3 4/** 5 * Watch for serverStatus changes in GlobalStore module 6 * to set isOperationInProgress state 7 * Stop watching status changes and resolve Promise when 8 * serverStatus value matches passed argument or after 5 minutes 9 * @param {string} serverStatus 10 * @returns {Promise} 11 */ 12const checkForServerStatus = function (serverStatus) { 13 return new Promise((resolve) => { 14 const timer = setTimeout(() => { 15 resolve(); 16 unwatch(); 17 }, 300000 /*5mins*/); 18 const unwatch = this.watch( 19 (state) => state.global.serverStatus, 20 (value) => { 21 if (value === serverStatus) { 22 resolve(); 23 unwatch(); 24 clearTimeout(timer); 25 } 26 }, 27 ); 28 }); 29}; 30 31const ControlStore = { 32 namespaced: true, 33 state: { 34 isOperationInProgress: false, 35 lastPowerOperationTime: null, 36 lastBmcRebootTime: null, 37 }, 38 getters: { 39 isOperationInProgress: (state) => state.isOperationInProgress, 40 lastPowerOperationTime: (state) => state.lastPowerOperationTime, 41 lastBmcRebootTime: (state) => state.lastBmcRebootTime, 42 }, 43 mutations: { 44 setOperationInProgress: (state, inProgress) => 45 (state.isOperationInProgress = inProgress), 46 setLastPowerOperationTime: (state, lastPowerOperationTime) => 47 (state.lastPowerOperationTime = lastPowerOperationTime), 48 setLastBmcRebootTime: (state, lastBmcRebootTime) => 49 (state.lastBmcRebootTime = lastBmcRebootTime), 50 }, 51 actions: { 52 async getLastPowerOperationTime({ commit }) { 53 return await api 54 .get(`${await this.dispatch('global/getSystemPath')}`) 55 .then((response) => { 56 const lastReset = response.data.LastResetTime; 57 if (lastReset) { 58 const lastPowerOperationTime = new Date(lastReset); 59 commit('setLastPowerOperationTime', lastPowerOperationTime); 60 } 61 }) 62 .catch((error) => console.log(error)); 63 }, 64 async getLastBmcRebootTime({ commit }) { 65 return api 66 .get(`${await this.dispatch('global/getBmcPath')}`) 67 .then((response) => { 68 const lastBmcReset = response.data.LastResetTime; 69 const lastBmcRebootTime = new Date(lastBmcReset); 70 commit('setLastBmcRebootTime', lastBmcRebootTime); 71 }) 72 .catch((error) => console.log(error)); 73 }, 74 async rebootBmc() { 75 const data = { ResetType: 'GracefulRestart' }; 76 return await api 77 .post( 78 `${await this.dispatch('global/getBmcPath')}/Actions/Manager.Reset`, 79 data, 80 ) 81 .then(() => i18n.global.t('pageRebootBmc.toast.successRebootStart')) 82 .catch((error) => { 83 console.log(error); 84 throw new Error( 85 i18n.global.t('pageRebootBmc.toast.errorRebootStart'), 86 ); 87 }); 88 }, 89 async serverPowerOn({ dispatch, commit }) { 90 const data = { ResetType: 'On' }; 91 dispatch('serverPowerChange', data); 92 await checkForServerStatus.bind(this, 'on')(); 93 commit('setOperationInProgress', false); 94 dispatch('getLastPowerOperationTime'); 95 }, 96 async serverSoftReboot({ dispatch, commit }) { 97 const data = { ResetType: 'GracefulRestart' }; 98 dispatch('serverPowerChange', data); 99 await checkForServerStatus.bind(this, 'on')(); 100 commit('setOperationInProgress', false); 101 dispatch('getLastPowerOperationTime'); 102 }, 103 async serverHardReboot({ dispatch, commit }) { 104 const data = { ResetType: 'ForceRestart' }; 105 dispatch('serverPowerChange', data); 106 await checkForServerStatus.bind(this, 'on')(); 107 commit('setOperationInProgress', false); 108 dispatch('getLastPowerOperationTime'); 109 }, 110 async serverSoftPowerOff({ dispatch, commit }) { 111 const data = { ResetType: 'GracefulShutdown' }; 112 dispatch('serverPowerChange', data); 113 await checkForServerStatus.bind(this, 'off')(); 114 commit('setOperationInProgress', false); 115 dispatch('getLastPowerOperationTime'); 116 }, 117 async serverHardPowerOff({ dispatch, commit }) { 118 const data = { ResetType: 'ForceOff' }; 119 dispatch('serverPowerChange', data); 120 await checkForServerStatus.bind(this, 'off')(); 121 commit('setOperationInProgress', false); 122 dispatch('getLastPowerOperationTime'); 123 }, 124 async serverPowerChange({ commit }, data) { 125 commit('setOperationInProgress', true); 126 api 127 .post( 128 `${await this.dispatch('global/getSystemPath')}/Actions/ComputerSystem.Reset`, 129 data, 130 ) 131 .catch((error) => { 132 console.log(error); 133 commit('setOperationInProgress', false); 134 }); 135 }, 136 }, 137}; 138 139export default ControlStore; 140