summaryrefslogtreecommitdiff
path: root/src/store/modules/Health/SensorsStore.js
blob: 5da15156cc3d3f05f5ab54150b8ba2ed767708dd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
import api from '../../api';
import { uniqBy } from 'lodash';

const SensorsStore = {
  namespaced: true,
  state: {
    sensors: []
  },
  getters: {
    sensors: state => state.sensors
  },
  mutations: {
    setSensors: (state, sensors) => {
      state.sensors = uniqBy([...state.sensors, ...sensors], 'name');
    }
  },
  actions: {
    getAllSensors({ dispatch }) {
      dispatch('getChassisCollection').then(collection => {
        collection.forEach(item => {
          dispatch('getSensors', item);
          dispatch('getThermalSensors', item);
          dispatch('getPowerSensors', item);
        });
      });
    },
    getChassisCollection() {
      return api
        .get('/redfish/v1/Chassis')
        .then(({ data: { Members } }) =>
          Members.map(member => member['@odata.id'])
        )
        .catch(error => console.log(error));
    },
    getSensors({ commit }, id) {
      api
        .get(`${id}/Sensors`)
        .then(({ data: { Members = [] } }) => {
          const promises = Members.map(sensor => api.get(sensor['@odata.id']));
          api.all(promises).then(
            api.spread((...responses) => {
              const sensorData = responses.map(({ data }) => {
                return {
                  name: data.Name,
                  status: data.Status.Health,
                  currentValue: data.Reading,
                  lowerCaution: data.Thresholds.LowerCaution.Reading,
                  upperCaution: data.Thresholds.UpperCaution.Reading,
                  lowerCritical: data.Thresholds.LowerCritical.Reading,
                  upperCritical: data.Thresholds.UpperCritical.Reading,
                  units: data.ReadingUnits
                };
              });
              commit('setSensors', sensorData);
            })
          );
        })
        .catch(error => console.log(error));
    },
    getThermalSensors({ commit }, id) {
      api
        .get(`${id}/Thermal`)
        .then(({ data: { Fans = [], Temperatures = [] } }) => {
          const sensorData = [];
          Fans.forEach(sensor => {
            sensorData.push({
              // TODO: add upper/lower threshold
              name: sensor.Name,
              status: sensor.Status.Health,
              currentValue: sensor.Reading,
              units: sensor.ReadingUnits
            });
          });
          Temperatures.forEach(sensor => {
            sensorData.push({
              name: sensor.Name,
              status: sensor.Status.Health,
              currentValue: sensor.ReadingCelsius,
              lowerCaution: sensor.LowerThresholdNonCritical,
              upperCaution: sensor.UpperThresholdNonCritical,
              lowerCritical: sensor.LowerThresholdCritical,
              upperCritical: sensor.UpperThresholdCritical,
              units: '℃'
            });
          });
          commit('setSensors', sensorData);
        })
        .catch(error => console.log(error));
    },
    getPowerSensors({ commit }, id) {
      api
        .get(`${id}/Power`)
        .then(({ data: { Voltages = [] } }) => {
          const sensorData = Voltages.map(sensor => {
            return {
              name: sensor.Name,
              status: sensor.Status.Health,
              currentValue: sensor.ReadingVolts,
              lowerCaution: sensor.LowerThresholdNonCritical,
              upperCaution: sensor.UpperThresholdNonCritical,
              lowerCritical: sensor.LowerThresholdCritical,
              upperCritical: sensor.UpperThresholdCritical,
              units: 'Volts'
            };
          });
          commit('setSensors', sensorData);
        })
        .catch(error => console.log(error));
    }
  }
};

export default SensorsStore;