You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

121 lines
2.5 KiB

const EventEmitter = require("events");
const _ = require("underscore");
class MockDriver {
constructor() {
this.started = false;
this.events = new EventEmitter();
this.seconds = 0;
this.level = 1;
this.workoutStatus = {
minutes: 0,
seconds: 0,
speed: 0,
rpm: 0,
distance: 0,
calories: 0,
pulse: 0,
watt: 0,
level: 1,
};
this.state = "disconnected";
this.interval = null;
}
connect() {
setTimeout(() => {
this.events.emit("maxLevel", 32);
})
this.state = "connected";
return Promise.resolve();
}
start() {
if (this.started) {
return;
}
this.state = "started";
this.started = true;
this.interval = setInterval(() => {
this.seconds++;
this.workoutStatus.minutes = Math.floor(this.seconds / 60);
this.workoutStatus.seconds = this.seconds % 60;
this.workoutStatus.speed = Math.floor(300 + Math.random() * 200) / 10;
this.workoutStatus.rpm = Math.floor(57 + Math.random() * 5);
this.workoutStatus.distance += (this.seconds % 5 == 0) ? 0.1 : 0;
this.workoutStatus.calories += (this.seconds % 2 == 0) ? 1 : 0;
this.workoutStatus.pulse = null;
this.workoutStatus.watt = Math.floor(100 + Math.random() * 20);
this.workoutStatus.level = this.level;
this.events.emit("workoutStatus", {...this.workoutStatus});
}, 1000);
this.workoutStatus.level = 1;
this.workoutStatus.speed = 0;
this.workoutStatus.rpm = 0;
this.workoutStatus.watt = 0;
if (this.seconds === 0) {
this.events.emit("workoutStatus", {...this.workoutStatus});
}
return Promise.resolve();
}
setLevel(n) {
if (n < 0 || n > 32) {
return Promise.reject(new Error("Level is out of range (0..=32)"))
}
this.level = n;
return Promise.resolve();
}
pause() {
clearInterval(this.interval);
this.started = false;
this.state = "connected";
return Promise.resolve();
}
resume() {
return this.start();
}
stop() {
this.pause();
this.workoutStatus = {
minutes: 0,
seconds: 0,
speed: 0,
rpm: 0,
distance: 0,
calories: 0,
pulse: 0,
watt: 0,
level: 1,
};
this.seconds = 0;
this.started = false;
this.state = "disconnected";
return Promise.resolve();
}
destroy() {
this.stop();
this.events.emit("destroy");
this.events.removeAllListeners();
}
}
module.exports = MockDriver;