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: null,
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 millseconds
57          const bmcTime = response.data.data.Elapsed / 1000;
58          commit('setBmcTime', bmcTime);
59        })
60        .catch(error => console.log(error));
61    },
62    getHostStatus({ commit }) {
63      api
64        .get('/xyz/openbmc_project/state/host0/attr/CurrentHostState')
65        .then(response => {
66          const hostState = response.data.data;
67          commit('setHostStatus', hostState);
68        })
69        .catch(error => console.log(error));
70    }
71  }
72};
73
74export default GlobalStore;
75