mirror of
https://github.com/nasa/openmct.git
synced 2025-04-09 20:31:26 +00:00
[Telemetry] Add subscriber service
Add a subscriber service to handle subscribing to whole sets of telemetry elements via delegation; this exposes TelemetrySubscription via a service which can be retrieved via dependency injection. Supports the use of this variety of subscription for autoflow tabular views, WTD-614.
This commit is contained in:
parent
3251be2d1c
commit
8dfee64dc8
@ -28,6 +28,11 @@
|
||||
{
|
||||
"key": "telemetryFormatter",
|
||||
"implementation": "TelemetryFormatter.js"
|
||||
},
|
||||
{
|
||||
"key": "telemetrySubscriber",
|
||||
"implementation": "TelemetrySubscriber.js",
|
||||
"depends": [ "$q", "$timeout" ]
|
||||
}
|
||||
]
|
||||
}
|
||||
|
@ -1,107 +1,23 @@
|
||||
/*global define*/
|
||||
|
||||
define(
|
||||
[],
|
||||
function () {
|
||||
["./TelemetrySubscription"],
|
||||
function (TelemetrySubscription) {
|
||||
"use strict";
|
||||
|
||||
function TelemetrySubscriber($q, $timeout, domainObject, callback) {
|
||||
var unsubscribePromise,
|
||||
latestValues = {},
|
||||
telemetryObjects = [],
|
||||
updatePending;
|
||||
|
||||
// Look up domain objects which have telemetry capabilities.
|
||||
// This will either be the object in view, or object that
|
||||
// this object delegates its telemetry capability to.
|
||||
function promiseRelevantObjects(domainObject) {
|
||||
// If object has been cleared, there are no relevant
|
||||
// telemetry-providing domain objects.
|
||||
if (!domainObject) {
|
||||
return $q.when([]);
|
||||
}
|
||||
|
||||
// Otherwise, try delegation first, and attach the
|
||||
// object itself if it has a telemetry capability.
|
||||
return $q.when(domainObject.useCapability(
|
||||
"delegation",
|
||||
"telemetry"
|
||||
)).then(function (result) {
|
||||
var head = domainObject.hasCapability("telemetry") ?
|
||||
[ domainObject ] : [],
|
||||
tail = result || [];
|
||||
return head.concat(tail);
|
||||
});
|
||||
}
|
||||
|
||||
function fireCallback(values) {
|
||||
callback(values);
|
||||
updatePending = false;
|
||||
}
|
||||
|
||||
function update(domainObject, telemetry) {
|
||||
var count = telemetry && telemetry.getPointCount();
|
||||
|
||||
if (!updatePending && count) {
|
||||
updatePending = true;
|
||||
$timeout(fireCallback, 0);
|
||||
}
|
||||
|
||||
if (count > 0) {
|
||||
latestValues[domainObject.getId()] = {
|
||||
domain: telemetry.getDomainValue(count - 1),
|
||||
range: telemetry.getRangeValue(count - 1)
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
function subscribe(domainObject) {
|
||||
var telemetryCapability =
|
||||
domainObject.getCapability("telemetry");
|
||||
return telemetryCapability.subscribe(function (telemetry) {
|
||||
update(domainObject, telemetry);
|
||||
});
|
||||
}
|
||||
|
||||
function subscribeAll(domainObjects) {
|
||||
return domainObjects.map(subscribe);
|
||||
}
|
||||
|
||||
function cacheObjectReferences(objects) {
|
||||
telemetryObjects = objects;
|
||||
return objects;
|
||||
}
|
||||
|
||||
// Get a reference to relevant objects (those with telemetry
|
||||
// capabilities)
|
||||
unsubscribePromise =
|
||||
promiseRelevantObjects(domainObject)
|
||||
.then(cacheObjectReferences)
|
||||
.then(subscribeAll);
|
||||
|
||||
function TelemetrySubscriber($q, $timeout) {
|
||||
return {
|
||||
unsubscribe: function () {
|
||||
return unsubscribePromise.then(function (unsubscribes) {
|
||||
return $q.all(unsubscribes.map(function (unsubscribe) {
|
||||
return unsubscribe();
|
||||
}));
|
||||
});
|
||||
},
|
||||
getDomainValue: function (domainObject) {
|
||||
var id = domainObject.getId();
|
||||
return (latestValues[id] || {}).domain;
|
||||
},
|
||||
getRangeValue: function (domainObject) {
|
||||
var id = domainObject.getId();
|
||||
return (latestValues[id] || {}).range;
|
||||
},
|
||||
getTelemetryObjects: function () {
|
||||
return telemetryObjects;
|
||||
subscribe: function (domainObject, callback) {
|
||||
return new TelemetrySubscription(
|
||||
$q,
|
||||
$timeout,
|
||||
domainObject,
|
||||
callback
|
||||
);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
return TelemetrySubscriber;
|
||||
|
||||
}
|
||||
);
|
107
platform/telemetry/src/TelemetrySubscription.js
Normal file
107
platform/telemetry/src/TelemetrySubscription.js
Normal file
@ -0,0 +1,107 @@
|
||||
/*global define*/
|
||||
|
||||
define(
|
||||
[],
|
||||
function () {
|
||||
"use strict";
|
||||
|
||||
function TelemetrySubscription($q, $timeout, domainObject, callback) {
|
||||
var unsubscribePromise,
|
||||
latestValues = {},
|
||||
telemetryObjects = [],
|
||||
updatePending;
|
||||
|
||||
// Look up domain objects which have telemetry capabilities.
|
||||
// This will either be the object in view, or object that
|
||||
// this object delegates its telemetry capability to.
|
||||
function promiseRelevantObjects(domainObject) {
|
||||
// If object has been cleared, there are no relevant
|
||||
// telemetry-providing domain objects.
|
||||
if (!domainObject) {
|
||||
return $q.when([]);
|
||||
}
|
||||
|
||||
// Otherwise, try delegation first, and attach the
|
||||
// object itself if it has a telemetry capability.
|
||||
return $q.when(domainObject.useCapability(
|
||||
"delegation",
|
||||
"telemetry"
|
||||
)).then(function (result) {
|
||||
var head = domainObject.hasCapability("telemetry") ?
|
||||
[ domainObject ] : [],
|
||||
tail = result || [];
|
||||
return head.concat(tail);
|
||||
});
|
||||
}
|
||||
|
||||
function fireCallback(values) {
|
||||
callback(values);
|
||||
updatePending = false;
|
||||
}
|
||||
|
||||
function update(domainObject, telemetry) {
|
||||
var count = telemetry && telemetry.getPointCount();
|
||||
|
||||
if (!updatePending && count) {
|
||||
updatePending = true;
|
||||
$timeout(fireCallback, 0);
|
||||
}
|
||||
|
||||
if (count > 0) {
|
||||
latestValues[domainObject.getId()] = {
|
||||
domain: telemetry.getDomainValue(count - 1),
|
||||
range: telemetry.getRangeValue(count - 1)
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
function subscribe(domainObject) {
|
||||
var telemetryCapability =
|
||||
domainObject.getCapability("telemetry");
|
||||
return telemetryCapability.subscribe(function (telemetry) {
|
||||
update(domainObject, telemetry);
|
||||
});
|
||||
}
|
||||
|
||||
function subscribeAll(domainObjects) {
|
||||
return domainObjects.map(subscribe);
|
||||
}
|
||||
|
||||
function cacheObjectReferences(objects) {
|
||||
telemetryObjects = objects;
|
||||
return objects;
|
||||
}
|
||||
|
||||
// Get a reference to relevant objects (those with telemetry
|
||||
// capabilities)
|
||||
unsubscribePromise =
|
||||
promiseRelevantObjects(domainObject)
|
||||
.then(cacheObjectReferences)
|
||||
.then(subscribeAll);
|
||||
|
||||
return {
|
||||
unsubscribe: function () {
|
||||
return unsubscribePromise.then(function (unsubscribes) {
|
||||
return $q.all(unsubscribes.map(function (unsubscribe) {
|
||||
return unsubscribe();
|
||||
}));
|
||||
});
|
||||
},
|
||||
getDomainValue: function (domainObject) {
|
||||
var id = domainObject.getId();
|
||||
return (latestValues[id] || {}).domain;
|
||||
},
|
||||
getRangeValue: function (domainObject) {
|
||||
var id = domainObject.getId();
|
||||
return (latestValues[id] || {}).range;
|
||||
},
|
||||
getTelemetryObjects: function () {
|
||||
return telemetryObjects;
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
return TelemetrySubscription;
|
||||
|
||||
}
|
||||
);
|
Loading…
x
Reference in New Issue
Block a user