forked from nasa/openmct-tutorial
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrealtime-telemetry-plugin.js
47 lines (43 loc) · 1.71 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
35
36
37
38
39
40
41
42
43
44
45
46
47
/**
* Basic Realtime telemetry plugin using websockets.
*/
function RealtimeTelemetryPlugin() {
return function (openmct) {
//var socket = new WebSocket('ws://localhost:8082');
var dynamic_url = 'ws://' + window.location.hostname + ':8082';
var socket = new WebSocket(dynamic_url);
var listeners = {};
socket.onmessage = function (event) {
point = JSON.parse(event.data);
if (listeners[point.id]) {
listeners[point.id].forEach(function (l) {
l(point);
});
}
};
var provider = {
supportsSubscribe: function (domainObject) {
return domainObject.type === 'example.telemetry';
},
subscribe: function (domainObject, callback, options) {
if (!listeners[domainObject.identifier.key]) {
listeners[domainObject.identifier.key] = [];
}
if (!listeners[domainObject.identifier.key].length) {
socket.send('subscribe ' + domainObject.identifier.key);
}
listeners[domainObject.identifier.key].push(callback);
return function () {
listeners[domainObject.identifier.key] =
listeners[domainObject.identifier.key].filter(function (c) {
return c !== callback;
});
if (!listeners[domainObject.identifier.key].length) {
socket.send('unsubscribe ' + domainObject.identifier.key);
}
};
}
};
openmct.telemetry.addProvider(provider);
}
}