Add scheduled and event-driven printing with a new web UI
The web server now runs a scheduler over schedules kept in SQLite
(~/.local/share/receipt-maker, outside the clone). A schedule is a
trigger, a receipt made of blocks, and options:
- Triggers: once, recurring (rrule-based, DST-safe), sun times,
reminders before calendar (ICS) events, RSS/JSON watchers, webhooks
with per-schedule secrets.
- Blocks, rendered fresh as one job: templated notes ({date},
{days_until:..}, rotating {choice}, trigger vars), QR, saved images,
random image, calendar agenda, feed headlines, dividers.
- Rules: quiet hours (hold/skip/ignore), daily cap, pause, catch-up
window for missed runs (never replayed), retries on connect failure
only; a mid-job drop (new PrinterDropped) is never resent.
receipt.py renders through composable Blocks (render_note/image/qr are
wrappers, output unchanged). Print history persists, with Print again.
The page moves to web/ with Print (note/image/QR + paper preview),
Schedules (presets, editor with live preview and next runs), History
and Settings tabs.
Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
+90
@@ -0,0 +1,90 @@
|
||||
// Talking to the server: every call is relative, so the /receipt/ prefix nginx adds keeps working.
|
||||
|
||||
function detailText(detail) {
|
||||
if (!Array.isArray(detail)) return detail;
|
||||
// Pydantic errors: say where, minus the "body" prefix and union tags.
|
||||
const tags = ["body", "once", "recurring", "sun", "calendar", "watch", "webhook", "note", "qr",
|
||||
"image", "random_image", "agenda", "headlines", "divider"];
|
||||
return detail.map((d) => {
|
||||
const where = (d.loc || [])
|
||||
.filter((p) => !tags.includes(p) && !/[[(]/.test(String(p)))
|
||||
.map((p) => (typeof p === "number" ? `#${p + 1}` : p.replaceAll("_", " ")));
|
||||
const msg = String(d.msg || "").replace(/^Value error, /, "");
|
||||
return where.length ? `${where.join(" › ")}: ${msg}` : msg;
|
||||
}).join("; ");
|
||||
}
|
||||
|
||||
export async function call(path, init = {}) {
|
||||
init.headers = Object.assign({"X-Requested-With": "receipt"}, init.headers || {});
|
||||
let res;
|
||||
try {
|
||||
res = await fetch(path, init);
|
||||
} catch (e) {
|
||||
return {ok: false, status: 0, text: "Network error: " + e.message, body: {}};
|
||||
}
|
||||
let body = {};
|
||||
try { body = await res.json(); } catch (e) { /* nginx pages aren't JSON */ }
|
||||
if (!("ok" in body) && !("detail" in body)) {
|
||||
// Not from this server: nginx's offline page, or its login redirect.
|
||||
const offline = [502, 503, 504].includes(res.status);
|
||||
return {ok: false, status: res.status, body,
|
||||
text: offline ? "The printer machine is offline."
|
||||
: "Signed out? Reload the page and sign in."};
|
||||
}
|
||||
return {ok: res.ok && body.ok !== false, status: res.status, body,
|
||||
text: body.output || detailText(body.detail) || res.statusText};
|
||||
}
|
||||
|
||||
export const get = (path) => call(path);
|
||||
const withJson = (method) => (path, data) => call(path, {
|
||||
method, headers: {"Content-Type": "application/json"},
|
||||
body: data === undefined ? undefined : JSON.stringify(data),
|
||||
});
|
||||
export const post = withJson("POST");
|
||||
export const put = withJson("PUT");
|
||||
export const del = withJson("DELETE");
|
||||
|
||||
// POST something that queues a print, then long-poll the job until it finishes.
|
||||
// onUpdate(text) reports progress; resolves {ok, text}.
|
||||
export async function printJob(request, onUpdate = () => {}) {
|
||||
onUpdate("Sending…");
|
||||
let r = await request();
|
||||
let job = null;
|
||||
while (r.ok && r.body.job && !["done", "failed"].includes(r.body.job.state)) {
|
||||
job = r.body.job;
|
||||
onUpdate(job.state === "queued" ? "Queued…" : "Printing…");
|
||||
r = await call(`api/jobs/${job.id}?wait=20`);
|
||||
}
|
||||
if (r.ok && r.body.skipped) return {ok: true, text: r.body.skipped};
|
||||
if (!r.ok || !r.body.job) {
|
||||
return {ok: false, text: r.text + (job && r.status === 0 ? " (the job may still print)" : "")};
|
||||
}
|
||||
return {ok: r.body.job.state === "done", text: r.body.job.message};
|
||||
}
|
||||
|
||||
// Lists the editor and settings share; refreshed whenever they change.
|
||||
export const cache = {sources: null, assets: null, settings: null};
|
||||
|
||||
export async function sources(refresh = false) {
|
||||
if (refresh || !cache.sources) {
|
||||
const r = await get("api/sources");
|
||||
cache.sources = r.ok ? r.body.sources : [];
|
||||
}
|
||||
return cache.sources;
|
||||
}
|
||||
|
||||
export async function assets(refresh = false) {
|
||||
if (refresh || !cache.assets) {
|
||||
const r = await get("api/assets");
|
||||
cache.assets = r.ok ? r.body.assets : [];
|
||||
}
|
||||
return cache.assets;
|
||||
}
|
||||
|
||||
export async function settings(refresh = false) {
|
||||
if (refresh || !cache.settings) {
|
||||
const r = await get("api/settings");
|
||||
cache.settings = r.ok ? r.body : {settings: {}, timezones: []};
|
||||
}
|
||||
return cache.settings;
|
||||
}
|
||||
Reference in New Issue
Block a user