From 0fe2a4e524ec130dbdb97a0371622859e83d28ad Mon Sep 17 00:00:00 2001 From: Nathan Kunicki Date: Tue, 19 Jun 2018 17:06:59 +0100 Subject: [PATCH] Manual unsubscribing --- boosthub.js | 8 ++++++++ hub.js | 27 +++++++++++++++++++++++++++ wedo2hub.js | 8 ++++++++ 3 files changed, 43 insertions(+) diff --git a/boosthub.js b/boosthub.js index 2bc845c..3daff12 100644 --- a/boosthub.js +++ b/boosthub.js @@ -197,6 +197,14 @@ class BoostHub extends Hub { } + _deactivatePortDevice (port, type, mode, format, callback) { + const characteristic = this._characteristics[Consts.BLE.Characteristics.Boost.ALL]; + if (characteristic) { + characteristic.write(Buffer.from([0x0a, 0x00, 0x41, port, mode, 0x01, 0x00, 0x00, 0x00, 0x00]), callback); + } + } + + _parseSensorMessage (data) { let port = this._getPortForPortNumber(data[3]); diff --git a/hub.js b/hub.js index 5321cc2..bb4d42b 100644 --- a/hub.js +++ b/hub.js @@ -112,6 +112,33 @@ class Hub extends EventEmitter { } + unsubscribe (port, mode = false) { + if (!mode) { + switch (this.ports[port].type) { + case Consts.Devices.BASIC_MOTOR: + mode = 0x02; + break; + case Consts.Devices.BOOST_INTERACTIVE_MOTOR: + mode = 0x02; + break; + case Consts.Devices.BOOST_MOVE_HUB_MOTOR: + mode = 0x02; + break; + case Consts.Devices.BOOST_DISTANCE: + mode = Consts.Hubs.WEDO2_SMART_HUB ? 0x00 : 0x08 + break; + case Consts.Devices.BOOST_TILT: + mode = 0x04; + break; + default: + mode = 0x00; + break; + } + } + this._deactivatePortDevice(this.ports[port].value, this.ports[port].type, mode, 0x00); + } + + _subscribeToCharacteristic (characteristic, callback) { characteristic.on("data", (data, isNotification) => { return callback(data); diff --git a/wedo2hub.js b/wedo2hub.js index 573c76a..c56467d 100644 --- a/wedo2hub.js +++ b/wedo2hub.js @@ -116,6 +116,14 @@ class WeDo2Hub extends Hub { } + _deactivatePortDevice (port, type, mode, format, callback) { + const characteristic = this._characteristics[Consts.BLE.Characteristics.WeDo2.PORT_TYPE_WRITE]; + if (characteristic) { + characteristic.write(Buffer.from([0x01, 0x02, port, type, mode, 0x01, 0x00, 0x00, 0x00, format, 0x00]), callback); + } + } + + _parseSensorMessage (data) {