forked from nasa/openmct-tutorial
-
Notifications
You must be signed in to change notification settings - Fork 0
/
realtime-telemetry-plugin.js
34 lines (31 loc) · 1.14 KB
/
realtime-telemetry-plugin.js
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
/**
* Basic Realtime telemetry plugin using websockets.
*/
function RealtimeTelemetryPlugin() {
return function (openmct) {
var socket = new WebSocket(location.origin.replace(/^http/, 'ws') + '/realtime/');
var listener = {};
socket.onmessage = function (event) {
point = JSON.parse(event.data);
// console.log(point)
if (listener[point.id]) {
listener[point.id](point);
}
};
var provider = {
supportsSubscribe: function (domainObject) {
return domainObject.type === 'example.telemetry';
},
subscribe: function (domainObject, callback) {
listener[domainObject.identifier.key] = callback;
var sol = 'subscribe ' + domainObject.identifier.key
socket.send(sol);
return function unsubscribe() {
delete listener[domainObject.identifier.key];
socket.send('unsubscribe ' + domainObject.identifier.key);
};
}
};
openmct.telemetry.addProvider(provider);
}
}