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    username: localStorage.getItem('storedUsername')
36  },
37  getters: {
38    hostStatus: state => state.hostStatus,
39    bmcTime: state => state.bmcTime,
40    languagePreference: state => state.languagePreference,
41    username: state => state.username
42  },
43  mutations: {
44    setBmcTime: (state, bmcTime) => (state.bmcTime = bmcTime),
45    setHostStatus: (state, hostState) =>
46      (state.hostStatus = hostStateMapper(hostState)),
47    setLanguagePreference: (state, language) =>
48      (state.languagePreference = language),
49    setUsername: (state, username) => (state.username = username)
50  },
51  actions: {
52    async getBmcTime({ commit }) {
53      return await api
54        .get('/redfish/v1/Managers/bmc')
55        .then(response => {
56          const bmcDateTime = response.data.DateTime;
57          const date = new Date(bmcDateTime);
58          commit('setBmcTime', date);
59        })
60        .catch(error => console.log(error));
61    },
62    getHostStatus({ commit }) {
63      api
64        .get('/redfish/v1/Systems/system')
65        .then(({ data: { PowerState, Status: { State } = {} } } = {}) => {
66          if (State === 'Quiesced' || State === 'InTest') {
67            // OpenBMC's host state interface is mapped to 2 Redfish
68            // properties "Status""State" and "PowerState". Look first
69            // at State for certain cases.
70            commit('setHostStatus', State);
71          } else {
72            commit('setHostStatus', PowerState);
73          }
74        })
75        .catch(error => console.log(error));
76    }
77  }
78};
79
80export default GlobalStore;
81