diff --git a/src/extensions/scratch3_ev3/index.js b/src/extensions/scratch3_ev3/index.js index a0b490e27..0f27c11e4 100644 --- a/src/extensions/scratch3_ev3/index.js +++ b/src/extensions/scratch3_ev3/index.js @@ -408,6 +408,11 @@ class EV3 { this._runtime = runtime; this._runtime.on('PROJECT_STOP_ALL', this.stopAll.bind(this)); + /** + * The id of the extension this peripheral belongs to. + */ + this._extensionId = extensionId; + /** * A list of the names of the sensors connected in ports 1,2,3,4. * @type {string[]} @@ -548,7 +553,7 @@ class EV3 { * Called by the runtime when user wants to scan for an EV3 peripheral. */ scan () { - this._bt = new BT(this._runtime, { + this._bt = new BT(this._runtime, this._extensionId, { majorDeviceClass: 8, minorDeviceClass: 1 }, this._onConnect, this._onMessage); diff --git a/src/extensions/scratch3_microbit/index.js b/src/extensions/scratch3_microbit/index.js index acac5c107..d32f2792a 100644 --- a/src/extensions/scratch3_microbit/index.js +++ b/src/extensions/scratch3_microbit/index.js @@ -73,6 +73,11 @@ class MicroBit { this._ble = null; this._runtime.registerPeripheralExtension(extensionId, this); + /** + * The id of the extension this peripheral belongs to. + */ + this._extensionId = extensionId; + /** * The most recently received value for each sensor. * @type {Object.} @@ -200,7 +205,7 @@ class MicroBit { * Called by the runtime when user wants to scan for a peripheral. */ scan () { - this._ble = new BLE(this._runtime, { + this._ble = new BLE(this._runtime, this._extensionId, { filters: [ {services: [BLEUUID.service]} ] diff --git a/src/extensions/scratch3_wedo2/index.js b/src/extensions/scratch3_wedo2/index.js index 6e6122f4a..f4ccdc3ce 100644 --- a/src/extensions/scratch3_wedo2/index.js +++ b/src/extensions/scratch3_wedo2/index.js @@ -357,6 +357,11 @@ class WeDo2 { this._runtime = runtime; this._runtime.on('PROJECT_STOP_ALL', this.stopAll.bind(this)); + /** + * The id of the extension this peripheral belongs to. + */ + this._extensionId = extensionId; + /** * A list of the ids of the motors or sensors in ports 1 and 2. * @type {string[]} @@ -549,7 +554,7 @@ class WeDo2 { * Called by the runtime when user wants to scan for a WeDo 2.0 peripheral. */ scan () { - this._ble = new BLE(this._runtime, { + this._ble = new BLE(this._runtime, this._extensionId, { filters: [{ services: [BLEService.DEVICE_SERVICE] }], diff --git a/src/io/ble.js b/src/io/ble.js index 14f927e02..af0dc3086 100644 --- a/src/io/ble.js +++ b/src/io/ble.js @@ -8,10 +8,11 @@ class BLE extends JSONRPCWebSocket { * A BLE peripheral socket object. It handles connecting, over web sockets, to * BLE peripherals, and reading and writing data to them. * @param {Runtime} runtime - the Runtime for sending/receiving GUI update events. + * @param {string} extensionId - the id of the extension using this socket. * @param {object} peripheralOptions - the list of options for peripheral discovery. * @param {object} connectCallback - a callback for connection. */ - constructor (runtime, peripheralOptions, connectCallback) { + constructor (runtime, extensionId, peripheralOptions, connectCallback) { const ws = new WebSocket(ScratchLinkWebSocket); super(ws); @@ -24,6 +25,7 @@ class BLE extends JSONRPCWebSocket { this._connectCallback = connectCallback; this._connected = false; this._characteristicDidChangeCallback = null; + this._extensionId = extensionId; this._peripheralOptions = peripheralOptions; this._discoverTimeoutID = null; this._runtime = runtime; @@ -172,7 +174,10 @@ class BLE extends JSONRPCWebSocket { _sendError (/* e */) { this.disconnect(); // log.error(`BLE error: ${JSON.stringify(e)}`); - this._runtime.emit(this._runtime.constructor.PERIPHERAL_ERROR); + this._runtime.emit(this._runtime.constructor.PERIPHERAL_ERROR, { + message: `Scratch lost connection to`, + extensionId: this._extensionId + }); } _sendDiscoverTimeout () { diff --git a/src/io/bt.js b/src/io/bt.js index fad67d004..60e74ff00 100644 --- a/src/io/bt.js +++ b/src/io/bt.js @@ -8,11 +8,12 @@ class BT extends JSONRPCWebSocket { * A BT peripheral socket object. It handles connecting, over web sockets, to * BT peripherals, and reading and writing data to them. * @param {Runtime} runtime - the Runtime for sending/receiving GUI update events. + * @param {string} extensionId - the id of the extension using this socket. * @param {object} peripheralOptions - the list of options for peripheral discovery. * @param {object} connectCallback - a callback for connection. * @param {object} messageCallback - a callback for message sending. */ - constructor (runtime, peripheralOptions, connectCallback, messageCallback) { + constructor (runtime, extensionId, peripheralOptions, connectCallback, messageCallback) { const ws = new WebSocket(ScratchLinkWebSocket); super(ws); @@ -25,6 +26,7 @@ class BT extends JSONRPCWebSocket { this._connectCallback = connectCallback; this._connected = false; this._characteristicDidChangeCallback = null; + this._extensionId = extensionId; this._peripheralOptions = peripheralOptions; this._discoverTimeoutID = null; this._messageCallback = messageCallback; @@ -115,7 +117,10 @@ class BT extends JSONRPCWebSocket { _sendError (/* e */) { this.disconnect(); // log.error(`BT error: ${JSON.stringify(e)}`); - this._runtime.emit(this._runtime.constructor.PERIPHERAL_ERROR); + this._runtime.emit(this._runtime.constructor.PERIPHERAL_ERROR, { + message: `Scratch lost connection to`, + extensionId: this._extensionId + }); } _sendDiscoverTimeout () { diff --git a/src/virtual-machine.js b/src/virtual-machine.js index a118ab47c..5be8a553c 100644 --- a/src/virtual-machine.js +++ b/src/virtual-machine.js @@ -111,8 +111,8 @@ class VirtualMachine extends EventEmitter { this.runtime.on(Runtime.PERIPHERAL_CONNECTED, () => this.emit(Runtime.PERIPHERAL_CONNECTED) ); - this.runtime.on(Runtime.PERIPHERAL_ERROR, () => - this.emit(Runtime.PERIPHERAL_ERROR) + this.runtime.on(Runtime.PERIPHERAL_ERROR, data => + this.emit(Runtime.PERIPHERAL_ERROR, data) ); this.runtime.on(Runtime.PERIPHERAL_SCAN_TIMEOUT, () => this.emit(Runtime.PERIPHERAL_SCAN_TIMEOUT)