Yoshie Muranaka | ce9a3ef | 2020-05-06 14:33:22 -0700 | [diff] [blame] | 1 | import { debounce } from 'lodash'; |
| 2 | |
Yoshie Muranaka | dc04feb | 2019-12-04 08:41:22 -0800 | [diff] [blame] | 3 | /** |
| 4 | * WebSocketPlugin will allow us to get new data from the server |
| 5 | * without having to poll for changes on the frontend. |
| 6 | * |
Yoshie Muranaka | 1ace1d9 | 2020-02-06 13:47:28 -0800 | [diff] [blame] | 7 | * This plugin is subscribed to host state property and logging |
| 8 | * changes, indicated in the app header Health and Power status. |
Yoshie Muranaka | dc04feb | 2019-12-04 08:41:22 -0800 | [diff] [blame] | 9 | * |
| 10 | * https://github.com/openbmc/docs/blob/b41aff0fabe137cdb0cfff584b5fe4a41c0c8e77/rest-api.md#event-subscription-protocol |
| 11 | */ |
| 12 | const WebSocketPlugin = store => { |
| 13 | let ws; |
| 14 | const data = { |
Yoshie Muranaka | 1ace1d9 | 2020-02-06 13:47:28 -0800 | [diff] [blame] | 15 | paths: ['/xyz/openbmc_project/state/host0', '/xyz/openbmc_project/logging'], |
| 16 | interfaces: [ |
| 17 | 'xyz.openbmc_project.State.Host', |
| 18 | 'xyz.openbmc_project.Logging.Entry' |
| 19 | ] |
Yoshie Muranaka | dc04feb | 2019-12-04 08:41:22 -0800 | [diff] [blame] | 20 | }; |
| 21 | |
| 22 | const initWebSocket = () => { |
Mateusz Gapski | 33a8c53 | 2020-07-24 08:52:53 +0200 | [diff] [blame] | 23 | const socketDisabled = |
| 24 | process.env.VUE_APP_SUBSCRIBE_SOCKET_DISABLED === 'true' ? true : false; |
| 25 | if (socketDisabled) return; |
Yoshie Muranaka | 23f227d | 2020-05-01 14:19:43 -0700 | [diff] [blame] | 26 | const token = store.getters['authentication/token']; |
| 27 | ws = new WebSocket(`wss://${window.location.host}/subscribe`, [token]); |
Yoshie Muranaka | dc04feb | 2019-12-04 08:41:22 -0800 | [diff] [blame] | 28 | ws.onopen = () => { |
| 29 | ws.send(JSON.stringify(data)); |
| 30 | }; |
| 31 | ws.onerror = event => { |
| 32 | console.error(event); |
| 33 | }; |
Yoshie Muranaka | ce9a3ef | 2020-05-06 14:33:22 -0700 | [diff] [blame] | 34 | ws.onmessage = debounce(event => { |
Yoshie Muranaka | 1ace1d9 | 2020-02-06 13:47:28 -0800 | [diff] [blame] | 35 | const data = JSON.parse(event.data); |
| 36 | const eventInterface = data.interface; |
Yoshie Muranaka | ce9a3ef | 2020-05-06 14:33:22 -0700 | [diff] [blame] | 37 | const path = data.path; |
Yoshie Muranaka | 1ace1d9 | 2020-02-06 13:47:28 -0800 | [diff] [blame] | 38 | |
| 39 | if (eventInterface === 'xyz.openbmc_project.State.Host') { |
| 40 | const { properties: { CurrentHostState } = {} } = data; |
| 41 | store.commit('global/setHostStatus', CurrentHostState); |
Yoshie Muranaka | ce9a3ef | 2020-05-06 14:33:22 -0700 | [diff] [blame] | 42 | } else if (path === '/xyz/openbmc_project/logging') { |
| 43 | store.dispatch('eventLog/getEventLogData'); |
Yoshie Muranaka | 1ace1d9 | 2020-02-06 13:47:28 -0800 | [diff] [blame] | 44 | } |
Yoshie Muranaka | ce9a3ef | 2020-05-06 14:33:22 -0700 | [diff] [blame] | 45 | // 2.5 sec debounce to avoid making multiple consecutive |
| 46 | // GET requests since log related server messages seem to |
| 47 | // come in clusters |
| 48 | }, 2500); |
Yoshie Muranaka | dc04feb | 2019-12-04 08:41:22 -0800 | [diff] [blame] | 49 | }; |
| 50 | |
| 51 | store.subscribe(({ type }) => { |
| 52 | if (type === 'authentication/authSuccess') { |
| 53 | initWebSocket(); |
| 54 | } |
| 55 | if (type === 'authentication/logout') { |
| 56 | if (ws) ws.close(); |
| 57 | } |
| 58 | }); |
| 59 | |
| 60 | if (store.getters['authentication/isLoggedIn']) initWebSocket(); |
| 61 | }; |
| 62 | |
| 63 | export default WebSocketPlugin; |