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 serverStateMapper = (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    assetTag: null,
33    bmcTime: null,
34    serverStatus: 'unreachable',
35    languagePreference: localStorage.getItem('storedLanguage') || 'en-US',
36    isUtcDisplay: localStorage.getItem('storedUtcDisplay')
37      ? JSON.parse(localStorage.getItem('storedUtcDisplay'))
38      : true,
39    username: localStorage.getItem('storedUsername'),
40    isAuthorized: true,
41  },
42  getters: {
43    assetTag: (state) => state.assetTag,
44    serverStatus: (state) => state.serverStatus,
45    bmcTime: (state) => state.bmcTime,
46    languagePreference: (state) => state.languagePreference,
47    isUtcDisplay: (state) => state.isUtcDisplay,
48    username: (state) => state.username,
49    isAuthorized: (state) => state.isAuthorized,
50  },
51  mutations: {
52    setAssetTag: (state, assetTag) => (state.assetTag = assetTag),
53    setBmcTime: (state, bmcTime) => (state.bmcTime = bmcTime),
54    setServerStatus: (state, serverState) =>
55      (state.serverStatus = serverStateMapper(serverState)),
56    setLanguagePreference: (state, language) =>
57      (state.languagePreference = language),
58    setUsername: (state, username) => (state.username = username),
59    setUtcTime: (state, isUtcDisplay) => (state.isUtcDisplay = isUtcDisplay),
60    setUnauthorized: (state) => {
61      state.isAuthorized = false;
62      window.setTimeout(() => {
63        state.isAuthorized = true;
64      }, 100);
65    },
66  },
67  actions: {
68    async getBmcTime({ commit }) {
69      return await api
70        .get('/redfish/v1/Managers/bmc')
71        .then((response) => {
72          const bmcDateTime = response.data.DateTime;
73          const date = new Date(bmcDateTime);
74          commit('setBmcTime', date);
75        })
76        .catch((error) => console.log(error));
77    },
78    getServerStatus({ commit }) {
79      api
80        .get('/redfish/v1/Systems/system')
81        .then(
82          ({ data: { AssetTag, PowerState, Status: { State } = {} } } = {}) => {
83            commit('setAssetTag', AssetTag);
84            if (State === 'Quiesced' || State === 'InTest') {
85              // OpenBMC's host state interface is mapped to 2 Redfish
86              // properties "Status""State" and "PowerState". Look first
87              // at State for certain cases.
88              commit('setServerStatus', State);
89            } else {
90              commit('setServerStatus', PowerState);
91            }
92          }
93        )
94        .catch((error) => console.log(error));
95    },
96  },
97};
98
99export default GlobalStore;
100