Reverted accidental formatting changes to wedo2smarthub.ts
This commit is contained in:
parent
e5bba46b80
commit
a1026547cf
@ -10,132 +10,91 @@ import { IBLEDevice } from "./interfaces";
|
|||||||
import { isWebBluetooth } from "./utils";
|
import { isWebBluetooth } from "./utils";
|
||||||
const debug = Debug("wedo2smarthub");
|
const debug = Debug("wedo2smarthub");
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The WeDo2SmartHub is emitted if the discovered device is a WeDo 2.0 Smart Hub.
|
* The WeDo2SmartHub is emitted if the discovered device is a WeDo 2.0 Smart Hub.
|
||||||
* @class WeDo2SmartHub
|
* @class WeDo2SmartHub
|
||||||
* @extends Hub
|
* @extends Hub
|
||||||
*/
|
*/
|
||||||
export class WeDo2SmartHub extends Hub {
|
export class WeDo2SmartHub extends Hub {
|
||||||
public static IsWeDo2SmartHub(peripheral: Peripheral) {
|
|
||||||
return (
|
|
||||||
peripheral.advertisement &&
|
public static IsWeDo2SmartHub (peripheral: Peripheral) {
|
||||||
|
return (peripheral.advertisement &&
|
||||||
peripheral.advertisement.serviceUuids &&
|
peripheral.advertisement.serviceUuids &&
|
||||||
peripheral.advertisement.serviceUuids.indexOf(
|
peripheral.advertisement.serviceUuids.indexOf(Consts.BLEService.WEDO2_SMART_HUB.replace(/-/g, "")) >= 0);
|
||||||
Consts.BLEService.WEDO2_SMART_HUB.replace(/-/g, "")
|
|
||||||
) >= 0
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
private _lastTiltX: number = 0;
|
private _lastTiltX: number = 0;
|
||||||
private _lastTiltY: number = 0;
|
private _lastTiltY: number = 0;
|
||||||
|
|
||||||
constructor(device: IBLEDevice, autoSubscribe: boolean = true) {
|
|
||||||
|
constructor (device: IBLEDevice, autoSubscribe: boolean = true) {
|
||||||
super(device, autoSubscribe);
|
super(device, autoSubscribe);
|
||||||
this.type = Consts.HubType.WEDO2_SMART_HUB;
|
this.type = Consts.HubType.WEDO2_SMART_HUB;
|
||||||
this._ports = {
|
this._ports = {
|
||||||
A: new Port("A", 1),
|
"A": new Port("A", 1),
|
||||||
B: new Port("B", 2)
|
"B": new Port("B", 2)
|
||||||
};
|
};
|
||||||
debug("Discovered WeDo 2.0 Smart Hub");
|
debug("Discovered WeDo 2.0 Smart Hub");
|
||||||
}
|
}
|
||||||
|
|
||||||
public connect() {
|
|
||||||
|
public connect () {
|
||||||
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();
|
||||||
await this._bleDevice.discoverCharacteristicsForService(
|
await this._bleDevice.discoverCharacteristicsForService(Consts.BLEService.WEDO2_SMART_HUB);
|
||||||
Consts.BLEService.WEDO2_SMART_HUB
|
await this._bleDevice.discoverCharacteristicsForService(Consts.BLEService.WEDO2_SMART_HUB_2);
|
||||||
);
|
|
||||||
await this._bleDevice.discoverCharacteristicsForService(
|
|
||||||
Consts.BLEService.WEDO2_SMART_HUB_2
|
|
||||||
);
|
|
||||||
if (!isWebBluetooth) {
|
if (!isWebBluetooth) {
|
||||||
await this._bleDevice.discoverCharacteristicsForService(
|
await this._bleDevice.discoverCharacteristicsForService(Consts.BLEService.WEDO2_SMART_HUB_3);
|
||||||
Consts.BLEService.WEDO2_SMART_HUB_3
|
await this._bleDevice.discoverCharacteristicsForService(Consts.BLEService.WEDO2_SMART_HUB_4);
|
||||||
);
|
await this._bleDevice.discoverCharacteristicsForService(Consts.BLEService.WEDO2_SMART_HUB_5);
|
||||||
await this._bleDevice.discoverCharacteristicsForService(
|
|
||||||
Consts.BLEService.WEDO2_SMART_HUB_4
|
|
||||||
);
|
|
||||||
await this._bleDevice.discoverCharacteristicsForService(
|
|
||||||
Consts.BLEService.WEDO2_SMART_HUB_5
|
|
||||||
);
|
|
||||||
} else {
|
} else {
|
||||||
await this._bleDevice.discoverCharacteristicsForService(
|
await this._bleDevice.discoverCharacteristicsForService("battery_service");
|
||||||
"battery_service"
|
await this._bleDevice.discoverCharacteristicsForService("device_information");
|
||||||
);
|
|
||||||
await this._bleDevice.discoverCharacteristicsForService(
|
|
||||||
"device_information"
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
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");
|
debug("Connect completed");
|
||||||
this.emit("connect");
|
this.emit("connect");
|
||||||
resolve();
|
resolve();
|
||||||
this._bleDevice.subscribeToCharacteristic(
|
this._bleDevice.subscribeToCharacteristic(Consts.BLECharacteristic.WEDO2_PORT_TYPE, this._parsePortMessage.bind(this));
|
||||||
Consts.BLECharacteristic.WEDO2_PORT_TYPE,
|
this._bleDevice.subscribeToCharacteristic(Consts.BLECharacteristic.WEDO2_SENSOR_VALUE, this._parseSensorMessage.bind(this));
|
||||||
this._parsePortMessage.bind(this)
|
this._bleDevice.subscribeToCharacteristic(Consts.BLECharacteristic.WEDO2_BUTTON, this._parseSensorMessage.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)
|
|
||||||
);
|
|
||||||
if (!isWebBluetooth) {
|
if (!isWebBluetooth) {
|
||||||
this._bleDevice.subscribeToCharacteristic(
|
this._bleDevice.subscribeToCharacteristic(Consts.BLECharacteristic.WEDO2_BATTERY, this._parseBatteryMessage.bind(this));
|
||||||
Consts.BLECharacteristic.WEDO2_BATTERY,
|
this._bleDevice.readFromCharacteristic(Consts.BLECharacteristic.WEDO2_BATTERY, (err, data) => {
|
||||||
this._parseBatteryMessage.bind(this)
|
|
||||||
);
|
|
||||||
this._bleDevice.readFromCharacteristic(
|
|
||||||
Consts.BLECharacteristic.WEDO2_BATTERY,
|
|
||||||
(err, data) => {
|
|
||||||
if (data) {
|
if (data) {
|
||||||
this._parseBatteryMessage(data);
|
this._parseBatteryMessage(data);
|
||||||
}
|
}
|
||||||
}
|
});
|
||||||
);
|
|
||||||
} else {
|
} else {
|
||||||
this._bleDevice.readFromCharacteristic(
|
this._bleDevice.readFromCharacteristic("00002a19-0000-1000-8000-00805f9b34fb", (err, data) => {
|
||||||
"00002a19-0000-1000-8000-00805f9b34fb",
|
|
||||||
(err, data) => {
|
|
||||||
if (data) {
|
if (data) {
|
||||||
this._parseBatteryMessage(data);
|
this._parseBatteryMessage(data);
|
||||||
}
|
}
|
||||||
|
});
|
||||||
|
this._bleDevice.subscribeToCharacteristic("00002a19-0000-1000-8000-00805f9b34fb", this._parseHighCurrentAlert.bind(this));
|
||||||
}
|
}
|
||||||
);
|
this._bleDevice.subscribeToCharacteristic(Consts.BLECharacteristic.WEDO2_HIGH_CURRENT_ALERT, this._parseHighCurrentAlert.bind(this));
|
||||||
this._bleDevice.subscribeToCharacteristic(
|
|
||||||
"00002a19-0000-1000-8000-00805f9b34fb",
|
|
||||||
this._parseHighCurrentAlert.bind(this)
|
|
||||||
);
|
|
||||||
}
|
|
||||||
this._bleDevice.subscribeToCharacteristic(
|
|
||||||
Consts.BLECharacteristic.WEDO2_HIGH_CURRENT_ALERT,
|
|
||||||
this._parseHighCurrentAlert.bind(this)
|
|
||||||
);
|
|
||||||
if (!isWebBluetooth) {
|
if (!isWebBluetooth) {
|
||||||
this._bleDevice.readFromCharacteristic(
|
this._bleDevice.readFromCharacteristic(Consts.BLECharacteristic.WEDO2_FIRMWARE_REVISION, (err, data) => {
|
||||||
Consts.BLECharacteristic.WEDO2_FIRMWARE_REVISION,
|
|
||||||
(err, data) => {
|
|
||||||
if (data) {
|
if (data) {
|
||||||
this._parseFirmwareRevisionString(data);
|
this._parseFirmwareRevisionString(data);
|
||||||
}
|
}
|
||||||
}
|
});
|
||||||
);
|
|
||||||
} else {
|
} else {
|
||||||
this._bleDevice.readFromCharacteristic(
|
this._bleDevice.readFromCharacteristic("00002a26-0000-1000-8000-00805f9b34fb", (err, data) => {
|
||||||
"00002a26-0000-1000-8000-00805f9b34fb",
|
|
||||||
(err, data) => {
|
|
||||||
if (data) {
|
if (data) {
|
||||||
this._parseFirmwareRevisionString(data);
|
this._parseFirmwareRevisionString(data);
|
||||||
}
|
}
|
||||||
}
|
|
||||||
);
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Set the name of the Hub.
|
* Set the name of the Hub.
|
||||||
@ -143,7 +102,7 @@ export class WeDo2SmartHub extends Hub {
|
|||||||
* @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 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");
|
||||||
}
|
}
|
||||||
@ -157,13 +116,14 @@ export class WeDo2SmartHub extends Hub {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Set the color of the LED on the Hub via a color value.
|
* Set the color of the LED on the Hub via a color value.
|
||||||
* @method WeDo2SmartHub#setLEDColor
|
* @method WeDo2SmartHub#setLEDColor
|
||||||
* @param {Color} color
|
* @param {Color} color
|
||||||
* @returns {Promise} Resolved upon successful issuance of command.
|
* @returns {Promise} Resolved upon successful issuance of command.
|
||||||
*/
|
*/
|
||||||
public setLEDColor(color: number | boolean) {
|
public setLEDColor (color: number | boolean) {
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
let data = Buffer.from([0x06, 0x17, 0x01, 0x01]);
|
let data = Buffer.from([0x06, 0x17, 0x01, 0x01]);
|
||||||
this._writeMessage(Consts.BLECharacteristic.WEDO2_PORT_TYPE_WRITE, data);
|
this._writeMessage(Consts.BLECharacteristic.WEDO2_PORT_TYPE_WRITE, data);
|
||||||
@ -171,31 +131,26 @@ export class WeDo2SmartHub extends Hub {
|
|||||||
color = 0;
|
color = 0;
|
||||||
}
|
}
|
||||||
data = Buffer.from([0x06, 0x04, 0x01, color]);
|
data = Buffer.from([0x06, 0x04, 0x01, color]);
|
||||||
this._writeMessage(
|
this._writeMessage(Consts.BLECharacteristic.WEDO2_MOTOR_VALUE_WRITE, data);
|
||||||
Consts.BLECharacteristic.WEDO2_MOTOR_VALUE_WRITE,
|
|
||||||
data
|
|
||||||
);
|
|
||||||
return resolve();
|
return resolve();
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Shutdown the Hub.
|
* Shutdown the Hub.
|
||||||
* @method WeDo2SmartHub#shutdown
|
* @method WeDo2SmartHub#shutdown
|
||||||
* @returns {Promise} Resolved upon successful disconnect.
|
* @returns {Promise} Resolved upon successful disconnect.
|
||||||
*/
|
*/
|
||||||
public shutdown() {
|
public shutdown () {
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
this._writeMessage(
|
this._writeMessage(Consts.BLECharacteristic.WEDO2_DISCONNECT, Buffer.from([0x00]), () => {
|
||||||
Consts.BLECharacteristic.WEDO2_DISCONNECT,
|
|
||||||
Buffer.from([0x00]),
|
|
||||||
() => {
|
|
||||||
return resolve();
|
return resolve();
|
||||||
}
|
});
|
||||||
);
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Set the color of the LED on the Hub via RGB values.
|
* Set the color of the LED on the Hub via RGB values.
|
||||||
* @method WeDo2SmartHub#setLEDRGB
|
* @method WeDo2SmartHub#setLEDRGB
|
||||||
@ -204,19 +159,17 @@ export class WeDo2SmartHub extends Hub {
|
|||||||
* @param {number} blue
|
* @param {number} blue
|
||||||
* @returns {Promise} Resolved upon successful issuance of command.
|
* @returns {Promise} Resolved upon successful issuance of command.
|
||||||
*/
|
*/
|
||||||
public setLEDRGB(red: number, green: number, blue: number) {
|
public setLEDRGB (red: number, green: number, blue: number) {
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
let data = Buffer.from([0x06, 0x17, 0x01, 0x02]);
|
let data = Buffer.from([0x06, 0x17, 0x01, 0x02]);
|
||||||
this._writeMessage(Consts.BLECharacteristic.WEDO2_PORT_TYPE_WRITE, data);
|
this._writeMessage(Consts.BLECharacteristic.WEDO2_PORT_TYPE_WRITE, data);
|
||||||
data = Buffer.from([0x06, 0x04, 0x03, red, green, blue]);
|
data = Buffer.from([0x06, 0x04, 0x03, red, green, blue]);
|
||||||
this._writeMessage(
|
this._writeMessage(Consts.BLECharacteristic.WEDO2_MOTOR_VALUE_WRITE, data);
|
||||||
Consts.BLECharacteristic.WEDO2_MOTOR_VALUE_WRITE,
|
|
||||||
data
|
|
||||||
);
|
|
||||||
return resolve();
|
return resolve();
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Set the motor speed on a given port.
|
* Set the motor speed on a given port.
|
||||||
* @method WeDo2SmartHub#setMotorSpeed
|
* @method WeDo2SmartHub#setMotorSpeed
|
||||||
@ -225,7 +178,7 @@ export class WeDo2SmartHub extends Hub {
|
|||||||
* @param {number} [time] How long to activate the motor for (in milliseconds). Leave empty to turn the motor on indefinitely.
|
* @param {number} [time] How long to activate the motor for (in milliseconds). Leave empty to turn the motor on indefinitely.
|
||||||
* @returns {Promise} Resolved upon successful completion of command. If time is specified, this is once the motor is finished.
|
* @returns {Promise} Resolved upon successful completion of command. If time is specified, this is once the motor is finished.
|
||||||
*/
|
*/
|
||||||
public setMotorSpeed(port: string, speed: number, time?: number | boolean) {
|
public setMotorSpeed (port: string, speed: number, time?: number | boolean) {
|
||||||
const portObj = this._portLookup(port);
|
const portObj = this._portLookup(port);
|
||||||
let cancelEventTimer = true;
|
let cancelEventTimer = true;
|
||||||
if (typeof time === "boolean") {
|
if (typeof time === "boolean") {
|
||||||
@ -238,16 +191,10 @@ export class WeDo2SmartHub extends Hub {
|
|||||||
portObj.cancelEventTimer();
|
portObj.cancelEventTimer();
|
||||||
}
|
}
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
this._writeMessage(
|
this._writeMessage(Consts.BLECharacteristic.WEDO2_MOTOR_VALUE_WRITE, Buffer.from([portObj.value, 0x01, 0x02, this._mapSpeed(speed)]));
|
||||||
Consts.BLECharacteristic.WEDO2_MOTOR_VALUE_WRITE,
|
|
||||||
Buffer.from([portObj.value, 0x01, 0x02, this._mapSpeed(speed)])
|
|
||||||
);
|
|
||||||
if (time && typeof time === "number") {
|
if (time && typeof time === "number") {
|
||||||
const timeout = global.setTimeout(() => {
|
const timeout = global.setTimeout(() => {
|
||||||
this._writeMessage(
|
this._writeMessage(Consts.BLECharacteristic.WEDO2_MOTOR_VALUE_WRITE, Buffer.from([portObj.value, 0x01, 0x02, 0x00]));
|
||||||
Consts.BLECharacteristic.WEDO2_MOTOR_VALUE_WRITE,
|
|
||||||
Buffer.from([portObj.value, 0x01, 0x02, 0x00])
|
|
||||||
);
|
|
||||||
return resolve();
|
return resolve();
|
||||||
}, time);
|
}, time);
|
||||||
portObj.setEventTimer(timeout);
|
portObj.setEventTimer(timeout);
|
||||||
@ -257,6 +204,7 @@ export class WeDo2SmartHub extends Hub {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Ramp the motor speed on a given port.
|
* Ramp the motor speed on a given port.
|
||||||
* @method WeDo2SmartHub#rampMotorSpeed
|
* @method WeDo2SmartHub#rampMotorSpeed
|
||||||
@ -266,12 +214,7 @@ export class WeDo2SmartHub extends Hub {
|
|||||||
* @param {number} time How long the ramp should last (in milliseconds).
|
* @param {number} time How long the ramp should last (in milliseconds).
|
||||||
* @returns {Promise} Resolved upon successful completion of command.
|
* @returns {Promise} Resolved upon successful completion of command.
|
||||||
*/
|
*/
|
||||||
public rampMotorSpeed(
|
public rampMotorSpeed (port: string, fromSpeed: number, toSpeed: number, time: number) {
|
||||||
port: string,
|
|
||||||
fromSpeed: number,
|
|
||||||
toSpeed: number,
|
|
||||||
time: number
|
|
||||||
) {
|
|
||||||
const portObj = this._portLookup(port);
|
const portObj = this._portLookup(port);
|
||||||
portObj.cancelEventTimer();
|
portObj.cancelEventTimer();
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
@ -283,16 +226,18 @@ export class WeDo2SmartHub extends Hub {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Fully (hard) stop the motor on a given port.
|
* Fully (hard) stop the motor on a given port.
|
||||||
* @method WeDo2SmartHub#brakeMotor
|
* @method WeDo2SmartHub#brakeMotor
|
||||||
* @param {string} port
|
* @param {string} port
|
||||||
* @returns {Promise} Resolved upon successful completion of command.
|
* @returns {Promise} Resolved upon successful completion of command.
|
||||||
*/
|
*/
|
||||||
public brakeMotor(port: string) {
|
public brakeMotor (port: string) {
|
||||||
return this.setMotorSpeed(port, 127);
|
return this.setMotorSpeed(port, 127);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Play a tone on the Hub's in-built buzzer
|
* Play a tone on the Hub's in-built buzzer
|
||||||
* @method WeDo2SmartHub#playTone
|
* @method WeDo2SmartHub#playTone
|
||||||
@ -300,19 +245,17 @@ export class WeDo2SmartHub extends Hub {
|
|||||||
* @param {number} time How long the tone should play for (in milliseconds).
|
* @param {number} time How long the tone should play for (in milliseconds).
|
||||||
* @returns {Promise} Resolved upon successful completion of command (ie. once the tone has finished playing).
|
* @returns {Promise} Resolved upon successful completion of command (ie. once the tone has finished playing).
|
||||||
*/
|
*/
|
||||||
public playTone(frequency: number, time: number) {
|
public playTone (frequency: number, time: number) {
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
const data = Buffer.from([0x05, 0x02, 0x04, 0x00, 0x00, 0x00, 0x00]);
|
const data = Buffer.from([0x05, 0x02, 0x04, 0x00, 0x00, 0x00, 0x00]);
|
||||||
data.writeUInt16LE(frequency, 3);
|
data.writeUInt16LE(frequency, 3);
|
||||||
data.writeUInt16LE(time, 5);
|
data.writeUInt16LE(time, 5);
|
||||||
this._writeMessage(
|
this._writeMessage(Consts.BLECharacteristic.WEDO2_MOTOR_VALUE_WRITE, data);
|
||||||
Consts.BLECharacteristic.WEDO2_MOTOR_VALUE_WRITE,
|
|
||||||
data
|
|
||||||
);
|
|
||||||
global.setTimeout(resolve, time);
|
global.setTimeout(resolve, time);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Set the light brightness on a given port.
|
* Set the light brightness on a given port.
|
||||||
* @method WeDo2SmartHub#setLightBrightness
|
* @method WeDo2SmartHub#setLightBrightness
|
||||||
@ -321,22 +264,16 @@ export class WeDo2SmartHub extends Hub {
|
|||||||
* @param {number} [time] How long to turn the light on (in milliseconds). Leave empty to turn the light on indefinitely.
|
* @param {number} [time] How long to turn the light on (in milliseconds). Leave empty to turn the light on indefinitely.
|
||||||
* @returns {Promise} Resolved upon successful completion of command. If time is specified, this is once the light is turned off.
|
* @returns {Promise} Resolved upon successful completion of command. If time is specified, this is once the light is turned off.
|
||||||
*/
|
*/
|
||||||
public setLightBrightness(port: string, brightness: number, time?: number) {
|
public setLightBrightness (port: string, brightness: number, time?: number) {
|
||||||
const portObj = this._portLookup(port);
|
const portObj = this._portLookup(port);
|
||||||
portObj.cancelEventTimer();
|
portObj.cancelEventTimer();
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
const data = Buffer.from([portObj.value, 0x01, 0x02, brightness]);
|
const data = Buffer.from([portObj.value, 0x01, 0x02, brightness]);
|
||||||
this._writeMessage(
|
this._writeMessage(Consts.BLECharacteristic.WEDO2_MOTOR_VALUE_WRITE, data);
|
||||||
Consts.BLECharacteristic.WEDO2_MOTOR_VALUE_WRITE,
|
|
||||||
data
|
|
||||||
);
|
|
||||||
if (time) {
|
if (time) {
|
||||||
const timeout = global.setTimeout(() => {
|
const timeout = global.setTimeout(() => {
|
||||||
const data = Buffer.from([portObj.value, 0x01, 0x02, 0x00]);
|
const data = Buffer.from([portObj.value, 0x01, 0x02, 0x00]);
|
||||||
this._writeMessage(
|
this._writeMessage(Consts.BLECharacteristic.WEDO2_MOTOR_VALUE_WRITE, data);
|
||||||
Consts.BLECharacteristic.WEDO2_MOTOR_VALUE_WRITE,
|
|
||||||
data
|
|
||||||
);
|
|
||||||
return resolve();
|
return resolve();
|
||||||
}, time);
|
}, time);
|
||||||
portObj.setEventTimer(timeout);
|
portObj.setEventTimer(timeout);
|
||||||
@ -346,10 +283,8 @@ export class WeDo2SmartHub extends Hub {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
public sendRaw(
|
|
||||||
message: Buffer,
|
public sendRaw (message: Buffer, characteristic: string = Consts.BLECharacteristic.WEDO2_MOTOR_VALUE_WRITE) {
|
||||||
characteristic: string = Consts.BLECharacteristic.WEDO2_MOTOR_VALUE_WRITE
|
|
||||||
) {
|
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
this._writeMessage(characteristic, message, () => {
|
this._writeMessage(characteristic, message, () => {
|
||||||
return resolve();
|
return resolve();
|
||||||
@ -357,69 +292,26 @@ export class WeDo2SmartHub extends Hub {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
protected _activatePortDevice(
|
|
||||||
port: number,
|
protected _activatePortDevice (port: number, type: number, mode: number, format: number, callback?: () => void) {
|
||||||
type: number,
|
this._writeMessage(Consts.BLECharacteristic.WEDO2_PORT_TYPE_WRITE, Buffer.from([0x01, 0x02, port, type, mode, 0x01, 0x00, 0x00, 0x00, format, 0x01]), callback);
|
||||||
mode: number,
|
|
||||||
format: number,
|
|
||||||
callback?: () => void
|
|
||||||
) {
|
|
||||||
this._writeMessage(
|
|
||||||
Consts.BLECharacteristic.WEDO2_PORT_TYPE_WRITE,
|
|
||||||
Buffer.from([
|
|
||||||
0x01,
|
|
||||||
0x02,
|
|
||||||
port,
|
|
||||||
type,
|
|
||||||
mode,
|
|
||||||
0x01,
|
|
||||||
0x00,
|
|
||||||
0x00,
|
|
||||||
0x00,
|
|
||||||
format,
|
|
||||||
0x01
|
|
||||||
]),
|
|
||||||
callback
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
protected _deactivatePortDevice(
|
|
||||||
port: number,
|
protected _deactivatePortDevice (port: number, type: number, mode: number, format: number, callback?: () => void) {
|
||||||
type: number,
|
this._writeMessage(Consts.BLECharacteristic.WEDO2_PORT_TYPE_WRITE, Buffer.from([0x01, 0x02, port, type, mode, 0x01, 0x00, 0x00, 0x00, format, 0x00]), callback);
|
||||||
mode: number,
|
|
||||||
format: number,
|
|
||||||
callback?: () => void
|
|
||||||
) {
|
|
||||||
this._writeMessage(
|
|
||||||
Consts.BLECharacteristic.WEDO2_PORT_TYPE_WRITE,
|
|
||||||
Buffer.from([
|
|
||||||
0x01,
|
|
||||||
0x02,
|
|
||||||
port,
|
|
||||||
type,
|
|
||||||
mode,
|
|
||||||
0x01,
|
|
||||||
0x00,
|
|
||||||
0x00,
|
|
||||||
0x00,
|
|
||||||
format,
|
|
||||||
0x00
|
|
||||||
]),
|
|
||||||
callback
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private _writeMessage(uuid: string, message: Buffer, callback?: () => void) {
|
|
||||||
|
private _writeMessage (uuid: string, message: Buffer, callback?: () => void) {
|
||||||
if (debug.enabled) {
|
if (debug.enabled) {
|
||||||
debug(
|
debug(`Sent Message (${this._getCharacteristicNameFromUUID(uuid)})`, message);
|
||||||
`Sent Message (${this._getCharacteristicNameFromUUID(uuid)})`,
|
|
||||||
message
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
this._bleDevice.writeToCharacteristic(uuid, message, callback);
|
this._bleDevice.writeToCharacteristic(uuid, message, callback);
|
||||||
}
|
}
|
||||||
|
|
||||||
private _getCharacteristicNameFromUUID(uuid: string) {
|
|
||||||
|
private _getCharacteristicNameFromUUID (uuid: string) {
|
||||||
const keys = Object.keys(Consts.BLECharacteristic);
|
const keys = Object.keys(Consts.BLECharacteristic);
|
||||||
for (let i = 0; i < keys.length; i++) {
|
for (let i = 0; i < keys.length; i++) {
|
||||||
const key = keys[i];
|
const key = keys[i];
|
||||||
@ -430,27 +322,27 @@ export class WeDo2SmartHub extends Hub {
|
|||||||
return "UNKNOWN";
|
return "UNKNOWN";
|
||||||
}
|
}
|
||||||
|
|
||||||
private _parseHighCurrentAlert(data: Buffer) {
|
|
||||||
|
private _parseHighCurrentAlert (data: Buffer) {
|
||||||
debug("Received Message (WEDO2_HIGH_CURRENT_ALERT)", data);
|
debug("Received Message (WEDO2_HIGH_CURRENT_ALERT)", data);
|
||||||
}
|
}
|
||||||
|
|
||||||
private _parseBatteryMessage(data: Buffer) {
|
|
||||||
|
private _parseBatteryMessage (data: Buffer) {
|
||||||
debug("Received Message (WEDO2_BATTERY)", data);
|
debug("Received Message (WEDO2_BATTERY)", data);
|
||||||
this._batteryLevel = data[0];
|
this._batteryLevel = data[0];
|
||||||
}
|
}
|
||||||
|
|
||||||
private _parseFirmwareRevisionString(data: Buffer) {
|
|
||||||
|
private _parseFirmwareRevisionString (data: Buffer) {
|
||||||
debug("Received Message (WEDO2_FIRMWARE_REVISION)", data);
|
debug("Received Message (WEDO2_FIRMWARE_REVISION)", data);
|
||||||
const parts = data.toString().split(".");
|
const parts = data.toString().split(".");
|
||||||
this._firmwareInfo = {
|
this._firmwareInfo = { major: parseInt(parts[0], 10), minor: parseInt(parts[1], 10), bugFix: parseInt(parts[2], 10), build: parseInt(parts[3], 10) };
|
||||||
major: parseInt(parts[0], 10),
|
|
||||||
minor: parseInt(parts[1], 10),
|
|
||||||
bugFix: parseInt(parts[2], 10),
|
|
||||||
build: parseInt(parts[3], 10)
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private _parsePortMessage(data: Buffer) {
|
|
||||||
|
private _parsePortMessage (data: Buffer) {
|
||||||
|
|
||||||
debug("Received Message (WEDO2_PORT_TYPE)", data);
|
debug("Received Message (WEDO2_PORT_TYPE)", data);
|
||||||
|
|
||||||
const port = this._getPortForPortNumber(data[0]);
|
const port = this._getPortForPortNumber(data[0]);
|
||||||
@ -461,9 +353,12 @@ export class WeDo2SmartHub extends Hub {
|
|||||||
|
|
||||||
port.connected = data[1] === 1 ? true : false;
|
port.connected = data[1] === 1 ? true : false;
|
||||||
this._registerDeviceAttachment(port, data[3]);
|
this._registerDeviceAttachment(port, data[3]);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private _parseSensorMessage(data: Buffer) {
|
|
||||||
|
private _parseSensorMessage (data: Buffer) {
|
||||||
|
|
||||||
debug("Received Message (WEDO2_SENSOR_VALUE)", data);
|
debug("Received Message (WEDO2_SENSOR_VALUE)", data);
|
||||||
|
|
||||||
if (data[0] === 0x01) {
|
if (data[0] === 0x01) {
|
||||||
@ -565,5 +460,8 @@ export class WeDo2SmartHub extends Hub {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user