Skip to content

Commit

Permalink
Peloton Trace logging support (#49)
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremydk authored Feb 12, 2021
1 parent 67496df commit 49d33d4
Showing 1 changed file with 20 additions and 11 deletions.
31 changes: 20 additions & 11 deletions src/bikes/peloton.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const PACKET_DELIMITER = Buffer.from('f6', 'hex');
const STATS_TIMEOUT = 1.0;

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

export class PelotonBikeClient extends EventEmitter {
/**
Expand Down Expand Up @@ -40,11 +41,13 @@ export class PelotonBikeClient extends EventEmitter {
this._port = new SerialPort(this.path, {baudRate: 19200, autoOpen: false});
const open = util.promisify(this._port.open.bind(this._port));
await open();
tracelog("Serial Opened");
this._port.on('close', this.onSerialClose);
this._parser = this._port.pipe(new Delimiter({ delimiter: PACKET_DELIMITER }));
this._parser.on('data', this.onSerialMessage);

this.state = 'connected';
tracelog("Serial Connected");
}

/**
Expand All @@ -64,27 +67,33 @@ export class PelotonBikeClient extends EventEmitter {
}

onSerialMessage(data) {
tracelog("RECV: ", data);
switch(data[1]) {
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;
}
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;
default:
debuglog("Unrecognized Message Type: ", data[1]);
return;
}
}

onSerialClose() {
this.emit('disconnect', {address: this.address});
tracelog("Serial Closed");
}

onStatsTimeout() {
this.power = 0;
this.cadence = 0;
tracelog("StatsTimeout exceeded");
this.onStatsUpdate();
}
}
Expand Down

0 comments on commit 49d33d4

Please sign in to comment.