// Starting points for new schedules. Source ids are filled with the first saved one that fits. import {localDateTimeValue} from "./dom.js"; const WEEKDAYS = [0, 1, 2, 3, 4]; const ALL_DAYS = [0, 1, 2, 3, 4, 5, 6]; export const OPTIONS = {quiet: "hold", skip_if_empty: false, catch_up_minutes: 15, retries: 3}; export function recurring(extra = {}) { return {type: "recurring", freq: "daily", interval: 1, times: ["08:00"], weekdays: [], monthday: null, setpos: null, month: null, start: null, until: null, rrule: null, ...extra}; } export function triggerDefaults(type, sources = []) { const first = (kind) => (sources.find((s) => s.kind === kind) || {}).id || ""; const inAnHour = new Date(Date.now() + 3600e3); inAnHour.setMinutes(0); return { once: () => ({type: "once", at: localDateTimeValue(inAnHour)}), recurring: () => recurring(), sun: () => ({type: "sun", event: "sunset", offset_minutes: 0, weekdays: [...ALL_DAYS]}), calendar: () => ({type: "calendar", source_id: first("ics"), minutes_before: 15, contains: "", include_all_day: false}), watch: () => ({type: "watch", kind: "rss", url: "", path: "", condition: "changed", threshold: null, every_minutes: 30, max_items: 3}), webhook: () => ({type: "webhook", per_hour: 10}), }[type](); } export function blockDefaults(type, sources = []) { const ids = (kind) => { const found = sources.find((s) => s.kind === kind); return found ? [found.id] : []; }; return { note: () => ({type: "note", text: "", size: 1, timestamp: false, bold: false, center: false, choices: [], pick: "sequential"}), qr: () => ({type: "qr", content: "", caption: ""}), image: () => ({type: "image", asset_id: "", caption: ""}), random_image: () => ({type: "random_image"}), agenda: () => ({type: "agenda", source_ids: ids("ics"), start_day: 0, days: 1, include_all_day: true, title: ""}), headlines: () => ({type: "headlines", source_ids: ids("rss"), count: 5, only_new: false, qr_first: false, title: ""}), divider: () => ({type: "divider"}), }[type](); } function note(text, extra = {}) { return {...blockDefaults("note"), text, ...extra}; } export const PRESETS = [ { id: "brief", icon: "☀", title: "Morning brief", blurb: "Today's date, your calendar and the headlines, every weekday morning.", make: (sources) => ({ name: "Morning brief", trigger: recurring({times: ["07:30"], weekdays: [...WEEKDAYS]}), blocks: [ note("{date:%A %d %B}", {size: 2, bold: true, center: true}), blockDefaults("agenda", sources), blockDefaults("divider"), {...blockDefaults("headlines", sources), title: "Headlines"}, ], options: {...OPTIONS}, }), }, { id: "chores", icon: "✓", title: "Chore rotation", blurb: "A different chore each day, taking turns through your list.", make: () => ({ name: "Today's chore", trigger: recurring({times: ["08:00"]}), blocks: [note("TODAY'S CHORE\n\n{choice}", {size: 2, center: true, choices: ["Hoover the living room", "Clean the bathroom", "Laundry", "Water the plants", "Take the bins out", "Change the bed", "Clear out the fridge"]})], options: {...OPTIONS}, }), }, { id: "countdown", icon: "⏳", title: "Countdown", blurb: "Days left until a date, printed every morning.", make: () => ({ name: "Christmas countdown", trigger: recurring({times: ["09:00"]}), blocks: [note("{days_until:12-25} days\nuntil Christmas", {size: 3, bold: true, center: true})], options: {...OPTIONS}, }), }, { id: "reminders", icon: "⏰", title: "Event reminders", blurb: "A slip before each event in a calendar.", make: (sources) => ({ name: "Event reminders", trigger: triggerDefaults("calendar", sources), blocks: [ note("IN {event.minutes} MIN", {bold: true, center: true}), note("{event.title}", {size: 2, center: true}), note("{event.start} - {event.end}\n{event.location}", {center: true}), ], options: {...OPTIONS, quiet: "skip"}, }), }, { id: "feed", icon: "✉", title: "New post alert", blurb: "Prints each new item in an RSS feed, with a QR code to open it.", make: () => ({ name: "New posts", trigger: triggerDefaults("watch"), blocks: [ note("{item.source}", {bold: true}), note("{item.title}", {size: 2}), {type: "qr", content: "{item.link}", caption: ""}, ], options: {...OPTIONS}, }), }, { id: "price", icon: "↗", title: "Value watch", blurb: "Watches a number in a JSON API and prints when it crosses a threshold.", make: () => ({ name: "Price alert", trigger: {...triggerDefaults("watch"), kind: "json", path: "price", condition: "above", threshold: 100}, blocks: [note("Now {value} (was {old_value})", {size: 2, center: true})], options: {...OPTIONS}, }), }, { id: "webhook", icon: "⚡", title: "Webhook", blurb: "Other services (Home Assistant, phone shortcuts) print by calling a URL.", make: () => ({ name: "Webhook print", trigger: triggerDefaults("webhook"), blocks: [note("{payload.title}", {bold: true}), note("{payload.text}", {size: 2})], options: {...OPTIONS, quiet: "skip"}, }), }, { id: "sunset", icon: "☾", title: "Sunset nudge", blurb: "A note half an hour before sunset.", make: () => ({ name: "Sunset soon", trigger: {...triggerDefaults("sun"), event: "sunset", offset_minutes: -30}, blocks: [note("Sunset in 30 minutes.\nGo and look outside.", {size: 2, center: true})], options: {...OPTIONS}, }), }, { id: "blank", icon: "+", title: "Start from scratch", blurb: "A one-off note, an hour from now.", make: () => ({ name: "Reminder", trigger: triggerDefaults("once"), blocks: [note("")], options: {...OPTIONS}, }), }, ];