diff --git a/DOCS.md b/DOCS.md index aced8b9..25a5c5a 100644 --- a/DOCS.md +++ b/DOCS.md @@ -72,6 +72,11 @@ Emits when a LPF2 Hub device is found. * [LPF2Hub](#LPF2Hub) ⇐ [Hub](#Hub) * [new LPF2Hub()](#new_LPF2Hub_new) + * [.name](#Hub+name) + * [.uuid](#Hub+uuid) + * [.rssi](#Hub+rssi) + * [.batteryLevel](#Hub+batteryLevel) + * [.current](#Hub+current) * [.setLEDColor(color)](#LPF2Hub+setLEDColor) ⇒ Promise * [.setMotorSpeed(port, speed, [time])](#LPF2Hub+setMotorSpeed) ⇒ Promise * [.setMotorAngle(port, angle, [speed])](#LPF2Hub+setMotorAngle) ⇒ Promise @@ -94,6 +99,61 @@ Emits when a LPF2 Hub device is found. ### new LPF2Hub() The LPF2Hub is emitted if the discovered device is either a Boost Move Hub, Powered Up Hub, or Powered Up Remote. + + +### lpF2Hub.name +**Kind**: instance property of [LPF2Hub](#LPF2Hub) +**Read only**: true +**Properties** + +| Name | Type | Description | +| --- | --- | --- | +| name | string | Name of the hub | + + + +### lpF2Hub.uuid +**Kind**: instance property of [LPF2Hub](#LPF2Hub) +**Read only**: true +**Properties** + +| Name | Type | Description | +| --- | --- | --- | +| uuid | string | UUID of the hub | + + + +### lpF2Hub.rssi +**Kind**: instance property of [LPF2Hub](#LPF2Hub) +**Read only**: true +**Properties** + +| Name | Type | Description | +| --- | --- | --- | +| rssi | number | Signal strength of the hub | + + + +### lpF2Hub.batteryLevel +**Kind**: instance property of [LPF2Hub](#LPF2Hub) +**Read only**: true +**Properties** + +| Name | Type | Description | +| --- | --- | --- | +| batteryLevel | number | Battery level of the hub (Percentage between 0-100) | + + + +### lpF2Hub.current +**Kind**: instance property of [LPF2Hub](#LPF2Hub) +**Read only**: true +**Properties** + +| Name | Type | Description | +| --- | --- | --- | +| current | number | Current usage of the hub (Amps) | + ### lpF2Hub.setLEDColor(color) ⇒ Promise @@ -294,6 +354,11 @@ Emits when an attached motor or sensor is detached from the Hub. * [WeDo2Hub](#WeDo2Hub) ⇐ [Hub](#Hub) * [new WeDo2Hub()](#new_WeDo2Hub_new) + * [.name](#Hub+name) + * [.uuid](#Hub+uuid) + * [.rssi](#Hub+rssi) + * [.batteryLevel](#Hub+batteryLevel) + * [.current](#Hub+current) * [.setLEDColor(color)](#WeDo2Hub+setLEDColor) ⇒ Promise * [.setLEDRGB(red, green, blue)](#WeDo2Hub+setLEDRGB) ⇒ Promise * [.setMotorSpeed(port, speed, [time])](#WeDo2Hub+setMotorSpeed) ⇒ Promise @@ -317,6 +382,61 @@ Emits when an attached motor or sensor is detached from the Hub. ### new WeDo2Hub() The WeDo2Hub is emitted if the discovered device is a WeDo 2.0 Smart Hub. + + +### weDo2Hub.name +**Kind**: instance property of [WeDo2Hub](#WeDo2Hub) +**Read only**: true +**Properties** + +| Name | Type | Description | +| --- | --- | --- | +| name | string | Name of the hub | + + + +### weDo2Hub.uuid +**Kind**: instance property of [WeDo2Hub](#WeDo2Hub) +**Read only**: true +**Properties** + +| Name | Type | Description | +| --- | --- | --- | +| uuid | string | UUID of the hub | + + + +### weDo2Hub.rssi +**Kind**: instance property of [WeDo2Hub](#WeDo2Hub) +**Read only**: true +**Properties** + +| Name | Type | Description | +| --- | --- | --- | +| rssi | number | Signal strength of the hub | + + + +### weDo2Hub.batteryLevel +**Kind**: instance property of [WeDo2Hub](#WeDo2Hub) +**Read only**: true +**Properties** + +| Name | Type | Description | +| --- | --- | --- | +| batteryLevel | number | Battery level of the hub (Percentage between 0-100) | + + + +### weDo2Hub.current +**Kind**: instance property of [WeDo2Hub](#WeDo2Hub) +**Read only**: true +**Properties** + +| Name | Type | Description | +| --- | --- | --- | +| current | number | Current usage of the hub (Amps) | + ### weDo2Hub.setLEDColor(color) ⇒ Promise @@ -529,6 +649,11 @@ Emits when an attached motor or sensor is detached from the Hub. **Extends**: EventEmitter * [Hub](#Hub) ⇐ EventEmitter + * [.name](#Hub+name) + * [.uuid](#Hub+uuid) + * [.rssi](#Hub+rssi) + * [.batteryLevel](#Hub+batteryLevel) + * [.current](#Hub+current) * [.connect()](#Hub+connect) ⇒ Promise * [.disconnect()](#Hub+disconnect) ⇒ Promise * [.subscribe(port, [mode])](#Hub+subscribe) ⇒ Promise @@ -538,6 +663,61 @@ Emits when an attached motor or sensor is detached from the Hub. * ["attach" (port, type)](#Hub+event_attach) * ["detach" (port)](#Hub+event_detach) + + +### hub.name +**Kind**: instance property of [Hub](#Hub) +**Read only**: true +**Properties** + +| Name | Type | Description | +| --- | --- | --- | +| name | string | Name of the hub | + + + +### hub.uuid +**Kind**: instance property of [Hub](#Hub) +**Read only**: true +**Properties** + +| Name | Type | Description | +| --- | --- | --- | +| uuid | string | UUID of the hub | + + + +### hub.rssi +**Kind**: instance property of [Hub](#Hub) +**Read only**: true +**Properties** + +| Name | Type | Description | +| --- | --- | --- | +| rssi | number | Signal strength of the hub | + + + +### hub.batteryLevel +**Kind**: instance property of [Hub](#Hub) +**Read only**: true +**Properties** + +| Name | Type | Description | +| --- | --- | --- | +| batteryLevel | number | Battery level of the hub (Percentage between 0-100) | + + + +### hub.current +**Kind**: instance property of [Hub](#Hub) +**Read only**: true +**Properties** + +| Name | Type | Description | +| --- | --- | --- | +| current | number | Current usage of the hub (Amps) | + ### hub.connect() ⇒ Promise diff --git a/README.md b/README.md index 75d435f..89d3800 100644 --- a/README.md +++ b/README.md @@ -122,6 +122,11 @@ Emits when a LPF2 Hub device is found. * [LPF2Hub](#LPF2Hub) ⇐ [Hub](#Hub) * [new LPF2Hub()](#new_LPF2Hub_new) + * [.name](#Hub+name) + * [.uuid](#Hub+uuid) + * [.rssi](#Hub+rssi) + * [.batteryLevel](#Hub+batteryLevel) + * [.current](#Hub+current) * [.setLEDColor(color)](#LPF2Hub+setLEDColor) ⇒ Promise * [.setMotorSpeed(port, speed, [time])](#LPF2Hub+setMotorSpeed) ⇒ Promise * [.setMotorAngle(port, angle, [speed])](#LPF2Hub+setMotorAngle) ⇒ Promise @@ -144,6 +149,61 @@ Emits when a LPF2 Hub device is found. ### new LPF2Hub() The LPF2Hub is emitted if the discovered device is either a Boost Move Hub, Powered Up Hub, or Powered Up Remote. + + +### lpF2Hub.name +**Kind**: instance property of [LPF2Hub](#LPF2Hub) +**Read only**: true +**Properties** + +| Name | Type | Description | +| --- | --- | --- | +| name | string | Name of the hub | + + + +### lpF2Hub.uuid +**Kind**: instance property of [LPF2Hub](#LPF2Hub) +**Read only**: true +**Properties** + +| Name | Type | Description | +| --- | --- | --- | +| uuid | string | UUID of the hub | + + + +### lpF2Hub.rssi +**Kind**: instance property of [LPF2Hub](#LPF2Hub) +**Read only**: true +**Properties** + +| Name | Type | Description | +| --- | --- | --- | +| rssi | number | Signal strength of the hub | + + + +### lpF2Hub.batteryLevel +**Kind**: instance property of [LPF2Hub](#LPF2Hub) +**Read only**: true +**Properties** + +| Name | Type | Description | +| --- | --- | --- | +| batteryLevel | number | Battery level of the hub (Percentage between 0-100) | + + + +### lpF2Hub.current +**Kind**: instance property of [LPF2Hub](#LPF2Hub) +**Read only**: true +**Properties** + +| Name | Type | Description | +| --- | --- | --- | +| current | number | Current usage of the hub (Amps) | + ### lpF2Hub.setLEDColor(color) ⇒ Promise @@ -344,6 +404,11 @@ Emits when an attached motor or sensor is detached from the Hub. * [WeDo2Hub](#WeDo2Hub) ⇐ [Hub](#Hub) * [new WeDo2Hub()](#new_WeDo2Hub_new) + * [.name](#Hub+name) + * [.uuid](#Hub+uuid) + * [.rssi](#Hub+rssi) + * [.batteryLevel](#Hub+batteryLevel) + * [.current](#Hub+current) * [.setLEDColor(color)](#WeDo2Hub+setLEDColor) ⇒ Promise * [.setLEDRGB(red, green, blue)](#WeDo2Hub+setLEDRGB) ⇒ Promise * [.setMotorSpeed(port, speed, [time])](#WeDo2Hub+setMotorSpeed) ⇒ Promise @@ -367,6 +432,61 @@ Emits when an attached motor or sensor is detached from the Hub. ### new WeDo2Hub() The WeDo2Hub is emitted if the discovered device is a WeDo 2.0 Smart Hub. + + +### weDo2Hub.name +**Kind**: instance property of [WeDo2Hub](#WeDo2Hub) +**Read only**: true +**Properties** + +| Name | Type | Description | +| --- | --- | --- | +| name | string | Name of the hub | + + + +### weDo2Hub.uuid +**Kind**: instance property of [WeDo2Hub](#WeDo2Hub) +**Read only**: true +**Properties** + +| Name | Type | Description | +| --- | --- | --- | +| uuid | string | UUID of the hub | + + + +### weDo2Hub.rssi +**Kind**: instance property of [WeDo2Hub](#WeDo2Hub) +**Read only**: true +**Properties** + +| Name | Type | Description | +| --- | --- | --- | +| rssi | number | Signal strength of the hub | + + + +### weDo2Hub.batteryLevel +**Kind**: instance property of [WeDo2Hub](#WeDo2Hub) +**Read only**: true +**Properties** + +| Name | Type | Description | +| --- | --- | --- | +| batteryLevel | number | Battery level of the hub (Percentage between 0-100) | + + + +### weDo2Hub.current +**Kind**: instance property of [WeDo2Hub](#WeDo2Hub) +**Read only**: true +**Properties** + +| Name | Type | Description | +| --- | --- | --- | +| current | number | Current usage of the hub (Amps) | + ### weDo2Hub.setLEDColor(color) ⇒ Promise @@ -579,6 +699,11 @@ Emits when an attached motor or sensor is detached from the Hub. **Extends**: EventEmitter * [Hub](#Hub) ⇐ EventEmitter + * [.name](#Hub+name) + * [.uuid](#Hub+uuid) + * [.rssi](#Hub+rssi) + * [.batteryLevel](#Hub+batteryLevel) + * [.current](#Hub+current) * [.connect()](#Hub+connect) ⇒ Promise * [.disconnect()](#Hub+disconnect) ⇒ Promise * [.subscribe(port, [mode])](#Hub+subscribe) ⇒ Promise @@ -588,6 +713,61 @@ Emits when an attached motor or sensor is detached from the Hub. * ["attach" (port, type)](#Hub+event_attach) * ["detach" (port)](#Hub+event_detach) + + +### hub.name +**Kind**: instance property of [Hub](#Hub) +**Read only**: true +**Properties** + +| Name | Type | Description | +| --- | --- | --- | +| name | string | Name of the hub | + + + +### hub.uuid +**Kind**: instance property of [Hub](#Hub) +**Read only**: true +**Properties** + +| Name | Type | Description | +| --- | --- | --- | +| uuid | string | UUID of the hub | + + + +### hub.rssi +**Kind**: instance property of [Hub](#Hub) +**Read only**: true +**Properties** + +| Name | Type | Description | +| --- | --- | --- | +| rssi | number | Signal strength of the hub | + + + +### hub.batteryLevel +**Kind**: instance property of [Hub](#Hub) +**Read only**: true +**Properties** + +| Name | Type | Description | +| --- | --- | --- | +| batteryLevel | number | Battery level of the hub (Percentage between 0-100) | + + + +### hub.current +**Kind**: instance property of [Hub](#Hub) +**Read only**: true +**Properties** + +| Name | Type | Description | +| --- | --- | --- | +| current | number | Current usage of the hub (Amps) | + ### hub.connect() ⇒ Promise diff --git a/hub.ts b/hub.ts index c28f964..de866ed 100644 --- a/hub.ts +++ b/hub.ts @@ -19,22 +19,69 @@ export class Hub extends EventEmitter { public autoSubscribe: boolean = true; public useSpeedMap: boolean = true; public type: Consts.Hubs = Consts.Hubs.UNKNOWN; - public uuid: string; - public name: string; protected _ports: {[port: string]: Port} = {}; protected _characteristics: {[uuid: string]: Characteristic} = {}; + protected _name: string; + protected _batteryLevel: number = 100; + protected _current: number = 0; + private _peripheral: Peripheral; + private _uuid: string; private _rssi: number = -100; - private _batteryLevel: number = 100; constructor (peripheral: Peripheral, autoSubscribe: boolean = true) { super(); this.autoSubscribe = !!autoSubscribe; this._peripheral = peripheral; - this.uuid = peripheral.uuid; - this.name = peripheral.advertisement.localName; + this._uuid = peripheral.uuid; + this._name = peripheral.advertisement.localName; + } + + + /** + * @readonly + * @property {string} name Name of the hub + */ + public get name () { + return this._name; + } + + + /** + * @readonly + * @property {string} uuid UUID of the hub + */ + public get uuid () { + return this._uuid; + } + + + /** + * @readonly + * @property {number} rssi Signal strength of the hub + */ + public get rssi () { + return this._rssi; + } + + + /** + * @readonly + * @property {number} batteryLevel Battery level of the hub (Percentage between 0-100) + */ + public get batteryLevel () { + return this._batteryLevel; + } + + + /** + * @readonly + * @property {number} current Current usage of the hub (Amps) + */ + public get current () { + return this._current; } @@ -57,8 +104,6 @@ export class Hub extends EventEmitter { if (!err) { if (this._rssi !== rssi) { this._rssi = rssi; - debug(`RSSI change ${rssi}`); - self.emit("rssiChange", rssi); } } }); diff --git a/lpf2hub.ts b/lpf2hub.ts index 8b4a8b3..fd63aad 100644 --- a/lpf2hub.ts +++ b/lpf2hub.ts @@ -76,7 +76,9 @@ export class LPF2Hub extends Hub { await super.connect(); const characteristic = this._characteristics[Consts.BLECharacteristics.BOOST_ALL]; this._subscribeToCharacteristic(characteristic, this._parseMessage.bind(this)); - this._writeMessage(Consts.BLECharacteristics.BOOST_ALL, Buffer.from([0x05, 0x00, 0x01, 0x02, 0x02])); + this._writeMessage(Consts.BLECharacteristics.BOOST_ALL, Buffer.from([0x05, 0x00, 0x01, 0x02, 0x02])); // Activate button reports + this._writeMessage(Consts.BLECharacteristics.BOOST_ALL, Buffer.from([0x0a, 0x00, 0x41, 0x3b, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01])); // Activate current reports + this._writeMessage(Consts.BLECharacteristics.BOOST_ALL, Buffer.from([0x0a, 0x00, 0x41, 0x3c, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01])); // Activate voltage reports debug("Connect completed"); return resolve(); }); @@ -296,8 +298,34 @@ export class LPF2Hub extends Hub { } + private _padMessage (data: Buffer, len: number) { + if (data.length < len) { + data = Buffer.concat([data, Buffer.alloc(len - data.length)]); + } + return data; + } + + private _parseSensorMessage (data: Buffer) { + if (data[3] === 0x3c) { // Voltage + data = this._padMessage(data, 6); + let batteryLevel = (data.readUInt16LE(4) / 4096) * 100; + if (this.type === Consts.Hubs.POWERED_UP_REMOTE) { + batteryLevel = (100 / 5.1) * ((data.readUInt16LE(4) / 4096) * 100); + } + this._batteryLevel = Math.floor(batteryLevel); + return; + } else if (data[3] === 0x3b) { // Current + data = this._padMessage(data, 6); + let current = data.readUInt16LE(4) / 4096; + if (this.type === Consts.Hubs.POWERED_UP_REMOTE) { + current = data.readUInt16LE(4) / 1000000; + } + this._current = current; + return; + } + const port = this._getPortForPortNumber(data[3]); if (!port) { diff --git a/wedo2hub.ts b/wedo2hub.ts index a5a8266..3505be6 100644 --- a/wedo2hub.ts +++ b/wedo2hub.ts @@ -166,7 +166,7 @@ export class WeDo2Hub extends Hub { private _parseSensorMessage (data: Buffer) { - if (data[0] === 1) { + if (data[0] === 0x01) { /** * Emits when a button is pressed. * @event WeDo2Hub#button @@ -175,7 +175,7 @@ export class WeDo2Hub extends Hub { */ this.emit("button", "GREEN", Consts.ButtonStates.PRESSED); return; - } else if (data[0] === 0) { + } else if (data[0] === 0x00) { this.emit("button", "GREEN", Consts.ButtonStates.RELEASED); return; }