From ab08218595ba2f9047ff8044c490f7357184ffbc Mon Sep 17 00:00:00 2001 From: Nathan Kunicki Date: Fri, 15 Jun 2018 14:35:02 +0100 Subject: [PATCH] Better common hub code --- boosthub.js | 58 +------------------------------------------------ hub.js | 62 +++++++++++++++++++++++++++++++++++++++++++++++++++++ test.js | 8 +++---- wedo2hub.js | 52 +------------------------------------------- 4 files changed, 68 insertions(+), 112 deletions(-) diff --git a/boosthub.js b/boosthub.js index d021c8a..4b650ec 100644 --- a/boosthub.js +++ b/boosthub.js @@ -171,64 +171,8 @@ class BoostHub extends Hub { } port.connected = (data[4] === 1 || data[4] === 2) ? true : false; + this._registerDeviceAttachment(port, data[5]); - if (port.connected) { - switch (data[5]) { - case Consts.Devices.WEDO2_TILT: - { - port.type = Consts.Devices.WEDO2_TILT; - debug(`Port ${port.id} connected, detected WEDO2_TILT`); - 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); - break; - } - case Consts.Devices.WEDO2_MOTOR: - { - port.type = Consts.Devices.WEDO2_MOTOR; - debug(`Port ${port.id} connected, detected WEDO2_MOTOR`); - 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, 0x08, 0x00); - 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); - 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); - 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); - break; - } - } - } else { - port.type = null; - debug(`Port ${port.id} disconnected`); - } - } diff --git a/hub.js b/hub.js index 1a6d18a..2b90efe 100644 --- a/hub.js +++ b/hub.js @@ -95,6 +95,68 @@ class Hub extends EventEmitter { }); } + + _registerDeviceAttachment (port, type) { + + if (port.connected) { + switch (type) { + case Consts.Devices.WEDO2_TILT: + { + port.type = Consts.Devices.WEDO2_TILT; + debug(`Port ${port.id} connected, detected WEDO2_TILT`); + 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); + break; + } + case Consts.Devices.WEDO2_MOTOR: + { + port.type = Consts.Devices.WEDO2_MOTOR; + debug(`Port ${port.id} connected, detected WEDO2_MOTOR`); + 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 + 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); + 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); + 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); + break; + } + } + } else { + port.type = null; + debug(`Port ${port.id} disconnected`); + } + + } + } diff --git a/test.js b/test.js index 16e39bd..a1997be 100644 --- a/test.js +++ b/test.js @@ -19,9 +19,9 @@ lpf2.on("discover", (hub) => { moveHub.on("distance", (port, distance) => { console.log(`Distance ${distance} received on port ${port}`); if (distance < 90) { - moveHub.setMotorSpeed("D", 40); + wedoHub.setMotorSpeed("B", 40); } else { - moveHub.setMotorSpeed("D", 0); + wedoHub.setMotorSpeed("B", 0); } }); @@ -32,9 +32,9 @@ lpf2.on("discover", (hub) => { wedoHub.on("distance", (port, distance) => { console.log(`Distance ${distance} received on port ${port}`); if (distance < 90) { - wedoHub.setMotorSpeed("B", 40); + moveHub.setMotorSpeed("D", 40); } else { - wedoHub.setMotorSpeed("B", 0); + moveHub.setMotorSpeed("D", 0); } }); diff --git a/wedo2hub.js b/wedo2hub.js index bf1e20c..b258eef 100644 --- a/wedo2hub.js +++ b/wedo2hub.js @@ -103,57 +103,7 @@ class WeDo2Hub extends Hub { } port.connected = data[1] === 1 ? true : false; - - if (port.connected) { - switch (data[3]) { - case Consts.Devices.WEDO2_TILT: - { - port.type = Consts.Devices.WEDO2_TILT; - debug(`Port ${port.id} connected, detected WEDO2_TILT`); - this._activatePortDevice(data[0], 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(data[0], port.type, 0x00, 0x00); - break; - } - case Consts.Devices.WEDO2_MOTOR: - { - port.type = Consts.Devices.WEDO2_MOTOR; - debug(`Port ${port.id} connected, detected WEDO2_MOTOR`); - this._activatePortDevice(data[0], port.type, 0x02, 0x00); - break; - } - case Consts.Devices.BOOST_DISTANCE: - { - port.type = Consts.Devices.BOOST_DISTANCE; - debug(`Port ${port.id} connected, detected BOOST_DISTANCE`); - // NK: BOOST_DISTANCE only supports color when connected to WeDo 2.0 Smart Hub - this._activatePortDevice(data[0], port.type, 0x00, 0x00); - 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(data[0], 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(data[0], port.type, 0x02, 0x00); - break; - } - } - } else { - port.type = null; - debug(`Port ${port.id} disconnected`); - } + this._registerDeviceAttachment(port, data[3]); }