make things work, hopefully.

This commit is contained in:
2019-10-30 20:07:39 +01:00
parent 2932123c38
commit 8d7f7fca16
6 changed files with 109 additions and 10 deletions
+67
View File
@@ -0,0 +1,67 @@
const Client = require("iconsole-bike-client");
const _ = require("underscore");
class IConsoleDriver {
constructor(connect) {
this.connectString = connect;
this.client = null;
this.maxLevel = 24;
this.lastLevel = 18;
this.workoutState = {};
}
async connect() {
this.client = await Client.scan(this.connectString, 15000)
this.client.events.on("maxLevel", ({maxLevel}) => {
this.maxLevel = maxLevel;
this.events.emit("maxLevel", maxLevel);
});
this.client.events.on("workoutState", ({workoutState}) => {
this.workoutState = workoutState;
if (!_.isEqual(this.workoutState, workoutState)) {
this.workoutState = {...workoutState};
this.events.emit("workoutState", {...workoutState});
}
});
}
async start() {
await this.client.start({level: this.lastLevel});
}
async pause() {
await this.client.pause();
}
async resume() {
await this.client.resume();
}
async stop() {
await this.client.pause();
}
async setLevel(n) {
if (n < 0 || n > this.maxLevel) {
return Promise.reject(new Error(`Level is out of range (0 - ${this.maxLevel})`))
}
this.lastLevel = n;
return await this.client.setLevel(n);
}
destroy() {
this.stop();
this.client.disconnect();
this.events.emit("destroy");
this.events.removeAllListeners();
}
}
module.exports = IConsoleDriver;
+5 -2
View File
@@ -1,9 +1,12 @@
const MockDriver = require("./mock");
const IConsoleDriver = require("./iconsole");
module.exports = function createDriver(name) {
module.exports = function createDriver(name, connect) {
switch (name) {
case "mock":
return new MockDriver();
return new MockDriver(connect);
case "iconsole":
return new IConsoleDriver(connect);
default:
throw new Error(`Driver ${name} doesn't exist!`)
}
+6
View File
@@ -23,6 +23,10 @@ class MockDriver {
}
connect() {
setTimeout(() => {
this.events.emit("maxLevel", 32);
})
return Promise.resolve();
}
@@ -66,6 +70,8 @@ class MockDriver {
}
this.level = n;
return Promise.resolve();
}
pause() {