Gisle Aune
5 years ago
6 changed files with 109 additions and 10 deletions
-
67src/drivers/iconsole.js
-
7src/drivers/index.js
-
6src/drivers/mock.js
-
29src/repositories/sqlite3.js
-
8src/server.js
-
2src/systems/workout.js
@ -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; |
@ -1,9 +1,12 @@ |
|||||
const MockDriver = require("./mock"); |
const MockDriver = require("./mock"); |
||||
|
const IConsoleDriver = require("./iconsole"); |
||||
|
|
||||
module.exports = function createDriver(name) { |
|
||||
|
module.exports = function createDriver(name, connect) { |
||||
switch (name) { |
switch (name) { |
||||
case "mock": |
case "mock": |
||||
return new MockDriver(); |
|
||||
|
return new MockDriver(connect); |
||||
|
case "iconsole": |
||||
|
return new IConsoleDriver(connect); |
||||
default: |
default: |
||||
throw new Error(`Driver ${name} doesn't exist!`) |
throw new Error(`Driver ${name} doesn't exist!`) |
||||
} |
} |
Write
Preview
Loading…
Cancel
Save
Reference in new issue