Manual subscribing

This commit is contained in:
Nathan Kunicki 2018-06-19 11:35:08 +01:00
parent 58a4ceb7b1
commit 8f6c26148e
4 changed files with 57 additions and 14 deletions

View File

@ -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),

44
hub.js
View File

@ -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`);
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`);
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`);
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`);
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`);
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`);
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`);
if (this.autoSubscribe) {
this._activatePortDevice(port.value, port.type, 0x04, 0x00);
}
break;
}
}

View File

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

View File

@ -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),