From 631e356b06f02503b2cff5516094d3e869f682d1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Joan=20Guill=C3=A9n=20i=20Pelegay?= Date: Mon, 22 May 2023 21:53:10 +0200 Subject: [PATCH] Updating chrome plugin to v8 to improve connections --- src/platforms/web/chromium/crx/bundle.js | 43 ++++++++++--------- src/platforms/web/chromium/crx/manifest.json | 5 ++- src/platforms/web/chromium/root/index.html | 4 +- .../web/chromium/root/plugin/arduino.js | 15 ++++++- 4 files changed, 41 insertions(+), 26 deletions(-) diff --git a/src/platforms/web/chromium/crx/bundle.js b/src/platforms/web/chromium/crx/bundle.js index 40a14537..dfcf3cdd 100644 --- a/src/platforms/web/chromium/crx/bundle.js +++ b/src/platforms/web/chromium/crx/bundle.js @@ -44,7 +44,7 @@ Dispatcher.prototype.getBoard = function (boardId) { Dispatcher.prototype.closeSerial = function (boardId) { Boards[boardId].reset(); - Boards[boardId].sp.close(); + if (Boards[boardId].connectionId >= 0) {Boards[boardId].sp.close();} Boards[boardId] = null; }; @@ -564,27 +564,27 @@ function SerialPort(path, options, openImmediately, callback) { this.options = convertOptions(options); this.options.serial.onReceiveError.addListener(function(info){ - - switch (info.error) { - - case 'disconnected': - case 'device_lost': - case 'system_error': - err = new Error('Disconnected'); - // send notification of disconnect - if (self.options.disconnectedCallback) { - self.options.disconnectedCallback(err); - } else { - self.emit('disconnect', err); - } - if(self.connectionId >= 0){ - self.close(); - } - break; - case 'timeout': - break; + if (self.connectionId == info.connectionId) { + switch (info.error) { + + case 'disconnected': + case 'device_lost': + case 'system_error': + err = new Error('Disconnected'); + // send notification of disconnect + if (self.options.disconnectedCallback) { + self.options.disconnectedCallback(err); + } else { + self.emit('disconnect', err); + } + if(self.connectionId >= 0){ + self.close(); + } + break; + case 'timeout': + break; + } } - }); this.path = path; @@ -8029,6 +8029,7 @@ function Board(port, options, callback) { }.bind(this)); this.transport.on("disconnect", function() { + this.version.major = null; this.emit("disconnect"); }.bind(this)); diff --git a/src/platforms/web/chromium/crx/manifest.json b/src/platforms/web/chromium/crx/manifest.json index 497807bf..c8fca161 100644 --- a/src/platforms/web/chromium/crx/manifest.json +++ b/src/platforms/web/chromium/crx/manifest.json @@ -1,7 +1,7 @@ { "name": "Snap4Arduino connector", "description": "Connect Berkeley Snap! to Arduino boards", - "version": "7.0", + "version": "8.0", "manifest_version": 2, "app": { "background": { @@ -26,7 +26,8 @@ "https://educaciodigital.cat/*", "https://cesire.cat/*", "https://www.robolot.online/*", - "https://campus.innovadidactic.com/*" + "https://campus.innovadidactic.com/*", + "https://snap2make.click/*" ], "accepts_tls_channel_id": false }, diff --git a/src/platforms/web/chromium/root/index.html b/src/platforms/web/chromium/root/index.html index 6da14339..0e5ca09c 100644 --- a/src/platforms/web/chromium/root/index.html +++ b/src/platforms/web/chromium/root/index.html @@ -66,8 +66,8 @@ } world = new WorldMorph(document.getElementById('world')); - // keepAlive should be handled at the plugin side - world.Arduino.keepAlive = false; + // keeping Alive... we need to ask to the plugin side continuosly + world.Arduino.keepAlive = true; ide = new IDE_Morph(); ide.openIn(world); diff --git a/src/platforms/web/chromium/root/plugin/arduino.js b/src/platforms/web/chromium/root/plugin/arduino.js index 5601428b..af4a4037 100644 --- a/src/platforms/web/chromium/root/plugin/arduino.js +++ b/src/platforms/web/chromium/root/plugin/arduino.js @@ -37,6 +37,18 @@ window.onunload = function (evt) { ide.sprites.asArray().forEach(function (each) { each.arduino.disconnect(true); }); }; +Arduino.prototype.keepAlive = function () { + this.board.getVersion(); + if (Arduino.keepAlive) { + if (this.board.version.major == null) { + // Connection dropped! Let's disconnect! + this.gotUnplugged = true; + this.board.sp.close(); + this.closeHandler(); + } + } +}; + Arduino.prototype.connect = function (port) { var myself = this; @@ -59,7 +71,7 @@ Arduino.prototype.connect = function (port) { // We need to populate the board with functions that make use of the browser plugin myself.populateBoard(myself.board); - myself.keepAliveIntervalID = setInterval(function() { myself.keepAlive() }, 5000); + myself.keepAliveIntervalID = setInterval(function() { myself.keepAlive() }, 3000); // These should be handled at plugin side // myself.board.sp.on('disconnect', myself.disconnectHandler); @@ -127,6 +139,7 @@ Arduino.prototype.populateBoard = function (board) { }; board.i2cReadOnce = function (address, reg, callback) { postal.sendCommand('i2cReadOnce', [board.id, address, reg], function (response) { board['i2cResponse-' + Number(address)] = response; }); }; board.i2cWriteReg = function (address, reg, byte) {postal.sendCommand('i2cWriteReg', [board.id, address, reg, byte]); }; + board.getVersion = function () { postal.sendCommand('getBoard', [board.id], function(response) {board.version.major = response.version.major}); }; }; // Fake Buffer definition, needed by some Firmata extensions