summaryrefslogtreecommitdiff
path: root/src/store/modules/GlobalStore.js
blob: 55b07965e81a9e735be3bd35624d221101b70fe7 (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
import api from '../api';

const HOST_STATE = {
  on: 'xyz.openbmc_project.State.Host.HostState.Running',
  off: 'xyz.openbmc_project.State.Host.HostState.Off',
  error: 'xyz.openbmc_project.State.Host.HostState.Quiesced',
  diagnosticMode: 'xyz.openbmc_project.State.Host.HostState.DiagnosticMode'
};

const hostStateMapper = hostState => {
  switch (hostState) {
    case HOST_STATE.on:
    case 'On': // Redfish PowerState
      return 'on';
    case HOST_STATE.off:
    case 'Off': // Redfish PowerState
      return 'off';
    case HOST_STATE.error:
    case 'Quiesced': // Redfish Status
      return 'error';
    case HOST_STATE.diagnosticMode:
    case 'InTest': // Redfish Status
      return 'diagnosticMode';
    default:
      return 'unreachable';
  }
};

const GlobalStore = {
  namespaced: true,
  state: {
    bmcTime: null,
    hostStatus: 'unreachable',
    languagePreference: localStorage.getItem('storedLanguage') || 'en-US',
    username: localStorage.getItem('storedUsername')
  },
  getters: {
    hostStatus: state => state.hostStatus,
    bmcTime: state => state.bmcTime,
    languagePreference: state => state.languagePreference,
    username: state => state.username
  },
  mutations: {
    setBmcTime: (state, bmcTime) => (state.bmcTime = bmcTime),
    setHostStatus: (state, hostState) =>
      (state.hostStatus = hostStateMapper(hostState)),
    setLanguagePreference: (state, language) =>
      (state.languagePreference = language),
    setUsername: (state, username) => (state.username = username)
  },
  actions: {
    async getBmcTime({ commit }) {
      return await api
        .get('/redfish/v1/Managers/bmc')
        .then(response => {
          const bmcDateTime = response.data.DateTime;
          const date = new Date(bmcDateTime);
          commit('setBmcTime', date);
        })
        .catch(error => console.log(error));
    },
    getHostStatus({ commit }) {
      api
        .get('/redfish/v1/Systems/system')
        .then(({ data: { PowerState, Status: { State } = {} } } = {}) => {
          if (State === 'Quiesced' || State === 'InTest') {
            // OpenBMC's host state interface is mapped to 2 Redfish
            // properties "Status""State" and "PowerState". Look first
            // at State for certain cases.
            commit('setHostStatus', State);
          } else {
            commit('setHostStatus', PowerState);
          }
        })
        .catch(error => console.log(error));
    }
  }
};

export default GlobalStore;