import api from '@/store/api'; const HOST_STATE = { on: 'xyz.openbmc_project.State.Host.HostState.Running', off: 'xyz.openbmc_project.State.Host.HostState.Off', error: 'xyz.openbmc_project.State.Host.HostState.Quiesced', diagnosticMode: 'xyz.openbmc_project.State.Host.HostState.DiagnosticMode', }; const convertTZ = (date, tzString) => { return new Date( (typeof date === 'string' ? new Date(date) : date).toLocaleString('en-US', { timeZone: tzString, }) ); }; const serverStateMapper = (hostState) => { switch (hostState) { case HOST_STATE.on: case 'On': // Redfish PowerState return 'on'; case HOST_STATE.off: case 'Off': // Redfish PowerState return 'off'; case HOST_STATE.error: case 'Quiesced': // Redfish Status return 'error'; case HOST_STATE.diagnosticMode: case 'InTest': // Redfish Status return 'diagnosticMode'; default: return 'unreachable'; } }; const GlobalStore = { namespaced: true, state: { assetTag: null, bmcTime: null, liveBmcTime: null, liveClock: null, clockInterval: null, modelType: null, serialNumber: null, serverStatus: 'unreachable', timeZone: localStorage.getItem('storedTimeZone'), languagePreference: localStorage.getItem('storedLanguage') || 'en-US', isUtcDisplay: localStorage.getItem('storedUtcDisplay') ? JSON.parse(localStorage.getItem('storedUtcDisplay')) : false, username: localStorage.getItem('storedUsername'), isAuthorized: true, }, getters: { assetTag: (state) => state.assetTag, modelType: (state) => state.modelType, serialNumber: (state) => state.serialNumber, serverStatus: (state) => state.serverStatus, bmcTime: (state) => state.bmcTime, liveBmcTime: (state) => state.liveBmcTime, liveClock: (state) => state.liveClock, timeZone: (state) => state.timeZone, languagePreference: (state) => state.languagePreference, isUtcDisplay: (state) => state.isUtcDisplay, username: (state) => state.username, isAuthorized: (state) => state.isAuthorized, }, mutations: { setAssetTag: (state, assetTag) => (state.assetTag = assetTag), setModelType: (state, modelType) => (state.modelType = modelType), setSerialNumber: (state, serialNumber) => (state.serialNumber = serialNumber), setBmcTime: (state, bmcTime) => (state.bmcTime = bmcTime), setLiveBmcTime: (state, liveBmcTime) => (state.liveBmcTime = liveBmcTime), setLiveClock: (state, liveClock) => (state.liveClock = liveClock), setTimeZone: (state, timeZone) => { state.timeZone = timeZone; localStorage.setItem('storedTimeZone', timeZone); }, setClockInterval: (state, clockInterval) => (state.clockInterval = clockInterval), setServerStatus: (state, serverState) => (state.serverStatus = serverStateMapper(serverState)), setLanguagePreference: (state, language) => (state.languagePreference = language), setUsername: (state, username) => (state.username = username), setUtcTime: (state, isUtcDisplay) => (state.isUtcDisplay = isUtcDisplay), setUnauthorized: (state) => { state.isAuthorized = false; window.setTimeout(() => { state.isAuthorized = true; }, 100); }, }, actions: { changeTimeZone({ commit }, timeZone) { commit('setTimeZone', timeZone); }, changeServerStatus({ commit, dispatch }, status) { commit('setServerStatus', status); if (serverStateMapper(status) === 'on') { dispatch('getBmcTime'); } }, initLiveClock({ commit, state }) { if (state.clockInterval) { clearInterval(state.clockInterval); commit('setClockInterval', null); } let clockData = state.bmcTime; let clockInterval = setInterval(() => { clockData.setSeconds(clockData.getSeconds() + 1); commit('setLiveBmcTime', clockData.toString()); let hours = clockData.getHours(); let minutes = clockData.getMinutes(); let sec = clockData.getSeconds(); if (minutes < 10) { minutes = '0' + minutes; } if (sec < 10) { sec = '0' + sec; } commit('setLiveClock', hours + ':' + minutes + ':' + sec); }, 1000); commit('setClockInterval', clockInterval); }, async getBmcTime({ commit, dispatch, getters }) { if (!getters.timeZone) { commit('setTimeZone', '(UTC+3) Europe/Moscow'); } return await api .get('/redfish/v1/Managers/bmc') .then((response) => { const bmcDateTime = response.data.DateTime; const date = new Date(bmcDateTime); const timeZone = getters.timeZone.split(' ')[1]; commit('setBmcTime', convertTZ(date, timeZone)); dispatch('initLiveClock'); }) .catch((error) => console.log(error)); }, getSystemInfo({ commit }) { api .get('/redfish/v1/Systems/system') .then( ({ data: { AssetTag, Model, PowerState, SerialNumber, Status: { State } = {}, }, } = {}) => { /* Trim AssetTag and SerialNumber */ var s, tag, srn; s = AssetTag; tag = s.replace(/\./g, ' ').trim(); s = SerialNumber; srn = s.replace(/\./g, ' ').trim(); commit('setAssetTag', tag); commit('setSerialNumber', srn); commit('setModelType', Model); if (State === 'Quiesced' || State === 'InTest') { // OpenBMC's host state interface is mapped to 2 Redfish // properties "Status""State" and "PowerState". Look first // at State for certain cases. commit('setServerStatus', State); } else { commit('setServerStatus', PowerState); } } ) .catch((error) => console.log(error)); }, }, }; export default GlobalStore;