UNPKG

node-test-bed-adapter

Version:

An adapter to connect a node.js application to the Test-bed's Common Information Space or Common Simulation Space.

77 lines 2.57 kB
import { TimeState } from 'test-bed-schemas'; /** * A service to maintain the simulation time and scenario duration. * When no simulation is running, returns the actual system time. */ export class TimeService { updatedSimTimeAt = Date.now(); /** * The date and time the simulationTime was updated as the number of milliseconds * from the unix epoch, 1 January 1970 00:00:00.000 UTC. */ timestamp = Date.now(); /** * The fictive date and time of the simulation / trial as the number of milliseconds * from the UNIX epoch, 1 January 1970 00:00:00.000 UTC. */ simTime = Date.now(); /** * The number of milliseconds from the start of the trial. */ simTimeElapsed = 0; /** * Positive number, indicating how fast the simulation / trial time moves with respect * to the actual time. A value of 0 means a pause, 1 is as fast as real-time. */ simSpeed = 1; /** The state of the Test-bed Time Service */ state = TimeState.Reset; /** * Set the simulation time. * * @param simTime Received time message */ setSimTime(tm) { if (tm.timestamp) { this.timestamp = tm.timestamp; } this.updatedSimTimeAt = Date.now(); // const latency = this.updatedSimTimeAt - this.timestamp; this.simSpeed = tm.simulationSpeed || 1; if (tm.tags && tm.tags.hasOwnProperty('timeElapsed')) { this.simTimeElapsed = +tm.tags.timeElapsed; } if (tm.state) { this.state = tm.state; } this.simTime = tm.simulationTime ? tm.simulationTime : Date.now(); } /** * Get the simulation time as Date. */ get simulationTime() { if (this.state === TimeState.Reset) { return new Date(); } const timePassedSinceLastUpdate = Date.now() - this.updatedSimTimeAt; return new Date(this.simTime + timePassedSinceLastUpdate * this.simSpeed); } /** * Get elapsed time in msec. */ get timeElapsed() { const timePassedSinceLastUpdate = Date.now() - this.updatedSimTimeAt; return this.simTimeElapsed + timePassedSinceLastUpdate; } get timeState() { return this.state; } /** * Positive number, indicating how fast the simulation / trial time moves with respect * to the actual time. A value of 0 means a pause, 1 is as fast as real-time. */ get simulationSpeed() { return this.simSpeed; } } //# sourceMappingURL=time-service.mjs.map