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