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([...state.sensors, ...sensors], 'name');
15    },
16  },
17  actions: {
18    async getAllSensors({ dispatch }) {
19      const collection = await dispatch('getChassisCollection');
20      if (!collection) return;
21      const promises = collection.reduce((acc, id) => {
22        acc.push(dispatch('getSensors', id));
23        acc.push(dispatch('getThermalSensors', id));
24        acc.push(dispatch('getPowerSensors', id));
25        return acc;
26      }, []);
27      return await api.all(promises);
28    },
29    async getChassisCollection() {
30      return await api
31        .get('/redfish/v1/Chassis')
32        .then(({ data: { Members } }) =>
33          Members.map((member) => member['@odata.id'])
34        )
35        .catch((error) => console.log(error));
36    },
37    async getSensors({ commit }, id) {
38      const sensors = await api
39        .get(`${id}/Sensors`)
40        .then((response) => response.data.Members)
41        .catch((error) => console.log(error));
42      if (!sensors) return;
43      const promises = sensors.map((sensor) => {
44        return api.get(sensor['@odata.id']).catch((error) => {
45          console.log(error);
46          return error;
47        });
48      });
49      return await api.all(promises).then(
50        api.spread((...responses) => {
51          const sensorData = responses.map(({ data }) => {
52            return {
53              name: data.Name,
54              status: data.Status.Health,
55              currentValue: data.Reading,
56              lowerCaution: data.Thresholds?.LowerCaution?.Reading,
57              upperCaution: data.Thresholds?.UpperCaution?.Reading,
58              lowerCritical: data.Thresholds?.LowerCritical?.Reading,
59              upperCritical: data.Thresholds?.UpperCritical?.Reading,
60              units: data.ReadingUnits,
61            };
62          });
63          commit('setSensors', sensorData);
64        })
65      );
66    },
67    async getThermalSensors({ commit }, id) {
68      return await api
69        .get(`${id}/Thermal`)
70        .then(({ data: { Fans = [], Temperatures = [] } }) => {
71          const sensorData = [];
72          Fans.forEach((sensor) => {
73            sensorData.push({
74              name: sensor.Name,
75              status: sensor.Status.Health,
76              currentValue: sensor.Reading,
77              lowerCaution: sensor.LowerThresholdNonCritical,
78              upperCaution: sensor.UpperThresholdNonCritical,
79              lowerCritical: sensor.LowerThresholdCritical,
80              upperCritical: sensor.UpperThresholdCritical,
81              units: sensor.ReadingUnits,
82            });
83          });
84          Temperatures.forEach((sensor) => {
85            sensorData.push({
86              name: sensor.Name,
87              status: sensor.Status.Health,
88              currentValue: sensor.ReadingCelsius,
89              lowerCaution: sensor.LowerThresholdNonCritical,
90              upperCaution: sensor.UpperThresholdNonCritical,
91              lowerCritical: sensor.LowerThresholdCritical,
92              upperCritical: sensor.UpperThresholdCritical,
93              units: '℃',
94            });
95          });
96          commit('setSensors', sensorData);
97        })
98        .catch((error) => console.log(error));
99    },
100    async getPowerSensors({ commit }, id) {
101      return await api
102        .get(`${id}/Power`)
103        .then(({ data: { Voltages = [] } }) => {
104          const sensorData = Voltages.map((sensor) => {
105            return {
106              name: sensor.Name,
107              status: sensor.Status.Health,
108              currentValue: sensor.ReadingVolts,
109              lowerCaution: sensor.LowerThresholdNonCritical,
110              upperCaution: sensor.UpperThresholdNonCritical,
111              lowerCritical: sensor.LowerThresholdCritical,
112              upperCritical: sensor.UpperThresholdCritical,
113              units: 'V',
114            };
115          });
116          commit('setSensors', sensorData);
117        })
118        .catch((error) => console.log(error));
119    },
120  },
121};
122
123export default SensorsStore;
124