From 8f6c26148ee62661cbd20e6b65c91ea3a5d4404b Mon Sep 17 00:00:00 2001 From: Nathan Kunicki Date: Tue, 19 Jun 2018 11:35:08 +0100 Subject: [PATCH] Manual subscribing --- boosthub.js | 4 ++-- hub.js | 58 +++++++++++++++++++++++++++++++++++++++++++++-------- lpf2.js | 5 +++-- wedo2hub.js | 4 ++-- 4 files changed, 57 insertions(+), 14 deletions(-) diff --git a/boosthub.js b/boosthub.js index 4b650ec..2bc845c 100644 --- a/boosthub.js +++ b/boosthub.js @@ -9,8 +9,8 @@ const Hub = require("./hub.js"), class BoostHub extends Hub { - constructor (peripheral) { - super(peripheral); + constructor (peripheral, autoSubscribe) { + super(peripheral, autoSubscribe); this.type = Consts.Hubs.BOOST_MOVE_HUB; this.ports = { "A": new Port("A", 55), diff --git a/hub.js b/hub.js index 1acef9f..5321cc2 100644 --- a/hub.js +++ b/hub.js @@ -8,8 +8,9 @@ const Port = require("./port.js"), class Hub extends EventEmitter { - constructor (peripheral) { + constructor (peripheral, autoSubscribe) { super(); + this.autoSubscribe = autoSubscribe || true; this._peripheral = peripheral; this._characteristics = {}; this._batteryLevel = 100; @@ -84,6 +85,33 @@ class Hub extends EventEmitter { } + subscribe (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._activatePortDevice(this.ports[port].value, this.ports[port].type, mode, 0x00); + } + + _subscribeToCharacteristic (characteristic, callback) { characteristic.on("data", (data, isNotification) => { return callback(data); @@ -104,49 +132,63 @@ class Hub extends EventEmitter { { port.type = Consts.Devices.WEDO2_TILT; debug(`Port ${port.id} connected, detected WEDO2_TILT`); - this._activatePortDevice(port.value, port.type, 0x00, 0x00); + if (this.autoSubscribe) { + this._activatePortDevice(port.value, port.type, 0x00, 0x00); + } break; } case Consts.Devices.WEDO2_DISTANCE: { port.type = Consts.Devices.WEDO2_DISTANCE; debug(`Port ${port.id} connected, detected WEDO2_DISTANCE`); - this._activatePortDevice(port.value, port.type, 0x00, 0x00); + if (this.autoSubscribe) { + this._activatePortDevice(port.value, port.type, 0x00, 0x00); + } break; } case Consts.Devices.BASIC_MOTOR: { port.type = Consts.Devices.BASIC_MOTOR; debug(`Port ${port.id} connected, detected BASIC_MOTOR`); - this._activatePortDevice(port.value, port.type, 0x02, 0x00); + if (this.autoSubscribe) { + this._activatePortDevice(port.value, port.type, 0x02, 0x00); + } break; } case Consts.Devices.BOOST_DISTANCE: { port.type = Consts.Devices.BOOST_DISTANCE; debug(`Port ${port.id} connected, detected BOOST_DISTANCE`); - this._activatePortDevice(port.value, port.type, this.type == Consts.Hubs.WEDO2_SMART_HUB ? 0x00 : 0x08, 0x00); // NK: 0x00 for WeDo 2.0 Smart hub, 0x08 for Boost Move Hub + if (this.autoSubscribe) { + this._activatePortDevice(port.value, port.type, this.type == Consts.Hubs.WEDO2_SMART_HUB ? 0x00 : 0x08, 0x00); // NK: 0x00 for WeDo 2.0 Smart hub, 0x08 for Boost Move Hub + } break; } case Consts.Devices.BOOST_INTERACTIVE_MOTOR: { port.type = Consts.Devices.BOOST_INTERACTIVE_MOTOR; debug(`Port ${port.id} connected, detected BOOST_INTERACTIVE_MOTOR`); - this._activatePortDevice(port.value, port.type, 0x02, 0x00); + if (this.autoSubscribe) { + this._activatePortDevice(port.value, port.type, 0x02, 0x00); + } break; } case Consts.Devices.BOOST_MOVE_HUB_MOTOR: { port.type = Consts.Devices.BOOST_MOVE_HUB_MOTOR; debug(`Port ${port.id} connected, detected BOOST_MOVE_HUB_MOTOR`); - this._activatePortDevice(port.value, port.type, 0x02, 0x00); + if (this.autoSubscribe) { + this._activatePortDevice(port.value, port.type, 0x02, 0x00); + } break; } case Consts.Devices.BOOST_TILT: { port.type = Consts.Devices.BOOST_TILT; debug(`Port ${port.id} connected, detected BOOST_TILT`); - this._activatePortDevice(port.value, port.type, 0x04, 0x00); + if (this.autoSubscribe) { + this._activatePortDevice(port.value, port.type, 0x04, 0x00); + } break; } } diff --git a/lpf2.js b/lpf2.js index 12b8ac7..feacde8 100644 --- a/lpf2.js +++ b/lpf2.js @@ -26,6 +26,7 @@ class LPF2 extends EventEmitter { constructor () { super(); + this.autoSubscribe = true; this._connectedDevices = {}; } @@ -38,9 +39,9 @@ class LPF2 extends EventEmitter { let hub = null; if (WeDo2Hub.isWeDo2Hub(peripheral)) { - hub = new WeDo2Hub(peripheral); + hub = new WeDo2Hub(peripheral, autoSubscribe); } else if (BoostHub.isBoostHub(peripheral)) { - hub = new BoostHub(peripheral); + hub = new BoostHub(peripheral autoSubscribe); } else { return; } diff --git a/wedo2hub.js b/wedo2hub.js index b258eef..573c76a 100644 --- a/wedo2hub.js +++ b/wedo2hub.js @@ -9,8 +9,8 @@ const Hub = require("./hub.js"), class WeDo2Hub extends Hub { - constructor (peripheral) { - super(peripheral); + constructor (peripheral, autoSubscribe) { + super(peripheral, autoSubscribe); this.type = Consts.Hubs.WEDO2_SMART_HUB; this.ports = { "A": new Port("A", 1),