Skip to content

Commit a676970

Browse files
authored
Merge pull request #1607 from evhan55/multiple-alerts
Show customized alerts on hardware extension peripheral errors.
2 parents f467d2d + cce03d0 commit a676970

File tree

6 files changed

+34
-9
lines changed

6 files changed

+34
-9
lines changed

src/extensions/scratch3_ev3/index.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -408,6 +408,11 @@ class EV3 {
408408
this._runtime = runtime;
409409
this._runtime.on('PROJECT_STOP_ALL', this.stopAll.bind(this));
410410

411+
/**
412+
* The id of the extension this peripheral belongs to.
413+
*/
414+
this._extensionId = extensionId;
415+
411416
/**
412417
* A list of the names of the sensors connected in ports 1,2,3,4.
413418
* @type {string[]}
@@ -548,7 +553,7 @@ class EV3 {
548553
* Called by the runtime when user wants to scan for an EV3 peripheral.
549554
*/
550555
scan () {
551-
this._bt = new BT(this._runtime, {
556+
this._bt = new BT(this._runtime, this._extensionId, {
552557
majorDeviceClass: 8,
553558
minorDeviceClass: 1
554559
}, this._onConnect, this._onMessage);

src/extensions/scratch3_microbit/index.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,11 @@ class MicroBit {
7373
this._ble = null;
7474
this._runtime.registerPeripheralExtension(extensionId, this);
7575

76+
/**
77+
* The id of the extension this peripheral belongs to.
78+
*/
79+
this._extensionId = extensionId;
80+
7681
/**
7782
* The most recently received value for each sensor.
7883
* @type {Object.<string, number>}
@@ -200,7 +205,7 @@ class MicroBit {
200205
* Called by the runtime when user wants to scan for a peripheral.
201206
*/
202207
scan () {
203-
this._ble = new BLE(this._runtime, {
208+
this._ble = new BLE(this._runtime, this._extensionId, {
204209
filters: [
205210
{services: [BLEUUID.service]}
206211
]

src/extensions/scratch3_wedo2/index.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -357,6 +357,11 @@ class WeDo2 {
357357
this._runtime = runtime;
358358
this._runtime.on('PROJECT_STOP_ALL', this.stopAll.bind(this));
359359

360+
/**
361+
* The id of the extension this peripheral belongs to.
362+
*/
363+
this._extensionId = extensionId;
364+
360365
/**
361366
* A list of the ids of the motors or sensors in ports 1 and 2.
362367
* @type {string[]}
@@ -549,7 +554,7 @@ class WeDo2 {
549554
* Called by the runtime when user wants to scan for a WeDo 2.0 peripheral.
550555
*/
551556
scan () {
552-
this._ble = new BLE(this._runtime, {
557+
this._ble = new BLE(this._runtime, this._extensionId, {
553558
filters: [{
554559
services: [BLEService.DEVICE_SERVICE]
555560
}],

src/io/ble.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,11 @@ class BLE extends JSONRPCWebSocket {
88
* A BLE peripheral socket object. It handles connecting, over web sockets, to
99
* BLE peripherals, and reading and writing data to them.
1010
* @param {Runtime} runtime - the Runtime for sending/receiving GUI update events.
11+
* @param {string} extensionId - the id of the extension using this socket.
1112
* @param {object} peripheralOptions - the list of options for peripheral discovery.
1213
* @param {object} connectCallback - a callback for connection.
1314
*/
14-
constructor (runtime, peripheralOptions, connectCallback) {
15+
constructor (runtime, extensionId, peripheralOptions, connectCallback) {
1516
const ws = new WebSocket(ScratchLinkWebSocket);
1617
super(ws);
1718

@@ -24,6 +25,7 @@ class BLE extends JSONRPCWebSocket {
2425
this._connectCallback = connectCallback;
2526
this._connected = false;
2627
this._characteristicDidChangeCallback = null;
28+
this._extensionId = extensionId;
2729
this._peripheralOptions = peripheralOptions;
2830
this._discoverTimeoutID = null;
2931
this._runtime = runtime;
@@ -172,7 +174,10 @@ class BLE extends JSONRPCWebSocket {
172174
_sendError (/* e */) {
173175
this.disconnect();
174176
// log.error(`BLE error: ${JSON.stringify(e)}`);
175-
this._runtime.emit(this._runtime.constructor.PERIPHERAL_ERROR);
177+
this._runtime.emit(this._runtime.constructor.PERIPHERAL_ERROR, {
178+
message: `Scratch lost connection to`,
179+
extensionId: this._extensionId
180+
});
176181
}
177182

178183
_sendDiscoverTimeout () {

src/io/bt.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,12 @@ class BT extends JSONRPCWebSocket {
88
* A BT peripheral socket object. It handles connecting, over web sockets, to
99
* BT peripherals, and reading and writing data to them.
1010
* @param {Runtime} runtime - the Runtime for sending/receiving GUI update events.
11+
* @param {string} extensionId - the id of the extension using this socket.
1112
* @param {object} peripheralOptions - the list of options for peripheral discovery.
1213
* @param {object} connectCallback - a callback for connection.
1314
* @param {object} messageCallback - a callback for message sending.
1415
*/
15-
constructor (runtime, peripheralOptions, connectCallback, messageCallback) {
16+
constructor (runtime, extensionId, peripheralOptions, connectCallback, messageCallback) {
1617
const ws = new WebSocket(ScratchLinkWebSocket);
1718
super(ws);
1819

@@ -25,6 +26,7 @@ class BT extends JSONRPCWebSocket {
2526
this._connectCallback = connectCallback;
2627
this._connected = false;
2728
this._characteristicDidChangeCallback = null;
29+
this._extensionId = extensionId;
2830
this._peripheralOptions = peripheralOptions;
2931
this._discoverTimeoutID = null;
3032
this._messageCallback = messageCallback;
@@ -115,7 +117,10 @@ class BT extends JSONRPCWebSocket {
115117
_sendError (/* e */) {
116118
this.disconnect();
117119
// log.error(`BT error: ${JSON.stringify(e)}`);
118-
this._runtime.emit(this._runtime.constructor.PERIPHERAL_ERROR);
120+
this._runtime.emit(this._runtime.constructor.PERIPHERAL_ERROR, {
121+
message: `Scratch lost connection to`,
122+
extensionId: this._extensionId
123+
});
119124
}
120125

121126
_sendDiscoverTimeout () {

src/virtual-machine.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,8 +111,8 @@ class VirtualMachine extends EventEmitter {
111111
this.runtime.on(Runtime.PERIPHERAL_CONNECTED, () =>
112112
this.emit(Runtime.PERIPHERAL_CONNECTED)
113113
);
114-
this.runtime.on(Runtime.PERIPHERAL_ERROR, () =>
115-
this.emit(Runtime.PERIPHERAL_ERROR)
114+
this.runtime.on(Runtime.PERIPHERAL_ERROR, data =>
115+
this.emit(Runtime.PERIPHERAL_ERROR, data)
116116
);
117117
this.runtime.on(Runtime.PERIPHERAL_SCAN_TIMEOUT, () =>
118118
this.emit(Runtime.PERIPHERAL_SCAN_TIMEOUT)

0 commit comments

Comments
 (0)