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({ commit }, id) { 45 const sensors = await api 46 .get(`${id}/Sensors`) 47 .then((response) => response.data.Members) 48 .catch((error) => console.log(error)); 49 if (!sensors) return; 50 const promises = sensors.map((sensor) => { 51 return api.get(sensor['@odata.id']).catch((error) => { 52 console.log(error); 53 return error; 54 }); 55 }); 56 return await api.all(promises).then((responses) => { 57 const sensorData = []; 58 responses.forEach((response) => { 59 if (response.data) { 60 sensorData.push({ 61 name: response.data.Name, 62 status: response.data.Status?.Health, 63 currentValue: response.data.Reading, 64 lowerCaution: response.data.Thresholds?.LowerCaution?.Reading, 65 upperCaution: response.data.Thresholds?.UpperCaution?.Reading, 66 lowerCritical: response.data.Thresholds?.LowerCritical?.Reading, 67 upperCritical: response.data.Thresholds?.UpperCritical?.Reading, 68 units: response.data.ReadingUnits, 69 }); 70 } 71 }); 72 commit('setSensors', sensorData); 73 }); 74 }, 75 async getThermalSensors({ commit }, id) { 76 return await api 77 .get(`${id}/Thermal`) 78 .then(({ data: { Fans = [], Temperatures = [] } }) => { 79 const sensorData = []; 80 Fans.forEach((sensor) => { 81 sensorData.push({ 82 name: sensor.Name, 83 status: sensor.Status.Health, 84 currentValue: sensor.Reading, 85 lowerCaution: sensor.LowerThresholdNonCritical, 86 upperCaution: sensor.UpperThresholdNonCritical, 87 lowerCritical: sensor.LowerThresholdCritical, 88 upperCritical: sensor.UpperThresholdCritical, 89 units: sensor.ReadingUnits, 90 }); 91 }); 92 Temperatures.forEach((sensor) => { 93 sensorData.push({ 94 name: sensor.Name, 95 status: sensor.Status.Health, 96 currentValue: sensor.ReadingCelsius, 97 lowerCaution: sensor.LowerThresholdNonCritical, 98 upperCaution: sensor.UpperThresholdNonCritical, 99 lowerCritical: sensor.LowerThresholdCritical, 100 upperCritical: sensor.UpperThresholdCritical, 101 units: '℃', 102 }); 103 }); 104 commit('setSensors', sensorData); 105 }) 106 .catch((error) => console.log(error)); 107 }, 108 async getPowerSensors({ commit }, id) { 109 return await api 110 .get(`${id}/Power`) 111 .then(({ data: { Voltages = [] } }) => { 112 const sensorData = Voltages.map((sensor) => { 113 return { 114 name: sensor.Name, 115 status: sensor.Status.Health, 116 currentValue: sensor.ReadingVolts, 117 lowerCaution: sensor.LowerThresholdNonCritical, 118 upperCaution: sensor.UpperThresholdNonCritical, 119 lowerCritical: sensor.LowerThresholdCritical, 120 upperCritical: sensor.UpperThresholdCritical, 121 units: 'V', 122 }; 123 }); 124 commit('setSensors', sensorData); 125 }) 126 .catch((error) => console.log(error)); 127 }, 128 }, 129}; 130 131export default SensorsStore; 132