first commit
This commit is contained in:
@@ -0,0 +1,10 @@
|
||||
const MockDriver = require("./mock");
|
||||
|
||||
module.exports = function createDriver(name) {
|
||||
switch (name) {
|
||||
case "mock":
|
||||
return new MockDriver();
|
||||
default:
|
||||
throw new Error(`Driver ${name} doesn't exist!`)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,103 @@
|
||||
const EventEmitter = require("events");
|
||||
const _ = require("underscore");
|
||||
|
||||
class MockDriver {
|
||||
constructor() {
|
||||
this.started = false;
|
||||
this.events = new EventEmitter();
|
||||
this.seconds = 0;
|
||||
this.level = 1;
|
||||
this.workoutState = {
|
||||
minutes: 0,
|
||||
seconds: 0,
|
||||
speed: 0,
|
||||
rpm: 0,
|
||||
distance: 0,
|
||||
calories: 0,
|
||||
pulse: 0,
|
||||
watt: 0,
|
||||
level: 1,
|
||||
};
|
||||
|
||||
this.interval = null;
|
||||
}
|
||||
|
||||
connect() {
|
||||
return Promise.resolve();
|
||||
}
|
||||
|
||||
start() {
|
||||
if (this.started) {
|
||||
return;
|
||||
}
|
||||
|
||||
this.started = true;
|
||||
this.interval = setInterval(() => {
|
||||
this.seconds++;
|
||||
|
||||
this.workoutState.minutes = Math.floor(this.seconds / 60);
|
||||
this.workoutState.seconds = this.seconds % 60;
|
||||
this.workoutState.speed = Math.floor(300 + Math.random() * 200) / 10;
|
||||
this.workoutState.rpm = Math.floor(57 + Math.random() * 5);
|
||||
this.workoutState.distance += (this.seconds % 5 == 0) ? 0.1 : 0;
|
||||
this.workoutState.calories += (this.seconds % 2 == 0) ? 1 : 0;
|
||||
this.workoutState.pulse = null;
|
||||
this.workoutState.watt = Math.floor(100 + Math.random() * 20);
|
||||
this.workoutState.level = this.level;
|
||||
|
||||
this.events.emit("workoutState", {...this.workoutState});
|
||||
}, 1000);
|
||||
|
||||
this.workoutState.level = 1;
|
||||
this.workoutState.speed = 0;
|
||||
this.workoutState.rpm = 0;
|
||||
this.workoutState.watt = 0;
|
||||
|
||||
if (this.seconds === 0) {
|
||||
this.events.emit("workoutState", {...this.workoutState});
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
pause() {
|
||||
clearInterval(this.interval);
|
||||
this.started = false;
|
||||
|
||||
return Promise.resolve();
|
||||
}
|
||||
|
||||
stop() {
|
||||
this.pause();
|
||||
this.workoutState = {
|
||||
minutes: 0,
|
||||
seconds: 0,
|
||||
speed: 0,
|
||||
rpm: 0,
|
||||
distance: 0,
|
||||
calories: 0,
|
||||
pulse: 0,
|
||||
watt: 0,
|
||||
level: 1,
|
||||
};
|
||||
this.seconds = 0;
|
||||
|
||||
return Promise.resolve();
|
||||
}
|
||||
|
||||
destroy() {
|
||||
this.stop();
|
||||
this.events.emit("destroy");
|
||||
this.events.removeAllListeners();
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = MockDriver;
|
||||
Reference in New Issue
Block a user