SurenNeware | 6185909 | 2020-10-01 09:37:32 +0530 | [diff] [blame] | 1 | import api from '@/store/api'; |
Sukanya Pandey | 9055d98 | 2020-03-31 17:37:53 +0530 | [diff] [blame] | 2 | import i18n from '@/i18n'; |
Dixsie Wolmers | 4c69f5b | 2020-02-26 11:23:52 -0600 | [diff] [blame] | 3 | |
| 4 | const PowerControlStore = { |
| 5 | namespaced: true, |
| 6 | state: { |
| 7 | powerCapValue: null, |
MichalX Szopinski | f4328ed | 2021-07-12 12:59:30 +0200 | [diff] [blame] | 8 | powerCapUri: '', |
Derick Montague | 602e98a | 2020-10-21 16:20:00 -0500 | [diff] [blame] | 9 | powerConsumptionValue: null, |
Dixsie Wolmers | 4c69f5b | 2020-02-26 11:23:52 -0600 | [diff] [blame] | 10 | }, |
| 11 | getters: { |
Derick Montague | 602e98a | 2020-10-21 16:20:00 -0500 | [diff] [blame] | 12 | powerCapValue: (state) => state.powerCapValue, |
MichalX Szopinski | f4328ed | 2021-07-12 12:59:30 +0200 | [diff] [blame] | 13 | powerCapUri: (state) => state.powerCapUri, |
Derick Montague | 602e98a | 2020-10-21 16:20:00 -0500 | [diff] [blame] | 14 | powerConsumptionValue: (state) => state.powerConsumptionValue, |
Dixsie Wolmers | 4c69f5b | 2020-02-26 11:23:52 -0600 | [diff] [blame] | 15 | }, |
| 16 | mutations: { |
| 17 | setPowerCapValue: (state, powerCapValue) => |
| 18 | (state.powerCapValue = powerCapValue), |
MichalX Szopinski | f4328ed | 2021-07-12 12:59:30 +0200 | [diff] [blame] | 19 | setPowerCapUri: (state, powerCapUri) => (state.powerCapUri = powerCapUri), |
Dixsie Wolmers | 4c69f5b | 2020-02-26 11:23:52 -0600 | [diff] [blame] | 20 | setPowerConsumptionValue: (state, powerConsumptionValue) => |
Derick Montague | 602e98a | 2020-10-21 16:20:00 -0500 | [diff] [blame] | 21 | (state.powerConsumptionValue = powerConsumptionValue), |
Dixsie Wolmers | 4c69f5b | 2020-02-26 11:23:52 -0600 | [diff] [blame] | 22 | }, |
| 23 | actions: { |
Sukanya Pandey | 9055d98 | 2020-03-31 17:37:53 +0530 | [diff] [blame] | 24 | setPowerCapUpdatedValue({ commit }, value) { |
| 25 | commit('setPowerCapValue', value); |
| 26 | }, |
MichalX Szopinski | f4328ed | 2021-07-12 12:59:30 +0200 | [diff] [blame] | 27 | async getChassisCollection() { |
Yoshie Muranaka | 598bf7e | 2020-05-01 12:26:00 -0700 | [diff] [blame] | 28 | return await api |
MichalX Szopinski | f4328ed | 2021-07-12 12:59:30 +0200 | [diff] [blame] | 29 | .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 Montague | 602e98a | 2020-10-21 16:20:00 -0500 | [diff] [blame] | 42 | .then((response) => { |
Dixsie Wolmers | 4c69f5b | 2020-02-26 11:23:52 -0600 | [diff] [blame] | 43 | const powerControl = response.data.PowerControl; |
MichalX Szopinski | f4328ed | 2021-07-12 12:59:30 +0200 | [diff] [blame] | 44 | if (!powerControl || powerControl.length === 0) return; |
Damian Celico | 2f2f64d | 2022-12-05 21:48:02 +0100 | [diff] [blame^] | 45 | const powerCapUri = response.data['@odata.id']; |
Dixsie Wolmers | 4c69f5b | 2020-02-26 11:23:52 -0600 | [diff] [blame] | 46 | 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 Szopinski | f4328ed | 2021-07-12 12:59:30 +0200 | [diff] [blame] | 49 | commit('setPowerCapUri', powerCapUri); |
Dixsie Wolmers | 4c69f5b | 2020-02-26 11:23:52 -0600 | [diff] [blame] | 50 | commit('setPowerCapValue', powerCap); |
| 51 | commit('setPowerConsumptionValue', powerConsumption); |
| 52 | }) |
Derick Montague | 602e98a | 2020-10-21 16:20:00 -0500 | [diff] [blame] | 53 | .catch((error) => { |
Dixsie Wolmers | 4c69f5b | 2020-02-26 11:23:52 -0600 | [diff] [blame] | 54 | console.log('Power control', error); |
| 55 | }); |
Sukanya Pandey | 9055d98 | 2020-03-31 17:37:53 +0530 | [diff] [blame] | 56 | }, |
MichalX Szopinski | f4328ed | 2021-07-12 12:59:30 +0200 | [diff] [blame] | 57 | async setPowerControl({ state }, powerCapValue) { |
Sukanya Pandey | 9055d98 | 2020-03-31 17:37:53 +0530 | [diff] [blame] | 58 | const data = { |
Derick Montague | 602e98a | 2020-10-21 16:20:00 -0500 | [diff] [blame] | 59 | PowerControl: [{ PowerLimit: { LimitInWatts: powerCapValue } }], |
Sukanya Pandey | 9055d98 | 2020-03-31 17:37:53 +0530 | [diff] [blame] | 60 | }; |
Sukanya Pandey | 9055d98 | 2020-03-31 17:37:53 +0530 | [diff] [blame] | 61 | return await api |
MichalX Szopinski | f4328ed | 2021-07-12 12:59:30 +0200 | [diff] [blame] | 62 | .patch(state.powerCapUri, data) |
Sukanya Pandey | 9055d98 | 2020-03-31 17:37:53 +0530 | [diff] [blame] | 63 | .then(() => |
| 64 | i18n.t('pageServerPowerOperations.toast.successSaveSettings') |
| 65 | ) |
Derick Montague | 602e98a | 2020-10-21 16:20:00 -0500 | [diff] [blame] | 66 | .catch((error) => { |
Sukanya Pandey | 9055d98 | 2020-03-31 17:37:53 +0530 | [diff] [blame] | 67 | console.log(error); |
| 68 | throw new Error( |
| 69 | i18n.t('pageServerPowerOperations.toast.errorSaveSettings') |
| 70 | ); |
| 71 | }); |
Derick Montague | 602e98a | 2020-10-21 16:20:00 -0500 | [diff] [blame] | 72 | }, |
| 73 | }, |
Dixsie Wolmers | 4c69f5b | 2020-02-26 11:23:52 -0600 | [diff] [blame] | 74 | }; |
| 75 | |
| 76 | export default PowerControlStore; |