From 9bc39e5d6060d6bc9dcbc5a506ba0c28802d6b66 Mon Sep 17 00:00:00 2001 From: Gisle Aune Date: Wed, 30 Oct 2019 20:56:17 +0100 Subject: [PATCH] add stue fixes. --- src/drivers/iconsole.js | 36 +++++++++++++++++++++--------------- src/drivers/mock.js | 36 ++++++++++++++++++------------------ src/systems/workout.js | 6 +++--- 3 files changed, 42 insertions(+), 36 deletions(-) diff --git a/src/drivers/iconsole.js b/src/drivers/iconsole.js index aaab96c..9169023 100644 --- a/src/drivers/iconsole.js +++ b/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() { diff --git a/src/drivers/mock.js b/src/drivers/mock.js index 245df10..cfa0da1 100644 --- a/src/drivers/mock.js +++ b/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, diff --git a/src/systems/workout.js b/src/systems/workout.js index c1b109f..dfcd29e 100644 --- a/src/systems/workout.js +++ b/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); } /**