blob: 8f1acb0c2bfbc35e915530c8b4fe1e5dc1118c4b (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
|
/**
* 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 and logging
* changes, indicated in the app header Health and 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',
'/xyz/openbmc_project/logging',
'/xyz/openbmc_project/time/bmc',
],
interfaces: [
'xyz.openbmc_project.State.Host',
'xyz.openbmc_project.Logging.Entry',
'xyz.openbmc_project.Time.EpochTime',
],
};
const initWebSocket = () => {
const socketDisabled =
process.env.VUE_APP_SUBSCRIBE_SOCKET_DISABLED === 'true' ? true : false;
if (socketDisabled) return;
const token = store.getters['authentication/token'];
ws = new WebSocket(`wss://${window.location.host}/subscribe`, [token]);
ws.onopen = () => {
ws.send(JSON.stringify(data));
};
ws.onerror = (event) => {
console.error(event);
};
ws.onmessage = (event) => {
const data = JSON.parse(event.data);
const eventInterface = data.interface;
const path = data.path;
if (eventInterface === 'xyz.openbmc_project.State.Host') {
const { properties: { CurrentHostState } = {} } = data;
if (CurrentHostState) {
store.commit('global/setServerStatus', CurrentHostState);
}
} else if (eventInterface === 'xyz.openbmc_project.Time.EpochTime') {
const { properties: { Elapsed } = {} } = data;
if (Elapsed) {
let millis = (+Elapsed - (+Elapsed % 1000)) / 1000;
let date = new Date(millis);
store.commit('global/setBmcTime', date);
}
} else if (path === '/xyz/openbmc_project/logging') {
store.dispatch('eventLog/getEventLogData');
}
};
};
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;
|