BLEDevice partially working for noble devices
This commit is contained in:
parent
0a3018af9c
commit
79542acef1
6
package-lock.json
generated
6
package-lock.json
generated
@ -25,12 +25,6 @@
|
||||
"integrity": "sha512-fh+pAqt4xRzPfqA6eh3Z2y6fyZavRIumvjhaCL753+TVkGKGhpPeyrJG2JftD0T9q4GF00KjefsQ+PQNDdWQaQ==",
|
||||
"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": {
|
||||
"version": "1.1.1",
|
||||
"resolved": "https://registry.npmjs.org/abbrev/-/abbrev-1.1.1.tgz",
|
||||
|
@ -22,7 +22,6 @@
|
||||
"@types/debug": "0.0.31",
|
||||
"@types/noble": "0.0.38",
|
||||
"@types/node": "^10.12.18",
|
||||
"@types/web-bluetooth": "0.0.4",
|
||||
"ink-docstrap": "^1.3.2",
|
||||
"jsdoc": "^3.5.5",
|
||||
"jsdoc-to-markdown": "^4.0.1",
|
||||
|
136
src/bledevice.ts
136
src/bledevice.ts
@ -1,31 +1,151 @@
|
||||
import { Peripheral } from "noble";
|
||||
import { Characteristic, Peripheral, Service } from "noble";
|
||||
|
||||
import Debug = require("debug");
|
||||
import { EventEmitter } from "events";
|
||||
const debug = Debug("bledevice");
|
||||
|
||||
|
||||
export class BLEDevice {
|
||||
export class BLEDevice extends EventEmitter {
|
||||
|
||||
|
||||
// @ts-ignore
|
||||
// @ts-ignore
|
||||
private _noblePeripheral: Peripheral | null;
|
||||
private _webBLEServer: any;
|
||||
|
||||
private _uuid: string;
|
||||
private _name: string = "";
|
||||
|
||||
constructor (device: Peripheral | BluetoothRemoteGATTServer) {
|
||||
if (device instanceof Peripheral) {
|
||||
private _characteristics: {[uuid: string]: Characteristic} = {};
|
||||
|
||||
|
||||
constructor (device: any) {
|
||||
super();
|
||||
if (device._noble) {
|
||||
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 {
|
||||
this._webBLEServer = device;
|
||||
this._uuid = device.uuid;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public discoverService (uuid: string) {
|
||||
if (this._peripheral instanceof BluetoothRemoteGATTServer) {
|
||||
public get uuid () {
|
||||
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, "");
|
||||
}
|
||||
|
||||
|
||||
}
|
@ -1,5 +1,6 @@
|
||||
import { Peripheral } from "noble";
|
||||
|
||||
import { BLEDevice } from "./bledevice";
|
||||
import { LPF2Hub } from "./lpf2hub";
|
||||
import { Port } from "./port";
|
||||
|
||||
@ -27,11 +28,13 @@ export class BoostMoveHub extends LPF2Hub {
|
||||
|
||||
|
||||
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);
|
||||
this.type = Consts.HubType.BOOST_MOVE_HUB;
|
||||
this._ports = {
|
||||
|
@ -131,6 +131,9 @@ export enum BLEManufacturerData {
|
||||
|
||||
export enum BLEService {
|
||||
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"
|
||||
}
|
||||
|
||||
|
@ -1,5 +1,6 @@
|
||||
import { Peripheral } from "noble";
|
||||
|
||||
import { BLEDevice } from "./bledevice";
|
||||
import { LPF2Hub } from "./lpf2hub";
|
||||
import { Port } from "./port";
|
||||
|
||||
@ -57,11 +58,13 @@ export class DuploTrainBase extends LPF2Hub {
|
||||
|
||||
|
||||
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);
|
||||
this.type = Consts.HubType.DUPLO_TRAIN_HUB;
|
||||
this._ports = {
|
||||
|
144
src/hub.ts
144
src/hub.ts
@ -1,6 +1,6 @@
|
||||
import { EventEmitter } from "events";
|
||||
|
||||
import { Characteristic, Peripheral, Service } from "noble";
|
||||
import { BLEDevice } from "./bledevice";
|
||||
import { Port } from "./port";
|
||||
|
||||
import * as Consts from "./consts";
|
||||
@ -29,7 +29,6 @@ export class Hub extends EventEmitter {
|
||||
public type: Consts.HubType = Consts.HubType.UNKNOWN;
|
||||
|
||||
protected _ports: {[port: string]: Port} = {};
|
||||
protected _characteristics: {[uuid: string]: Characteristic} = {};
|
||||
|
||||
protected _name: string = "";
|
||||
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 _current: number = 0;
|
||||
|
||||
private _peripheral: Peripheral;
|
||||
private _uuid: string;
|
||||
protected _bleDevice: BLEDevice;
|
||||
private _rssi: number = -100;
|
||||
|
||||
private _isConnecting = false;
|
||||
private _isConnected = false;
|
||||
|
||||
constructor (peripheral: Peripheral, autoSubscribe: boolean = true) {
|
||||
constructor (device: BLEDevice, autoSubscribe: boolean = true) {
|
||||
super();
|
||||
this.autoSubscribe = !!autoSubscribe;
|
||||
this._peripheral = peripheral;
|
||||
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);
|
||||
this._bleDevice = device;
|
||||
}
|
||||
|
||||
|
||||
@ -62,7 +54,7 @@ export class Hub extends EventEmitter {
|
||||
* @property {string} name Name of the hub
|
||||
*/
|
||||
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
|
||||
*/
|
||||
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.
|
||||
*/
|
||||
public connect () {
|
||||
return new Promise((connectResolve, connectReject) => {
|
||||
return new Promise(async (connectResolve, connectReject) => {
|
||||
|
||||
const self = this;
|
||||
|
||||
@ -137,57 +129,59 @@ export class Hub extends EventEmitter {
|
||||
}
|
||||
|
||||
this._isConnecting = true;
|
||||
this._peripheral.connect((err: string) => {
|
||||
await this._bleDevice.connect();
|
||||
return connectResolve();
|
||||
// this._peripheral.connect((err: string) => {
|
||||
|
||||
this._rssi = this._peripheral.rssi;
|
||||
const rssiUpdateInterval = setInterval(() => {
|
||||
this._peripheral.updateRssi((err: string, rssi: number) => {
|
||||
if (!err) {
|
||||
if (this._rssi !== rssi) {
|
||||
this._rssi = rssi;
|
||||
}
|
||||
}
|
||||
});
|
||||
}, 2000);
|
||||
// this._rssi = this._peripheral.rssi;
|
||||
// const rssiUpdateInterval = setInterval(() => {
|
||||
// this._peripheral.updateRssi((err: string, rssi: number) => {
|
||||
// if (!err) {
|
||||
// if (this._rssi !== rssi) {
|
||||
// this._rssi = rssi;
|
||||
// }
|
||||
// }
|
||||
// });
|
||||
// }, 2000);
|
||||
|
||||
self._peripheral.on("disconnect", () => {
|
||||
clearInterval(rssiUpdateInterval);
|
||||
this._isConnecting = false;
|
||||
this._isConnected = false;
|
||||
this.emit("disconnect");
|
||||
});
|
||||
// self._peripheral.on("disconnect", () => {
|
||||
// clearInterval(rssiUpdateInterval);
|
||||
// this._isConnecting = false;
|
||||
// this._isConnected = false;
|
||||
// this.emit("disconnect");
|
||||
// });
|
||||
|
||||
self._peripheral.discoverServices([], (err: string, services: Service[]) => {
|
||||
// self._peripheral.discoverServices([], (err: string, services: Service[]) => {
|
||||
|
||||
if (err) {
|
||||
this.emit("error", err);
|
||||
return;
|
||||
}
|
||||
// if (err) {
|
||||
// this.emit("error", err);
|
||||
// return;
|
||||
// }
|
||||
|
||||
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();
|
||||
});
|
||||
}));
|
||||
});
|
||||
// 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");
|
||||
this._isConnecting = false;
|
||||
this._isConnected = true;
|
||||
this.emit("connect");
|
||||
return connectResolve();
|
||||
});
|
||||
// Promise.all(servicePromises).then(() => {
|
||||
// debug("Service/characteristic discovery finished");
|
||||
// this._isConnecting = false;
|
||||
// this._isConnected = true;
|
||||
// this.emit("connect");
|
||||
// return connectResolve();
|
||||
// });
|
||||
|
||||
});
|
||||
// });
|
||||
|
||||
});
|
||||
// });
|
||||
|
||||
});
|
||||
|
||||
@ -199,12 +193,8 @@ export class Hub extends EventEmitter {
|
||||
* @method Hub#disconnect
|
||||
* @returns {Promise} Resolved upon successful disconnect.
|
||||
*/
|
||||
public disconnect () {
|
||||
return new Promise((resolve, reject) => {
|
||||
this._peripheral.disconnect(() => {
|
||||
return resolve();
|
||||
});
|
||||
});
|
||||
public async disconnect () {
|
||||
await this._bleDevice.disconnect();
|
||||
}
|
||||
|
||||
|
||||
@ -292,21 +282,21 @@ export class Hub extends EventEmitter {
|
||||
}
|
||||
|
||||
|
||||
protected _getCharacteristic (uuid: string) {
|
||||
return this._characteristics[uuid.replace(/-/g, "")];
|
||||
}
|
||||
// protected _getCharacteristic (uuid: string) {
|
||||
// return this._characteristics[uuid.replace(/-/g, "")];
|
||||
// }
|
||||
|
||||
|
||||
protected _subscribeToCharacteristic (characteristic: Characteristic, callback: (data: Buffer) => void) {
|
||||
characteristic.on("data", (data: Buffer) => {
|
||||
return callback(data);
|
||||
});
|
||||
characteristic.subscribe((err) => {
|
||||
if (err) {
|
||||
this.emit("error", err);
|
||||
}
|
||||
});
|
||||
}
|
||||
// protected _subscribeToCharacteristic (characteristic: Characteristic, callback: (data: Buffer) => void) {
|
||||
// characteristic.on("data", (data: Buffer) => {
|
||||
// return callback(data);
|
||||
// });
|
||||
// characteristic.subscribe((err) => {
|
||||
// if (err) {
|
||||
// this.emit("error", err);
|
||||
// }
|
||||
// });
|
||||
// }
|
||||
|
||||
|
||||
protected _activatePortDevice (port: number, type: number, mode: number, format: number, callback?: () => void) {
|
||||
|
@ -24,8 +24,8 @@ export class LPF2Hub extends Hub {
|
||||
public connect () {
|
||||
return new Promise(async (resolve, reject) => {
|
||||
await super.connect();
|
||||
const characteristic = this._getCharacteristic(Consts.BLECharacteristic.LPF2_ALL);
|
||||
this._subscribeToCharacteristic(characteristic, this._parseMessage.bind(this));
|
||||
await this._bleDevice.discoverCharacteristicsForService(Consts.BLEService.LPF2_HUB);
|
||||
this._bleDevice.subscribeToCharacteristic(Consts.BLECharacteristic.LPF2_ALL, this._parseMessage.bind(this));
|
||||
setTimeout(() => {
|
||||
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
|
||||
@ -35,8 +35,9 @@ export class LPF2Hub extends 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.emit("connect");
|
||||
return resolve();
|
||||
}, 1000);
|
||||
return resolve();
|
||||
});
|
||||
}
|
||||
|
||||
@ -136,13 +137,10 @@ export class LPF2Hub extends Hub {
|
||||
|
||||
|
||||
protected _writeMessage (uuid: string, message: Buffer, callback?: () => void) {
|
||||
const characteristic = this._getCharacteristic(uuid);
|
||||
if (characteristic) {
|
||||
message = Buffer.concat([Buffer.alloc(2), message]);
|
||||
message[0] = message.length;
|
||||
debug("Sent Message (LPF2_ALL)", message);
|
||||
characteristic.write(message, false, callback);
|
||||
}
|
||||
message = Buffer.concat([Buffer.alloc(2), message]);
|
||||
message[0] = message.length;
|
||||
debug("Sent Message (LPF2_ALL)", message);
|
||||
this._bleDevice.writeDataToCharacteristic(uuid, message, callback);
|
||||
}
|
||||
|
||||
|
||||
|
@ -1,5 +1,6 @@
|
||||
import { Peripheral } from "noble-mac";
|
||||
|
||||
import { BLEDevice } from "./bledevice";
|
||||
import { BoostMoveHub } from "./boostmovehub";
|
||||
import { DuploTrainBase } from "./duplotrainbase";
|
||||
import { Hub } from "./hub";
|
||||
@ -129,18 +130,20 @@ export class PoweredUP extends EventEmitter {
|
||||
|
||||
private async _discoveryEventHandler (peripheral: Peripheral) {
|
||||
|
||||
const device = new BLEDevice(peripheral);
|
||||
|
||||
let hub: Hub;
|
||||
|
||||
if (await WeDo2SmartHub.IsWeDo2SmartHub(peripheral)) {
|
||||
hub = new WeDo2SmartHub(peripheral, this.autoSubscribe);
|
||||
hub = new WeDo2SmartHub(device, this.autoSubscribe);
|
||||
} else if (await BoostMoveHub.IsBoostMoveHub(peripheral)) {
|
||||
hub = new BoostMoveHub(peripheral, this.autoSubscribe);
|
||||
hub = new BoostMoveHub(device, this.autoSubscribe);
|
||||
} else if (await PUPHub.IsPUPHub(peripheral)) {
|
||||
hub = new PUPHub(peripheral, this.autoSubscribe);
|
||||
hub = new PUPHub(device, this.autoSubscribe);
|
||||
} else if (await PUPRemote.IsPUPRemote(peripheral)) {
|
||||
hub = new PUPRemote(peripheral, this.autoSubscribe);
|
||||
hub = new PUPRemote(device, this.autoSubscribe);
|
||||
} else if (await DuploTrainBase.IsDuploTrainBase(peripheral)) {
|
||||
hub = new DuploTrainBase(peripheral, this.autoSubscribe);
|
||||
hub = new DuploTrainBase(device, this.autoSubscribe);
|
||||
} else {
|
||||
return;
|
||||
}
|
||||
@ -151,7 +154,7 @@ export class PoweredUP extends EventEmitter {
|
||||
// startScanning();
|
||||
// }
|
||||
|
||||
hub.on("discoverComplete", () => {
|
||||
device.on("discoverComplete", () => {
|
||||
|
||||
hub.on("connect", () => {
|
||||
debug(`Hub ${hub.uuid} connected`);
|
||||
|
@ -1,5 +1,6 @@
|
||||
import { Peripheral } from "noble";
|
||||
|
||||
import { BLEDevice } from "./bledevice";
|
||||
import { LPF2Hub } from "./lpf2hub";
|
||||
import { Port } from "./port";
|
||||
|
||||
@ -32,11 +33,13 @@ export class PUPHub extends LPF2Hub {
|
||||
|
||||
|
||||
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);
|
||||
this.type = Consts.HubType.POWERED_UP_HUB;
|
||||
this._ports = {
|
||||
|
@ -1,5 +1,6 @@
|
||||
import { Peripheral } from "noble";
|
||||
|
||||
import { BLEDevice } from "./bledevice";
|
||||
import { LPF2Hub } from "./lpf2hub";
|
||||
import { Port } from "./port";
|
||||
|
||||
@ -57,11 +58,13 @@ export class PUPRemote extends LPF2Hub {
|
||||
|
||||
|
||||
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);
|
||||
this.type = Consts.HubType.POWERED_UP_REMOTE;
|
||||
this._ports = {
|
||||
|
@ -1,5 +1,6 @@
|
||||
import { Peripheral } from "noble";
|
||||
|
||||
import { BLEDevice } from "./bledevice";
|
||||
import { Hub } from "./hub";
|
||||
import { Port } from "./port";
|
||||
|
||||
@ -26,7 +27,9 @@ export class WeDo2SmartHub extends Hub {
|
||||
|
||||
|
||||
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;
|
||||
|
||||
|
||||
constructor (peripheral: Peripheral, autoSubscribe: boolean = true) {
|
||||
constructor (peripheral: BLEDevice, autoSubscribe: boolean = true) {
|
||||
super(peripheral, autoSubscribe);
|
||||
this.type = Consts.HubType.WEDO2_SMART_HUB;
|
||||
this._ports = {
|
||||
@ -49,23 +52,28 @@ export class WeDo2SmartHub extends Hub {
|
||||
return new Promise(async (resolve, reject) => {
|
||||
debug("Connecting to WeDo 2.0 Smart Hub");
|
||||
await super.connect();
|
||||
this._subscribeToCharacteristic(this._getCharacteristic(Consts.BLECharacteristic.WEDO2_PORT_TYPE), this._parsePortMessage.bind(this));
|
||||
this._subscribeToCharacteristic(this._getCharacteristic(Consts.BLECharacteristic.WEDO2_SENSOR_VALUE), this._parseSensorMessage.bind(this));
|
||||
this._subscribeToCharacteristic(this._getCharacteristic(Consts.BLECharacteristic.WEDO2_BUTTON), this._parseSensorMessage.bind(this));
|
||||
this._subscribeToCharacteristic(this._getCharacteristic(Consts.BLECharacteristic.WEDO2_BATTERY), this._parseBatteryMessage.bind(this));
|
||||
this._subscribeToCharacteristic(this._getCharacteristic(Consts.BLECharacteristic.WEDO2_HIGH_CURRENT_ALERT), this._parseHighCurrentAlert.bind(this));
|
||||
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);
|
||||
});
|
||||
await this._bleDevice.discoverCharacteristicsForService(Consts.BLEService.WEDO2_SMART_HUB);
|
||||
await this._bleDevice.discoverCharacteristicsForService(Consts.BLEService.WEDO2_SMART_HUB_2);
|
||||
await this._bleDevice.discoverCharacteristicsForService(Consts.BLEService.WEDO2_SMART_HUB_3);
|
||||
await this._bleDevice.discoverCharacteristicsForService(Consts.BLEService.WEDO2_SMART_HUB_4);
|
||||
this._bleDevice.subscribeToCharacteristic(Consts.BLECharacteristic.WEDO2_PORT_TYPE, this._parsePortMessage.bind(this));
|
||||
this._bleDevice.subscribeToCharacteristic(Consts.BLECharacteristic.WEDO2_SENSOR_VALUE, this._parseSensorMessage.bind(this));
|
||||
this._bleDevice.subscribeToCharacteristic(Consts.BLECharacteristic.WEDO2_BUTTON, this._parseSensorMessage.bind(this));
|
||||
this._bleDevice.subscribeToCharacteristic(Consts.BLECharacteristic.WEDO2_BATTERY, this._parseBatteryMessage.bind(this));
|
||||
this._bleDevice.subscribeToCharacteristic(Consts.BLECharacteristic.WEDO2_HIGH_CURRENT_ALERT, this._parseHighCurrentAlert.bind(this));
|
||||
// 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(() => {
|
||||
this._activatePortDevice(0x03, 0x15, 0x00, 0x00); // Activate voltage reports
|
||||
this._activatePortDevice(0x04, 0x14, 0x00, 0x00); // Activate current reports
|
||||
debug("Connect completed");
|
||||
this.emit("connect");
|
||||
return resolve();
|
||||
}, 1000);
|
||||
debug("Connect completed");
|
||||
return resolve();
|
||||
});
|
||||
}
|
||||
|
||||
@ -278,13 +286,10 @@ export class WeDo2SmartHub extends Hub {
|
||||
|
||||
|
||||
private _writeMessage (uuid: string, message: Buffer, callback?: () => void) {
|
||||
const characteristic = this._getCharacteristic(uuid);
|
||||
if (characteristic) {
|
||||
if (debug.enabled) {
|
||||
debug(`Sent Message (${this._getCharacteristicNameFromUUID(uuid)})`, message);
|
||||
}
|
||||
characteristic.write(message, false, callback);
|
||||
if (debug.enabled) {
|
||||
debug(`Sent Message (${this._getCharacteristicNameFromUUID(uuid)})`, message);
|
||||
}
|
||||
this._bleDevice.writeDataToCharacteristic(uuid, message, callback);
|
||||
}
|
||||
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user