Better common hub code

This commit is contained in:
Nathan Kunicki 2018-06-15 14:35:02 +01:00
parent b0400856cc
commit ab08218595
4 changed files with 68 additions and 112 deletions

View File

@ -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`);
}
}

62
hub.js
View File

@ -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`);
}
}
}

View File

@ -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);
}
});

View File

@ -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]);
}