Subscribe working

This commit is contained in:
Nathan Kunicki 2018-06-20 00:19:09 +01:00
parent 2d39fa7222
commit 00f1664840
3 changed files with 16 additions and 13 deletions

7
hub.js
View File

@ -10,7 +10,7 @@ class Hub extends EventEmitter {
constructor (peripheral, autoSubscribe) { constructor (peripheral, autoSubscribe) {
super(); super();
this.autoSubscribe = autoSubscribe || true; this.autoSubscribe = !!autoSubscribe;
this._peripheral = peripheral; this._peripheral = peripheral;
this._characteristics = {}; this._characteristics = {};
this._batteryLevel = 100; this._batteryLevel = 100;
@ -110,7 +110,7 @@ class Hub extends EventEmitter {
case Consts.Devices.BOOST_MOVE_HUB_MOTOR: case Consts.Devices.BOOST_MOVE_HUB_MOTOR:
return 0x02; return 0x02;
case Consts.Devices.BOOST_DISTANCE: case Consts.Devices.BOOST_DISTANCE:
return (Consts.Hubs.WEDO2_SMART_HUB ? 0x00 : 0x08); return (this.type == Consts.Hubs.WEDO2_SMART_HUB ? 0x00 : 0x08);
case Consts.Devices.BOOST_TILT: case Consts.Devices.BOOST_TILT:
return 0x04; return 0x04;
default: default:
@ -134,8 +134,9 @@ class Hub extends EventEmitter {
_registerDeviceAttachment (port, type) { _registerDeviceAttachment (port, type) {
if (port.connected) { if (port.connected) {
port.type = type;
if (this.autoSubscribe) { if (this.autoSubscribe) {
this._activatePortDevice(port.value, this._getModeForDeviceType(port.type), 0x00, 0x00); this._activatePortDevice(port.value, type, this._getModeForDeviceType(type), 0x00);
} }
} else { } else {
port.type = null; port.type = null;

View File

@ -39,9 +39,9 @@ class LPF2 extends EventEmitter {
let hub = null; let hub = null;
if (WeDo2Hub.isWeDo2Hub(peripheral)) { if (WeDo2Hub.isWeDo2Hub(peripheral)) {
hub = new WeDo2Hub(peripheral, autoSubscribe); hub = new WeDo2Hub(peripheral, this.autoSubscribe);
} else if (BoostHub.isBoostHub(peripheral)) { } else if (BoostHub.isBoostHub(peripheral)) {
hub = new BoostHub(peripheral autoSubscribe); hub = new BoostHub(peripheral, this.autoSubscribe);
} else { } else {
return; return;
} }

18
test.js
View File

@ -2,26 +2,28 @@ const LPF2 = require("./lpf2.js");
const lpf2 = new LPF2(); const lpf2 = new LPF2();
//lpf2.autoSubscribe = false;
lpf2.scan(); lpf2.scan();
let moveHub = null, let moveHub = null,
moveHubUUID = "782a5fbbcef64c5cb31ab4791c191f5d", moveHubUUID = "e6e40e4f00e34dbe955da2b187adcd2f",
moveHub2 = null, moveHub2 = null,
moveHub2UUID = "2ea54c6e1c744406bdc8567daf3a692a", moveHub2UUID = "c72413db7ce24411967ff25184d4609a",
wedoHub = null, wedoHub = null,
wedoHubUUID = "0ae95acf801e47f9bda4752392756eed"; wedoHubUUID = "f4924139c6684be19840f97738c707f3";
lpf2.on("discover", (hub) => { lpf2.on("discover", (hub) => {
console.log(hub.uuid);
hub.connect(() => { hub.connect(() => {
//console.log(hub.uuid);
if (hub.uuid === moveHubUUID) { if (hub.uuid === moveHubUUID) {
moveHub = hub; moveHub = hub;
console.log("Connected to Move Hub"); console.log("Connected to Move Hub");
moveHub.on("distance", (port, distance) => { moveHub.on("distance", (port, distance) => {
console.log(`Distance ${distance} received on port ${port}`); //console.log(`Distance ${distance} received on port ${port}`);
if (distance < 90) { if (distance < 90) {
if (wedoHub) wedoHub.setMotorSpeed("B", 40); if (wedoHub) wedoHub.setMotorSpeed("B", 40);
if (moveHub2) moveHub2.setMotorSpeed("D", 40); if (moveHub2) moveHub2.setMotorSpeed("D", 40);
@ -36,7 +38,7 @@ lpf2.on("discover", (hub) => {
console.log("Connected to Move Hub 2"); console.log("Connected to Move Hub 2");
moveHub2.on("distance", (port, distance) => { moveHub2.on("distance", (port, distance) => {
console.log(`Distance ${distance} received on port ${port}`); //console.log(`Distance ${distance} received on port ${port}`);
if (distance < 90) { if (distance < 90) {
if (wedoHub) wedoHub.setMotorSpeed("B", 40); if (wedoHub) wedoHub.setMotorSpeed("B", 40);
if (moveHub) moveHub.setMotorSpeed("D", 40); if (moveHub) moveHub.setMotorSpeed("D", 40);
@ -51,13 +53,13 @@ lpf2.on("discover", (hub) => {
console.log("Connected to Smart Hub"); console.log("Connected to Smart Hub");
wedoHub.on("distance", (port, distance) => { wedoHub.on("distance", (port, distance) => {
console.log(`Distance ${distance} received on port ${port}`); //console.log(`Distance ${distance} received on port ${port}`);
if (distance < 90) { if (distance < 90) {
if (moveHub) moveHub.setMotorSpeed("D", 40); if (moveHub) moveHub.setMotorSpeed("D", 40);
if (moveHub2) moveHub2.setMotorSpeed("D", 40); if (moveHub2) moveHub2.setMotorSpeed("D", 40);
} else { } else {
if (moveHub) moveHub.setMotorSpeed("D", 0); if (moveHub) moveHub.setMotorSpeed("D", 0);
if (moveHub2) moveHub.setMotorSpeed("D", 0); if (moveHub2) moveHub2.setMotorSpeed("D", 0);
} }
}); });