blob: 55b07965e81a9e735be3bd35624d221101b70fe7 [file] [log] [blame]
Derick Montaguefded0d12019-12-11 06:16:40 -06001import api from '../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',
35 username: localStorage.getItem('storedUsername')
Yoshie Muranakab8b6f792019-12-03 14:47:32 -080036 },
37 getters: {
Dixsie Wolmersf65ee342020-01-22 19:47:56 -060038 hostStatus: state => state.hostStatus,
Yoshie Muranaka68bbba22020-05-18 09:49:37 -070039 bmcTime: state => state.bmcTime,
Sukanya Pandeyb1f559f2020-04-28 20:18:28 +053040 languagePreference: state => state.languagePreference,
41 username: state => state.username
Yoshie Muranakab8b6f792019-12-03 14:47:32 -080042 },
43 mutations: {
Dixsie Wolmersf65ee342020-01-22 19:47:56 -060044 setBmcTime: (state, bmcTime) => (state.bmcTime = bmcTime),
45 setHostStatus: (state, hostState) =>
Yoshie Muranaka68bbba22020-05-18 09:49:37 -070046 (state.hostStatus = hostStateMapper(hostState)),
47 setLanguagePreference: (state, language) =>
Sukanya Pandeyb1f559f2020-04-28 20:18:28 +053048 (state.languagePreference = language),
49 setUsername: (state, username) => (state.username = username)
Yoshie Muranakab8b6f792019-12-03 14:47:32 -080050 },
51 actions: {
Yoshie Muranaka598bf7e2020-05-01 12:26:00 -070052 async getBmcTime({ commit }) {
53 return await api
Yoshie Muranakaa371d202020-04-13 10:42:09 -070054 .get('/redfish/v1/Managers/bmc')
Dixsie Wolmersf65ee342020-01-22 19:47:56 -060055 .then(response => {
Yoshie Muranakaa371d202020-04-13 10:42:09 -070056 const bmcDateTime = response.data.DateTime;
57 const date = new Date(bmcDateTime);
Dixsie Wolmers97f41872020-02-23 15:56:16 -060058 commit('setBmcTime', date);
Dixsie Wolmersf65ee342020-01-22 19:47:56 -060059 })
60 .catch(error => console.log(error));
61 },
Yoshie Muranakadc04feb2019-12-04 08:41:22 -080062 getHostStatus({ commit }) {
63 api
Yoshie Muranaka881ddc42020-04-21 19:24:29 -070064 .get('/redfish/v1/Systems/system')
Yoshie Muranakaa3cbc652020-05-13 12:55:48 -070065 .then(({ data: { PowerState, Status: { State } = {} } } = {}) => {
66 if (State === 'Quiesced' || State === 'InTest') {
67 // OpenBMC's host state interface is mapped to 2 Redfish
68 // properties "Status""State" and "PowerState". Look first
69 // at State for certain cases.
70 commit('setHostStatus', State);
71 } else {
72 commit('setHostStatus', PowerState);
73 }
Yoshie Muranakadc04feb2019-12-04 08:41:22 -080074 })
75 .catch(error => console.log(error));
Yoshie Muranakab8b6f792019-12-03 14:47:32 -080076 }
77 }
78};
79
80export default GlobalStore;