Merge pull request #76 from highflying/feature/return-promises
Feature/return promises
This commit is contained in:
commit
854666fd06
@ -28,10 +28,7 @@ export class BasicMotor extends Device {
|
|||||||
if (interrupt) {
|
if (interrupt) {
|
||||||
this.cancelEventTimer();
|
this.cancelEventTimer();
|
||||||
}
|
}
|
||||||
return new Promise((resolve) => {
|
return this.writeDirect(0x00, Buffer.from([mapSpeed(power)]));
|
||||||
this.writeDirect(0x00, Buffer.from([mapSpeed(power)]));
|
|
||||||
return resolve();
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -126,17 +126,17 @@ export class Device extends EventEmitter {
|
|||||||
return this._isVirtualPort;
|
return this._isVirtualPort;
|
||||||
}
|
}
|
||||||
|
|
||||||
public writeDirect (mode: number, data: Buffer, callback?: () => void) {
|
public writeDirect (mode: number, data: Buffer) {
|
||||||
if (this.isWeDo2SmartHub) {
|
if (this.isWeDo2SmartHub) {
|
||||||
this.send(Buffer.concat([Buffer.from([this.portId, 0x01, 0x02]), data]), Consts.BLECharacteristic.WEDO2_MOTOR_VALUE_WRITE);
|
return this.send(Buffer.concat([Buffer.from([this.portId, 0x01, 0x02]), data]), Consts.BLECharacteristic.WEDO2_MOTOR_VALUE_WRITE);
|
||||||
} else {
|
} else {
|
||||||
this.send(Buffer.concat([Buffer.from([0x81, this.portId, 0x11, 0x51, mode]), data]), Consts.BLECharacteristic.LPF2_ALL, callback);
|
return this.send(Buffer.concat([Buffer.from([0x81, this.portId, 0x11, 0x51, mode]), data]), Consts.BLECharacteristic.LPF2_ALL);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public send (data: Buffer, characteristic: string = Consts.BLECharacteristic.LPF2_ALL, callback?: () => void) {
|
public send (data: Buffer, characteristic: string = Consts.BLECharacteristic.LPF2_ALL) {
|
||||||
this._ensureConnected();
|
this._ensureConnected();
|
||||||
this.hub.send(data, characteristic, callback);
|
return this.hub.send(data, characteristic);
|
||||||
}
|
}
|
||||||
|
|
||||||
public subscribe (mode: number) {
|
public subscribe (mode: number) {
|
||||||
|
@ -165,16 +165,12 @@ export class BaseHub extends EventEmitter {
|
|||||||
* @returns {Promise} Resolved upon successful connect.
|
* @returns {Promise} Resolved upon successful connect.
|
||||||
*/
|
*/
|
||||||
public connect () {
|
public connect () {
|
||||||
return new Promise(async (connectResolve, connectReject) => {
|
|
||||||
if (this._bleDevice.connecting) {
|
if (this._bleDevice.connecting) {
|
||||||
return connectReject("Already connecting");
|
throw new Error("Already connecting");
|
||||||
} else if (this._bleDevice.connected) {
|
} else if (this._bleDevice.connected) {
|
||||||
return connectReject("Already connected");
|
throw new Error("Already connected");
|
||||||
}
|
}
|
||||||
await this._bleDevice.connect();
|
return this._bleDevice.connect();
|
||||||
return connectResolve();
|
|
||||||
});
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -320,10 +316,8 @@ export class BaseHub extends EventEmitter {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public send (message: Buffer, uuid: string, callback?: () => void) {
|
public send (message: Buffer, uuid: string) {
|
||||||
if (callback) {
|
return Promise.resolve();
|
||||||
callback();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -36,13 +36,10 @@ export class DuploTrainBase extends LPF2Hub {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public connect () {
|
public async connect () {
|
||||||
return new Promise(async (resolve, reject) => {
|
|
||||||
debug("Connecting to Duplo Train Base");
|
debug("Connecting to Duplo Train Base");
|
||||||
await super.connect();
|
await super.connect();
|
||||||
debug("Connect completed");
|
debug("Connect completed");
|
||||||
return resolve();
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -39,13 +39,10 @@ export class Hub extends LPF2Hub {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public connect () {
|
public async connect () {
|
||||||
return new Promise(async (resolve, reject) => {
|
|
||||||
debug("Connecting to Powered UP Hub");
|
debug("Connecting to Powered UP Hub");
|
||||||
await super.connect();
|
await super.connect();
|
||||||
debug("Connect completed");
|
debug("Connect completed");
|
||||||
return resolve();
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -20,23 +20,19 @@ export class LPF2Hub extends BaseHub {
|
|||||||
private _propertyRequestCallbacks: {[property: number]: ((data: Buffer) => void)} = {};
|
private _propertyRequestCallbacks: {[property: number]: ((data: Buffer) => void)} = {};
|
||||||
|
|
||||||
|
|
||||||
public connect () {
|
public async connect () {
|
||||||
return new Promise(async (resolve, reject) => {
|
|
||||||
debug("LPF2Hub connecting");
|
debug("LPF2Hub connecting");
|
||||||
await super.connect();
|
await super.connect();
|
||||||
await this._bleDevice.discoverCharacteristicsForService(Consts.BLEService.LPF2_HUB);
|
await this._bleDevice.discoverCharacteristicsForService(Consts.BLEService.LPF2_HUB);
|
||||||
this._bleDevice.subscribeToCharacteristic(Consts.BLECharacteristic.LPF2_ALL, this._parseMessage.bind(this));
|
this._bleDevice.subscribeToCharacteristic(Consts.BLECharacteristic.LPF2_ALL, this._parseMessage.bind(this));
|
||||||
await this.sleep(500);
|
await this._requestHubPropertyReports(0x02); // Activate button reports
|
||||||
this._requestHubPropertyReports(0x02); // Activate button reports
|
|
||||||
await this._requestHubPropertyValue(0x03); // Request firmware version
|
await this._requestHubPropertyValue(0x03); // Request firmware version
|
||||||
await this._requestHubPropertyValue(0x04); // Request hardware version
|
await this._requestHubPropertyValue(0x04); // Request hardware version
|
||||||
this._requestHubPropertyReports(0x05); // Activate RSSI updates
|
await this._requestHubPropertyReports(0x05); // Activate RSSI updates
|
||||||
this._requestHubPropertyReports(0x06); // Activate battery level reports
|
await this._requestHubPropertyReports(0x06); // Activate battery level reports
|
||||||
await this._requestHubPropertyValue(0x0d); // Request primary MAC address
|
await this._requestHubPropertyValue(0x0d); // Request primary MAC address
|
||||||
this.emit("connect");
|
this.emit("connect");
|
||||||
debug("LPF2Hub connected");
|
debug("LPF2Hub connected");
|
||||||
resolve();
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -46,11 +42,7 @@ export class LPF2Hub extends BaseHub {
|
|||||||
* @returns {Promise} Resolved upon successful disconnect.
|
* @returns {Promise} Resolved upon successful disconnect.
|
||||||
*/
|
*/
|
||||||
public shutdown () {
|
public shutdown () {
|
||||||
return new Promise((resolve, reject) => {
|
return this.send(Buffer.from([0x02, 0x01]), Consts.BLECharacteristic.LPF2_ALL);
|
||||||
this.send(Buffer.from([0x02, 0x01]), Consts.BLECharacteristic.LPF2_ALL, () => {
|
|
||||||
return resolve();
|
|
||||||
});
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -60,37 +52,34 @@ export class LPF2Hub extends BaseHub {
|
|||||||
* @param {string} name New name of the hub (14 characters or less, ASCII only).
|
* @param {string} name New name of the hub (14 characters or less, ASCII only).
|
||||||
* @returns {Promise} Resolved upon successful issuance of command.
|
* @returns {Promise} Resolved upon successful issuance of command.
|
||||||
*/
|
*/
|
||||||
public setName (name: string) {
|
public async setName (name: string) {
|
||||||
if (name.length > 14) {
|
if (name.length > 14) {
|
||||||
throw new Error("Name must be 14 characters or less");
|
throw new Error("Name must be 14 characters or less");
|
||||||
}
|
}
|
||||||
return new Promise((resolve, reject) => {
|
|
||||||
let data = Buffer.from([0x01, 0x01, 0x01]);
|
let data = Buffer.from([0x01, 0x01, 0x01]);
|
||||||
data = Buffer.concat([data, Buffer.from(name, "ascii")]);
|
data = Buffer.concat([data, Buffer.from(name, "ascii")]);
|
||||||
// Send this twice, as sometimes the first time doesn't take
|
// Send this twice, as sometimes the first time doesn't take
|
||||||
this.send(data, Consts.BLECharacteristic.LPF2_ALL);
|
await this.send(data, Consts.BLECharacteristic.LPF2_ALL);
|
||||||
this.send(data, Consts.BLECharacteristic.LPF2_ALL);
|
await this.send(data, Consts.BLECharacteristic.LPF2_ALL);
|
||||||
this._name = name;
|
this._name = name;
|
||||||
return resolve();
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public send (message: Buffer, uuid: string, callback?: () => void) {
|
public send (message: Buffer, uuid: string) {
|
||||||
message = Buffer.concat([Buffer.alloc(2), message]);
|
message = Buffer.concat([Buffer.alloc(2), message]);
|
||||||
message[0] = message.length;
|
message[0] = message.length;
|
||||||
debug("Sent Message (LPF2_ALL)", message);
|
debug("Sent Message (LPF2_ALL)", message);
|
||||||
this._bleDevice.writeToCharacteristic(uuid, message, callback);
|
return this._bleDevice.writeToCharacteristic(uuid, message);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public subscribe (portId: number, deviceType: number, mode: number) {
|
public subscribe (portId: number, deviceType: number, mode: number) {
|
||||||
this.send(Buffer.from([0x41, portId, mode, 0x01, 0x00, 0x00, 0x00, 0x01]), Consts.BLECharacteristic.LPF2_ALL);
|
return this.send(Buffer.from([0x41, portId, mode, 0x01, 0x00, 0x00, 0x00, 0x01]), Consts.BLECharacteristic.LPF2_ALL);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public unsubscribe (portId: number, mode: number) {
|
public unsubscribe (portId: number, mode: number) {
|
||||||
this.send(Buffer.from([0x41, portId, mode, 0x01, 0x00, 0x00, 0x00, 0x00]), Consts.BLECharacteristic.LPF2_ALL);
|
return this.send(Buffer.from([0x41, portId, mode, 0x01, 0x00, 0x00, 0x00, 0x00]), Consts.BLECharacteristic.LPF2_ALL);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -115,7 +104,7 @@ export class LPF2Hub extends BaseHub {
|
|||||||
if (firstDevice.type !== secondDevice.type) {
|
if (firstDevice.type !== secondDevice.type) {
|
||||||
throw new Error(`Both devices must be of the same type to create a virtual port`);
|
throw new Error(`Both devices must be of the same type to create a virtual port`);
|
||||||
}
|
}
|
||||||
this.send(Buffer.from([0x61, 0x01, firstDevice.portId, secondDevice.portId]), Consts.BLECharacteristic.LPF2_ALL);
|
return this.send(Buffer.from([0x61, 0x01, firstDevice.portId, secondDevice.portId]), Consts.BLECharacteristic.LPF2_ALL);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -196,7 +185,7 @@ export class LPF2Hub extends BaseHub {
|
|||||||
|
|
||||||
|
|
||||||
private _requestHubPropertyReports (property: number) {
|
private _requestHubPropertyReports (property: number) {
|
||||||
this.send(Buffer.from([0x01, property, 0x02]), Consts.BLECharacteristic.LPF2_ALL);
|
return this.send(Buffer.from([0x01, property, 0x02]), Consts.BLECharacteristic.LPF2_ALL);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -250,7 +239,7 @@ export class LPF2Hub extends BaseHub {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private _parsePortMessage (message: Buffer) {
|
private async _parsePortMessage (message: Buffer) {
|
||||||
|
|
||||||
const portId = message[3];
|
const portId = message[3];
|
||||||
const event = message[4];
|
const event = message[4];
|
||||||
@ -265,7 +254,7 @@ export class LPF2Hub extends BaseHub {
|
|||||||
const hwVersion = decodeVersion(message.readInt32LE(7));
|
const hwVersion = decodeVersion(message.readInt32LE(7));
|
||||||
const swVersion = decodeVersion(message.readInt32LE(11));
|
const swVersion = decodeVersion(message.readInt32LE(11));
|
||||||
modeInfoDebug(`Port ${toHex(portId)}, hardware version ${hwVersion}, software version ${swVersion}`);
|
modeInfoDebug(`Port ${toHex(portId)}, hardware version ${hwVersion}, software version ${swVersion}`);
|
||||||
this._sendPortInformationRequest(portId);
|
await this._sendPortInformationRequest(portId);
|
||||||
}
|
}
|
||||||
|
|
||||||
const device = this._createDevice(deviceType, portId);
|
const device = this._createDevice(deviceType, portId);
|
||||||
@ -301,13 +290,13 @@ export class LPF2Hub extends BaseHub {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
private _sendPortInformationRequest (port: number) {
|
private async _sendPortInformationRequest (port: number) {
|
||||||
this.send(Buffer.from([0x21, port, 0x01]), Consts.BLECharacteristic.LPF2_ALL);
|
await this.send(Buffer.from([0x21, port, 0x01]), Consts.BLECharacteristic.LPF2_ALL);
|
||||||
this.send(Buffer.from([0x21, port, 0x02]), Consts.BLECharacteristic.LPF2_ALL); // Mode combinations
|
await this.send(Buffer.from([0x21, port, 0x02]), Consts.BLECharacteristic.LPF2_ALL); // Mode combinations
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
private _parsePortInformationResponse (message: Buffer) {
|
private async _parsePortInformationResponse (message: Buffer) {
|
||||||
const port = message[3];
|
const port = message[3];
|
||||||
if (message[4] === 2) {
|
if (message[4] === 2) {
|
||||||
const modeCombinationMasks: number[] = [];
|
const modeCombinationMasks: number[] = [];
|
||||||
@ -323,18 +312,18 @@ export class LPF2Hub extends BaseHub {
|
|||||||
modeInfoDebug(`Port ${toHex(port)}, total modes ${count}, input modes ${input}, output modes ${output}`);
|
modeInfoDebug(`Port ${toHex(port)}, total modes ${count}, input modes ${input}, output modes ${output}`);
|
||||||
|
|
||||||
for (let i = 0; i < count; i++) {
|
for (let i = 0; i < count; i++) {
|
||||||
this._sendModeInformationRequest(port, i, 0x00); // Mode Name
|
await this._sendModeInformationRequest(port, i, 0x00); // Mode Name
|
||||||
this._sendModeInformationRequest(port, i, 0x01); // RAW Range
|
await this._sendModeInformationRequest(port, i, 0x01); // RAW Range
|
||||||
this._sendModeInformationRequest(port, i, 0x02); // PCT Range
|
await this._sendModeInformationRequest(port, i, 0x02); // PCT Range
|
||||||
this._sendModeInformationRequest(port, i, 0x03); // SI Range
|
await this._sendModeInformationRequest(port, i, 0x03); // SI Range
|
||||||
this._sendModeInformationRequest(port, i, 0x04); // SI Symbol
|
await this._sendModeInformationRequest(port, i, 0x04); // SI Symbol
|
||||||
this._sendModeInformationRequest(port, i, 0x80); // Value Format
|
await this._sendModeInformationRequest(port, i, 0x80); // Value Format
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
private _sendModeInformationRequest (port: number, mode: number, type: number) {
|
private _sendModeInformationRequest (port: number, mode: number, type: number) {
|
||||||
this.send(Buffer.from([0x22, port, mode, type]), Consts.BLECharacteristic.LPF2_ALL);
|
return this.send(Buffer.from([0x22, port, mode, type]), Consts.BLECharacteristic.LPF2_ALL);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -37,13 +37,10 @@ export class MoveHub extends LPF2Hub {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public connect () {
|
public async connect () {
|
||||||
return new Promise(async (resolve, reject) => {
|
|
||||||
debug("Connecting to Move Hub");
|
debug("Connecting to Move Hub");
|
||||||
await super.connect();
|
await super.connect();
|
||||||
debug("Connect completed");
|
debug("Connect completed");
|
||||||
return resolve();
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -37,13 +37,10 @@ export class RemoteControl extends LPF2Hub {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public connect () {
|
public async connect () {
|
||||||
return new Promise(async (resolve, reject) => {
|
|
||||||
debug("Connecting to Powered UP Remote");
|
debug("Connecting to Powered UP Remote");
|
||||||
await super.connect();
|
await super.connect();
|
||||||
debug("Connect completed");
|
debug("Connect completed");
|
||||||
return resolve();
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -36,14 +36,11 @@ export class TechnicMediumHub extends LPF2Hub {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public connect () {
|
public async connect () {
|
||||||
return new Promise(async (resolve, reject) => {
|
|
||||||
debug("Connecting to Control+ Hub");
|
debug("Connecting to Control+ Hub");
|
||||||
await super.connect();
|
await super.connect();
|
||||||
this.send(Buffer.from([0x41, 0x3d, 0x00, 0x0a, 0x00, 0x00, 0x00, 0x01]), Consts.BLECharacteristic.LPF2_ALL); // Temperature
|
await this.send(Buffer.from([0x41, 0x3d, 0x00, 0x0a, 0x00, 0x00, 0x00, 0x01]), Consts.BLECharacteristic.LPF2_ALL); // Temperature
|
||||||
debug("Connect completed");
|
debug("Connect completed");
|
||||||
return resolve();
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -41,7 +41,7 @@ export class WeDo2SmartHub extends BaseHub {
|
|||||||
|
|
||||||
|
|
||||||
public connect () {
|
public connect () {
|
||||||
return new Promise(async (resolve, reject) => {
|
return new Promise<void>(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();
|
||||||
await this._bleDevice.discoverCharacteristicsForService(Consts.BLEService.WEDO2_SMART_HUB);
|
await this._bleDevice.discoverCharacteristicsForService(Consts.BLEService.WEDO2_SMART_HUB);
|
||||||
@ -99,11 +99,7 @@ export class WeDo2SmartHub extends BaseHub {
|
|||||||
* @returns {Promise} Resolved upon successful disconnect.
|
* @returns {Promise} Resolved upon successful disconnect.
|
||||||
*/
|
*/
|
||||||
public shutdown () {
|
public shutdown () {
|
||||||
return new Promise((resolve, reject) => {
|
return this.send(Buffer.from([0x00]), Consts.BLECharacteristic.WEDO2_DISCONNECT);
|
||||||
this.send(Buffer.from([0x00]), Consts.BLECharacteristic.WEDO2_DISCONNECT, () => {
|
|
||||||
return resolve();
|
|
||||||
});
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -128,11 +124,11 @@ export class WeDo2SmartHub extends BaseHub {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public send (message: Buffer, uuid: string, callback?: () => void) {
|
public send (message: Buffer, uuid: string) {
|
||||||
if (debug.enabled) {
|
if (debug.enabled) {
|
||||||
debug(`Sent Message (${this._getCharacteristicNameFromUUID(uuid)})`, message);
|
debug(`Sent Message (${this._getCharacteristicNameFromUUID(uuid)})`, message);
|
||||||
}
|
}
|
||||||
this._bleDevice.writeToCharacteristic(uuid, message, callback);
|
return this._bleDevice.writeToCharacteristic(uuid, message);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -7,19 +7,19 @@ export interface IBLEAbstraction extends EventEmitter {
|
|||||||
name: string;
|
name: string;
|
||||||
connecting: boolean;
|
connecting: boolean;
|
||||||
connected: boolean;
|
connected: boolean;
|
||||||
connect: () => Promise<any>;
|
connect: () => Promise<void>;
|
||||||
disconnect: () => Promise<any>;
|
disconnect: () => Promise<void>;
|
||||||
discoverCharacteristicsForService: (uuid: string) => Promise<any>;
|
discoverCharacteristicsForService: (uuid: string) => Promise<void>;
|
||||||
subscribeToCharacteristic: (uuid: string, callback: (data: Buffer) => void) => void;
|
subscribeToCharacteristic: (uuid: string, callback: (data: Buffer) => void) => void;
|
||||||
addToCharacteristicMailbox: (uuid: string, data: Buffer) => void;
|
addToCharacteristicMailbox: (uuid: string, data: Buffer) => void;
|
||||||
readFromCharacteristic: (uuid: string, callback: (err: string | null, data: Buffer | null) => void) => void;
|
readFromCharacteristic: (uuid: string, callback: (err: string | null, data: Buffer | null) => void) => void;
|
||||||
writeToCharacteristic: (uuid: string, data: Buffer, callback?: () => void) => void;
|
writeToCharacteristic: (uuid: string, data: Buffer) => Promise<void>;
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface IDeviceInterface extends EventEmitter {
|
export interface IDeviceInterface extends EventEmitter {
|
||||||
type: Consts.HubType;
|
type: Consts.HubType;
|
||||||
getPortNameForPortId: (portId: number) => string | undefined;
|
getPortNameForPortId: (portId: number) => string | undefined;
|
||||||
send: (message: Buffer, uuid: string, callback?: () => void) => void;
|
send: (message: Buffer, uuid: string) => Promise<void>;
|
||||||
subscribe: (portId: number, deviceType: number, mode: number) => void;
|
subscribe: (portId: number, deviceType: number, mode: number) => void;
|
||||||
isPortVirtual: (portId: number) => boolean;
|
isPortVirtual: (portId: number) => boolean;
|
||||||
sleep: (delay: number) => Promise<any>;
|
sleep: (delay: number) => Promise<any>;
|
||||||
|
@ -61,9 +61,13 @@ export class NobleDevice extends EventEmitter implements IBLEAbstraction {
|
|||||||
|
|
||||||
|
|
||||||
public connect () {
|
public connect () {
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise<void>((resolve, reject) => {
|
||||||
this._connecting = true;
|
this._connecting = true;
|
||||||
this._noblePeripheral.connect((err: string) => {
|
this._noblePeripheral.connect((err: string) => {
|
||||||
|
if(err) {
|
||||||
|
return reject(err);
|
||||||
|
}
|
||||||
|
|
||||||
this._connecting = false;
|
this._connecting = false;
|
||||||
this._connected = true;
|
this._connected = true;
|
||||||
return resolve();
|
return resolve();
|
||||||
@ -73,7 +77,7 @@ export class NobleDevice extends EventEmitter implements IBLEAbstraction {
|
|||||||
|
|
||||||
|
|
||||||
public disconnect () {
|
public disconnect () {
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise<void>((resolve) => {
|
||||||
this._noblePeripheral.disconnect();
|
this._noblePeripheral.disconnect();
|
||||||
return resolve();
|
return resolve();
|
||||||
});
|
});
|
||||||
@ -81,7 +85,7 @@ export class NobleDevice extends EventEmitter implements IBLEAbstraction {
|
|||||||
|
|
||||||
|
|
||||||
public discoverCharacteristicsForService (uuid: string) {
|
public discoverCharacteristicsForService (uuid: string) {
|
||||||
return new Promise(async (discoverResolve, discoverReject) => {
|
return new Promise<void>(async (discoverResolve, discoverReject) => {
|
||||||
uuid = this._sanitizeUUID(uuid);
|
uuid = this._sanitizeUUID(uuid);
|
||||||
this._noblePeripheral.discoverServices([uuid], (err: string, services: Service[]) => {
|
this._noblePeripheral.discoverServices([uuid], (err: string, services: Service[]) => {
|
||||||
if (err) {
|
if (err) {
|
||||||
@ -135,9 +139,17 @@ export class NobleDevice extends EventEmitter implements IBLEAbstraction {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public writeToCharacteristic (uuid: string, data: Buffer, callback?: () => void) {
|
public writeToCharacteristic (uuid: string, data: Buffer) {
|
||||||
|
return new Promise<void>((resolve, reject) => {
|
||||||
uuid = this._sanitizeUUID(uuid);
|
uuid = this._sanitizeUUID(uuid);
|
||||||
this._characteristics[uuid].write(data, false, callback);
|
this._characteristics[uuid].write(data, false, (error) => {
|
||||||
|
if(error) {
|
||||||
|
return reject(error);
|
||||||
|
}
|
||||||
|
|
||||||
|
return resolve();
|
||||||
|
});
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -152,17 +152,17 @@ export class PoweredUP extends EventEmitter {
|
|||||||
|
|
||||||
let hub: BaseHub;
|
let hub: BaseHub;
|
||||||
|
|
||||||
if (await WeDo2SmartHub.IsWeDo2SmartHub(peripheral)) {
|
if (WeDo2SmartHub.IsWeDo2SmartHub(peripheral)) {
|
||||||
hub = new WeDo2SmartHub(device);
|
hub = new WeDo2SmartHub(device);
|
||||||
} else if (await MoveHub.IsMoveHub(peripheral)) {
|
} else if (MoveHub.IsMoveHub(peripheral)) {
|
||||||
hub = new MoveHub(device);
|
hub = new MoveHub(device);
|
||||||
} else if (await Hub.IsHub(peripheral)) {
|
} else if (Hub.IsHub(peripheral)) {
|
||||||
hub = new Hub(device);
|
hub = new Hub(device);
|
||||||
} else if (await RemoteControl.IsRemoteControl(peripheral)) {
|
} else if (RemoteControl.IsRemoteControl(peripheral)) {
|
||||||
hub = new RemoteControl(device);
|
hub = new RemoteControl(device);
|
||||||
} else if (await DuploTrainBase.IsDuploTrainBase(peripheral)) {
|
} else if (DuploTrainBase.IsDuploTrainBase(peripheral)) {
|
||||||
hub = new DuploTrainBase(device);
|
hub = new DuploTrainBase(device);
|
||||||
} else if (await TechnicMediumHub.IsTechnicMediumHub(peripheral)) {
|
} else if (TechnicMediumHub.IsTechnicMediumHub(peripheral)) {
|
||||||
hub = new TechnicMediumHub(device);
|
hub = new TechnicMediumHub(device);
|
||||||
} else {
|
} else {
|
||||||
return;
|
return;
|
||||||
|
@ -58,7 +58,7 @@ export class WebBLEDevice extends EventEmitter implements IBLEAbstraction {
|
|||||||
|
|
||||||
|
|
||||||
public connect () {
|
public connect () {
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise<void>((resolve, reject) => {
|
||||||
this._connected = true;
|
this._connected = true;
|
||||||
return resolve();
|
return resolve();
|
||||||
});
|
});
|
||||||
@ -66,29 +66,21 @@ export class WebBLEDevice extends EventEmitter implements IBLEAbstraction {
|
|||||||
|
|
||||||
|
|
||||||
public disconnect () {
|
public disconnect () {
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise<void>((resolve, reject) => {
|
||||||
this._webBLEServer.device.gatt.disconnect();
|
this._webBLEServer.device.gatt.disconnect();
|
||||||
return resolve();
|
return resolve();
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public discoverCharacteristicsForService (uuid: string) {
|
public async discoverCharacteristicsForService (uuid: string) {
|
||||||
return new Promise(async (discoverResolve, discoverReject) => {
|
|
||||||
debug("Service/characteristic discovery started");
|
debug("Service/characteristic discovery started");
|
||||||
let service;
|
const service = await this._webBLEServer.getPrimaryService(uuid);
|
||||||
try {
|
|
||||||
service = await this._webBLEServer.getPrimaryService(uuid);
|
|
||||||
} catch (err) {
|
|
||||||
return discoverReject(err);
|
|
||||||
}
|
|
||||||
const characteristics = await service.getCharacteristics();
|
const characteristics = await service.getCharacteristics();
|
||||||
for (const characteristic of characteristics) {
|
for (const characteristic of characteristics) {
|
||||||
this._characteristics[characteristic.uuid] = characteristic;
|
this._characteristics[characteristic.uuid] = characteristic;
|
||||||
}
|
}
|
||||||
debug("Service/characteristic discovery finished");
|
debug("Service/characteristic discovery finished");
|
||||||
return discoverResolve();
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -134,12 +126,8 @@ export class WebBLEDevice extends EventEmitter implements IBLEAbstraction {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public writeToCharacteristic (uuid: string, data: Buffer, callback?: () => void) {
|
public writeToCharacteristic (uuid: string, data: Buffer) {
|
||||||
this._queue = this._queue.then(() => this._characteristics[uuid].writeValue(data)).then(() => {
|
return this._queue = this._queue.then(() => this._characteristics[uuid].writeValue(data));
|
||||||
if (callback) {
|
|
||||||
callback();
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user