1import api from '@/store/api';
2import i18n from '@/i18n';
3
4const PowerControlStore = {
5  namespaced: true,
6  state: {
7    powerCapValue: null,
8    powerConsumptionValue: null,
9  },
10  getters: {
11    powerCapValue: (state) => state.powerCapValue,
12    powerConsumptionValue: (state) => state.powerConsumptionValue,
13  },
14  mutations: {
15    setPowerCapValue: (state, powerCapValue) =>
16      (state.powerCapValue = powerCapValue),
17    setPowerConsumptionValue: (state, powerConsumptionValue) =>
18      (state.powerConsumptionValue = powerConsumptionValue),
19  },
20  actions: {
21    setPowerCapUpdatedValue({ commit }, value) {
22      commit('setPowerCapValue', value);
23    },
24    async getPowerControl({ commit }) {
25      return await api
26        .get('/redfish/v1/Chassis/chassis/Power')
27        .then((response) => {
28          const powerControl = response.data.PowerControl;
29          const powerCap = powerControl[0].PowerLimit.LimitInWatts;
30          // If system is powered off, power consumption does not exist in the PowerControl
31          const powerConsumption = powerControl[0].PowerConsumedWatts || null;
32
33          commit('setPowerCapValue', powerCap);
34          commit('setPowerConsumptionValue', powerConsumption);
35        })
36        .catch((error) => {
37          console.log('Power control', error);
38        });
39    },
40    async setPowerControl(_, powerCapValue) {
41      const data = {
42        PowerControl: [{ PowerLimit: { LimitInWatts: powerCapValue } }],
43      };
44
45      return await api
46        .patch('/redfish/v1/Chassis/chassis/Power', data)
47        .then(() =>
48          i18n.t('pageServerPowerOperations.toast.successSaveSettings')
49        )
50        .catch((error) => {
51          console.log(error);
52          throw new Error(
53            i18n.t('pageServerPowerOperations.toast.errorSaveSettings')
54          );
55        });
56    },
57  },
58};
59
60export default PowerControlStore;
61