Adding disconnect/state resetting behavior to rest of hardware extensions.

This commit is contained in:
Evelyn Eastmond 2019-06-06 16:01:01 -04:00
parent f8a0ea281f
commit 37fe83c057
6 changed files with 59 additions and 19 deletions

View file

@ -827,6 +827,7 @@ class Boost {
*/ */
disconnect () { disconnect () {
console.log('BOOST DISCONNECT CALLED'); console.log('BOOST DISCONNECT CALLED');
if (this._ble) { if (this._ble) {
this._ble.disconnect(); this._ble.disconnect();
} }
@ -839,6 +840,7 @@ class Boost {
*/ */
reset () { reset () {
console.log('BOOST RESET CALLED'); console.log('BOOST RESET CALLED');
this._ports = []; this._ports = [];
this._motors = []; this._motors = [];
this._sensors = { this._sensors = {

View file

@ -601,6 +601,7 @@ class EV3 {
*/ */
disconnect() { disconnect() {
console.log('EV3 DISCONNECT CALLED'); console.log('EV3 DISCONNECT CALLED');
if (this._bt) { if (this._bt) {
this._bt.disconnect(); this._bt.disconnect();
} }
@ -613,6 +614,7 @@ class EV3 {
*/ */
reset() { reset() {
console.log('EV3 RESET CALLED'); console.log('EV3 RESET CALLED');
this._sensorPorts = []; this._sensorPorts = [];
this._motorPorts = []; this._motorPorts = [];
this._sensors = { this._sensors = {

View file

@ -172,7 +172,7 @@ class GdxFor {
*/ */
this._timeoutID = null; this._timeoutID = null;
this.disconnect = this.disconnect.bind(this); this.reset = this.reset.bind(this);
this._onConnect = this._onConnect.bind(this); this._onConnect = this._onConnect.bind(this);
} }
@ -192,7 +192,7 @@ class GdxFor {
optionalServices: [ optionalServices: [
BLEUUID.service BLEUUID.service
] ]
}, this._onConnect, this.disconnect); }, this._onConnect, this.reset);
} }
/** /**
@ -210,7 +210,21 @@ class GdxFor {
* Disconnect from the GDX FOR. * Disconnect from the GDX FOR.
*/ */
disconnect() { disconnect() {
window.clearInterval(this._timeoutID); console.log('GDXFOR DISCONNECT CALLED');
if (this._scratchLinkSocket) {
this._scratchLinkSocket.disconnect();
}
this.reset();
}
/**
* Reset all the state and timeout/interval ids.
*/
reset () {
console.log('GDXFOR RESET CALLED');
this._sensors = { this._sensors = {
force: 0, force: 0,
accelerationX: 0, accelerationX: 0,
@ -220,9 +234,8 @@ class GdxFor {
spinSpeedY: 0, spinSpeedY: 0,
spinSpeedZ: 0 spinSpeedZ: 0
}; };
if (this._scratchLinkSocket) {
this._scratchLinkSocket.disconnect(); window.clearInterval(this._timeoutID);
}
} }
/** /**

View file

@ -144,7 +144,7 @@ class MicroBit {
*/ */
this._busyTimeoutID = null; this._busyTimeoutID = null;
this.disconnect = this.disconnect.bind(this); this.reset = this.reset.bind(this);
this._onConnect = this._onConnect.bind(this); this._onConnect = this._onConnect.bind(this);
this._onMessage = this._onMessage.bind(this); this._onMessage = this._onMessage.bind(this);
} }
@ -222,7 +222,7 @@ class MicroBit {
filters: [ filters: [
{services: [BLEUUID.service]} {services: [BLEUUID.service]}
] ]
}, this._onConnect, this.disconnect); }, this._onConnect, this.reset);
} }
/** /**
@ -239,10 +239,22 @@ class MicroBit {
* Disconnect from the micro:bit. * Disconnect from the micro:bit.
*/ */
disconnect() { disconnect() {
window.clearTimeout(this._timeoutID); console.log('MICROBIT DISCONNECT CALLED');
if (this._ble) { if (this._ble) {
this._ble.disconnect(); this._ble.disconnect();
} }
this.reset();
}
/**
* Reset all the state and timeout/interval ids.
*/
reset() {
console.log('MICROBIT RESET CALLED');
window.clearTimeout(this._timeoutID);
} }
/** /**

View file

@ -435,7 +435,7 @@ class WeDo2 {
*/ */
this._batteryLevelIntervalId = null; this._batteryLevelIntervalId = null;
this.disconnect = this.disconnect.bind(this); this.reset = this.reset.bind(this);
this._onConnect = this._onConnect.bind(this); this._onConnect = this._onConnect.bind(this);
this._onMessage = this._onMessage.bind(this); this._onMessage = this._onMessage.bind(this);
this._checkBatteryLevel = this._checkBatteryLevel.bind(this); this._checkBatteryLevel = this._checkBatteryLevel.bind(this);
@ -594,7 +594,7 @@ class WeDo2 {
services: [BLEService.DEVICE_SERVICE] services: [BLEService.DEVICE_SERVICE]
}], }],
optionalServices: [BLEService.IO_SERVICE] optionalServices: [BLEService.IO_SERVICE]
}, this._onConnect, this.disconnect); }, this._onConnect, this.reset);
} }
/** /**
@ -611,6 +611,21 @@ class WeDo2 {
* Disconnects from the current BLE socket. * Disconnects from the current BLE socket.
*/ */
disconnect() { disconnect() {
console.log('WEDO2 DISCONNECT CALLED');
if (this._ble) {
this._ble.disconnect();
}
this.reset();
}
/**
* Reset all the state and timeout/interval ids.
*/
reset() {
console.log('WEDO2 RESET CALLED');
this._ports = ['none', 'none']; this._ports = ['none', 'none'];
this._motors = [null, null]; this._motors = [null, null];
this._sensors = { this._sensors = {
@ -619,10 +634,6 @@ class WeDo2 {
distance: 0 distance: 0
}; };
if (this._ble) {
this._ble.disconnect();
}
if (this._batteryLevelIntervalId) { if (this._batteryLevelIntervalId) {
window.clearInterval(this._batteryLevelIntervalId); window.clearInterval(this._batteryLevelIntervalId);
this._batteryLevelIntervalId = null; this._batteryLevelIntervalId = null;

View file

@ -205,9 +205,9 @@ class BLE extends JSONRPC {
*/ */
handleDisconnectError (/* e */) { handleDisconnectError (/* e */) {
// log.error(`BLE error: ${JSON.stringify(e)}`); // log.error(`BLE error: ${JSON.stringify(e)}`);
console.log('BLE HANDLEDISCONNECTERROR CALLED');
if (!this._connected) return; if (!this._connected) return;
console.log('BLE HANDLEDISCONNECTERROR CALLED');
this.disconnect(); this.disconnect();