diff --git a/package-lock.json b/package-lock.json index a2db6220..45d14ec5 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "arduino-create-agent-js-client", - "version": "1.0.4", + "version": "1.0.5", "lockfileVersion": 1, "requires": true, "dependencies": { diff --git a/package.json b/package.json index 52f2fe3a..d163a7d5 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "arduino-create-agent-js-client", - "version": "1.0.4", + "version": "1.0.5", "description": "JS module providing discovery of the Arduino Create Plugin and communication with it", "main": "./src/index.js", "module": "es/index.js", diff --git a/src/boardConfiguration.js b/src/boardConfiguration.js index 6b29aab5..2c843157 100644 --- a/src/boardConfiguration.js +++ b/src/boardConfiguration.js @@ -33,9 +33,13 @@ export default class BoardConfiguration { this.daemon = daemon; this.serialMonitorContent = ''; this.configuring = new BehaviorSubject({ status: this.CONFIGURE_NOPE }); - this.configureDone = this.configuring.pipe(filter(configure => configure.status === this.CONFIGURE_DONE)); + this.configureDone = this.configuring.pipe(filter(configure => configure.status === this.CONFIGURE_DONE)) + .pipe(first()) + .pipe(takeUntil(this.configuring.pipe(filter(configure => configure.status === this.CONFIGURE_ERROR)))); + this.configureError = this.configuring.pipe(filter(configure => configure.status === this.CONFIGURE_ERROR)) + .pipe(first()) + .pipe(takeUntil(this.configureDone)); this.configureInProgress = this.configuring.pipe(filter(configure => configure.status === this.CONFIGURE_IN_PROGRESS)); - this.configureError = this.configuring.pipe(filter(configure => configure.status === this.CONFIGURE_ERROR)); this.daemon.serialMonitorMessages.subscribe(message => { this.serialMonitorContent += message; }); @@ -45,8 +49,8 @@ export default class BoardConfiguration { this.configuring.next({ status: this.CONFIGURE_IN_PROGRESS, msg: 'Starting board configuration...' }); } - notifyError(msg) { - this.configuring.next({ status: this.CONFIGURE_ERROR, msg: msg, err: msg}); + notifyError(err, msg) { + this.configuring.next({ status: this.CONFIGURE_ERROR, err, msg }); } /** @@ -189,7 +193,7 @@ export default class BoardConfiguration { return; } - this.daemon.uploadingDone.pipe(first()).subscribe(() => { + this.daemon.uploadingDone.subscribe(() => { this.configuring.next({ status: this.CONFIGURE_IN_PROGRESS, msg: 'Provisioning sketch uploaded successfully. Opening serial monitor...' @@ -206,7 +210,7 @@ export default class BoardConfiguration { status: this.CONFIGURE_IN_PROGRESS, msg: 'CSR generated. Creating device...' }); - return createDeviceCb(csr) + return createDeviceCb(csr); }) .then(data => { this.configuring.next({ @@ -230,10 +234,10 @@ export default class BoardConfiguration { err: error.toString() }); }); - this.daemon.openSerialMonitor(board.port, BAUDRATE); + this.daemon.openSerialMonitor(board.port, BAUDRATE); }); - this.daemon.uploadingError.pipe(first()).subscribe(upload => { + this.daemon.uploadingError.subscribe(upload => { this.configuring.next({ status: this.CONFIGURE_ERROR, err: `Couldn't configure board at port ${board.port}. Upload failed with error: ${upload.err}` }); }); diff --git a/src/daemon.js b/src/daemon.js index d0692089..21423e13 100644 --- a/src/daemon.js +++ b/src/daemon.js @@ -39,9 +39,13 @@ export default class Daemon { this.serialMonitorOpened = new BehaviorSubject(false); this.serialMonitorMessages = new Subject(); this.uploading = new BehaviorSubject({ status: this.UPLOAD_NOPE }); - this.uploadingDone = this.uploading.pipe(filter(upload => upload.status === this.UPLOAD_DONE)); + this.uploadingDone = this.uploading.pipe(filter(upload => upload.status === this.UPLOAD_DONE)) + .pipe(first()) + .pipe(takeUntil(this.uploading.pipe(filter(upload => upload.status === this.UPLOAD_ERROR)))); + this.uploadingError = this.uploading.pipe(filter(upload => upload.status === this.UPLOAD_ERROR)) + .pipe(first()) + .pipe(takeUntil(this.uploadingDone)); this.uploadInProgress = this.uploading.pipe(filter(upload => upload.status === this.UPLOAD_IN_PROGRESS)); - this.uploadingError = this.uploading.pipe(filter(upload => upload.status === this.UPLOAD_ERROR)); this.devicesList = new BehaviorSubject({ serial: [], network: [] @@ -57,6 +61,10 @@ export default class Daemon { .subscribe(() => this.closeAllPorts()); } + notifyUploadError(err) { + this.uploading.next({ status: this.UPLOAD_ERROR, err }); + } + openChannel(cb) { this.channelOpen .subscribe(open => { diff --git a/src/socket-daemon.js b/src/socket-daemon.js index c298d021..f8607530 100644 --- a/src/socket-daemon.js +++ b/src/socket-daemon.js @@ -65,8 +65,12 @@ export default class SocketDaemon extends Daemon { this.pluginURL = null; this.downloading = new BehaviorSubject({ status: DOWNLOAD_NOPE }); - this.downloadingDone = this.downloading.pipe(filter(download => download.status === DOWNLOAD_DONE)); - this.downloadingError = this.downloading.pipe(filter(download => download.status === DOWNLOAD_ERROR)); + this.downloadingDone = this.downloading.pipe(filter(download => download.status === DOWNLOAD_DONE)) + .pipe(first()) + .pipe(takeUntil(this.downloading.pipe(filter(download => download.status === this.DOWNLOAD_ERROR)))); + this.downloadingError = this.downloading.pipe(filter(download => download.status === DOWNLOAD_ERROR)) + .pipe(first()) + .pipe(takeUntil(this.downloadingDone)); this.openChannel(() => this.socket.emit('command', 'list')); @@ -92,6 +96,10 @@ export default class SocketDaemon extends Daemon { }); } + notifyDownloadError(err) { + this.downloading.next({ status: this.DOWNLOAD_ERROR, err }); + } + /** * Look for the agent endpoint. * First search in http://LOOPBACK_ADDRESS, after in https://LOOPBACK_HOSTNAME if in Chrome or Firefox, otherwise vice versa. @@ -443,7 +451,6 @@ export default class SocketDaemon extends Daemon { this.serialMonitorOpened.pipe(filter(open => !open)) .pipe(first()) .subscribe(() => { - fetch(`${this.pluginURL}/upload`, { method: 'POST', headers: { @@ -473,7 +480,10 @@ export default class SocketDaemon extends Daemon { * Interrupt upload */ stopUploadCommand() { - this.uploading.next(false); + this.uploading.next({ + status: this.UPLOAD_ERROR, + err: 'upload stopped' + }); this.socket.emit('command', 'killprogrammer'); } }