test-bed-time-service
Version:
A time service for the test-bed, producing messages with real time, fictive time and scenario duration.
63 lines • 3.18 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const node_test_bed_adapter_1 = require("node-test-bed-adapter");
const time_service_states_1 = require("./time-service-states");
const time_service_stopped_state_1 = require("./time-service-stopped-state");
const time_service_started_state_1 = require("./time-service-started-state");
class Paused extends time_service_states_1.TimeServiceBaseState {
get name() {
return node_test_bed_adapter_1.TimeState.Paused;
}
transition(controlMsg) {
switch (controlMsg.command) {
case node_test_bed_adapter_1.TimeCommand.Start: {
this.timeService.progressTrialTime(); // progress time using old speed (zero)
if (controlMsg.trialTimeSpeed == null) {
this.log.warn('No Trial Time Speed provided when resuming the Time Service. Defaulting to 1.0');
this.timeService.trialTimeSpeed = 1.0;
}
else {
this.timeService.trialTimeSpeed = controlMsg.trialTimeSpeed;
}
this.log.info('Received command ' + controlMsg.command + '. Transitioning to Started.');
return new time_service_started_state_1.Started(this.timeService);
}
case node_test_bed_adapter_1.TimeCommand.Stop: {
this.timeService.progressTrialTime(); // progress time using old speed (zero)
this.timeService.stopScenario(); // stop sending periodic messages
this.log.info('Received command ' + controlMsg.command + '. Transitioning to Stopped.');
return new time_service_stopped_state_1.Stopped(this.timeService);
}
case node_test_bed_adapter_1.TimeCommand.Update: {
if (controlMsg.trialTimeSpeed != null) {
this.log.info('Received command ' + controlMsg.command + ', but cannot update trial time speed when in paused state.');
}
if (controlMsg.trialTime != null) {
this.timeService.trialTime = controlMsg.trialTime;
}
return this;
}
default: {
this.log.warn('Received command ' + controlMsg.command + ' while in Paused state. Doing nothing!');
return this;
}
}
}
createTimeMessage() {
const newUpdateTime = Date.now();
const timeElapsed = newUpdateTime - this.timeService.realStartTime;
// unlike when started, don't progress the trialtime before sending an update
const trialTime = this.timeService.trialTime;
const trialTimeSpeed = this.timeService.trialTimeSpeed;
const timeMsg = {
updatedAt: newUpdateTime,
trialTime: trialTime,
timeElapsed: timeElapsed,
trialTimeSpeed: trialTimeSpeed,
state: node_test_bed_adapter_1.TimeState.Paused
};
return timeMsg;
}
}
exports.Paused = Paused;
//# sourceMappingURL=time-service-paused-state.js.map