Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reset Peloton cadence/power when ride ends. #37

Merged
merged 5 commits into from
Feb 2, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions src/bikes/peloton.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import {once, EventEmitter} from 'events';
import {Timer} from '../util/timer';
import util from 'util';

const SerialPort = require('serialport')
const Delimiter = require('@serialport/parser-delimiter')

const PACKET_DELIMITER = Buffer.from('f6', 'hex');
const STATS_TIMEOUT = 1.0;

const debuglog = util.debuglog('gymnasticon:bikes:peloton');

Expand All @@ -24,6 +26,10 @@ export class PelotonBikeClient extends EventEmitter {
// initial stats
this.power = 0;
this.cadence = 0;

// reset stats to 0 when the user leaves the ride screen or turns the bike off
this.statsTimeout = new Timer(STATS_TIMEOUT, {repeats: false});
this.statsTimeout.on('timeout', this.onStatsTimeout.bind(this));
}

async connect() {
Expand Down Expand Up @@ -62,17 +68,25 @@ export class PelotonBikeClient extends EventEmitter {
case 65:
this.cadence = decodePeloton(data, data[2], false);
this.onStatsUpdate();
this.statsTimeout.reset();
return;
case 68:
this.power = decodePeloton(data, data[2], true);
this.onStatsUpdate();
this.statsTimeout.reset();
return;
}
}

onSerialClose() {
this.emit('disconnect', {address: this.address});
}

onStatsTimeout() {
this.power = 0;
this.cadence = 0;
this.onStatsUpdate();
}
}

export function decodePeloton(bufferArray, byteLength, isPower) {
Expand Down