diff --git a/src/app/effects/dialog.effects.ts b/src/app/effects/dialog.effects.ts index bc2e2fe2..ffd4c14e 100644 --- a/src/app/effects/dialog.effects.ts +++ b/src/app/effects/dialog.effects.ts @@ -64,15 +64,14 @@ export class DialogEffects { // If the isSerialOutputWindowOpen is set to true open the dialog this.dialogState.isSerialOutputWindowOpen$ .subscribe(() => { - console.log('Serial output window is open ', this.dialogState.getIsSerialOutputWindowOpen()); if (this.dialogState.getIsSerialOutputWindowOpen() !== true) - return; + return; this.dialog.open(SerialOutputComponent, { - width: "800px", - disableClose: true, - hasBackdrop: false, + width: "800px", + disableClose: true, + hasBackdrop: false, }).afterClosed().subscribe(() => { - this.dialogState.setIsSerialOutputWindowOpen(false); + this.dialogState.setIsSerialOutputWindowOpen(false); }); }); diff --git a/src/app/effects/robot.wired.effects.ts b/src/app/effects/robot.wired.effects.ts index 1bd7e26b..05035199 100644 --- a/src/app/effects/robot.wired.effects.ts +++ b/src/app/effects/robot.wired.effects.ts @@ -18,63 +18,68 @@ export class RobotWiredEffects { this.backEndState.backEndMessages$ .pipe(filter(message => !!message)) .subscribe(message => { - if (message.event == 'SERIAL_DATA') - { - const serialData = {time: new Date(), data: String(message.payload)} - this.robotWiredState.setIncomingSerialData(serialData); - } + if (message.event == 'SERIAL_DATA') + { + const serialData = {time: new Date(), data: String(message.payload)} + this.robotWiredState.setIncomingSerialData(serialData); + } }); this.dialogState.isSerialOutputListening$ .pipe(filter(isListening => !!isListening)) .subscribe(async () => { - if (this.robotWiredState.getSerialPort() == null) { - return; - } - console.log("Serial output listening"); - try { - try { - await this.robotWiredState.getSerialPort().open({ baudRate: 115200 }); - } catch (e) { - if (!e.message.includes("already open")) { - console.log(e); - } + if (this.robotWiredState.getSerialPort() == null) { + return; } - this.robotWiredState.setIsSerialOutputStillListening(true); - const abortController = new AbortController(); - - const readableStream = this.robotWiredState.getSerialPort().readable; + try { + try { + await this.robotWiredState.getSerialPort().open({ baudRate: 115200 }); + } catch (e) { + if (!e.message.includes("already open")) { + console.log(e); + } + } + this.robotWiredState.setIsSerialOutputStillListening(true); + const abortController = new AbortController(); - const writableStream = new WritableStream({ - write: async (chunk) => { - const str = new TextDecoder("utf-8").decode(chunk); - const serialData = { time: new Date(), data: str }; - this.robotWiredState.setIncomingSerialData(serialData); - }, - }); + const readableStream = this.robotWiredState.getSerialPort().readable; - const pipePromise = readableStream.pipeTo(writableStream, { signal: abortController.signal }); + const writableStream = new WritableStream({ + write: async (chunk) => { + const str = new TextDecoder("utf-8").decode(chunk); + const serialData = { time: new Date(), data: str }; + this.robotWiredState.setIncomingSerialData(serialData); + }, + }); - pipePromise.catch((error) => { + const pipePromise = readableStream.pipeTo(writableStream, { signal: abortController.signal }); - if (error.toString().includes('Upload started')) { - writableStream.abort("Upload started") - console.log('Stream aborted'); - } else { - console.error('Error while piping stream:', error); - } - }).then( - async () => { - await this.robotWiredState.getSerialPort().close(); - await this.robotWiredState.getSerialPort().open({baudRate: 115200}); - this.robotWiredState.setIsSerialOutputStillListening(false); - } - ); + pipePromise.catch((error) => { + if (error.toString().includes('Upload started')) { + writableStream.abort("Upload started") + console.log('Stream aborted'); + } else if (error.toString().includes('The device has been lost.')) { + this.robotWiredState.setSerialPort(null); + console.log('Device disconnected'); + } else { + this.robotWiredState.setSerialPort(null); + console.error('Error while piping stream:', error); + } + }).then( + async () => { + if (this.robotWiredState.getSerialPort() == null) { + return; + } + await this.robotWiredState.getSerialPort().close(); + await this.robotWiredState.getSerialPort().open({baudRate: 115200}); + this.robotWiredState.setIsSerialOutputStillListening(false); + } + ); - this.robotWiredState.setAbortController(abortController); - } catch (e) { - console.log(e); - } + this.robotWiredState.setAbortController(abortController); + } catch (e) { + console.log(e); + } }); }