Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion src/extensions/scratch3_ev3/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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[]}
Expand Down Expand Up @@ -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);
Expand Down
7 changes: 6 additions & 1 deletion src/extensions/scratch3_microbit/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.<string, number>}
Expand Down Expand Up @@ -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]}
]
Expand Down
7 changes: 6 additions & 1 deletion src/extensions/scratch3_wedo2/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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[]}
Expand Down Expand Up @@ -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]
}],
Expand Down
9 changes: 7 additions & 2 deletions src/io/ble.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand All @@ -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;
Expand Down Expand Up @@ -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, {

This comment was marked as abuse.

This comment was marked as abuse.

message: `Scratch lost connection to`,
extensionId: this._extensionId
});
}

_sendDiscoverTimeout () {
Expand Down
9 changes: 7 additions & 2 deletions src/io/bt.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand All @@ -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;
Expand Down Expand Up @@ -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

This comment was marked as abuse.

This comment was marked as abuse.

});
}

_sendDiscoverTimeout () {
Expand Down
4 changes: 2 additions & 2 deletions src/virtual-machine.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down