Browse Source

add stue fixes.

master
Gisle Aune 5 years ago
parent
commit
9bc39e5d60
  1. 36
      src/drivers/iconsole.js
  2. 36
      src/drivers/mock.js
  3. 6
      src/systems/workout.js

36
src/drivers/iconsole.js

@ -1,33 +1,39 @@
const Client = require("iconsole-bike-client");
const _ = require("underscore");
const EventEmitter = require("events");
class IConsoleDriver {
constructor(connect) {
this.connectString = connect;
this.client = null;
this.events = new EventEmitter();
this.maxLevel = 24;
this.lastLevel = 18;
this.workoutState = {};
this.workoutStatus = {};
}
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 = await Client.scan(this.connectString.toLowerCase(), 15000)
await this.client.connect();
this.client.events.on("workoutState", ({workoutState}) => {
this.workoutState = workoutState;
this.client.events.on("data", ({kind, workoutStatus, maxLevel}) => {
switch (kind) {
case "workoutStatus": {
if (!_.isEqual(this.workoutStatus, workoutStatus)) {
this.workoutStatus = {...workoutStatus};
this.events.emit("workoutStatus", {...workoutStatus});
}
break;
}
if (!_.isEqual(this.workoutState, workoutState)) {
this.workoutState = {...workoutState};
this.events.emit("workoutState", {...workoutState});
case "maxLevel": {
this.events.emit("maxLevel", maxLevel);
break;
}
}
});
})
}
async start() {

36
src/drivers/mock.js

@ -7,7 +7,7 @@ class MockDriver {
this.events = new EventEmitter();
this.seconds = 0;
this.level = 1;
this.workoutState = {
this.workoutStatus = {
minutes: 0,
seconds: 0,
speed: 0,
@ -39,26 +39,26 @@ class MockDriver {
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});
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.workoutState.level = 1;
this.workoutState.speed = 0;
this.workoutState.rpm = 0;
this.workoutState.watt = 0;
this.workoutStatus.level = 1;
this.workoutStatus.speed = 0;
this.workoutStatus.rpm = 0;
this.workoutStatus.watt = 0;
if (this.seconds === 0) {
this.events.emit("workoutState", {...this.workoutState});
this.events.emit("workoutStatus", {...this.workoutStatus});
}
return Promise.resolve();
@ -83,7 +83,7 @@ class MockDriver {
stop() {
this.pause();
this.workoutState = {
this.workoutStatus = {
minutes: 0,
seconds: 0,
speed: 0,

6
src/systems/workout.js

@ -37,7 +37,7 @@ class Workout {
async connect() {
this.driver = createDriver(this.bike.driver, this.bike.connect);
this.driver.events.on("workoutState", ws => this.handleWorkoutState(ws));
this.driver.events.on("workoutStatus", ws => this.handleworkoutStatus(ws));
return this.driver.connect();
}
@ -68,7 +68,7 @@ class Workout {
return this.repo.listMeasurements(this.id);
}
handleWorkoutState(ws) {
handleworkoutStatus(ws) {
if (this.offsetSeconds > 0) {
const seconds = (ws.minutes * 60) + ws.seconds + this.offsetSeconds;
ws.minutes = Math.floor(seconds / 60);
@ -85,7 +85,7 @@ class Workout {
this.repo.insertMeasurement({...ws, workoutId: this.id});
this.events.emit("workoutState", ws);
this.events.emit("workoutStatus", ws);
}
/**

Loading…
Cancel
Save