Skip to content

Commit

Permalink
Select appropriate tick source based on mode
Browse files Browse the repository at this point in the history
  • Loading branch information
akhenry committed Aug 4, 2016
1 parent 579233a commit f96f78f
Show file tree
Hide file tree
Showing 5 changed files with 97 additions and 19 deletions.
56 changes: 56 additions & 0 deletions example/localTimeSystem/src/LADTickSource.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*****************************************************************************
* 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.
*****************************************************************************/

define(['../../../platform/features/conductor-v2/conductor/src/timeSystems/LocalClock'], function (LocalClock) {
/**
* @implements TickSource
* @constructor
*/
function LADTickSource ($timeout, period) {
LocalClock.call(this, $timeout, period);

this.metadata = {
key: 'test-lad',
cssclass: 'icon-clock',
label: 'Latest Available Data',
name: 'Latest available data',
description: 'Monitor real-time streaming data as it comes in. The Time Conductor and displays will automatically advance themselves based on a UTC clock.'
};
}
LADTickSource.prototype = Object.create(LocalClock.prototype);

LADTickSource.prototype.tick = function () {
console.log('data tick');
var now = Date.now();
this.listeners.forEach(function (listener){
listener(now);
});
this.timeoutHandle = this.$timeout(this.tick.bind(this), this.period);
};


LADTickSource.prototype.type = function () {
return 'data';
};

return LADTickSource;
});
7 changes: 4 additions & 3 deletions example/localTimeSystem/src/LocalTimeSystem.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,9 @@

define([
'../../../platform/features/conductor-v2/conductor/src/timeSystems/TimeSystem',
'../../../platform/features/conductor-v2/conductor/src/timeSystems/LocalClock'
], function (TimeSystem, LocalClock) {
'../../../platform/features/conductor-v2/conductor/src/timeSystems/LocalClock',
'./LADTickSource'
], function (TimeSystem, LocalClock, LADTickSource) {
var FIFTEEN_MINUTES = 15 * 60 * 1000,
DEFAULT_PERIOD = 1000;

Expand All @@ -47,7 +48,7 @@ define([
};

this._formats = ['local-format'];
this._tickSources = [new LocalClock($timeout, DEFAULT_PERIOD)];
this._tickSources = [new LocalClock($timeout, DEFAULT_PERIOD), new LADTickSource($timeout, DEFAULT_PERIOD)];
}

LocalTimeSystem.prototype = Object.create(TimeSystem.prototype);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ define(['./TickSource'], function (TickSource) {
};

LocalClock.prototype.tick = function () {
console.log('clock tick');
var now = Date.now();
this.listeners.forEach(function (listener){
listener(now);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ define(
cssclass: 'icon-clock',
label: 'Real-time',
name: 'Real-time Mode',
tickSourceType: 'clock',
description: 'Monitor real-time streaming data as it comes in. The Time Conductor and displays will automatically advance themselves based on a UTC clock.'
};
}
Expand All @@ -77,6 +78,7 @@ define(
cssclass: 'icon-database',
label: 'LAD',
name: 'LAD Mode',
tickSourceType: 'data',
description: 'Latest Available Data mode monitors real-time streaming data as it comes in. The Time Conductor and displays will only advance when data becomes available.'
};
}
Expand Down Expand Up @@ -184,6 +186,12 @@ define(
});
}
};

TimeConductorController.prototype.selectTickSource = function (timeSystem, sourceType) {
return timeSystem.tickSources().filter(function (source){
return source.type() === sourceType;
})[0];
}

/**
* Change the selected Time Conductor mode. This will call destroy
Expand All @@ -197,6 +205,7 @@ define(
var newMode = undefined;
var timeSystems = [];
var timeSystem = undefined;
var tickSourceType = this.modes[newModeKey].tickSourceType;

this.$scope.modeModel.selectedKey = newModeKey;

Expand All @@ -210,19 +219,23 @@ define(
timeSystem = timeSystems[0];
newMode = new FixedMode(this.conductor, timeSystem, newModeKey);
break;

case 'realtime':
// Filter time systems to only those with clock tick
// sources
timeSystems = this.timeSystemsForSourceType('clock');
timeSystems = this.timeSystemsForSourceType(tickSourceType);
timeSystem = timeSystems[0];
newMode = new FollowMode(this.conductor, timeSystem, newModeKey);
newMode.tickSource(this.selectTickSource(timeSystem, tickSourceType));
break;

case 'latest':
// Filter time systems to only those with data tick
// sources
timeSystems = this.timeSystemsForSourceType('data');
timeSystems = this.timeSystemsForSourceType(tickSourceType);
timeSystem = timeSystems[0];
newMode = new FollowMode(this.conductor, timeSystem, newModeKey);
newMode.tickSource(this.selectTickSource(timeSystem, tickSourceType));
break;
}
newMode.initialize();
Expand Down Expand Up @@ -283,6 +296,13 @@ define(
var mode = this.conductorService.mode();
mode.timeSystem(newTimeSystem);
this.setDeltasFromTimeSystem(newTimeSystem);

// If current mode supports ticking, set an appropriate tick
// source from the new time system
if (mode.tickSource) {
var tickSourceType = this.modes[mode.key()].tickSourceType;
mode.tickSource(this.selectTickSource(newTimeSystem, tickSourceType));
}
}
};

Expand Down
28 changes: 14 additions & 14 deletions platform/features/conductor-v2/conductor/src/ui/modes/FollowMode.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ define(
TimeConductorMode.call(this, conductor, timeSystem, key);

this._deltas = undefined;
this._tickSource = undefined;
this._tickSourceUnlisten = undefined;
}

FollowMode.prototype = Object.create(TimeConductorMode.prototype);
Expand All @@ -59,16 +61,20 @@ define(
};

/**
* @private
* Get or set tick source. Setting tick source will also start
* listening to it and unlisten from any existing tick source
* @param tickSource
* @returns {undefined|*}
*/
FollowMode.prototype.listenToTickSource = function () {
if (this._timeSystem) {
var tickSource = this._timeSystem.tickSources()[0];
if (tickSource) {
this.tickSourceUnlisten = tickSource.listen(this.tick.bind(this));
FollowMode.prototype.tickSource = function (tickSource) {
if (tickSource) {
if (this._tickSourceUnlisten) {
this._tickSourceUnlisten();
}
this._tickSource = tickSource;
this._tickSourceUnlisten = tickSource.listen(this.tick.bind(this));
}
return this._tickSource;
};

/**
Expand All @@ -81,10 +87,6 @@ define(
TimeConductorMode.prototype.timeSystem.apply(this, arguments);

if (timeSystem) {
if (this.tickSourceUnlisten) {
this.tickSourceUnlisten();
}

var defaults = timeSystem.defaults()[0];

if (arguments.length > 0) {
Expand All @@ -100,8 +102,6 @@ define(
}

this.conductor.timeSystem(timeSystem, bounds);

this.listenToTickSource();
}
}
return this._timeSystem;
Expand Down Expand Up @@ -139,8 +139,8 @@ define(
* Stop listening to tick sources
*/
FollowMode.prototype.destroy = function () {
if (this.tickSourceUnlisten) {
this.tickSourceUnlisten();
if (this._tickSourceUnlisten) {
this._tickSourceUnlisten();
}
};

Expand Down

0 comments on commit f96f78f

Please sign in to comment.