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