SurenNeware | 6185909 | 2020-10-01 09:37:32 +0530 | [diff] [blame] | 1 | import api from '@/store/api'; |
Yoshie Muranaka | b8b6f79 | 2019-12-03 14:47:32 -0800 | [diff] [blame] | 2 | |
Yoshie Muranaka | dc04feb | 2019-12-04 08:41:22 -0800 | [diff] [blame] | 3 | const 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', |
Derick Montague | 602e98a | 2020-10-21 16:20:00 -0500 | [diff] [blame^] | 7 | diagnosticMode: 'xyz.openbmc_project.State.Host.HostState.DiagnosticMode', |
Yoshie Muranaka | dc04feb | 2019-12-04 08:41:22 -0800 | [diff] [blame] | 8 | }; |
| 9 | |
Derick Montague | 602e98a | 2020-10-21 16:20:00 -0500 | [diff] [blame^] | 10 | const hostStateMapper = (hostState) => { |
Yoshie Muranaka | dc04feb | 2019-12-04 08:41:22 -0800 | [diff] [blame] | 11 | switch (hostState) { |
| 12 | case HOST_STATE.on: |
Yoshie Muranaka | 881ddc4 | 2020-04-21 19:24:29 -0700 | [diff] [blame] | 13 | case 'On': // Redfish PowerState |
Yoshie Muranaka | dc04feb | 2019-12-04 08:41:22 -0800 | [diff] [blame] | 14 | return 'on'; |
| 15 | case HOST_STATE.off: |
Yoshie Muranaka | 881ddc4 | 2020-04-21 19:24:29 -0700 | [diff] [blame] | 16 | case 'Off': // Redfish PowerState |
Yoshie Muranaka | dc04feb | 2019-12-04 08:41:22 -0800 | [diff] [blame] | 17 | return 'off'; |
| 18 | case HOST_STATE.error: |
Yoshie Muranaka | a3cbc65 | 2020-05-13 12:55:48 -0700 | [diff] [blame] | 19 | case 'Quiesced': // Redfish Status |
Yoshie Muranaka | dc04feb | 2019-12-04 08:41:22 -0800 | [diff] [blame] | 20 | return 'error'; |
Yoshie Muranaka | a3cbc65 | 2020-05-13 12:55:48 -0700 | [diff] [blame] | 21 | case HOST_STATE.diagnosticMode: |
| 22 | case 'InTest': // Redfish Status |
| 23 | return 'diagnosticMode'; |
Yoshie Muranaka | dc04feb | 2019-12-04 08:41:22 -0800 | [diff] [blame] | 24 | default: |
| 25 | return 'unreachable'; |
| 26 | } |
| 27 | }; |
| 28 | |
Yoshie Muranaka | b8b6f79 | 2019-12-03 14:47:32 -0800 | [diff] [blame] | 29 | const GlobalStore = { |
| 30 | namespaced: true, |
| 31 | state: { |
Yoshie Muranaka | e45f54b | 2020-03-26 15:23:34 -0700 | [diff] [blame] | 32 | bmcTime: null, |
Yoshie Muranaka | 68bbba2 | 2020-05-18 09:49:37 -0700 | [diff] [blame] | 33 | hostStatus: 'unreachable', |
Sukanya Pandey | b1f559f | 2020-04-28 20:18:28 +0530 | [diff] [blame] | 34 | languagePreference: localStorage.getItem('storedLanguage') || 'en-US', |
Sukanya Pandey | fc16f3c | 2020-06-23 22:54:27 +0530 | [diff] [blame] | 35 | isUtcDisplay: localStorage.getItem('storedUtcDisplay') |
| 36 | ? JSON.parse(localStorage.getItem('storedUtcDisplay')) |
| 37 | : true, |
Sukanya Pandey | dd6aa0a | 2020-10-08 20:47:39 +0530 | [diff] [blame] | 38 | username: localStorage.getItem('storedUsername'), |
Derick Montague | 602e98a | 2020-10-21 16:20:00 -0500 | [diff] [blame^] | 39 | isAuthorized: true, |
Yoshie Muranaka | b8b6f79 | 2019-12-03 14:47:32 -0800 | [diff] [blame] | 40 | }, |
| 41 | getters: { |
Derick Montague | 602e98a | 2020-10-21 16:20:00 -0500 | [diff] [blame^] | 42 | hostStatus: (state) => state.hostStatus, |
| 43 | bmcTime: (state) => state.bmcTime, |
| 44 | languagePreference: (state) => state.languagePreference, |
| 45 | isUtcDisplay: (state) => state.isUtcDisplay, |
| 46 | username: (state) => state.username, |
| 47 | isAuthorized: (state) => state.isAuthorized, |
Yoshie Muranaka | b8b6f79 | 2019-12-03 14:47:32 -0800 | [diff] [blame] | 48 | }, |
| 49 | mutations: { |
Dixsie Wolmers | f65ee34 | 2020-01-22 19:47:56 -0600 | [diff] [blame] | 50 | setBmcTime: (state, bmcTime) => (state.bmcTime = bmcTime), |
| 51 | setHostStatus: (state, hostState) => |
Yoshie Muranaka | 68bbba2 | 2020-05-18 09:49:37 -0700 | [diff] [blame] | 52 | (state.hostStatus = hostStateMapper(hostState)), |
| 53 | setLanguagePreference: (state, language) => |
Sukanya Pandey | b1f559f | 2020-04-28 20:18:28 +0530 | [diff] [blame] | 54 | (state.languagePreference = language), |
Sukanya Pandey | fc16f3c | 2020-06-23 22:54:27 +0530 | [diff] [blame] | 55 | setUsername: (state, username) => (state.username = username), |
Sukanya Pandey | dd6aa0a | 2020-10-08 20:47:39 +0530 | [diff] [blame] | 56 | setUtcTime: (state, isUtcDisplay) => (state.isUtcDisplay = isUtcDisplay), |
Derick Montague | 602e98a | 2020-10-21 16:20:00 -0500 | [diff] [blame^] | 57 | setUnauthorized: (state) => { |
Sukanya Pandey | dd6aa0a | 2020-10-08 20:47:39 +0530 | [diff] [blame] | 58 | state.isAuthorized = false; |
| 59 | window.setTimeout(() => { |
| 60 | state.isAuthorized = true; |
| 61 | }, 100); |
Derick Montague | 602e98a | 2020-10-21 16:20:00 -0500 | [diff] [blame^] | 62 | }, |
Yoshie Muranaka | b8b6f79 | 2019-12-03 14:47:32 -0800 | [diff] [blame] | 63 | }, |
| 64 | actions: { |
Yoshie Muranaka | 598bf7e | 2020-05-01 12:26:00 -0700 | [diff] [blame] | 65 | async getBmcTime({ commit }) { |
| 66 | return await api |
Yoshie Muranaka | a371d20 | 2020-04-13 10:42:09 -0700 | [diff] [blame] | 67 | .get('/redfish/v1/Managers/bmc') |
Derick Montague | 602e98a | 2020-10-21 16:20:00 -0500 | [diff] [blame^] | 68 | .then((response) => { |
Yoshie Muranaka | a371d20 | 2020-04-13 10:42:09 -0700 | [diff] [blame] | 69 | const bmcDateTime = response.data.DateTime; |
| 70 | const date = new Date(bmcDateTime); |
Dixsie Wolmers | 97f4187 | 2020-02-23 15:56:16 -0600 | [diff] [blame] | 71 | commit('setBmcTime', date); |
Dixsie Wolmers | f65ee34 | 2020-01-22 19:47:56 -0600 | [diff] [blame] | 72 | }) |
Derick Montague | 602e98a | 2020-10-21 16:20:00 -0500 | [diff] [blame^] | 73 | .catch((error) => console.log(error)); |
Dixsie Wolmers | f65ee34 | 2020-01-22 19:47:56 -0600 | [diff] [blame] | 74 | }, |
Yoshie Muranaka | dc04feb | 2019-12-04 08:41:22 -0800 | [diff] [blame] | 75 | getHostStatus({ commit }) { |
| 76 | api |
Yoshie Muranaka | 881ddc4 | 2020-04-21 19:24:29 -0700 | [diff] [blame] | 77 | .get('/redfish/v1/Systems/system') |
Yoshie Muranaka | a3cbc65 | 2020-05-13 12:55:48 -0700 | [diff] [blame] | 78 | .then(({ data: { PowerState, Status: { State } = {} } } = {}) => { |
| 79 | if (State === 'Quiesced' || State === 'InTest') { |
| 80 | // OpenBMC's host state interface is mapped to 2 Redfish |
| 81 | // properties "Status""State" and "PowerState". Look first |
| 82 | // at State for certain cases. |
| 83 | commit('setHostStatus', State); |
| 84 | } else { |
| 85 | commit('setHostStatus', PowerState); |
| 86 | } |
Yoshie Muranaka | dc04feb | 2019-12-04 08:41:22 -0800 | [diff] [blame] | 87 | }) |
Derick Montague | 602e98a | 2020-10-21 16:20:00 -0500 | [diff] [blame^] | 88 | .catch((error) => console.log(error)); |
| 89 | }, |
| 90 | }, |
Yoshie Muranaka | b8b6f79 | 2019-12-03 14:47:32 -0800 | [diff] [blame] | 91 | }; |
| 92 | |
| 93 | export default GlobalStore; |