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    userPrivilege: null,
44  },
45  getters: {
46    assetTag: (state) => state.assetTag,
47    modelType: (state) => state.modelType,
48    serialNumber: (state) => state.serialNumber,
49    serverStatus: (state) => state.serverStatus,
50    bmcTime: (state) => state.bmcTime,
51    languagePreference: (state) => state.languagePreference,
52    isUtcDisplay: (state) => state.isUtcDisplay,
53    username: (state) => state.username,
54    isAuthorized: (state) => state.isAuthorized,
55    userPrivilege: (state) => state.userPrivilege,
56  },
57  mutations: {
58    setAssetTag: (state, assetTag) => (state.assetTag = assetTag),
59    setModelType: (state, modelType) => (state.modelType = modelType),
60    setSerialNumber: (state, serialNumber) =>
61      (state.serialNumber = serialNumber),
62    setBmcTime: (state, bmcTime) => (state.bmcTime = bmcTime),
63    setServerStatus: (state, serverState) =>
64      (state.serverStatus = serverStateMapper(serverState)),
65    setLanguagePreference: (state, language) =>
66      (state.languagePreference = language),
67    setUsername: (state, username) => (state.username = username),
68    setUtcTime: (state, isUtcDisplay) => (state.isUtcDisplay = isUtcDisplay),
69    setUnauthorized: (state) => {
70      state.isAuthorized = false;
71      window.setTimeout(() => {
72        state.isAuthorized = true;
73      }, 100);
74    },
75    setPrivilege: (state, privilege) => {
76      state.userPrivilege = privilege;
77    },
78  },
79  actions: {
80    async getBmcPath() {
81      const serviceRoot = await api
82        .get('/redfish/v1')
83        .catch((error) => console.log(error));
84      let bmcPath = serviceRoot?.data?.ManagerProvidingService?.['@odata.id'];
85      if (!bmcPath) {
86        const managers = await api
87          .get('/redfish/v1/Managers')
88          .catch((error) => console.log(error));
89        bmcPath = managers.data?.Members?.[0]?.['@odata.id'];
90      }
91      return bmcPath;
92    },
93    async getSystemPath() {
94      const systems = await api
95        .get('/redfish/v1/Systems')
96        .catch((error) => console.log(error));
97      let systemPath = systems?.data?.Members?.[0]?.['@odata.id'];
98      return systemPath;
99    },
100    async getBmcTime({ commit }) {
101      return await api
102        .get(`${await this.dispatch('global/getBmcPath')}`)
103        .then((response) => {
104          const bmcDateTime = response.data.DateTime;
105          const date = new Date(bmcDateTime);
106          commit('setBmcTime', date);
107        })
108        .catch((error) => console.log(error));
109    },
110    async getSystemInfo({ commit }) {
111      api
112        .get(`${await this.dispatch('global/getSystemPath')}`)
113        .then(
114          ({
115            data: {
116              AssetTag,
117              Model,
118              PowerState,
119              SerialNumber,
120              Status: { State } = {},
121            },
122          } = {}) => {
123            commit('setAssetTag', AssetTag);
124            commit('setSerialNumber', SerialNumber);
125            commit('setModelType', Model);
126            if (State === 'Quiesced' || State === 'InTest') {
127              // OpenBMC's host state interface is mapped to 2 Redfish
128              // properties "Status""State" and "PowerState". Look first
129              // at State for certain cases.
130              commit('setServerStatus', State);
131            } else {
132              commit('setServerStatus', PowerState);
133            }
134          },
135        )
136        .catch((error) => console.log(error));
137    },
138  },
139};
140
141export default GlobalStore;
142