Default Log

This commit is contained in:
chk
2026-06-14 12:47:12 +02:00
parent 988e8ec752
commit e6abe047dc
3 changed files with 31 additions and 25 deletions

View File

@@ -9,6 +9,7 @@
const fsp = require('fs/promises');
const path = require('path');
const cfg = require('../config');
const units = require('../gcode/units');
const { ApiError } = require('../errors');
const ID_RE = /^[a-z0-9_]+$/;
@@ -55,25 +56,35 @@ function splitLines(text) {
.filter((l) => l.trim().length > 0);
}
/** Liest ein Programm: { id, name, lines (Grad, mit Kommentaren), meta }. */
/** Liest ein Programm: { id, name, cursor, lines (Grad, sauber ohne '!'), meta }. */
async function read(id) {
assertValidId(id);
if (!(await exists(id))) {
throw new ApiError(404, 'PROGRAM_NOT_FOUND', `no program '${id}'`);
}
const text = await fsp.readFile(gcodePath(id), 'utf8');
const lines = splitLines(text);
let meta = {};
try {
meta = JSON.parse(await fsp.readFile(jsonPath(id), 'utf8'));
} catch {
/* Sidecar ist optional */
}
return { id, name: meta.name || id, lines, meta };
// Migration: alter '!'-Cursor-Marker in .gcode → Cursor liegt jetzt im .json.
let legacyCursor = null;
const lines = splitLines(text).map((line, i) => {
if (units.hasCursorMarker(line)) { legacyCursor = i; return units.removeCursorMarker(line); }
return line;
});
const cursor = meta.cursor ?? legacyCursor ?? 0;
return { id, name: meta.name || id, cursor, lines, meta };
}
/** Schreibt .gcode + .json. lines = gespeicherte Zeilen (Grad, inkl. Kommentar/Cursor). */
async function write(id, { name, lines }) {
/**
* Schreibt .gcode + .json.
* lines = saubere G-Code-Zeilen (Grad, ohne '!'-Cursor-Marker).
* cursor = Cursor-Index (landet im .json, nicht in .gcode).
*/
async function write(id, { name, lines, cursor = 0 }) {
assertValidId(id);
await ensureDir();
const now = new Date().toISOString();
@@ -88,6 +99,7 @@ async function write(id, { name, lines }) {
id,
name: name || id,
lineCount: lines.length,
cursor,
angleUnit: cfg.storeAngleUnit,
createdAt,
updatedAt: now,