blob: 03301538abb4a78ae06c6d875bde101803a165cf [file] [log] [blame]
SurenNeware61859092020-10-01 09:37:32 +05301import api from '@/store/api';
Yoshie Muranakab8b6f792019-12-03 14:47:32 -08002
Yoshie Muranakadc04feb2019-12-04 08:41:22 -08003const 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:
Yoshie Muranaka881ddc42020-04-21 19:24:29 -070013 case 'On': // Redfish PowerState
Yoshie Muranakadc04feb2019-12-04 08:41:22 -080014 return 'on';
15 case HOST_STATE.off:
Yoshie Muranaka881ddc42020-04-21 19:24:29 -070016 case 'Off': // Redfish PowerState
Yoshie Muranakadc04feb2019-12-04 08:41:22 -080017 return 'off';
18 case HOST_STATE.error:
Yoshie Muranakaa3cbc652020-05-13 12:55:48 -070019 case 'Quiesced': // Redfish Status
Yoshie Muranakadc04feb2019-12-04 08:41:22 -080020 return 'error';
Yoshie Muranakaa3cbc652020-05-13 12:55:48 -070021 case HOST_STATE.diagnosticMode:
22 case 'InTest': // Redfish Status
23 return 'diagnosticMode';
Yoshie Muranakadc04feb2019-12-04 08:41:22 -080024 default:
25 return 'unreachable';
26 }
27};
28
Yoshie Muranakab8b6f792019-12-03 14:47:32 -080029const GlobalStore = {
30 namespaced: true,
31 state: {
Yoshie Muranakae45f54b2020-03-26 15:23:34 -070032 bmcTime: null,
Yoshie Muranaka68bbba22020-05-18 09:49:37 -070033 hostStatus: 'unreachable',
Sukanya Pandeyb1f559f2020-04-28 20:18:28 +053034 languagePreference: localStorage.getItem('storedLanguage') || 'en-US',
Sukanya Pandeyfc16f3c2020-06-23 22:54:27 +053035 isUtcDisplay: localStorage.getItem('storedUtcDisplay')
36 ? JSON.parse(localStorage.getItem('storedUtcDisplay'))
37 : true,
Sukanya Pandeyb1f559f2020-04-28 20:18:28 +053038 username: localStorage.getItem('storedUsername')
Yoshie Muranakab8b6f792019-12-03 14:47:32 -080039 },
40 getters: {
Dixsie Wolmersf65ee342020-01-22 19:47:56 -060041 hostStatus: state => state.hostStatus,
Yoshie Muranaka68bbba22020-05-18 09:49:37 -070042 bmcTime: state => state.bmcTime,
Sukanya Pandeyb1f559f2020-04-28 20:18:28 +053043 languagePreference: state => state.languagePreference,
Sukanya Pandeyfc16f3c2020-06-23 22:54:27 +053044 isUtcDisplay: state => state.isUtcDisplay,
Sukanya Pandeyb1f559f2020-04-28 20:18:28 +053045 username: state => state.username
Yoshie Muranakab8b6f792019-12-03 14:47:32 -080046 },
47 mutations: {
Dixsie Wolmersf65ee342020-01-22 19:47:56 -060048 setBmcTime: (state, bmcTime) => (state.bmcTime = bmcTime),
49 setHostStatus: (state, hostState) =>
Yoshie Muranaka68bbba22020-05-18 09:49:37 -070050 (state.hostStatus = hostStateMapper(hostState)),
51 setLanguagePreference: (state, language) =>
Sukanya Pandeyb1f559f2020-04-28 20:18:28 +053052 (state.languagePreference = language),
Sukanya Pandeyfc16f3c2020-06-23 22:54:27 +053053 setUsername: (state, username) => (state.username = username),
54 setUtcTime: (state, isUtcDisplay) => (state.isUtcDisplay = isUtcDisplay)
Yoshie Muranakab8b6f792019-12-03 14:47:32 -080055 },
56 actions: {
Yoshie Muranaka598bf7e2020-05-01 12:26:00 -070057 async getBmcTime({ commit }) {
58 return await api
Yoshie Muranakaa371d202020-04-13 10:42:09 -070059 .get('/redfish/v1/Managers/bmc')
Dixsie Wolmersf65ee342020-01-22 19:47:56 -060060 .then(response => {
Yoshie Muranakaa371d202020-04-13 10:42:09 -070061 const bmcDateTime = response.data.DateTime;
62 const date = new Date(bmcDateTime);
Dixsie Wolmers97f41872020-02-23 15:56:16 -060063 commit('setBmcTime', date);
Dixsie Wolmersf65ee342020-01-22 19:47:56 -060064 })
65 .catch(error => console.log(error));
66 },
Yoshie Muranakadc04feb2019-12-04 08:41:22 -080067 getHostStatus({ commit }) {
68 api
Yoshie Muranaka881ddc42020-04-21 19:24:29 -070069 .get('/redfish/v1/Systems/system')
Yoshie Muranakaa3cbc652020-05-13 12:55:48 -070070 .then(({ data: { PowerState, Status: { State } = {} } } = {}) => {
71 if (State === 'Quiesced' || State === 'InTest') {
72 // OpenBMC's host state interface is mapped to 2 Redfish
73 // properties "Status""State" and "PowerState". Look first
74 // at State for certain cases.
75 commit('setHostStatus', State);
76 } else {
77 commit('setHostStatus', PowerState);
78 }
Yoshie Muranakadc04feb2019-12-04 08:41:22 -080079 })
80 .catch(error => console.log(error));
Yoshie Muranakab8b6f792019-12-03 14:47:32 -080081 }
82 }
83};
84
85export default GlobalStore;