diff options
Diffstat (limited to 'src/store')
-rw-r--r-- | src/store/api.js | 7 | ||||
-rw-r--r-- | src/store/index.js | 5 | ||||
-rw-r--r-- | src/store/modules/GlobalStore.js | 35 | ||||
-rw-r--r-- | src/store/plugins/WebSocketPlugin.js | 46 |
4 files changed, 85 insertions, 8 deletions
diff --git a/src/store/api.js b/src/store/api.js index da6f3982..463e0d86 100644 --- a/src/store/api.js +++ b/src/store/api.js @@ -4,10 +4,6 @@ const api = Axios.create({ withCredentials: true }); -// TODO: Permanent authentication solution -// Using defaults to set auth for sending -// auth object in header - export default { get(path) { return api.get(path); @@ -26,6 +22,5 @@ export default { }, all(promises) { return Axios.all(promises); - }, - defaults: api.defaults + } }; diff --git a/src/store/index.js b/src/store/index.js index 4ef1c9d0..889e52b4 100644 --- a/src/store/index.js +++ b/src/store/index.js @@ -5,6 +5,8 @@ import GlobalStore from './modules/GlobalStore'; import AuthenticationStore from './modules/Authentication/AuthenticanStore'; import LocalUserManagementStore from './modules/AccessControl/LocalUserMangementStore'; +import WebSocketPlugin from './plugins/WebSocketPlugin'; + Vue.use(Vuex); export default new Vuex.Store({ @@ -15,5 +17,6 @@ export default new Vuex.Store({ global: GlobalStore, authentication: AuthenticationStore, localUsers: LocalUserManagementStore - } + }, + plugins: [WebSocketPlugin] }); diff --git a/src/store/modules/GlobalStore.js b/src/store/modules/GlobalStore.js index 8cf2e8eb..80d9c1a3 100644 --- a/src/store/modules/GlobalStore.js +++ b/src/store/modules/GlobalStore.js @@ -1,10 +1,31 @@ import api from '../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 hostStateMapper = hostState => { + switch (hostState) { + case HOST_STATE.on: + return 'on'; + case HOST_STATE.off: + return 'off'; + case HOST_STATE.error: + return 'error'; + // TODO: Add mapping for DiagnosticMode + default: + return 'unreachable'; + } +}; + const GlobalStore = { namespaced: true, state: { hostName: '--', - hostStatus: null + hostStatus: 'unreachable' }, getters: { hostName(state) { @@ -17,6 +38,9 @@ const GlobalStore = { mutations: { setHostName(state, hostName) { state.hostName = hostName; + }, + setHostStatus(state, hostState) { + state.hostStatus = hostStateMapper(hostState); } }, actions: { @@ -28,6 +52,15 @@ const GlobalStore = { commit('setHostName', hostName); }) .catch(error => console.log(error)); + }, + getHostStatus({ commit }) { + api + .get('/xyz/openbmc_project/state/host0/attr/CurrentHostState') + .then(response => { + const hostState = response.data.data; + commit('setHostStatus', hostState); + }) + .catch(error => console.log(error)); } } }; diff --git a/src/store/plugins/WebSocketPlugin.js b/src/store/plugins/WebSocketPlugin.js new file mode 100644 index 00000000..3e2139dd --- /dev/null +++ b/src/store/plugins/WebSocketPlugin.js @@ -0,0 +1,46 @@ +/** + * WebSocketPlugin will allow us to get new data from the server + * without having to poll for changes on the frontend. + * + * This plugin is subscribed to host state property changes, which + * is indicated in the app header Power status. + * + * https://github.com/openbmc/docs/blob/b41aff0fabe137cdb0cfff584b5fe4a41c0c8e77/rest-api.md#event-subscription-protocol + */ +const WebSocketPlugin = store => { + let ws; + const data = { + paths: ['/xyz/openbmc_project/state/host0'], + interfaces: ['xyz.openbmc_project.State.Host'] + }; + + const initWebSocket = () => { + ws = new WebSocket(`wss://${window.location.host}/subscribe`); + ws.onopen = () => { + ws.send(JSON.stringify(data)); + }; + ws.onerror = event => { + console.error(event); + }; + ws.onmessage = event => { + const { + properties: { CurrentHostState, RequestedHostTransition } = {} + } = JSON.parse(event.data); + const hostState = CurrentHostState || RequestedHostTransition; + store.commit('global/setHostStatus', hostState); + }; + }; + + store.subscribe(({ type }) => { + if (type === 'authentication/authSuccess') { + initWebSocket(); + } + if (type === 'authentication/logout') { + if (ws) ws.close(); + } + }); + + if (store.getters['authentication/isLoggedIn']) initWebSocket(); +}; + +export default WebSocketPlugin; |