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      return 'on';
14    case HOST_STATE.off:
15      return 'off';
16    case HOST_STATE.error:
17      return 'error';
18    // TODO: Add mapping for DiagnosticMode
19    default:
20      return 'unreachable';
21  }
22};
23
24const GlobalStore = {
25  namespaced: true,
26  state: {
27    hostName: '--',
28    bmcTime: '--',
29    hostStatus: 'unreachable'
30  },
31  getters: {
32    hostName: state => state.hostName,
33    hostStatus: state => state.hostStatus,
34    bmcTime: state => state.bmcTime
35  },
36  mutations: {
37    setHostName: (state, hostName) => (state.hostName = hostName),
38    setBmcTime: (state, bmcTime) => (state.bmcTime = bmcTime),
39    setHostStatus: (state, hostState) =>
40      (state.hostStatus = hostStateMapper(hostState))
41  },
42  actions: {
43    getHostName({ commit }) {
44      api
45        .get('/xyz/openbmc_project/network/config/attr/HostName')
46        .then(response => {
47          const hostName = response.data.data;
48          commit('setHostName', hostName);
49        })
50        .catch(error => console.log(error));
51    },
52    getBmcTime({ commit }) {
53      api
54        .get('/xyz/openbmc_project/time/bmc')
55        .then(response => {
56          // bmcTime is stored in microseconds, convert to milliseconds
57          const bmcEpochTime = response.data.data.Elapsed / 1000;
58          const date = new Date(bmcEpochTime);
59          commit('setBmcTime', date);
60        })
61        .catch(error => console.log(error));
62    },
63    getHostStatus({ commit }) {
64      api
65        .get('/xyz/openbmc_project/state/host0/attr/CurrentHostState')
66        .then(response => {
67          const hostState = response.data.data;
68          commit('setHostStatus', hostState);
69        })
70        .catch(error => console.log(error));
71    }
72  }
73};
74
75export default GlobalStore;
76