From 2d39fa72229f9fc3d068cf787a76f1ad23c517d0 Mon Sep 17 00:00:00 2001 From: Nathan Kunicki Date: Tue, 19 Jun 2018 17:16:42 +0100 Subject: [PATCH] Simplified and deduplicated type selection --- hub.js | 126 ++++++++++----------------------------------------------- 1 file changed, 22 insertions(+), 104 deletions(-) diff --git a/hub.js b/hub.js index 5eddbff..6381796 100644 --- a/hub.js +++ b/hub.js @@ -87,26 +87,7 @@ 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; - } + mode = this._getModeForDeviceType(this.ports[port].type); } this._activatePortDevice(this.ports[port].value, this.ports[port].type, mode, 0x00); } @@ -114,31 +95,30 @@ 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; - } + mode = this._getModeForDeviceType(this.ports[port].type); } this._deactivatePortDevice(this.ports[port].value, this.ports[port].type, mode, 0x00); } + _getModeForDeviceType (type) { + switch (type) { + case Consts.Devices.BASIC_MOTOR: + return 0x02; + case Consts.Devices.BOOST_INTERACTIVE_MOTOR: + return 0x02; + case Consts.Devices.BOOST_MOVE_HUB_MOTOR: + return 0x02; + case Consts.Devices.BOOST_DISTANCE: + return (Consts.Hubs.WEDO2_SMART_HUB ? 0x00 : 0x08); + case Consts.Devices.BOOST_TILT: + return 0x04; + default: + return 0x00; + } + } + + _subscribeToCharacteristic (characteristic, callback) { characteristic.on("data", (data, isNotification) => { return callback(data); @@ -154,70 +134,8 @@ class Hub extends EventEmitter { _registerDeviceAttachment (port, type) { if (port.connected) { - switch (type) { - case Consts.Devices.WEDO2_TILT: - { - port.type = type; - debug(`Port ${port.id} connected, detected WEDO2_TILT`); - if (this.autoSubscribe) { - this._activatePortDevice(port.value, port.type, 0x00, 0x00); - } - break; - } - case Consts.Devices.WEDO2_DISTANCE: - { - port.type = type; - debug(`Port ${port.id} connected, detected WEDO2_DISTANCE`); - if (this.autoSubscribe) { - this._activatePortDevice(port.value, port.type, 0x00, 0x00); - } - break; - } - case Consts.Devices.BASIC_MOTOR: - { - port.type = type; - debug(`Port ${port.id} connected, detected BASIC_MOTOR`); - if (this.autoSubscribe) { - this._activatePortDevice(port.value, port.type, 0x02, 0x00); - } - break; - } - case Consts.Devices.BOOST_DISTANCE: - { - port.type = type; - debug(`Port ${port.id} connected, detected BOOST_DISTANCE`); - 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 = type; - debug(`Port ${port.id} connected, detected BOOST_INTERACTIVE_MOTOR`); - if (this.autoSubscribe) { - this._activatePortDevice(port.value, port.type, 0x02, 0x00); - } - break; - } - case Consts.Devices.BOOST_MOVE_HUB_MOTOR: - { - port.type = type; - debug(`Port ${port.id} connected, detected BOOST_MOVE_HUB_MOTOR`); - if (this.autoSubscribe) { - this._activatePortDevice(port.value, port.type, 0x02, 0x00); - } - break; - } - case Consts.Devices.BOOST_TILT: - { - port.type = type; - debug(`Port ${port.id} connected, detected BOOST_TILT`); - if (this.autoSubscribe) { - this._activatePortDevice(port.value, port.type, 0x04, 0x00); - } - break; - } + if (this.autoSubscribe) { + this._activatePortDevice(port.value, this._getModeForDeviceType(port.type), 0x00, 0x00); } } else { port.type = null;