BLEDevice partially working for noble devices

This commit is contained in:
Nathan Kellenicki 2019-02-05 16:15:09 -08:00
parent 0a3018af9c
commit 79542acef1
12 changed files with 262 additions and 138 deletions

6
package-lock.json generated
View File

@ -25,12 +25,6 @@
"integrity": "sha512-fh+pAqt4xRzPfqA6eh3Z2y6fyZavRIumvjhaCL753+TVkGKGhpPeyrJG2JftD0T9q4GF00KjefsQ+PQNDdWQaQ==", "integrity": "sha512-fh+pAqt4xRzPfqA6eh3Z2y6fyZavRIumvjhaCL753+TVkGKGhpPeyrJG2JftD0T9q4GF00KjefsQ+PQNDdWQaQ==",
"dev": true "dev": true
}, },
"@types/web-bluetooth": {
"version": "0.0.4",
"resolved": "https://registry.npmjs.org/@types/web-bluetooth/-/web-bluetooth-0.0.4.tgz",
"integrity": "sha512-C+BgVBBGY9c6ixcc5PsKAmGaCy3bswZ5zx/AWIAik9dgFuBkFsXBA3ze69jJi05xdZQ99QkfBSVIX6zl+6Tmww==",
"dev": true
},
"abbrev": { "abbrev": {
"version": "1.1.1", "version": "1.1.1",
"resolved": "https://registry.npmjs.org/abbrev/-/abbrev-1.1.1.tgz", "resolved": "https://registry.npmjs.org/abbrev/-/abbrev-1.1.1.tgz",

View File

@ -22,7 +22,6 @@
"@types/debug": "0.0.31", "@types/debug": "0.0.31",
"@types/noble": "0.0.38", "@types/noble": "0.0.38",
"@types/node": "^10.12.18", "@types/node": "^10.12.18",
"@types/web-bluetooth": "0.0.4",
"ink-docstrap": "^1.3.2", "ink-docstrap": "^1.3.2",
"jsdoc": "^3.5.5", "jsdoc": "^3.5.5",
"jsdoc-to-markdown": "^4.0.1", "jsdoc-to-markdown": "^4.0.1",

View File

@ -1,31 +1,151 @@
import { Peripheral } from "noble"; import { Characteristic, Peripheral, Service } from "noble";
import Debug = require("debug"); import Debug = require("debug");
import { EventEmitter } from "events";
const debug = Debug("bledevice"); const debug = Debug("bledevice");
export class BLEDevice { export class BLEDevice extends EventEmitter {
// @ts-ignore
// @ts-ignore
private _noblePeripheral: Peripheral | null; private _noblePeripheral: Peripheral | null;
private _webBLEServer: any; private _webBLEServer: any;
private _uuid: string;
private _name: string = "";
constructor (device: Peripheral | BluetoothRemoteGATTServer) { private _characteristics: {[uuid: string]: Characteristic} = {};
if (device instanceof Peripheral) {
constructor (device: any) {
super();
if (device._noble) {
this._noblePeripheral = device; this._noblePeripheral = device;
this._uuid = device.uuid;
// NK: This hack allows LPF2.0 hubs to send a second advertisement packet consisting of the hub name before we try to read it
setTimeout(() => {
this._name = device.advertisement.localName;
this.emit("discoverComplete");
}, 1000);
} else { } else {
this._webBLEServer = device; this._webBLEServer = device;
this._uuid = device.uuid;
} }
} }
public discoverService (uuid: string) { public get uuid () {
if (this._peripheral instanceof BluetoothRemoteGATTServer) { return this._uuid;
}
public get name () {
return this._name;
}
public connect () {
return new Promise((resolve, reject) => {
if (this._noblePeripheral) {
this._noblePeripheral.connect((err: string) => {
return resolve();
});
} else {
return resolve();
}
});
}
public disconnect () {
return new Promise((resolve, reject) => {
if (this._noblePeripheral) {
this._noblePeripheral.connect((err: string) => {
return resolve();
});
} else {
return resolve();
}
});
}
public discoverCharacteristicsForService (uuid: string) {
uuid = this._sanitizeUUID(uuid);
return new Promise(async (discoverResolve, discoverReject) => {
if (this._noblePeripheral) {
this._noblePeripheral.discoverServices([uuid], (err: string, services: Service[]) => {
if (err) {
return discoverReject(err);
}
debug("Service/characteristic discovery started");
const servicePromises: Array<Promise<null>> = [];
services.forEach((service) => {
servicePromises.push(new Promise((resolve, reject) => {
service.discoverCharacteristics([], (err, characteristics) => {
characteristics.forEach((characteristic) => {
this._characteristics[characteristic.uuid] = characteristic;
});
return resolve();
});
}));
});
Promise.all(servicePromises).then(() => {
debug("Service/characteristic discovery finished");
return discoverResolve();
});
});
} else if (this._webBLEServer) {
debug("Service/characteristic discovery started");
let service;
try {
service = await this._webBLEServer.getPrimaryService(uuid);
} catch (err) {
return discoverReject(err);
}
const characteristics = await service.getCharacteristics();
for (const characteristic of characteristics) {
this._characteristics[characteristic.uuid] = characteristic;
}
debug("Service/characteristic discovery finished");
return discoverResolve();
}
});
}
public subscribeToCharacteristic (uuid: string, callback: (data: Buffer) => void) {
uuid = this._sanitizeUUID(uuid);
if (this._noblePeripheral) {
this._characteristics[uuid].on("data", (data: Buffer) => {
return callback(data);
});
this._characteristics[uuid].subscribe((err) => {
if (err) {
throw new Error(err);
}
});
} else if (this._webBLEServer) {
// @ts-ignore
this._characteristics[uuid].addEventListener("characteristicvaluechanged", (event) => {
return callback(event.target.value.buffer);
});
} }
} }
} public writeDataToCharacteristic (uuid: string, data: Buffer, callback?: () => void) {
uuid = this._sanitizeUUID(uuid);
if (this._noblePeripheral) {
this._characteristics[uuid].write(data, false, callback);
}
}
private _sanitizeUUID (uuid: string) {
return uuid.replace(/-/g, "");
}
}

View File

@ -1,5 +1,6 @@
import { Peripheral } from "noble"; import { Peripheral } from "noble";
import { BLEDevice } from "./bledevice";
import { LPF2Hub } from "./lpf2hub"; import { LPF2Hub } from "./lpf2hub";
import { Port } from "./port"; import { Port } from "./port";
@ -27,11 +28,13 @@ export class BoostMoveHub extends LPF2Hub {
public static IsBoostMoveHub (peripheral: Peripheral) { public static IsBoostMoveHub (peripheral: Peripheral) {
return (peripheral.advertisement.serviceUuids.indexOf(Consts.BLEService.LPF2_HUB.replace(/-/g, "")) >= 0 && peripheral.advertisement.manufacturerData[3] === Consts.BLEManufacturerData.BOOST_MOVE_HUB_ID); return (peripheral.advertisement &&
peripheral.advertisement.serviceUuids &&
peripheral.advertisement.serviceUuids.indexOf(Consts.BLEService.LPF2_HUB.replace(/-/g, "")) >= 0 && peripheral.advertisement.manufacturerData[3] === Consts.BLEManufacturerData.BOOST_MOVE_HUB_ID);
} }
constructor (peripheral: Peripheral, autoSubscribe: boolean = true) { constructor (peripheral: BLEDevice, autoSubscribe: boolean = true) {
super(peripheral, autoSubscribe); super(peripheral, autoSubscribe);
this.type = Consts.HubType.BOOST_MOVE_HUB; this.type = Consts.HubType.BOOST_MOVE_HUB;
this._ports = { this._ports = {

View File

@ -131,6 +131,9 @@ export enum BLEManufacturerData {
export enum BLEService { export enum BLEService {
WEDO2_SMART_HUB = "00001523-1212-efde-1523-785feabcd123", WEDO2_SMART_HUB = "00001523-1212-efde-1523-785feabcd123",
WEDO2_SMART_HUB_2 = "00004f0e-1212-efde-1523-785feabcd123",
WEDO2_SMART_HUB_3 = "2a19",
WEDO2_SMART_HUB_4 = "180f",
LPF2_HUB = "00001623-1212-efde-1623-785feabcd123" LPF2_HUB = "00001623-1212-efde-1623-785feabcd123"
} }

View File

@ -1,5 +1,6 @@
import { Peripheral } from "noble"; import { Peripheral } from "noble";
import { BLEDevice } from "./bledevice";
import { LPF2Hub } from "./lpf2hub"; import { LPF2Hub } from "./lpf2hub";
import { Port } from "./port"; import { Port } from "./port";
@ -57,11 +58,13 @@ export class DuploTrainBase extends LPF2Hub {
public static IsDuploTrainBase (peripheral: Peripheral) { public static IsDuploTrainBase (peripheral: Peripheral) {
return (peripheral.advertisement.serviceUuids.indexOf(Consts.BLEService.LPF2_HUB.replace(/-/g, "")) >= 0 && peripheral.advertisement.manufacturerData[3] === Consts.BLEManufacturerData.DUPLO_TRAIN_HUB_ID); return (peripheral.advertisement &&
peripheral.advertisement.serviceUuids &&
peripheral.advertisement.serviceUuids.indexOf(Consts.BLEService.LPF2_HUB.replace(/-/g, "")) >= 0 && peripheral.advertisement.manufacturerData[3] === Consts.BLEManufacturerData.DUPLO_TRAIN_HUB_ID);
} }
constructor (peripheral: Peripheral, autoSubscribe: boolean = true) { constructor (peripheral: BLEDevice, autoSubscribe: boolean = true) {
super(peripheral, autoSubscribe); super(peripheral, autoSubscribe);
this.type = Consts.HubType.DUPLO_TRAIN_HUB; this.type = Consts.HubType.DUPLO_TRAIN_HUB;
this._ports = { this._ports = {

View File

@ -1,6 +1,6 @@
import { EventEmitter } from "events"; import { EventEmitter } from "events";
import { Characteristic, Peripheral, Service } from "noble"; import { BLEDevice } from "./bledevice";
import { Port } from "./port"; import { Port } from "./port";
import * as Consts from "./consts"; import * as Consts from "./consts";
@ -29,7 +29,6 @@ export class Hub extends EventEmitter {
public type: Consts.HubType = Consts.HubType.UNKNOWN; public type: Consts.HubType = Consts.HubType.UNKNOWN;
protected _ports: {[port: string]: Port} = {}; protected _ports: {[port: string]: Port} = {};
protected _characteristics: {[uuid: string]: Characteristic} = {};
protected _name: string = ""; protected _name: string = "";
protected _firmwareInfo: IFirmwareInfo = { major: 0, minor: 0, bugFix: 0, build: 0 }; protected _firmwareInfo: IFirmwareInfo = { major: 0, minor: 0, bugFix: 0, build: 0 };
@ -37,23 +36,16 @@ export class Hub extends EventEmitter {
protected _voltage: number = 0; protected _voltage: number = 0;
protected _current: number = 0; protected _current: number = 0;
private _peripheral: Peripheral; protected _bleDevice: BLEDevice;
private _uuid: string;
private _rssi: number = -100; private _rssi: number = -100;
private _isConnecting = false; private _isConnecting = false;
private _isConnected = false; private _isConnected = false;
constructor (peripheral: Peripheral, autoSubscribe: boolean = true) { constructor (device: BLEDevice, autoSubscribe: boolean = true) {
super(); super();
this.autoSubscribe = !!autoSubscribe; this.autoSubscribe = !!autoSubscribe;
this._peripheral = peripheral; this._bleDevice = device;
this._uuid = peripheral.uuid;
// NK: This hack allows LPF2.0 hubs to send a second advertisement packet consisting of the hub name before we try to read it
setTimeout(() => {
this._name = peripheral.advertisement.localName;
this.emit("discoverComplete");
}, 1000);
} }
@ -62,7 +54,7 @@ export class Hub extends EventEmitter {
* @property {string} name Name of the hub * @property {string} name Name of the hub
*/ */
public get name () { public get name () {
return this._name; return this._bleDevice.name;
} }
@ -80,7 +72,7 @@ export class Hub extends EventEmitter {
* @property {string} uuid UUID of the hub * @property {string} uuid UUID of the hub
*/ */
public get uuid () { public get uuid () {
return this._uuid; return this._bleDevice.uuid;
} }
@ -126,7 +118,7 @@ export class Hub extends EventEmitter {
* @returns {Promise} Resolved upon successful connect. * @returns {Promise} Resolved upon successful connect.
*/ */
public connect () { public connect () {
return new Promise((connectResolve, connectReject) => { return new Promise(async (connectResolve, connectReject) => {
const self = this; const self = this;
@ -137,57 +129,59 @@ export class Hub extends EventEmitter {
} }
this._isConnecting = true; this._isConnecting = true;
this._peripheral.connect((err: string) => { await this._bleDevice.connect();
return connectResolve();
// this._peripheral.connect((err: string) => {
this._rssi = this._peripheral.rssi; // this._rssi = this._peripheral.rssi;
const rssiUpdateInterval = setInterval(() => { // const rssiUpdateInterval = setInterval(() => {
this._peripheral.updateRssi((err: string, rssi: number) => { // this._peripheral.updateRssi((err: string, rssi: number) => {
if (!err) { // if (!err) {
if (this._rssi !== rssi) { // if (this._rssi !== rssi) {
this._rssi = rssi; // this._rssi = rssi;
} // }
} // }
}); // });
}, 2000); // }, 2000);
self._peripheral.on("disconnect", () => { // self._peripheral.on("disconnect", () => {
clearInterval(rssiUpdateInterval); // clearInterval(rssiUpdateInterval);
this._isConnecting = false; // this._isConnecting = false;
this._isConnected = false; // this._isConnected = false;
this.emit("disconnect"); // this.emit("disconnect");
}); // });
self._peripheral.discoverServices([], (err: string, services: Service[]) => { // self._peripheral.discoverServices([], (err: string, services: Service[]) => {
if (err) { // if (err) {
this.emit("error", err); // this.emit("error", err);
return; // return;
} // }
debug("Service/characteristic discovery started"); // debug("Service/characteristic discovery started");
const servicePromises: Array<Promise<null>> = []; // const servicePromises: Array<Promise<null>> = [];
services.forEach((service) => { // services.forEach((service) => {
servicePromises.push(new Promise((resolve, reject) => { // servicePromises.push(new Promise((resolve, reject) => {
service.discoverCharacteristics([], (err, characteristics) => { // service.discoverCharacteristics([], (err, characteristics) => {
characteristics.forEach((characteristic) => { // characteristics.forEach((characteristic) => {
this._characteristics[characteristic.uuid] = characteristic; // this._characteristics[characteristic.uuid] = characteristic;
}); // });
return resolve(); // return resolve();
}); // });
})); // }));
}); // });
Promise.all(servicePromises).then(() => { // Promise.all(servicePromises).then(() => {
debug("Service/characteristic discovery finished"); // debug("Service/characteristic discovery finished");
this._isConnecting = false; // this._isConnecting = false;
this._isConnected = true; // this._isConnected = true;
this.emit("connect"); // this.emit("connect");
return connectResolve(); // return connectResolve();
}); // });
}); // });
}); // });
}); });
@ -199,12 +193,8 @@ export class Hub extends EventEmitter {
* @method Hub#disconnect * @method Hub#disconnect
* @returns {Promise} Resolved upon successful disconnect. * @returns {Promise} Resolved upon successful disconnect.
*/ */
public disconnect () { public async disconnect () {
return new Promise((resolve, reject) => { await this._bleDevice.disconnect();
this._peripheral.disconnect(() => {
return resolve();
});
});
} }
@ -292,21 +282,21 @@ export class Hub extends EventEmitter {
} }
protected _getCharacteristic (uuid: string) { // protected _getCharacteristic (uuid: string) {
return this._characteristics[uuid.replace(/-/g, "")]; // return this._characteristics[uuid.replace(/-/g, "")];
} // }
protected _subscribeToCharacteristic (characteristic: Characteristic, callback: (data: Buffer) => void) { // protected _subscribeToCharacteristic (characteristic: Characteristic, callback: (data: Buffer) => void) {
characteristic.on("data", (data: Buffer) => { // characteristic.on("data", (data: Buffer) => {
return callback(data); // return callback(data);
}); // });
characteristic.subscribe((err) => { // characteristic.subscribe((err) => {
if (err) { // if (err) {
this.emit("error", err); // this.emit("error", err);
} // }
}); // });
} // }
protected _activatePortDevice (port: number, type: number, mode: number, format: number, callback?: () => void) { protected _activatePortDevice (port: number, type: number, mode: number, format: number, callback?: () => void) {

View File

@ -24,8 +24,8 @@ export class LPF2Hub extends Hub {
public connect () { public connect () {
return new Promise(async (resolve, reject) => { return new Promise(async (resolve, reject) => {
await super.connect(); await super.connect();
const characteristic = this._getCharacteristic(Consts.BLECharacteristic.LPF2_ALL); await this._bleDevice.discoverCharacteristicsForService(Consts.BLEService.LPF2_HUB);
this._subscribeToCharacteristic(characteristic, this._parseMessage.bind(this)); this._bleDevice.subscribeToCharacteristic(Consts.BLECharacteristic.LPF2_ALL, this._parseMessage.bind(this));
setTimeout(() => { setTimeout(() => {
this._writeMessage(Consts.BLECharacteristic.LPF2_ALL, Buffer.from([0x01, 0x02, 0x02])); // Activate button reports this._writeMessage(Consts.BLECharacteristic.LPF2_ALL, Buffer.from([0x01, 0x02, 0x02])); // Activate button reports
this._writeMessage(Consts.BLECharacteristic.LPF2_ALL, Buffer.from([0x01, 0x03, 0x05])); // Request firmware version this._writeMessage(Consts.BLECharacteristic.LPF2_ALL, Buffer.from([0x01, 0x03, 0x05])); // Request firmware version
@ -35,8 +35,9 @@ export class LPF2Hub extends Hub {
if (this.type === Consts.HubType.DUPLO_TRAIN_HUB) { if (this.type === Consts.HubType.DUPLO_TRAIN_HUB) {
this._writeMessage(Consts.BLECharacteristic.LPF2_ALL, Buffer.from([0x41, 0x01, 0x01, 0x01, 0x00, 0x00, 0x00, 0x01])); this._writeMessage(Consts.BLECharacteristic.LPF2_ALL, Buffer.from([0x41, 0x01, 0x01, 0x01, 0x00, 0x00, 0x00, 0x01]));
} }
this.emit("connect");
return resolve();
}, 1000); }, 1000);
return resolve();
}); });
} }
@ -136,13 +137,10 @@ export class LPF2Hub extends Hub {
protected _writeMessage (uuid: string, message: Buffer, callback?: () => void) { protected _writeMessage (uuid: string, message: Buffer, callback?: () => void) {
const characteristic = this._getCharacteristic(uuid); message = Buffer.concat([Buffer.alloc(2), message]);
if (characteristic) { message[0] = message.length;
message = Buffer.concat([Buffer.alloc(2), message]); debug("Sent Message (LPF2_ALL)", message);
message[0] = message.length; this._bleDevice.writeDataToCharacteristic(uuid, message, callback);
debug("Sent Message (LPF2_ALL)", message);
characteristic.write(message, false, callback);
}
} }

View File

@ -1,5 +1,6 @@
import { Peripheral } from "noble-mac"; import { Peripheral } from "noble-mac";
import { BLEDevice } from "./bledevice";
import { BoostMoveHub } from "./boostmovehub"; import { BoostMoveHub } from "./boostmovehub";
import { DuploTrainBase } from "./duplotrainbase"; import { DuploTrainBase } from "./duplotrainbase";
import { Hub } from "./hub"; import { Hub } from "./hub";
@ -129,18 +130,20 @@ export class PoweredUP extends EventEmitter {
private async _discoveryEventHandler (peripheral: Peripheral) { private async _discoveryEventHandler (peripheral: Peripheral) {
const device = new BLEDevice(peripheral);
let hub: Hub; let hub: Hub;
if (await WeDo2SmartHub.IsWeDo2SmartHub(peripheral)) { if (await WeDo2SmartHub.IsWeDo2SmartHub(peripheral)) {
hub = new WeDo2SmartHub(peripheral, this.autoSubscribe); hub = new WeDo2SmartHub(device, this.autoSubscribe);
} else if (await BoostMoveHub.IsBoostMoveHub(peripheral)) { } else if (await BoostMoveHub.IsBoostMoveHub(peripheral)) {
hub = new BoostMoveHub(peripheral, this.autoSubscribe); hub = new BoostMoveHub(device, this.autoSubscribe);
} else if (await PUPHub.IsPUPHub(peripheral)) { } else if (await PUPHub.IsPUPHub(peripheral)) {
hub = new PUPHub(peripheral, this.autoSubscribe); hub = new PUPHub(device, this.autoSubscribe);
} else if (await PUPRemote.IsPUPRemote(peripheral)) { } else if (await PUPRemote.IsPUPRemote(peripheral)) {
hub = new PUPRemote(peripheral, this.autoSubscribe); hub = new PUPRemote(device, this.autoSubscribe);
} else if (await DuploTrainBase.IsDuploTrainBase(peripheral)) { } else if (await DuploTrainBase.IsDuploTrainBase(peripheral)) {
hub = new DuploTrainBase(peripheral, this.autoSubscribe); hub = new DuploTrainBase(device, this.autoSubscribe);
} else { } else {
return; return;
} }
@ -151,7 +154,7 @@ export class PoweredUP extends EventEmitter {
// startScanning(); // startScanning();
// } // }
hub.on("discoverComplete", () => { device.on("discoverComplete", () => {
hub.on("connect", () => { hub.on("connect", () => {
debug(`Hub ${hub.uuid} connected`); debug(`Hub ${hub.uuid} connected`);

View File

@ -1,5 +1,6 @@
import { Peripheral } from "noble"; import { Peripheral } from "noble";
import { BLEDevice } from "./bledevice";
import { LPF2Hub } from "./lpf2hub"; import { LPF2Hub } from "./lpf2hub";
import { Port } from "./port"; import { Port } from "./port";
@ -32,11 +33,13 @@ export class PUPHub extends LPF2Hub {
public static IsPUPHub (peripheral: Peripheral) { public static IsPUPHub (peripheral: Peripheral) {
return (peripheral.advertisement.serviceUuids.indexOf(Consts.BLEService.LPF2_HUB.replace(/-/g, "")) >= 0 && peripheral.advertisement.manufacturerData[3] === Consts.BLEManufacturerData.POWERED_UP_HUB_ID); return (peripheral.advertisement &&
peripheral.advertisement.serviceUuids &&
peripheral.advertisement.serviceUuids.indexOf(Consts.BLEService.LPF2_HUB.replace(/-/g, "")) >= 0 && peripheral.advertisement.manufacturerData[3] === Consts.BLEManufacturerData.POWERED_UP_HUB_ID);
} }
constructor (peripheral: Peripheral, autoSubscribe: boolean = true) { constructor (peripheral: BLEDevice, autoSubscribe: boolean = true) {
super(peripheral, autoSubscribe); super(peripheral, autoSubscribe);
this.type = Consts.HubType.POWERED_UP_HUB; this.type = Consts.HubType.POWERED_UP_HUB;
this._ports = { this._ports = {

View File

@ -1,5 +1,6 @@
import { Peripheral } from "noble"; import { Peripheral } from "noble";
import { BLEDevice } from "./bledevice";
import { LPF2Hub } from "./lpf2hub"; import { LPF2Hub } from "./lpf2hub";
import { Port } from "./port"; import { Port } from "./port";
@ -57,11 +58,13 @@ export class PUPRemote extends LPF2Hub {
public static IsPUPRemote (peripheral: Peripheral) { public static IsPUPRemote (peripheral: Peripheral) {
return (peripheral.advertisement.serviceUuids.indexOf(Consts.BLEService.LPF2_HUB.replace(/-/g, "")) >= 0 && peripheral.advertisement.manufacturerData[3] === Consts.BLEManufacturerData.POWERED_UP_REMOTE_ID); return (peripheral.advertisement &&
peripheral.advertisement.serviceUuids &&
peripheral.advertisement.serviceUuids.indexOf(Consts.BLEService.LPF2_HUB.replace(/-/g, "")) >= 0 && peripheral.advertisement.manufacturerData[3] === Consts.BLEManufacturerData.POWERED_UP_REMOTE_ID);
} }
constructor (peripheral: Peripheral, autoSubscribe: boolean = true) { constructor (peripheral: BLEDevice, autoSubscribe: boolean = true) {
super(peripheral, autoSubscribe); super(peripheral, autoSubscribe);
this.type = Consts.HubType.POWERED_UP_REMOTE; this.type = Consts.HubType.POWERED_UP_REMOTE;
this._ports = { this._ports = {

View File

@ -1,5 +1,6 @@
import { Peripheral } from "noble"; import { Peripheral } from "noble";
import { BLEDevice } from "./bledevice";
import { Hub } from "./hub"; import { Hub } from "./hub";
import { Port } from "./port"; import { Port } from "./port";
@ -26,7 +27,9 @@ export class WeDo2SmartHub extends Hub {
public static IsWeDo2SmartHub (peripheral: Peripheral) { public static IsWeDo2SmartHub (peripheral: Peripheral) {
return (peripheral.advertisement.serviceUuids.indexOf(Consts.BLEService.WEDO2_SMART_HUB.replace(/-/g, "")) >= 0); return (peripheral.advertisement &&
peripheral.advertisement.serviceUuids &&
peripheral.advertisement.serviceUuids.indexOf(Consts.BLEService.WEDO2_SMART_HUB.replace(/-/g, "")) >= 0);
} }
@ -34,7 +37,7 @@ export class WeDo2SmartHub extends Hub {
private _lastTiltY: number = 0; private _lastTiltY: number = 0;
constructor (peripheral: Peripheral, autoSubscribe: boolean = true) { constructor (peripheral: BLEDevice, autoSubscribe: boolean = true) {
super(peripheral, autoSubscribe); super(peripheral, autoSubscribe);
this.type = Consts.HubType.WEDO2_SMART_HUB; this.type = Consts.HubType.WEDO2_SMART_HUB;
this._ports = { this._ports = {
@ -49,23 +52,28 @@ export class WeDo2SmartHub extends Hub {
return new Promise(async (resolve, reject) => { return new Promise(async (resolve, reject) => {
debug("Connecting to WeDo 2.0 Smart Hub"); debug("Connecting to WeDo 2.0 Smart Hub");
await super.connect(); await super.connect();
this._subscribeToCharacteristic(this._getCharacteristic(Consts.BLECharacteristic.WEDO2_PORT_TYPE), this._parsePortMessage.bind(this)); await this._bleDevice.discoverCharacteristicsForService(Consts.BLEService.WEDO2_SMART_HUB);
this._subscribeToCharacteristic(this._getCharacteristic(Consts.BLECharacteristic.WEDO2_SENSOR_VALUE), this._parseSensorMessage.bind(this)); await this._bleDevice.discoverCharacteristicsForService(Consts.BLEService.WEDO2_SMART_HUB_2);
this._subscribeToCharacteristic(this._getCharacteristic(Consts.BLECharacteristic.WEDO2_BUTTON), this._parseSensorMessage.bind(this)); await this._bleDevice.discoverCharacteristicsForService(Consts.BLEService.WEDO2_SMART_HUB_3);
this._subscribeToCharacteristic(this._getCharacteristic(Consts.BLECharacteristic.WEDO2_BATTERY), this._parseBatteryMessage.bind(this)); await this._bleDevice.discoverCharacteristicsForService(Consts.BLEService.WEDO2_SMART_HUB_4);
this._subscribeToCharacteristic(this._getCharacteristic(Consts.BLECharacteristic.WEDO2_HIGH_CURRENT_ALERT), this._parseHighCurrentAlert.bind(this)); this._bleDevice.subscribeToCharacteristic(Consts.BLECharacteristic.WEDO2_PORT_TYPE, this._parsePortMessage.bind(this));
this._getCharacteristic(Consts.BLECharacteristic.WEDO2_BATTERY).read((err, data) => { this._bleDevice.subscribeToCharacteristic(Consts.BLECharacteristic.WEDO2_SENSOR_VALUE, this._parseSensorMessage.bind(this));
this._parseBatteryMessage(data); this._bleDevice.subscribeToCharacteristic(Consts.BLECharacteristic.WEDO2_BUTTON, this._parseSensorMessage.bind(this));
}); this._bleDevice.subscribeToCharacteristic(Consts.BLECharacteristic.WEDO2_BATTERY, this._parseBatteryMessage.bind(this));
this._getCharacteristic(Consts.BLECharacteristic.WEDO2_FIRMWARE_REVISION).read((err, data) => { this._bleDevice.subscribeToCharacteristic(Consts.BLECharacteristic.WEDO2_HIGH_CURRENT_ALERT, this._parseHighCurrentAlert.bind(this));
this._parseFirmwareRevisionString(data); // this._getCharacteristic(Consts.BLECharacteristic.WEDO2_BATTERY).read((err, data) => {
}); // this._parseBatteryMessage(data);
// });
// this._getCharacteristic(Consts.BLECharacteristic.WEDO2_FIRMWARE_REVISION).read((err, data) => {
// this._parseFirmwareRevisionString(data);
// });
setTimeout(() => { setTimeout(() => {
this._activatePortDevice(0x03, 0x15, 0x00, 0x00); // Activate voltage reports this._activatePortDevice(0x03, 0x15, 0x00, 0x00); // Activate voltage reports
this._activatePortDevice(0x04, 0x14, 0x00, 0x00); // Activate current reports this._activatePortDevice(0x04, 0x14, 0x00, 0x00); // Activate current reports
debug("Connect completed");
this.emit("connect");
return resolve();
}, 1000); }, 1000);
debug("Connect completed");
return resolve();
}); });
} }
@ -278,13 +286,10 @@ export class WeDo2SmartHub extends Hub {
private _writeMessage (uuid: string, message: Buffer, callback?: () => void) { private _writeMessage (uuid: string, message: Buffer, callback?: () => void) {
const characteristic = this._getCharacteristic(uuid); if (debug.enabled) {
if (characteristic) { debug(`Sent Message (${this._getCharacteristicNameFromUUID(uuid)})`, message);
if (debug.enabled) {
debug(`Sent Message (${this._getCharacteristicNameFromUUID(uuid)})`, message);
}
characteristic.write(message, false, callback);
} }
this._bleDevice.writeDataToCharacteristic(uuid, message, callback);
} }