1import api from '../api';
2
3const HOST_STATE = {
4  on: 'xyz.openbmc_project.State.Host.HostState.Running',
5  off: 'xyz.openbmc_project.State.Host.HostState.Off',
6  error: 'xyz.openbmc_project.State.Host.HostState.Quiesced',
7  diagnosticMode: 'xyz.openbmc_project.State.Host.HostState.DiagnosticMode'
8};
9
10const hostStateMapper = hostState => {
11  switch (hostState) {
12    case HOST_STATE.on:
13    case 'On': // Redfish PowerState
14      return 'on';
15    case HOST_STATE.off:
16    case 'Off': // Redfish PowerState
17      return 'off';
18    case HOST_STATE.error:
19    case 'Quiesced': // Redfish Status
20      return 'error';
21    case HOST_STATE.diagnosticMode:
22    case 'InTest': // Redfish Status
23      return 'diagnosticMode';
24    default:
25      return 'unreachable';
26  }
27};
28
29const GlobalStore = {
30  namespaced: true,
31  state: {
32    bmcTime: null,
33    hostStatus: 'unreachable',
34    languagePreference: localStorage.getItem('storedLanguage') || 'en-US'
35  },
36  getters: {
37    hostStatus: state => state.hostStatus,
38    bmcTime: state => state.bmcTime,
39    languagePreference: state => state.languagePreference
40  },
41  mutations: {
42    setBmcTime: (state, bmcTime) => (state.bmcTime = bmcTime),
43    setHostStatus: (state, hostState) =>
44      (state.hostStatus = hostStateMapper(hostState)),
45    setLanguagePreference: (state, language) =>
46      (state.languagePreference = language)
47  },
48  actions: {
49    async getBmcTime({ commit }) {
50      return await api
51        .get('/redfish/v1/Managers/bmc')
52        .then(response => {
53          const bmcDateTime = response.data.DateTime;
54          const date = new Date(bmcDateTime);
55          commit('setBmcTime', date);
56        })
57        .catch(error => console.log(error));
58    },
59    getHostStatus({ commit }) {
60      api
61        .get('/redfish/v1/Systems/system')
62        .then(({ data: { PowerState, Status: { State } = {} } } = {}) => {
63          if (State === 'Quiesced' || State === 'InTest') {
64            // OpenBMC's host state interface is mapped to 2 Redfish
65            // properties "Status""State" and "PowerState". Look first
66            // at State for certain cases.
67            commit('setHostStatus', State);
68          } else {
69            commit('setHostStatus', PowerState);
70          }
71        })
72        .catch(error => console.log(error));
73    }
74  }
75};
76
77export default GlobalStore;
78