Skip to content
This repository has been archived by the owner on Aug 6, 2024. It is now read-only.

Commit

Permalink
Merge pull request #77 from leaphy-robotics/stability
Browse files Browse the repository at this point in the history
enhancement: remove useless debug; fix upload system dying when you disconnect the arduino
  • Loading branch information
koen1711 authored Oct 28, 2023
2 parents 2a91ad5 + 223c39a commit 2c9cb29
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 52 deletions.
11 changes: 5 additions & 6 deletions src/app/effects/dialog.effects.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
});

Expand Down
97 changes: 51 additions & 46 deletions src/app/effects/robot.wired.effects.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
});
}

Expand Down

0 comments on commit 2c9cb29

Please sign in to comment.