1import api from '@/store/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 serverStateMapper = (hostState) => { 11 switch (hostState) { 12 case HOST_STATE.on: 13 case 'On': // Redfish PowerState 14 return 'on'; 15 case HOST_STATE.off: 16 case 'Off': // Redfish PowerState 17 return 'off'; 18 case HOST_STATE.error: 19 case 'Quiesced': // Redfish Status 20 return 'error'; 21 case HOST_STATE.diagnosticMode: 22 case 'InTest': // Redfish Status 23 return 'diagnosticMode'; 24 default: 25 return 'unreachable'; 26 } 27}; 28 29const GlobalStore = { 30 namespaced: true, 31 state: { 32 assetTag: null, 33 bmcTime: null, 34 modelType: null, 35 serialNumber: null, 36 serverStatus: 'unreachable', 37 languagePreference: localStorage.getItem('storedLanguage') || 'en-US', 38 isUtcDisplay: localStorage.getItem('storedUtcDisplay') 39 ? JSON.parse(localStorage.getItem('storedUtcDisplay')) 40 : true, 41 username: localStorage.getItem('storedUsername'), 42 isAuthorized: true, 43 userPrivilege: null, 44 }, 45 getters: { 46 assetTag: (state) => state.assetTag, 47 modelType: (state) => state.modelType, 48 serialNumber: (state) => state.serialNumber, 49 serverStatus: (state) => state.serverStatus, 50 bmcTime: (state) => state.bmcTime, 51 languagePreference: (state) => state.languagePreference, 52 isUtcDisplay: (state) => state.isUtcDisplay, 53 username: (state) => state.username, 54 isAuthorized: (state) => state.isAuthorized, 55 userPrivilege: (state) => state.userPrivilege, 56 }, 57 mutations: { 58 setAssetTag: (state, assetTag) => (state.assetTag = assetTag), 59 setModelType: (state, modelType) => (state.modelType = modelType), 60 setSerialNumber: (state, serialNumber) => 61 (state.serialNumber = serialNumber), 62 setBmcTime: (state, bmcTime) => (state.bmcTime = bmcTime), 63 setServerStatus: (state, serverState) => 64 (state.serverStatus = serverStateMapper(serverState)), 65 setLanguagePreference: (state, language) => 66 (state.languagePreference = language), 67 setUsername: (state, username) => (state.username = username), 68 setUtcTime: (state, isUtcDisplay) => (state.isUtcDisplay = isUtcDisplay), 69 setUnauthorized: (state) => { 70 state.isAuthorized = false; 71 window.setTimeout(() => { 72 state.isAuthorized = true; 73 }, 100); 74 }, 75 setPrivilege: (state, privilege) => { 76 state.userPrivilege = privilege; 77 }, 78 }, 79 actions: { 80 async getBmcTime({ commit }) { 81 return await api 82 .get('/redfish/v1/Managers/bmc') 83 .then((response) => { 84 const bmcDateTime = response.data.DateTime; 85 const date = new Date(bmcDateTime); 86 commit('setBmcTime', date); 87 }) 88 .catch((error) => console.log(error)); 89 }, 90 getSystemInfo({ commit }) { 91 api 92 .get('/redfish/v1/Systems/system') 93 .then( 94 ({ 95 data: { 96 AssetTag, 97 Model, 98 PowerState, 99 SerialNumber, 100 Status: { State } = {}, 101 }, 102 } = {}) => { 103 commit('setAssetTag', AssetTag); 104 commit('setSerialNumber', SerialNumber); 105 commit('setModelType', Model); 106 if (State === 'Quiesced' || State === 'InTest') { 107 // OpenBMC's host state interface is mapped to 2 Redfish 108 // properties "Status""State" and "PowerState". Look first 109 // at State for certain cases. 110 commit('setServerStatus', State); 111 } else { 112 commit('setServerStatus', PowerState); 113 } 114 } 115 ) 116 .catch((error) => console.log(error)); 117 }, 118 }, 119}; 120 121export default GlobalStore; 122