mirror of
https://github.com/Vale54321/schafkop-neu.git
synced 2025-12-15 11:29:32 +01:00
feat: enhance serial communication handling and logging in backend services
This commit is contained in:
@@ -65,10 +65,10 @@ export class SerialController {
|
||||
res.write(`data: ${JSON.stringify(data)}\n\n`);
|
||||
};
|
||||
|
||||
const offData = this.serial.on('data', (line) => sendEvent('data', { line }));
|
||||
const offOpen = this.serial.on('open', () => sendEvent('open', { ts: Date.now() }));
|
||||
const offClose = this.serial.on('close', () => sendEvent('close', { ts: Date.now() }));
|
||||
const offErr = this.serial.on('error', (err) => sendEvent('error', { message: String(err) }));
|
||||
const offData = this.serial.on('data', (line) => sendEvent('data', line));
|
||||
const offOpen = this.serial.on('open', () => sendEvent('open', ''));
|
||||
const offClose = this.serial.on('close', () => sendEvent('close', ''));
|
||||
const offErr = this.serial.on('error', (err) => sendEvent('error', String(err)));
|
||||
|
||||
console.log('[SerialController] SSE client connected');
|
||||
|
||||
|
||||
@@ -22,6 +22,23 @@ export class SerialService implements OnModuleDestroy, OnModuleInit {
|
||||
private emitter = new EventEmitter();
|
||||
private port: any = null;
|
||||
private parser: any = null;
|
||||
private verbose = true; // internal debug; does not change emitted payload format
|
||||
|
||||
constructor() {
|
||||
// Wrap emitter.emit to log every emitted event centrally
|
||||
const origEmit = this.emitter.emit.bind(this.emitter);
|
||||
this.emitter.emit = (event: string, ...args: any[]) => {
|
||||
if (this.verbose) {
|
||||
try {
|
||||
const printable = args.map(a => typeof a === 'object' ? JSON.stringify(a) : String(a));
|
||||
console.log('[SerialService][EVENT]', event, printable.join(' '));
|
||||
} catch {
|
||||
console.log('[SerialService][EVENT]', event, args.length, 'arg(s)');
|
||||
}
|
||||
}
|
||||
return origEmit(event, ...args);
|
||||
};
|
||||
}
|
||||
|
||||
async listPorts(): Promise<PortInfo[]> {
|
||||
console.log('[SerialService] listPorts()');
|
||||
@@ -70,9 +87,11 @@ export class SerialService implements OnModuleDestroy, OnModuleInit {
|
||||
console.error('[SerialService] port error', err);
|
||||
this.emitter.emit('error', err?.message ?? String(err));
|
||||
});
|
||||
this.port.on('close', () => { this.emitter.emit('close'); });
|
||||
this.parser.on('data', (line: string) => {
|
||||
console.log('[SerialService] RX:', line);
|
||||
this.emitter.emit('data', line);
|
||||
const clean = line.replace(/[\r\n]+$/, '');
|
||||
if (this.verbose) console.log('[SerialService] RX:', clean);
|
||||
this.emitter.emit('data', clean);
|
||||
});
|
||||
|
||||
// open the port
|
||||
@@ -134,6 +153,11 @@ export class SerialService implements OnModuleDestroy, OnModuleInit {
|
||||
this.emitter.on(event, cb);
|
||||
return () => this.emitter.off(event, cb);
|
||||
}
|
||||
// Allow subscription to extended events (disconnect, drain, etc.)
|
||||
onAny(event: string, cb: (...args: any[]) => void) {
|
||||
this.emitter.on(event, cb);
|
||||
return () => this.emitter.off(event, cb);
|
||||
}
|
||||
|
||||
onModuleDestroy() {
|
||||
this.close();
|
||||
|
||||
Reference in New Issue
Block a user