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.
67 lines
1.4 KiB
67 lines
1.4 KiB
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;
|