Change cleanup/disconnect behavior for BT and EV3.

This commit is contained in:
Evelyn Eastmond 2019-06-06 15:52:38 -04:00
parent 6a032087ce
commit a1243fd897
2 changed files with 28 additions and 30 deletions

View file

@ -497,7 +497,7 @@ class EV3 {
*/ */
this._rateLimiter = new RateLimiter(BTSendRateMax); this._rateLimiter = new RateLimiter(BTSendRateMax);
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._pollValues = this._pollValues.bind(this); this._pollValues = this._pollValues.bind(this);
@ -583,7 +583,7 @@ class EV3 {
this._bt = new BT(this._runtime, this._extensionId, { this._bt = new BT(this._runtime, this._extensionId, {
majorDeviceClass: 8, majorDeviceClass: 8,
minorDeviceClass: 1 minorDeviceClass: 1
}, this._onConnect, this.disconnect, this._onMessage); }, this._onConnect, this.reset, this._onMessage);
} }
/** /**
@ -600,13 +600,28 @@ class EV3 {
* Called by the runtime when user wants to disconnect from the EV3 peripheral. * Called by the runtime when user wants to disconnect from the EV3 peripheral.
*/ */
disconnect () { disconnect () {
this._clearSensorsAndMotors();
window.clearInterval(this._pollingIntervalID);
this._pollingIntervalID = null;
if (this._bt) { if (this._bt) {
this._bt.disconnect(); this._bt.disconnect();
} }
this.reset();
}
/**
* Reset all the state and timeout/interval ids.
*/
reset () {
this._sensorPorts = [];
this._motorPorts = [];
this._sensors = {
distance: 0,
brightness: 0,
buttons: [0, 0, 0, 0]
};
this._motors = [null, null, null, null];
window.clearInterval(this._pollingIntervalID);
this._pollingIntervalID = null;
} }
/** /**
@ -886,22 +901,6 @@ class EV3 {
} }
} }
} }
/**
* Clear all the senor port and motor names, and their values.
* @private
*/
_clearSensorsAndMotors () {
this._sensorPorts = [];
this._motorPorts = [];
this._sensors = {
distance: 0,
brightness: 0,
buttons: [0, 0, 0, 0]
};
this._motors = [null, null, null, null];
}
} }
/** /**

View file

@ -9,10 +9,10 @@ class BT extends JSONRPC {
* @param {string} extensionId - the id of the extension using this socket. * @param {string} extensionId - the id of the extension using this socket.
* @param {object} peripheralOptions - the list of options for peripheral discovery. * @param {object} peripheralOptions - the list of options for peripheral discovery.
* @param {object} connectCallback - a callback for connection. * @param {object} connectCallback - a callback for connection.
* @param {object} disconnectCallback - a callback for disconnection. * @param {object} resetCallback - a callback for resetting extension state.
* @param {object} messageCallback - a callback for message sending. * @param {object} messageCallback - a callback for message sending.
*/ */
constructor (runtime, extensionId, peripheralOptions, connectCallback, disconnectCallback = null, messageCallback) { constructor (runtime, extensionId, peripheralOptions, connectCallback, resetCallback = null, messageCallback) {
super(); super();
this._socket = runtime.getScratchLinkSocket('BT'); this._socket = runtime.getScratchLinkSocket('BT');
@ -27,7 +27,7 @@ class BT extends JSONRPC {
this._connectCallback = connectCallback; this._connectCallback = connectCallback;
this._connected = false; this._connected = false;
this._characteristicDidChangeCallback = null; this._characteristicDidChangeCallback = null;
this._disconnectCallback = disconnectCallback; this._resetCallback = resetCallback;
this._discoverTimeoutID = null; this._discoverTimeoutID = null;
this._extensionId = extensionId; this._extensionId = extensionId;
this._peripheralOptions = peripheralOptions; this._peripheralOptions = peripheralOptions;
@ -152,11 +152,10 @@ class BT extends JSONRPC {
if (!this._connected) return; if (!this._connected) return;
// TODO: Fix branching by splitting up cleanup/disconnect in extension
if (this._disconnectCallback) {
this._disconnectCallback(); // must call disconnect()
} else {
this.disconnect(); this.disconnect();
if (this._resetCallback) {
this._resetCallback();
} }
this._runtime.emit(this._runtime.constructor.PERIPHERAL_CONNECTION_LOST_ERROR, { this._runtime.emit(this._runtime.constructor.PERIPHERAL_CONNECTION_LOST_ERROR, {