[Conductor] improve validation, properly set defaults

Update conductor validation logic to trigger easier-to-understand
error messages for bad configurations.

Update default-setting-behavior to properly set clock and offsets
when the default option is clock-based menu option.

Only set defaults when timeSystem has not already been configured.

fixes https://github.com/nasa/openmct/issues/1580
This commit is contained in:
Pete Richards 2017-05-16 13:07:17 -07:00
parent 344a325cb5
commit fbf736aaf7

View File

@ -22,28 +22,75 @@
define([], function () { define([], function () {
return function (config) { function validateMenuOption(menuOption, index) {
if (menuOption.clock && !menuOption.clockOffsets) {
return "clock-based menuOption at index " + index + " is " +
"missing required property 'clockOffsets'.";
}
if (!menuOption.timeSystem) {
return "menuOption at index " + index + " is missing " +
"required property 'timeSystem'.";
}
if (!menuOption.bounds && !menuOption.clock) {
return "fixed-bounds menuOption at index " + index + " is " +
"missing required property 'bounds'";
}
}
function validateConfiguration() { function validateConfiguration(config) {
if (config === undefined || config.menuOptions === undefined || config.menuOptions.length === 0) { if (config === undefined ||
return "Please provide some configuration for the time conductor. https://github.com/nasa/openmct/blob/master/API.md#the-time-conductor"; config.menuOptions === undefined ||
config.menuOptions.length === 0) {
return "You must specify one or more 'menuOptions'.";
}
if (config.menuOptions.some(validateMenuOption)) {
return config.menuOptions.map(validateMenuOption)
.filter(function (err) { return !!err; })
.join('\n');
} }
return undefined; return undefined;
} }
function validateRuntimeConfiguration(config, openmct) {
var systems = openmct.time.getAllTimeSystems()
.reduce(function (m, ts) {
m[ts.key] = ts;
return m;
}, {});
var clocks = openmct.time.getAllClocks()
.reduce(function (m, c) {
m[c.key] = c;
return m;
}, {});
return config.menuOptions.map(function (menuOption, index) {
if (menuOption.timeSystem && !systems[menuOption.timeSystem]) {
return "menuOption at index " + index + " specifies a " +
"timeSystem that does not exist: " + menuOption.timeSystem;
}
if (menuOption.clock && !clocks[menuOption.clock]) {
return "menuOption at index " + index + " specifies a " +
"clock that does not exist: " + menuOption.clock;
}
})
.filter(function (err) { return !!err; })
.join('\n');
}
function throwConfigErrorIfExists(error) {
if (error) {
throw new Error("Invalid Time Conductor Configuration: \n" +
error + '\n' +
"https://github.com/nasa/openmct/blob/master/API.md#the-time-conductor");
}
}
return function (config) {
throwConfigErrorIfExists(validateConfiguration(config));
return function (openmct) { return function (openmct) {
function getTimeSystem(key) {
return openmct.time.getAllTimeSystems().filter(function (timeSystem) {
return timeSystem.key === key;
})[0];
}
var validationError = validateConfiguration();
if (validationError) {
throw validationError;
}
openmct.legacyExtension('constants', { openmct.legacyExtension('constants', {
key: 'CONDUCTOR_CONFIG', key: 'CONDUCTOR_CONFIG',
value: config, value: config,
@ -54,39 +101,24 @@ define([], function () {
openmct.legacyRegistry.enable('platform/features/conductor/compatibility'); openmct.legacyRegistry.enable('platform/features/conductor/compatibility');
openmct.on('start', function () { openmct.on('start', function () {
throwConfigErrorIfExists(validateRuntimeConfiguration(config, openmct));
/* /*
On app startup, default the conductor On app startup, default the conductor if not already set.
*/ */
var timeSystem = openmct.time.timeSystem(); if (openmct.time.timeSystem() !== undefined) {
var clock = openmct.time.clock(); return;
if (timeSystem === undefined) {
timeSystem = getTimeSystem(config.menuOptions[0].timeSystem);
if (timeSystem === undefined) {
throw 'Please install and configure at least one time system';
}
} }
var configForTimeSystem = config.menuOptions.filter(function (menuOption) { var defaults = config.menuOptions[0];
return menuOption.timeSystem === timeSystem.key && menuOption.clock === (clock && clock.key); if (defaults.clock) {
})[0]; openmct.time.clock(defaults.clock, defaults.clockOffsets);
openmct.time.timeSystem(defaults.timeSystem, openmct.time.bounds());
if (configForTimeSystem !== undefined) {
var bounds;
if (clock === undefined) {
bounds = configForTimeSystem.bounds;
} else { } else {
var clockOffsets = configForTimeSystem.clockOffsets; openmct.time.timeSystem(defaults.timeSystem, defaults.bounds);
}
bounds = {
start: clock.currentValue() + clockOffsets.start,
end: clock.currentValue() + clockOffsets.end
};
}
openmct.time.timeSystem(timeSystem, bounds);
} else {
throw 'Invalid time conductor configuration. Please define defaults for time system "' + timeSystem.key + '"';
}
}); });
}; };
}; };