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      // TODO: Map Redfish Quiesced when bmcweb supports
20      return 'error';
21    // TODO: Add mapping for DiagnosticMode
22    default:
23      return 'unreachable';
24  }
25};
26
27const GlobalStore = {
28  namespaced: true,
29  state: {
30    bmcTime: null,
31    hostStatus: 'unreachable'
32  },
33  getters: {
34    hostStatus: state => state.hostStatus,
35    bmcTime: state => state.bmcTime
36  },
37  mutations: {
38    setBmcTime: (state, bmcTime) => (state.bmcTime = bmcTime),
39    setHostStatus: (state, hostState) =>
40      (state.hostStatus = hostStateMapper(hostState))
41  },
42  actions: {
43    getBmcTime({ commit }) {
44      api
45        .get('/redfish/v1/Managers/bmc')
46        .then(response => {
47          const bmcDateTime = response.data.DateTime;
48          const date = new Date(bmcDateTime);
49          commit('setBmcTime', date);
50        })
51        .catch(error => console.log(error));
52    },
53    getHostStatus({ commit }) {
54      api
55        .get('/redfish/v1/Systems/system')
56        .then(({ data: { PowerState } } = {}) => {
57          commit('setHostStatus', PowerState);
58        })
59        .catch(error => console.log(error));
60    }
61  }
62};
63
64export default GlobalStore;
65