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

Commit

Permalink
enhancement: new improved Serial Monitor with @rmoesbergen
Browse files Browse the repository at this point in the history
  • Loading branch information
koen1711 committed Nov 9, 2023
1 parent 62efe15 commit 1e7d727
Show file tree
Hide file tree
Showing 5 changed files with 513 additions and 449 deletions.
78 changes: 31 additions & 47 deletions src/app/effects/robot.wired.effects.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,21 @@ import { filter } from 'rxjs/operators';
import { BackEndState } from '../state/backend.state';
import { RobotWiredState } from '../state/robot.wired.state';
import {DialogState} from "../state/dialog.state";
import ArduinoWebserial from "../services/webserial/ArduinoWebserial";

@Injectable({
providedIn: 'root',
})

export class RobotWiredEffects {
private webserial: ArduinoWebserial;

constructor(
private robotWiredState: RobotWiredState,
private backEndState: BackEndState,
private dialogState: DialogState,
) {
this.webserial = new ArduinoWebserial(this.robotWiredState);
this.backEndState.backEndMessages$
.pipe(filter(message => !!message))
.subscribe(message => {
Expand All @@ -28,58 +31,39 @@ export class RobotWiredEffects {
this.dialogState.isSerialOutputListening$
.pipe(filter(isListening => !!isListening))
.subscribe(async () => {
if (this.robotWiredState.getSerialPort() == null) {
return;
}
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 readableStream = this.robotWiredState.getSerialPort().readable;
const robotWiredState = this.robotWiredState;

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 writableStream = new WritableStream({
write: async (chunk) => {
let entireStr = new TextDecoder("utf-8").decode(chunk);
const date = new Date();

const pipePromise = readableStream.pipeTo(writableStream, { signal: abortController.signal });

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);
function makeString (chunkedStr: string) {
const serialData = { time: date, data: chunkedStr };
robotWiredState.setIncomingSerialData(serialData);
}
}).then(
async () => {
if (this.robotWiredState.getSerialPort() == null) {
return;
}
await this.robotWiredState.getSerialPort().close();
await this.robotWiredState.getSerialPort().open({baudRate: 115200});
this.robotWiredState.setIsSerialOutputStillListening(false);
function makeStringFinal (chunkedStr: string) {
const serialData = { time: date, data: chunkedStr };
robotWiredState.setFinalSerialData(serialData);
}
);
console.log("entireStr: " + entireStr.replace(/\n/g, "\\n"));
let icount = 0;
let i;
do {
console.log("icount: " + icount);
i = entireStr.indexOf("\n");
let part = entireStr.slice(0, i);
entireStr = entireStr.slice(i + 1);
console.log("part: " + part);
console.log("entireStr: " + entireStr);
makeStringFinal(part);
icount++;
} while (i != -1);
if (entireStr !== '\n') { makeString(entireStr); }
},
});

this.robotWiredState.setAbortController(abortController);
} catch (e) {
console.log(e);
}
this.webserial.serialMonitor(writableStream);
});
}

Expand Down
4 changes: 2 additions & 2 deletions src/app/modules/core/dialogs/upload/upload.dialog.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {Component, Inject} from '@angular/core';
import {MAT_DIALOG_DATA, MatDialogRef} from '@angular/material/dialog';
import {TranslateService} from "@ngx-translate/core";
import ArduinoUploader from "../../../../services/webserial/ArduinoUploader";
import ArduinoWebserial from "../../../../services/webserial/ArduinoWebserial";
import {DialogState} from "../../../../state/dialog.state";
import {RobotWiredState} from "../../../../state/robot.wired.state";

Expand All @@ -15,7 +15,7 @@ export class UploadDialog {
progressBarWidth: number = 0;
uploadFailed: boolean = false;
protected readonly document = document;
private upload = new ArduinoUploader(this.robotWiredState);
private upload = new ArduinoWebserial(this.robotWiredState);

constructor(
public dialogRef: MatDialogRef<UploadDialog>,
Expand Down
Loading

0 comments on commit 1e7d727

Please sign in to comment.