Skip to content

Commit

Permalink
Update for brickd 1.1.0
Browse files Browse the repository at this point in the history
- Fixes battery showing N/A until the battery voltate changes.
- Gets serial number for future use
  • Loading branch information
dlech committed Oct 19, 2017
1 parent de28fb6 commit d310274
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 4 deletions.
42 changes: 38 additions & 4 deletions src/brickd.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@ import * as readline from 'readline';
import * as ssh2 from 'ssh2';
import * as Observable from 'zen-observable';

const minBrickdVersion = '1.0.0';
const minBrickdVersion = '1.1.0';
const maxBrickdVersion = '2.0.0';

enum BrickdConnectionState {
start,
handshake,
watchPower,
getBatteryVoltage,
getSerialNum,
ok,
bad
}
Expand All @@ -20,7 +22,16 @@ enum BrickdConnectionState {
* Connection to a brickd server.
*/
export class Brickd extends events.EventEmitter {
public constructor(private channel: ssh2.ClientChannel) {
private _serialNumber = '';

/**
* Gets the serial number of the main board.
*/
public get serialNumber(): string {
return this._serialNumber;
}

public constructor(private readonly channel: ssh2.ClientChannel) {
super();
const reader = readline.createInterface(channel);
const observable = new Observable<string>(observer => {
Expand Down Expand Up @@ -48,7 +59,7 @@ export class Brickd extends events.EventEmitter {
const version = m2[1];
if (compareVersions(version, minBrickdVersion) < 0) {
state = BrickdConnectionState.bad;
this.emit('error', new Error('Brickd version is too old.'));
this.emit('error', new Error(`Brickd is too old. Please upgrade to version >= ${minBrickdVersion}`));
break;
}
if (compareVersions(version, maxBrickdVersion) >= 0) {
Expand Down Expand Up @@ -76,13 +87,36 @@ export class Brickd extends events.EventEmitter {
break;
case BrickdConnectionState.watchPower:
if (m1 == "OK") {
state = BrickdConnectionState.ok;
state = BrickdConnectionState.getBatteryVoltage;
channel.write("GET system.battery.voltage\n");
}
else {
state = BrickdConnectionState.bad;
this.emit('error', new Error("Brickd failed to register for power events."));
}
break;
case BrickdConnectionState.getBatteryVoltage:
if (m1 == "OK") {
this.emit('message', `PROPERTY system.battery.voltage ${m2.join(' ')}`);
state = BrickdConnectionState.getSerialNum;
channel.write("GET system.info.serial\n");
}
else {
state = BrickdConnectionState.bad;
this.emit('error', new Error("Brickd failed to get battery voltage"));
}
break;
case BrickdConnectionState.getSerialNum:
if (m1 == "OK") {
this._serialNumber = m2.join(' ');
state = BrickdConnectionState.ok;
this.emit('ready');
}
else {
state = BrickdConnectionState.bad;
this.emit('error', new Error("Brickd failed to get serial number"));
}
break;
}
});
}
Expand Down
4 changes: 4 additions & 0 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -775,6 +775,10 @@ class DeviceStatusTreeItem extends CommandTreeItem {
this.brickd.on('error', err => {
vscode.window.showErrorMessage(`${this.device.name}: ${err.message}`);
});
this.brickd.on('ready', () => {
// serialNumber is used elsewhere, so tack it on to the device object
this.device['serialNumber'] = this.brickd.serialNumber;
});
}
catch (err) {
vscode.window.showWarningMessage('Failed to get brickd connection. No status will be available.');
Expand Down

0 comments on commit d310274

Please sign in to comment.