blob: 366d3bd2f83bf40c68f752ee2a6bd0b1a9e81f37 [file] [log] [blame]
SurenNeware61859092020-10-01 09:37:32 +05301import api from '@/store/api';
Sukanya Pandey9055d982020-03-31 17:37:53 +05302import i18n from '@/i18n';
Dixsie Wolmers4c69f5b2020-02-26 11:23:52 -06003
4const PowerControlStore = {
5 namespaced: true,
6 state: {
7 powerCapValue: null,
MichalX Szopinskif4328ed2021-07-12 12:59:30 +02008 powerCapUri: '',
Derick Montague602e98a2020-10-21 16:20:00 -05009 powerConsumptionValue: null,
Dixsie Wolmers4c69f5b2020-02-26 11:23:52 -060010 },
11 getters: {
Derick Montague602e98a2020-10-21 16:20:00 -050012 powerCapValue: (state) => state.powerCapValue,
MichalX Szopinskif4328ed2021-07-12 12:59:30 +020013 powerCapUri: (state) => state.powerCapUri,
Derick Montague602e98a2020-10-21 16:20:00 -050014 powerConsumptionValue: (state) => state.powerConsumptionValue,
Dixsie Wolmers4c69f5b2020-02-26 11:23:52 -060015 },
16 mutations: {
17 setPowerCapValue: (state, powerCapValue) =>
18 (state.powerCapValue = powerCapValue),
MichalX Szopinskif4328ed2021-07-12 12:59:30 +020019 setPowerCapUri: (state, powerCapUri) => (state.powerCapUri = powerCapUri),
Dixsie Wolmers4c69f5b2020-02-26 11:23:52 -060020 setPowerConsumptionValue: (state, powerConsumptionValue) =>
Derick Montague602e98a2020-10-21 16:20:00 -050021 (state.powerConsumptionValue = powerConsumptionValue),
Dixsie Wolmers4c69f5b2020-02-26 11:23:52 -060022 },
23 actions: {
Sukanya Pandey9055d982020-03-31 17:37:53 +053024 setPowerCapUpdatedValue({ commit }, value) {
25 commit('setPowerCapValue', value);
26 },
MichalX Szopinskif4328ed2021-07-12 12:59:30 +020027 async getChassisCollection() {
Yoshie Muranaka598bf7e2020-05-01 12:26:00 -070028 return await api
MichalX Szopinskif4328ed2021-07-12 12:59:30 +020029 .get('/redfish/v1/')
30 .then((response) => api.get(response.data.Chassis['@odata.id']))
31 .then(({ data: { Members } }) =>
32 Members.map((member) => member['@odata.id'])
33 )
34 .catch((error) => console.log(error));
35 },
36 async getPowerControl({ dispatch, commit }) {
37 const collection = await dispatch('getChassisCollection');
38 if (!collection || collection.length === 0) return;
39 return await api
40 .get(`${collection[0]}`)
41 .then((response) => api.get(response.data.Power['@odata.id']))
Derick Montague602e98a2020-10-21 16:20:00 -050042 .then((response) => {
Dixsie Wolmers4c69f5b2020-02-26 11:23:52 -060043 const powerControl = response.data.PowerControl;
MichalX Szopinskif4328ed2021-07-12 12:59:30 +020044 if (!powerControl || powerControl.length === 0) return;
Damian Celico2f2f64d2022-12-05 21:48:02 +010045 const powerCapUri = response.data['@odata.id'];
Dixsie Wolmers4c69f5b2020-02-26 11:23:52 -060046 const powerCap = powerControl[0].PowerLimit.LimitInWatts;
47 // If system is powered off, power consumption does not exist in the PowerControl
48 const powerConsumption = powerControl[0].PowerConsumedWatts || null;
MichalX Szopinskif4328ed2021-07-12 12:59:30 +020049 commit('setPowerCapUri', powerCapUri);
Dixsie Wolmers4c69f5b2020-02-26 11:23:52 -060050 commit('setPowerCapValue', powerCap);
51 commit('setPowerConsumptionValue', powerConsumption);
52 })
Derick Montague602e98a2020-10-21 16:20:00 -050053 .catch((error) => {
Dixsie Wolmers4c69f5b2020-02-26 11:23:52 -060054 console.log('Power control', error);
55 });
Sukanya Pandey9055d982020-03-31 17:37:53 +053056 },
MichalX Szopinskif4328ed2021-07-12 12:59:30 +020057 async setPowerControl({ state }, powerCapValue) {
Sukanya Pandey9055d982020-03-31 17:37:53 +053058 const data = {
Derick Montague602e98a2020-10-21 16:20:00 -050059 PowerControl: [{ PowerLimit: { LimitInWatts: powerCapValue } }],
Sukanya Pandey9055d982020-03-31 17:37:53 +053060 };
Sukanya Pandey9055d982020-03-31 17:37:53 +053061 return await api
MichalX Szopinskif4328ed2021-07-12 12:59:30 +020062 .patch(state.powerCapUri, data)
Sukanya Pandey9055d982020-03-31 17:37:53 +053063 .then(() =>
64 i18n.t('pageServerPowerOperations.toast.successSaveSettings')
65 )
Derick Montague602e98a2020-10-21 16:20:00 -050066 .catch((error) => {
Sukanya Pandey9055d982020-03-31 17:37:53 +053067 console.log(error);
68 throw new Error(
69 i18n.t('pageServerPowerOperations.toast.errorSaveSettings')
70 );
71 });
Derick Montague602e98a2020-10-21 16:20:00 -050072 },
73 },
Dixsie Wolmers4c69f5b2020-02-26 11:23:52 -060074};
75
76export default PowerControlStore;