1import api from '@/store/api'; 2import { uniqBy } from 'lodash'; 3 4const SensorsStore = { 5 namespaced: true, 6 state: { 7 sensors: [], 8 }, 9 getters: { 10 sensors: (state) => state.sensors, 11 }, 12 mutations: { 13 setSensors: (state, sensors) => { 14 state.sensors = uniqBy([...sensors, ...state.sensors], 'name'); 15 }, 16 setSensorsDefault: (state) => { 17 state.sensors = []; 18 }, 19 }, 20 actions: { 21 async getAllSensors({ dispatch }) { 22 const collection = await dispatch('getChassisCollection'); 23 if (!collection) return; 24 dispatch('resetSensors'); 25 const promises = collection.reduce((acc, id) => { 26 acc.push(dispatch('getSensors', id)); 27 acc.push(dispatch('getThermalSensors', id)); 28 acc.push(dispatch('getPowerSensors', id)); 29 return acc; 30 }, []); 31 return await api.all(promises); 32 }, 33 async getChassisCollection() { 34 return await api 35 .get('/redfish/v1/Chassis') 36 .then(({ data: { Members } }) => 37 Members.map((member) => member['@odata.id']), 38 ) 39 .catch((error) => console.log(error)); 40 }, 41 async resetSensors({ commit }) { 42 commit('setSensorsDefault'); 43 }, 44 async getSensors({ dispatch }, id) { 45 await api 46 .get('/redfish/v1/') 47 .then(({ data }) => { 48 if (data?.ProtocolFeaturesSupported?.ExpandQuery?.MaxLevels > 0) { 49 return dispatch('getSensorsUsingQueryParams', id); 50 } else { 51 return dispatch('getSensorsWithoutQueryParams', id); 52 } 53 }) 54 .catch((error) => console.log(error)); 55 }, 56 async getSensorsWithoutQueryParams({ commit }, id) { 57 const sensors = await api 58 .get(`${id}/Sensors`) 59 .then((response) => response.data.Members) 60 .catch((error) => console.log(error)); 61 if (!sensors) return; 62 const promises = sensors.map((sensor) => { 63 return api.get(sensor['@odata.id']).catch((error) => { 64 console.log(error); 65 return error; 66 }); 67 }); 68 return await api.all(promises).then((responses) => { 69 const sensorData = []; 70 responses.forEach((response) => { 71 if (response.data) { 72 sensorData.push({ 73 name: response.data.Name, 74 status: response.data.Status?.Health, 75 currentValue: response.data.Reading, 76 lowerCaution: response.data.Thresholds?.LowerCaution?.Reading, 77 upperCaution: response.data.Thresholds?.UpperCaution?.Reading, 78 lowerCritical: response.data.Thresholds?.LowerCritical?.Reading, 79 upperCritical: response.data.Thresholds?.UpperCritical?.Reading, 80 units: response.data.ReadingUnits, 81 }); 82 } 83 }); 84 commit('setSensors', sensorData); 85 }); 86 }, 87 async getSensorsUsingQueryParams({ commit }, id) { 88 await api 89 .get(`${id}/Sensors?$expand=.($levels=1)`) 90 .then((response) => { 91 let sensorData = []; 92 response.data.Members.map((sensor) => { 93 const oneSensordata = { 94 name: sensor.Name, 95 status: sensor.Status?.Health, 96 currentValue: sensor.Reading, 97 lowerCaution: sensor.Thresholds?.LowerCaution?.Reading, 98 upperCaution: sensor.Thresholds?.UpperCaution?.Reading, 99 lowerCritical: sensor.Thresholds?.LowerCritical?.Reading, 100 upperCritical: sensor.Thresholds?.UpperCritical?.Reading, 101 units: sensor.ReadingUnits, 102 }; 103 sensorData.push(oneSensordata); 104 commit('setSensors', sensorData); 105 }); 106 }) 107 .then(() => { 108 return; 109 }) 110 .catch((error) => console.log(error)); 111 }, 112 async getThermalSensors({ commit }, id) { 113 return await api 114 .get(`${id}/Thermal`) 115 .then(({ data: { Fans = [], Temperatures = [] } }) => { 116 const sensorData = []; 117 Fans.forEach((sensor) => { 118 sensorData.push({ 119 name: sensor.Name, 120 status: sensor.Status.Health, 121 currentValue: sensor.Reading, 122 lowerCaution: sensor.LowerThresholdNonCritical, 123 upperCaution: sensor.UpperThresholdNonCritical, 124 lowerCritical: sensor.LowerThresholdCritical, 125 upperCritical: sensor.UpperThresholdCritical, 126 units: sensor.ReadingUnits, 127 }); 128 }); 129 Temperatures.forEach((sensor) => { 130 sensorData.push({ 131 name: sensor.Name, 132 status: sensor.Status.Health, 133 currentValue: sensor.ReadingCelsius, 134 lowerCaution: sensor.LowerThresholdNonCritical, 135 upperCaution: sensor.UpperThresholdNonCritical, 136 lowerCritical: sensor.LowerThresholdCritical, 137 upperCritical: sensor.UpperThresholdCritical, 138 units: '℃', 139 }); 140 }); 141 commit('setSensors', sensorData); 142 }) 143 .catch((error) => console.log(error)); 144 }, 145 async getPowerSensors({ commit }, id) { 146 return await api 147 .get(`${id}/Power`) 148 .then(({ data: { Voltages = [] } }) => { 149 const sensorData = Voltages.map((sensor) => { 150 return { 151 name: sensor.Name, 152 status: sensor.Status.Health, 153 currentValue: sensor.ReadingVolts, 154 lowerCaution: sensor.LowerThresholdNonCritical, 155 upperCaution: sensor.UpperThresholdNonCritical, 156 lowerCritical: sensor.LowerThresholdCritical, 157 upperCritical: sensor.UpperThresholdCritical, 158 units: 'V', 159 }; 160 }); 161 commit('setSensors', sensorData); 162 }) 163 .catch((error) => console.log(error)); 164 }, 165 }, 166}; 167 168export default SensorsStore; 169