2015-05-13 23:42:35 +00:00
|
|
|
/*****************************************************************************
|
|
|
|
* Open MCT Web, Copyright (c) 2014-2015, United States Government
|
|
|
|
* as represented by the Administrator of the National Aeronautics and Space
|
|
|
|
* Administration. All rights reserved.
|
|
|
|
*
|
|
|
|
* Open MCT Web is licensed under the Apache License, Version 2.0 (the
|
|
|
|
* "License"); you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0.
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
|
|
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
|
|
* License for the specific language governing permissions and limitations
|
|
|
|
* under the License.
|
|
|
|
*
|
|
|
|
* Open MCT Web includes source code licensed under additional open source
|
|
|
|
* licenses. See the Open Source Licenses file (LICENSES.md) included with
|
|
|
|
* this source code distribution or the Licensing information page available
|
|
|
|
* at runtime from the About dialog for additional information.
|
|
|
|
*****************************************************************************/
|
2014-11-29 02:39:19 +00:00
|
|
|
/*global define*/
|
2014-11-29 00:06:54 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Module defining TelemetryCapability. Created by vwoeltje on 11/12/14.
|
|
|
|
*/
|
|
|
|
define(
|
|
|
|
[],
|
|
|
|
function () {
|
|
|
|
"use strict";
|
|
|
|
|
|
|
|
/**
|
2014-11-29 04:59:41 +00:00
|
|
|
* A telemetry capability provides a means of requesting telemetry
|
|
|
|
* for a specific object, and for unwrapping the response (to get
|
|
|
|
* at the specific data which is appropriate to the domain object.)
|
2014-11-29 00:06:54 +00:00
|
|
|
*
|
|
|
|
* @constructor
|
|
|
|
*/
|
2014-11-29 04:29:30 +00:00
|
|
|
function TelemetryCapability($injector, $q, $log, domainObject) {
|
2014-12-29 21:36:53 +00:00
|
|
|
var telemetryService,
|
|
|
|
subscriptions = [],
|
|
|
|
unsubscribeFunction;
|
2014-11-29 04:29:30 +00:00
|
|
|
|
|
|
|
// We could depend on telemetryService directly, but
|
|
|
|
// there isn't a platform implementation of this;
|
|
|
|
function getTelemetryService() {
|
2014-12-29 21:36:53 +00:00
|
|
|
if (telemetryService === undefined) {
|
2014-11-29 04:29:30 +00:00
|
|
|
try {
|
|
|
|
telemetryService =
|
2014-12-29 21:36:53 +00:00
|
|
|
$injector.get("telemetryService");
|
2014-11-29 04:29:30 +00:00
|
|
|
} catch (e) {
|
2014-12-30 21:45:42 +00:00
|
|
|
// $injector should throw if telemetryService
|
2014-11-29 04:59:41 +00:00
|
|
|
// is unavailable or unsatisfiable.
|
2014-11-29 04:29:30 +00:00
|
|
|
$log.warn("Telemetry service unavailable");
|
2014-12-29 21:36:53 +00:00
|
|
|
telemetryService = null;
|
2014-11-29 04:29:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return telemetryService;
|
|
|
|
}
|
|
|
|
|
2014-11-29 04:59:41 +00:00
|
|
|
// Build a request object. This takes the request that was
|
|
|
|
// passed to the capability, and adds source, id, and key
|
|
|
|
// fields associated with the object (from its type definition
|
|
|
|
// and/or its model)
|
2014-11-29 00:06:54 +00:00
|
|
|
function buildRequest(request) {
|
2014-11-29 04:59:41 +00:00
|
|
|
// Start with any "telemetry" field in type; use that as a
|
|
|
|
// basis for the request.
|
2014-11-29 00:06:54 +00:00
|
|
|
var type = domainObject.getCapability("type"),
|
2014-11-29 04:29:30 +00:00
|
|
|
typeRequest = (type && type.getDefinition().telemetry) || {},
|
2014-11-29 00:06:54 +00:00
|
|
|
modelTelemetry = domainObject.getModel().telemetry,
|
|
|
|
fullRequest = Object.create(typeRequest);
|
|
|
|
|
2014-11-29 04:59:41 +00:00
|
|
|
// Add properties from the telemetry field of this
|
|
|
|
// specific domain object.
|
2014-11-29 00:06:54 +00:00
|
|
|
Object.keys(modelTelemetry).forEach(function (k) {
|
|
|
|
fullRequest[k] = modelTelemetry[k];
|
|
|
|
});
|
|
|
|
|
2014-11-29 04:59:41 +00:00
|
|
|
// Add properties from this specific requestData call.
|
2014-11-29 00:06:54 +00:00
|
|
|
Object.keys(request).forEach(function (k) {
|
|
|
|
fullRequest[k] = request[k];
|
|
|
|
});
|
|
|
|
|
2014-11-29 04:59:41 +00:00
|
|
|
// Ensure an ID and key are present
|
2014-11-29 00:06:54 +00:00
|
|
|
if (!fullRequest.id) {
|
|
|
|
fullRequest.id = domainObject.getId();
|
|
|
|
}
|
|
|
|
if (!fullRequest.key) {
|
|
|
|
fullRequest.key = domainObject.getId();
|
|
|
|
}
|
|
|
|
|
|
|
|
return fullRequest;
|
|
|
|
}
|
|
|
|
|
2014-11-29 04:59:41 +00:00
|
|
|
// Issue a request for telemetry data
|
2014-11-29 00:06:54 +00:00
|
|
|
function requestTelemetry(request) {
|
|
|
|
// Bring in any defaults from the object model
|
|
|
|
var fullRequest = buildRequest(request || {}),
|
|
|
|
source = fullRequest.source,
|
|
|
|
key = fullRequest.key;
|
|
|
|
|
2014-11-29 04:59:41 +00:00
|
|
|
// Pull out the relevant field from the larger,
|
|
|
|
// structured response.
|
2014-11-29 00:06:54 +00:00
|
|
|
function getRelevantResponse(response) {
|
2014-11-29 04:29:30 +00:00
|
|
|
return ((response || {})[source] || {})[key] || {};
|
|
|
|
}
|
|
|
|
|
2014-11-29 04:59:41 +00:00
|
|
|
// Issue a request to the service
|
2014-12-29 21:36:53 +00:00
|
|
|
function requestTelemetryFromService() {
|
2014-11-29 04:29:30 +00:00
|
|
|
return telemetryService.requestTelemetry([fullRequest]);
|
2014-11-29 00:06:54 +00:00
|
|
|
}
|
|
|
|
|
2014-11-29 04:59:41 +00:00
|
|
|
// If a telemetryService is not available,
|
|
|
|
// getTelemetryService() should reject, and this should
|
|
|
|
// bubble through subsequent then calls.
|
2014-12-29 21:36:53 +00:00
|
|
|
return getTelemetryService() &&
|
|
|
|
requestTelemetryFromService()
|
|
|
|
.then(getRelevantResponse);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Listen for real-time and/or streaming updates
|
|
|
|
function subscribe(callback, request) {
|
|
|
|
var fullRequest = buildRequest(request || {});
|
|
|
|
|
|
|
|
// Unpack the relevant telemetry series
|
|
|
|
function update(telemetries) {
|
|
|
|
var source = fullRequest.source,
|
|
|
|
key = fullRequest.key,
|
|
|
|
result = ((telemetries || {})[source] || {})[key];
|
|
|
|
if (result) {
|
|
|
|
callback(result);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return getTelemetryService() &&
|
|
|
|
telemetryService.subscribe(update, [fullRequest]);
|
2014-11-29 00:06:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
2014-11-29 04:59:41 +00:00
|
|
|
/**
|
|
|
|
* Request telemetry data for this specific domain object.
|
|
|
|
* @param {TelemetryRequest} [request] parameters for this
|
|
|
|
* specific request
|
|
|
|
* @returns {Promise} a promise for the resulting telemetry
|
|
|
|
* object
|
|
|
|
*/
|
2014-11-29 00:06:54 +00:00
|
|
|
requestData: requestTelemetry,
|
2014-11-29 04:59:41 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get metadata about this domain object's associated
|
|
|
|
* telemetry.
|
|
|
|
*/
|
2014-11-29 00:06:54 +00:00
|
|
|
getMetadata: function () {
|
2014-11-29 04:59:41 +00:00
|
|
|
// metadata just looks like a request,
|
|
|
|
// so use buildRequest to bring in both
|
|
|
|
// type-level and object-level telemetry
|
|
|
|
// properties
|
2014-11-29 00:06:54 +00:00
|
|
|
return buildRequest({});
|
2014-12-29 21:36:53 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Subscribe to updates to telemetry data for this domain
|
|
|
|
* object.
|
|
|
|
* @param {Function} callback a function to call when new
|
|
|
|
* data becomes available; the telemetry series
|
|
|
|
* containing the data will be given as an argument.
|
|
|
|
* @param {TelemetryRequest} [request] parameters for the
|
|
|
|
* subscription request
|
|
|
|
*/
|
|
|
|
subscribe: subscribe
|
2014-11-29 00:06:54 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2014-11-29 04:59:41 +00:00
|
|
|
/**
|
|
|
|
* The telemetry capability is applicable when a
|
|
|
|
* domain object model has a "telemetry" field.
|
|
|
|
*/
|
2014-11-29 00:06:54 +00:00
|
|
|
TelemetryCapability.appliesTo = function (model) {
|
2014-11-29 04:29:30 +00:00
|
|
|
return (model &&
|
2014-11-29 04:59:41 +00:00
|
|
|
model.telemetry) ? true : false;
|
2014-11-29 02:39:19 +00:00
|
|
|
};
|
2014-11-29 00:06:54 +00:00
|
|
|
|
|
|
|
return TelemetryCapability;
|
|
|
|
}
|
|
|
|
);
|