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