Fix Read .grbl

This commit is contained in:
chk
2026-06-14 13:57:02 +02:00
parent c68ce406a6
commit 551dded4dc
3 changed files with 28 additions and 3 deletions

View File

@@ -47,7 +47,8 @@ class ActiveState {
}
}
/** API-Repräsentation (ActiveState). currentLine = driver-nativ (Radian). */
/** API-Repräsentation (ActiveState). currentLine = driver-nativ (Radian);
* lines = volle Programmliste in GRAD (wie gespeichert) — für die Anzeige (FShow). */
getState() {
const currentLine = this.lines.length ? units.toExecutable(this.lines[this.cursor]) : null;
return {
@@ -55,11 +56,19 @@ class ActiveState {
cursor: this.cursor,
lineCount: this.lines.length,
currentLine,
lines: this.lines,
playing: this.playing,
version: this.version,
};
}
/** FShow: lädt bei Bedarf das Default-Programm (Lesen mit implizitem log) und
* liefert den vollen Zustand inkl. Zeilenliste. */
async show() {
await this._ensureActive();
return this.getState();
}
/**
* Setzt ein Programm aktiv (FLoad). Existiert es nicht, wird es leer angelegt
* (nötig für Teaching). Ein vorher aktives Programm wird zuvor persistiert.

View File

@@ -9,8 +9,8 @@ const { ApiError } = require('../errors');
const router = express.Router();
const asyncH = (fn) => (req, res, next) => Promise.resolve(fn(req, res, next)).catch(next);
// GET /api/active
router.get('/', (req, res) => res.json(active.getState()));
// GET /api/active (FShow) — lädt bei Bedarf das Default-Programm, liefert volle Liste
router.get('/', asyncH(async (req, res) => res.json(await active.show())));
// PUT /api/active (FLoad) — existiert nicht → leer anlegen (für Teaching)
router.put(

View File

@@ -82,6 +82,22 @@ test('Stepping ohne aktives Programm → auto-lädt Default (leer → EMPTY_PROG
expect(a.programId).toBe(cfg.defaultProgramId); // Default wurde geladen
});
test('FShow ohne FLoad → auto-lädt Default und liefert volle Zeilenliste', async () => {
await store.write(cfg.defaultProgramId, {
name: 'log',
lines: [
'G90 G1 x0 y300 z0 a90.00 b-90.00 c0.00 e0.00 f1000 ;1',
'G90 G1 x10 y300 z0 a0.00 b-90.00 c0.00 e0.00 f1000 ;2',
],
});
const a = new ActiveState();
const state = await a.show(); // frisch, ohne vorheriges FLoad/FPoint
expect(state.programId).toBe(cfg.defaultProgramId);
expect(state.lineCount).toBe(2);
expect(state.lines).toHaveLength(2); // volle Liste für die Anzeige
expect(state.lines[0]).toContain('a90.00'); // in Grad, wie gespeichert
});
test('Stepping nach Neustart liest Default-Programm von Disk (FFirst ohne FLoad)', async () => {
// Simuliert: log.gcode liegt auf Disk, frischer ActiveState (wie nach Container-Neustart)
await store.write(cfg.defaultProgramId, {