From 6f5ff31eb3248605ae2c2ed5809a16ff3aa96fa7 Mon Sep 17 00:00:00 2001 From: Eric Rosenbaum Date: Wed, 27 Jun 2018 14:21:11 -0400 Subject: [PATCH] Add disconnectExtensionSession and getPeripheralIsConnected --- src/engine/runtime.js | 14 ++++++++++++++ src/extensions/scratch3_microbit/index.js | 12 ++++++++++++ src/io/bleSession.js | 19 +++++++++++++++++++ src/io/btSession.js | 19 +++++++++++++++++++ src/virtual-machine.js | 8 ++++++++ 5 files changed, 72 insertions(+) diff --git a/src/engine/runtime.js b/src/engine/runtime.js index 64e469e05..530db3b40 100644 --- a/src/engine/runtime.js +++ b/src/engine/runtime.js @@ -909,6 +909,20 @@ class Runtime extends EventEmitter { } } + disconnectExtensionSession (extensionId) { + if (this.extensionDevices[extensionId]) { + this.extensionDevices[extensionId].disconnectSession(); + } + } + + getPeripheralIsConnected (extensionId) { + let isConnected = false; + if (this.extensionDevices[extensionId]) { + isConnected = this.extensionDevices[extensionId].getPeripheralIsConnected(); + } + return isConnected; + } + /** * Retrieve the function associated with the given opcode. * @param {!string} opcode The opcode to look up. diff --git a/src/extensions/scratch3_microbit/index.js b/src/extensions/scratch3_microbit/index.js index 22203ccb4..e9ad6bab7 100644 --- a/src/extensions/scratch3_microbit/index.js +++ b/src/extensions/scratch3_microbit/index.js @@ -128,6 +128,18 @@ class MicroBit { this._ble.connectDevice(id); } + disconnectSession () { + this._ble.disconnectSession(); + } + + getPeripheralIsConnected () { + let connected = false; + if (this._ble) { + connected = this._ble.getPeripheralIsConnected(); + } + return connected; + } + /** * @param {string} text - the text to display. * @return {Promise} - a Promise that resolves when writing to device. diff --git a/src/io/bleSession.js b/src/io/bleSession.js index 9a4cd1aab..983399c90 100644 --- a/src/io/bleSession.js +++ b/src/io/bleSession.js @@ -25,6 +25,8 @@ class BLESession extends JSONRPCWebSocket { this._characteristicDidChangeCallback = null; this._deviceOptions = deviceOptions; this._runtime = runtime; + + this._connected = false; } /** @@ -50,6 +52,7 @@ class BLESession extends JSONRPCWebSocket { .then(() => { log.info('should have connected'); this._runtime.emit(this._runtime.constructor.PERIPHERAL_CONNECTED); + this._connected = true; this._connectCallback(); }) .catch(e => { @@ -57,6 +60,21 @@ class BLESession extends JSONRPCWebSocket { }); } + /** + * Close the websocket. + */ + disconnectSession () { + this._ws.close(); + this._connected = false; + } + + /** + * @return {bool} whether the peripheral is connected. + */ + getPeripheralIsConnected () { + return this._connected; + } + /** * Handle a received call from the socket. * @param {string} method - a received method label. @@ -119,6 +137,7 @@ class BLESession extends JSONRPCWebSocket { } _sendError (e) { + this._connected = false; log.error(`BLESession error:`); log.error(e); this._runtime.emit(this._runtime.constructor.PERIPHERAL_ERROR); diff --git a/src/io/btSession.js b/src/io/btSession.js index 14228cc60..7d1f6a4c8 100644 --- a/src/io/btSession.js +++ b/src/io/btSession.js @@ -26,6 +26,8 @@ class BTSession extends JSONRPCWebSocket { this._deviceOptions = deviceOptions; this._messageCallback = messageCallback; this._runtime = runtime; + + this._connected = false; } /** @@ -51,6 +53,7 @@ class BTSession extends JSONRPCWebSocket { .then(() => { log.info('should have connected'); this._runtime.emit(this._runtime.constructor.PERIPHERAL_CONNECTED); + this._connected = true; this._connectCallback(); }) .catch(e => { @@ -58,6 +61,22 @@ class BTSession extends JSONRPCWebSocket { }); } + /** + * Close the websocket. + */ + disconnectSession () { + this._ws.close(); + this._connected = false; + } + + /** + * @return {bool} whether the peripheral is connected. + */ + getPeripheralIsConnected () { + return this._connected; + } + + sendMessage (options) { return this.sendRemoteRequest('send', options); } diff --git a/src/virtual-machine.js b/src/virtual-machine.js index dc53ce800..e95258fb4 100644 --- a/src/virtual-machine.js +++ b/src/virtual-machine.js @@ -213,6 +213,14 @@ class VirtualMachine extends EventEmitter { this.runtime.connectToPeripheral(extensionId, peripheralId); } + disconnectExtensionSession (extensionId) { + this.runtime.disconnectExtensionSession(extensionId); + } + + getPeripheralIsConnected (extensionId) { + return this.runtime.getPeripheralIsConnected(extensionId); + } + /** * Load a Scratch project from a .sb, .sb2, .sb3 or json string. * @param {string | object} input A json string, object, or ArrayBuffer representing the project to load.