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.t('pageRebootBmc.toast.successRebootStart'))
82        .catch((error) => {
83          console.log(error);
84          throw new Error(i18n.t('pageRebootBmc.toast.errorRebootStart'));
85        });
86    },
87    async serverPowerOn({ dispatch, commit }) {
88      const data = { ResetType: 'On' };
89      dispatch('serverPowerChange', data);
90      await checkForServerStatus.bind(this, 'on')();
91      commit('setOperationInProgress', false);
92      dispatch('getLastPowerOperationTime');
93    },
94    async serverSoftReboot({ dispatch, commit }) {
95      const data = { ResetType: 'GracefulRestart' };
96      dispatch('serverPowerChange', data);
97      await checkForServerStatus.bind(this, 'on')();
98      commit('setOperationInProgress', false);
99      dispatch('getLastPowerOperationTime');
100    },
101    async serverHardReboot({ dispatch, commit }) {
102      const data = { ResetType: 'ForceRestart' };
103      dispatch('serverPowerChange', data);
104      await checkForServerStatus.bind(this, 'on')();
105      commit('setOperationInProgress', false);
106      dispatch('getLastPowerOperationTime');
107    },
108    async serverSoftPowerOff({ dispatch, commit }) {
109      const data = { ResetType: 'GracefulShutdown' };
110      dispatch('serverPowerChange', data);
111      await checkForServerStatus.bind(this, 'off')();
112      commit('setOperationInProgress', false);
113      dispatch('getLastPowerOperationTime');
114    },
115    async serverHardPowerOff({ dispatch, commit }) {
116      const data = { ResetType: 'ForceOff' };
117      dispatch('serverPowerChange', data);
118      await checkForServerStatus.bind(this, 'off')();
119      commit('setOperationInProgress', false);
120      dispatch('getLastPowerOperationTime');
121    },
122    async serverPowerChange({ commit }, data) {
123      commit('setOperationInProgress', true);
124      api
125        .post(
126          `${await this.dispatch('global/getSystemPath')}/Actions/ComputerSystem.Reset`,
127          data,
128        )
129        .catch((error) => {
130          console.log(error);
131          commit('setOperationInProgress', false);
132        });
133    },
134  },
135};
136
137export default ControlStore;
138