test-bed-time-service
Version:
A time service for the test-bed, producing messages with real time, fictive time and scenario duration.
57 lines • 2.85 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_paused_state_1 = require("./time-service-paused-state");
const time_service_stopped_state_1 = require("./time-service-stopped-state");
class Started extends time_service_states_1.TimeServiceBaseState {
get name() {
return node_test_bed_adapter_1.TimeState.Started;
}
transition(controlMsg) {
switch (controlMsg.command) {
case node_test_bed_adapter_1.TimeCommand.Pause: {
this.timeService.progressTrialTime(); // progress time using old speed
this.timeService.trialTimeSpeed = 0; // set speed to 0 to pause
this.log.info('Received command ' + controlMsg.command + '. Transitioning to Paused.');
return new time_service_paused_state_1.Paused(this.timeService);
}
case node_test_bed_adapter_1.TimeCommand.Stop: {
this.timeService.progressTrialTime(); // progress time using old speed
this.timeService.trialTimeSpeed = 0; // set speed to 0 to stop
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.timeService.progressTrialTime(); // progress time using old speed
this.timeService.trialTimeSpeed = controlMsg.trialTimeSpeed;
}
if (controlMsg.trialTime != null) {
this.timeService.trialTime = controlMsg.trialTime;
}
return this;
}
default: {
this.log.warn('Received command ' + controlMsg.command + ' while in Started state. Doing nothing!');
return this;
}
}
}
createTimeMessage() {
const newUpdateTime = Date.now();
const timeElapsed = newUpdateTime - this.timeService.realStartTime;
const trialTime = this.timeService.progressTrialTime();
const timeMsg = {
updatedAt: newUpdateTime,
trialTime: trialTime,
timeElapsed: timeElapsed,
trialTimeSpeed: this.timeService.trialTimeSpeed,
state: node_test_bed_adapter_1.TimeState.Started
};
return timeMsg;
}
}
exports.Started = Started;
//# sourceMappingURL=time-service-started-state.js.map