Browse Source

add stuff

master
Gisle Aune 5 years ago
parent
commit
a8e7b5b1fa
  1. 105
      src/api/workout.js
  2. 12
      src/drivers/iconsole.js
  3. 12
      src/drivers/mock.js
  4. 46
      src/systems/workout.js

105
src/api/workout.js

@ -10,6 +10,20 @@ module.exports = function workoutRouter(repo) {
/** @type {Workout[]} */ /** @type {Workout[]} */
let workouts = []; let workouts = [];
function activeData(id) {
const workout = workouts.find(w2 => w2.id === id);
if (workout == null) {
return {
active: false,
};
}
return {
active: true,
state: workout.state,
}
}
router.get("/", async(req, res) => { router.get("/", async(req, res) => {
if (req.query.active === "true") { if (req.query.active === "true") {
return res.status(200).json(workouts.map(w => ({ return res.status(200).json(workouts.map(w => ({
@ -17,7 +31,7 @@ module.exports = function workoutRouter(repo) {
bike: w.bike, bike: w.bike,
program: w.program, program: w.program,
date: w.date, date: w.date,
active: true,
...activeData(w.id),
}))); })));
} }
@ -25,7 +39,7 @@ module.exports = function workoutRouter(repo) {
const dbWorkouts = await repo.listWorkouts(); const dbWorkouts = await repo.listWorkouts();
return res.status(200).json(dbWorkouts.map(w => ({ return res.status(200).json(dbWorkouts.map(w => ({
...w, ...w,
active: workouts.find(w2 => w2.id === w.id) != null,
...activeData(w.id),
}))); })));
} catch (err) { } catch (err) {
return res.status(400).json({code: 400, message: err.message || err}) return res.status(400).json({code: 400, message: err.message || err})
@ -35,7 +49,13 @@ module.exports = function workoutRouter(repo) {
router.get("/:id", async(req, res) => { router.get("/:id", async(req, res) => {
let workout = workouts.find(w => w.id == req.params.id); let workout = workouts.find(w => w.id == req.params.id);
if (workout != null) { if (workout != null) {
return res.status(200).json({id: workout.id, bike: workout.bike, program: workout.program, date: workout.date, active: true})
return res.status(200).json({
id: workout.id,
bike: workout.bike,
program: workout.program,
date: workout.date,
...activeData(w.id),
});
} }
try { try {
@ -70,7 +90,13 @@ module.exports = function workoutRouter(repo) {
const workout = await Workout.create(repo, bikeId, programId); const workout = await Workout.create(repo, bikeId, programId);
workouts.push(workout); workouts.push(workout);
return res.status(200).json({id: workout.id, bike: workout.bike, program: workout.program, date: workout.date});
return res.status(200).json({
id: workout.id,
bike: workout.bike,
program: workout.program,
date: workout.date,
...activeData(workout.id),
});
} catch (err) { } catch (err) {
return res.status(400).json({code: 400, message: err.message || err}); return res.status(400).json({code: 400, message: err.message || err});
} }
@ -86,7 +112,13 @@ module.exports = function workoutRouter(repo) {
workout = await Workout.continue(repo, req.params.id); workout = await Workout.continue(repo, req.params.id);
workouts.push(workout); workouts.push(workout);
return res.status(200).json({id: workout.id, bike: workout.bike, program: workout.program, date: workout.date});
return res.status(200).json({
id: workout.id,
bike: workout.bike,
program: workout.program,
date: workout.date,
...activeData(w.id),
});
} catch (err) { } catch (err) {
return res.status(400).json({code: 400, message: err.message || err}) return res.status(400).json({code: 400, message: err.message || err})
} }
@ -100,7 +132,13 @@ module.exports = function workoutRouter(repo) {
try { try {
await workout.connect(); await workout.connect();
return res.status(200).json({id: workout.id, bike: workout.bike, program: workout.program, date: workout.date});
return res.status(200).json({
id: workout.id,
bike: workout.bike,
program: workout.program,
date: workout.date,
...activeData(workout.id),
});
} catch(err) { } catch(err) {
return res.status(500).json({code: 500, message: err.message || err}); return res.status(500).json({code: 500, message: err.message || err});
} }
@ -114,7 +152,13 @@ module.exports = function workoutRouter(repo) {
try { try {
await workout.start(); await workout.start();
return res.status(200).json({id: workout.id, bike: workout.bike, program: workout.program, date: workout.date});
return res.status(200).json({
id: workout.id,
bike: workout.bike,
program: workout.program,
date: workout.date,
...activeData(workout.id),
});
} catch(err) { } catch(err) {
return res.status(500).json({code: 500, message: err.message || err}); return res.status(500).json({code: 500, message: err.message || err});
} }
@ -128,7 +172,13 @@ module.exports = function workoutRouter(repo) {
try { try {
await workout.pause(); await workout.pause();
return res.status(200).json({id: workout.id, bike: workout.bike, program: workout.program, date: workout.date});
return res.status(200).json({
id: workout.id,
bike: workout.bike,
program: workout.program,
date: workout.date,
...activeData(workout.id),
});
} catch(err) { } catch(err) {
return res.status(500).json({code: 500, message: err.message || err}); return res.status(500).json({code: 500, message: err.message || err});
} }
@ -148,7 +198,13 @@ module.exports = function workoutRouter(repo) {
workouts = workouts.filter(w => w !== workout); workouts = workouts.filter(w => w !== workout);
await workout.destroy(); await workout.destroy();
return res.status(200).json({id: workout.id, bike: workout.bike, program: workout.program, date: workout.date});
return res.status(200).json({
id: workout.id,
bike: workout.bike,
program: workout.program,
date: workout.date,
...activeData(workout.id),
});
} catch(err) { } catch(err) {
return res.status(500).json({code: 500, message: err.message || err}); return res.status(500).json({code: 500, message: err.message || err});
} }
@ -168,15 +224,19 @@ module.exports = function workoutRouter(repo) {
await repo.deleteWorkout(workout); await repo.deleteWorkout(workout);
return res.status(200).json(workout);
return res.status(200).json({
id: workout.id,
bike: workout.bike,
program: workout.program,
date: workout.date,
...activeData(workout.id),
});
} catch (err) { } catch (err) {
return res.status(500).json({code: 500, message: err.message || err}) return res.status(500).json({code: 500, message: err.message || err})
} }
}); });
router.ws("/:id/subscribe", (ws, req) => { router.ws("/:id/subscribe", (ws, req) => {
console.log("HELLO!")
const workout = workouts.find(w => w.id === req.params.id); const workout = workouts.find(w => w.id === req.params.id);
if (workout == null) { if (workout == null) {
ws.send(JSON.stringify({error: "workout not found"})); ws.send(JSON.stringify({error: "workout not found"}));
@ -184,6 +244,8 @@ module.exports = function workoutRouter(repo) {
} }
workout.listMeasurements().then((list) => { workout.listMeasurements().then((list) => {
list = list.map(v => {delete v.id; delete v.workoutId; return v});
for (let i = 0; i < list.length; i += 240) { for (let i = 0; i < list.length; i += 240) {
ws.send(JSON.stringify({workoutStatusBackfill: list.slice(i, i+240)})); ws.send(JSON.stringify({workoutStatusBackfill: list.slice(i, i+240)}));
} }
@ -192,21 +254,28 @@ module.exports = function workoutRouter(repo) {
ws.close(); ws.close();
}); });
const handler = (workotuStatus) => {
workotuStatus = {...workotuStatus};
delete workotuStatus.id;
const onWorkoutStatus = (workoutStatus) => {
workoutStatus = {...workoutStatus};
delete workoutStatus.id;
ws.send(JSON.stringify({workotuStatus}))
ws.send(JSON.stringify({workoutStatus}))
}
workout.events.on("workoutStatus", onWorkoutStatus);
const onState = (state) => {
ws.send(JSON.stringify({state}))
} }
workout.events.on("state", onState);
workout.events.on("workoutStatus", handler);
ws.onclose = () => { ws.onclose = () => {
workout.events.removeListener("workoutStatus", handler);
workout.events.removeListener("workoutStatus", onWorkoutStatus);
workout.events.removeListener("state", onState);
ws.removeAllListeners(); ws.removeAllListeners();
}; };
ws.send(JSON.stringify({ ws.send(JSON.stringify({
state: workout.state,
workout: { workout: {
id: workout.id, id: workout.id,
bike: workout.bike, bike: workout.bike,

12
src/drivers/iconsole.js

@ -12,6 +12,7 @@ class IConsoleDriver {
this.maxLevel = 24; this.maxLevel = 24;
this.lastLevel = 18; this.lastLevel = 18;
this.workoutStatus = {}; this.workoutStatus = {};
this.started = false;
} }
async connect() { async connect() {
@ -36,8 +37,17 @@ class IConsoleDriver {
}) })
} }
get state() {
return this.client.state;
}
async start() { async start() {
await this.client.start({level: this.lastLevel});
if (!this.started) {
await this.client.start({level: this.lastLevel});
this.started = true;
} else {
await this.client.resume();
}
} }
async pause() { async pause() {

12
src/drivers/mock.js

@ -18,6 +18,7 @@ class MockDriver {
watt: 0, watt: 0,
level: 1, level: 1,
}; };
this.state = "disconnected";
this.interval = null; this.interval = null;
} }
@ -27,6 +28,8 @@ class MockDriver {
this.events.emit("maxLevel", 32); this.events.emit("maxLevel", 32);
}) })
this.state = "connected";
return Promise.resolve(); return Promise.resolve();
} }
@ -34,6 +37,7 @@ class MockDriver {
if (this.started) { if (this.started) {
return; return;
} }
this.state = "started";
this.started = true; this.started = true;
this.interval = setInterval(() => { this.interval = setInterval(() => {
@ -77,10 +81,15 @@ class MockDriver {
pause() { pause() {
clearInterval(this.interval); clearInterval(this.interval);
this.started = false; this.started = false;
this.state = "connected";
return Promise.resolve(); return Promise.resolve();
} }
resume() {
return this.start();
}
stop() { stop() {
this.pause(); this.pause();
this.workoutStatus = { this.workoutStatus = {
@ -95,6 +104,9 @@ class MockDriver {
level: 1, level: 1,
}; };
this.seconds = 0; this.seconds = 0;
this.started = false;
this.state = "disconnected";
return Promise.resolve(); return Promise.resolve();
} }

46
src/systems/workout.js

@ -29,29 +29,65 @@ class Workout {
this.date = date; this.date = date;
this.driver = null; this.driver = null;
this.state = "disconnected";
this.offsetSeconds = 0; this.offsetSeconds = 0;
this.offsets = {}; this.offsets = {};
this.events = new EventEmitter(); this.events = new EventEmitter();
this.events.emit("state", this.state);
} }
async connect() { async connect() {
if (this.state !== "disconnected") {
return Promise.resolve();
}
this.driver = createDriver(this.bike.driver, this.bike.connect); this.driver = createDriver(this.bike.driver, this.bike.connect);
this.driver.events.on("workoutStatus", ws => this.handleworkoutStatus(ws)); this.driver.events.on("workoutStatus", ws => this.handleworkoutStatus(ws));
return this.driver.connect();
return this.driver.connect().then((v) => {
this.state = this.driver.state;
this.events.emit("state", this.state);
return v;
});
} }
start() { start() {
return this.driver.start();
if (this.state === "started") {
return Promise.resolve();
}
return this.driver.start().then((v) => {
this.state = this.driver.state;
this.events.emit("state", this.state);
return v;
});
} }
pause() { pause() {
return this.driver.pause();
if (this.state !== "started") {
return Promise.resolve();
}
return this.driver.pause().then((v) => {
this.state = this.driver.state;
this.events.emit("state", this.state);
return v;
});
} }
stop() { stop() {
return this.driver.stop();
if (this.state === "started") {
return Promise.resolve();
}
return this.driver.stop().then((v) => {
this.state = this.driver.state;
this.events.emit("state", this.state);
return v;
});
} }
destroy() { destroy() {
@ -60,6 +96,8 @@ class Workout {
if (this.driver != null) { if (this.driver != null) {
this.driver.destroy(); this.driver.destroy();
this.state = this.driver.state;
this.events.emit("state", this.state);
this.driver = null; this.driver = null;
} }
} }

Loading…
Cancel
Save