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