Briefkasten
Der Ordner demo-kit/ simuliert das Gerät für die Demo auf der Startseite. Auf dem Gerät wird er nicht benötigt.
Der Ordner demo-kit/ simuliert das Gerät für die Demo auf der Startseite. Auf dem Gerät wird er nicht benötigt.
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, viewport-fit=cover">
<meta name="color-scheme" content="light">
<title>Mailbox</title>
<style>
:root {
--paper: #f4ecdc; --card: #fbf6ea; --ink: #2b2420; --soft: #6d6157; --faint: #948779; --line: rgba(43,36,32,.13);
--red: #c8362b; --red-dark: #9f251c; --blue: #2f4a6b; --green: #3d7a4f; --amber: #b0700f;
--serif: "Iowan Old Style", "Palatino Linotype", Palatino, "Book Antiqua", Georgia, "Noto Serif", serif;
--sans: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
}
* { box-sizing: border-box; }
[hidden] { display: none !important; }
html { -webkit-text-size-adjust: 100%; }
body { margin: 0; background: var(--paper); color: var(--ink); font: 14px/1.45 var(--sans); overflow-x: hidden;
-webkit-font-smoothing: antialiased; -webkit-tap-highlight-color: transparent; font-variant-numeric: tabular-nums; }
button { font: inherit; color: inherit; border: 0; background: none; padding: 0; cursor: pointer; }
:focus-visible { outline: 2px solid var(--blue); outline-offset: 3px; }
main { max-width: 520px; margin: 0 auto; padding: calc(18px + env(safe-area-inset-top)) calc(16px + env(safe-area-inset-right)) calc(22px + env(safe-area-inset-bottom)) calc(16px + env(safe-area-inset-left)); }
/* ---------- the postmark and the headline ---------- */
header { display: flex; justify-content: space-between; align-items: flex-start; gap: 12px; }
.eyebrow { font-size: 11px; letter-spacing: .22em; text-transform: uppercase; color: var(--faint); font-weight: 600; }
h1 { margin: 4px 0 0; font: italic 400 32px/1.12 var(--serif); letter-spacing: -.005em; }
.sub { margin: 6px 0 0; color: var(--soft); min-height: 20px; }
.stamp { flex: none; width: 62px; height: 62px; margin-top: 2px; border-radius: 50%; border: 1.5px solid var(--faint); color: var(--faint); display: grid; place-items: center; text-align: center;
font-size: 8.5px; line-height: 1.25; letter-spacing: .12em; text-transform: uppercase; font-weight: 700; transform: rotate(-10deg); box-shadow: inset 0 0 0 3px var(--paper), inset 0 0 0 4px var(--faint); transition: color .4s, border-color .4s, box-shadow .4s; }
.stamp b { display: block; font-size: 15px; letter-spacing: 0; font-weight: 700; }
.up .stamp { color: var(--red); border-color: var(--red); box-shadow: inset 0 0 0 3px var(--paper), inset 0 0 0 4px var(--red); }
/* ---------- the mailbox ---------- */
.scene { position: relative; margin: 8px -16px 0; height: 236px; overflow: hidden; }
.scene svg { position: absolute; inset: 0; width: 100%; height: 100%; display: block; }
#flag { transform-box: fill-box; transform-origin: 9% 92%; transform: rotate(90deg); transition: transform .7s cubic-bezier(.3,1.5,.5,1); }
.up #flag { transform: rotate(0deg); }
#letter { transform: translateX(26px); opacity: 0; transition: transform .6s .15s cubic-bezier(.3,1.2,.5,1), opacity .3s .15s; }
.up #letter { transform: translateX(0); opacity: 1; }
#door { transform-box: fill-box; transform-origin: 50% 100%; transition: transform .5s; }
.open #door { transform: scaleY(.18); }
#clear { display: block; width: 100%; margin-top: 6px; padding: 15px 12px; border-radius: 16px; background: var(--red); color: #fff7ee; font-weight: 650; font-size: 16px; letter-spacing: .01em;
box-shadow: 0 10px 22px -12px var(--red-dark), inset 0 -3px 0 rgba(0,0,0,.16), inset 0 1px 0 rgba(255,255,255,.28); transition: transform .15s, background .3s, color .3s, box-shadow .3s; }
#clear:active { transform: scale(.98); }
#clear:disabled { background: rgba(43,36,32,.07); color: var(--faint); box-shadow: none; cursor: default; }
/* ---------- the sensor ---------- */
.card { margin-top: 16px; padding: 14px 16px; border-radius: 18px; background: var(--card); border: 1px solid var(--line); }
.card h2 { margin: 0; font-size: 11px; letter-spacing: .18em; text-transform: uppercase; color: var(--faint); font-weight: 600; }
.facts { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; margin-top: 10px; }
.facts div { min-width: 0; }
.facts b { display: block; font: 400 19px/1.2 var(--serif); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.facts span { font-size: 12px; color: var(--soft); white-space: nowrap; }
.warn { margin: 10px 0 0; padding: 8px 11px; border-radius: 10px; background: rgba(176,112,15,.12); color: var(--amber); font-size: 13px; font-weight: 550; }
/* ---------- history ---------- */
h3 { margin: 22px 2px 0; font: italic 400 19px/1.2 var(--serif); }
ol { list-style: none; margin: 8px 0 0; padding: 0; border-top: 1px solid var(--line); }
li { display: flex; align-items: center; gap: 10px; padding: 10px 2px; border-bottom: 1px dashed var(--line); font-size: 13.5px; white-space: nowrap; }
li time { min-width: 62px; font-size: 12.5px; color: var(--faint); flex: none; }
li i { width: 9px; height: 9px; border-radius: 2px; background: var(--red); flex: none; }
li.cleared i { background: none; border: 1.5px solid var(--faint); border-radius: 50%; }
li span { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; }
li em { font-style: normal; color: var(--faint); font-size: 12.5px; }
.empty { color: var(--faint); justify-content: center; }
.note { margin: 12px 4px 0; font-size: 12px; color: var(--faint); text-align: center; }
footer { display: flex; justify-content: center; gap: 6px 16px; flex-wrap: wrap; margin-top: 14px; color: var(--faint); font-size: 12px; }
footer span { white-space: nowrap; }
#toast { position: fixed; left: 50%; bottom: calc(18px + env(safe-area-inset-bottom)); transform: translate(-50%, 20px); background: var(--ink); color: var(--paper);
padding: 9px 16px; border-radius: 99px; font-size: 13px; font-weight: 600; opacity: 0; pointer-events: none; transition: opacity .3s, transform .3s; z-index: 9; max-width: 90vw; }
#toast.show { opacity: 1; transform: translate(-50%, 0); }
@media (max-width: 359px) { h1 { font-size: 27px; } .facts b { font-size: 17px; } }
@media (prefers-reduced-motion: reduce) { *, *::before, *::after { animation: none !important; transition: none !important; } }
</style>
</head>
<body>
<main id="app">
<header>
<div>
<div class="eyebrow" id="who">Mailbox</div>
<h1 id="head">Asking the flag</h1>
<p class="sub" id="sub"></p>
</div>
<div class="stamp" aria-hidden="true"><span><b id="n">0</b><span id="nL">opened</span></span></div>
</header>
<section class="scene" aria-hidden="true">
<svg viewBox="0 0 390 236" preserveAspectRatio="xMidYMax slice">
<defs>
<linearGradient id="body" x1="0" y1="0" x2="0" y2="1"><stop offset="0" stop-color="#3d5f88"/><stop offset="1" stop-color="#27405e"/></linearGradient>
<linearGradient id="hill" x1="0" y1="0" x2="0" y2="1"><stop offset="0" stop-color="#d9cfb6"/><stop offset="1" stop-color="#e9dfc8"/></linearGradient>
</defs>
<circle cx="318" cy="52" r="22" fill="#efd9a4"/>
<path d="M0 196c60-26 120-30 190-18s140 4 200-14v72H0z" fill="url(#hill)"/>
<path d="M0 214c80-14 150-10 220-4s120 2 170-8v34H0z" fill="#cfc4a8"/>
<g fill="none" stroke="#a89c80" stroke-width="2" stroke-linecap="round"><path d="M52 208v-13M58 208v-9M46 208v-7M300 204v-12M307 204v-8M330 200v-10"/></g>
<rect x="181" y="150" width="14" height="76" rx="2" fill="#7b5a3c"/>
<rect x="173" y="146" width="30" height="8" rx="2" fill="#5f452e"/>
<g id="letter"><rect x="246" y="94" width="34" height="24" rx="2" fill="#fffaf0" stroke="#b9ad96"/><path d="M246 96l17 12 17-12" fill="none" stroke="#b9ad96"/></g>
<path d="M112 148V104a34 34 0 0 1 34-34h84a34 34 0 0 1 34 34v44z" fill="url(#body)"/>
<path d="M118 144V104a28 28 0 0 1 28-28h10" fill="none" stroke="rgba(255,255,255,.22)" stroke-width="3" stroke-linecap="round"/>
<path id="door" d="M230 70a34 34 0 0 1 34 34v44h-36V104a34 34 0 0 1 2-34z" fill="#1d3049"/>
<circle cx="248" cy="112" r="3.2" fill="#c9b27a"/>
<text x="140" y="136" fill="rgba(255,255,255,.75)" font-size="13" font-family="Georgia, serif" font-style="italic" text-anchor="middle">Post</text>
<g id="flag"><rect x="142" y="58" width="6" height="50" rx="2" fill="#9f251c"/><path d="M148 58h30l-8 9 8 9h-30z" fill="#c8362b"/><circle cx="145" cy="104" r="5" fill="#7d1c15"/></g>
</svg>
</section>
<button id="clear" type="button" disabled>Clear the flag</button>
<section class="card" aria-label="The sensor at the mailbox">
<h2>Sensor at the mailbox · <span id="sName"></span></h2>
<div class="facts">
<div><b id="lid">–</b><span>lid</span></div>
<div><b id="seen">–</b><span>last call</span></div>
<div><b id="sRssi">–</b><span>Wi-Fi there</span></div>
</div>
<p class="warn" id="late" hidden></p>
</section>
<h3>What happened</h3>
<ol id="list"></ol>
<p class="note">The flag stays up over a restart. This list is kept in memory and starts again then.</p>
<footer>
<span id="rssi">Wi-Fi</span>
<span id="up"></span>
</footer>
</main>
<div id="toast" role="alert"></div>
<script>
(function () {
'use strict';
var $ = function (id) { return document.getElementById(id); };
var state = null, seenChanges = -1, busy = false;
function text(node, value) { if (node.textContent !== value) node.textContent = value; }
function api(path, body) {
var init = body ? { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(body) } : undefined;
var late = new Promise(function (_, reject) { setTimeout(function () { reject(new Error('No answer from the flag')); }, 7000); });
return Promise.race([fetch(path, init), late]).then(function (r) {
return r.text().then(function (raw) {
var data = null;
try { data = JSON.parse(raw); } catch (e) { /* not JSON */ }
if (!r.ok || data === null) throw new Error((data && data.error) || 'The flag answered ' + r.status);
return data;
});
});
}
var toastTimer = 0;
function toast(message) {
var node = $('toast');
node.textContent = message;
node.classList.add('show');
clearTimeout(toastTimer);
toastTimer = setTimeout(function () { node.classList.remove('show'); }, 3500);
}
// The time of day, with the day of the week in front when it was not today.
var DAYS = ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'];
function clock(secondsAgo) {
var d = new Date(Date.now() - secondsAgo * 1000), h = d.getHours(), m = d.getMinutes();
var time = (h < 10 ? '0' : '') + h + ':' + (m < 10 ? '0' : '') + m;
return d.toDateString() === new Date().toDateString() ? time : DAYS[d.getDay()] + ' ' + time;
}
function ago(seconds) {
if (seconds < 60) return 'just now';
if (seconds < 3600) return Math.floor(seconds / 60) + ' min ago';
if (seconds < 86400) return Math.floor(seconds / 3600) + ' h ago';
return Math.floor(seconds / 86400) + ' d ago';
}
function span(seconds) {
var d = Math.floor(seconds / 86400), h = Math.floor((seconds % 86400) / 3600), m = Math.floor((seconds % 3600) / 60);
return d > 0 ? d + ' d ' + h + ' h' : h > 0 ? h + ' h ' + m + ' min' : m + ' min';
}
var num = function (v) { return typeof v === 'number' && isFinite(v); };
function show(s) {
state = s;
var app = $('app');
app.classList.toggle('up', s.flag === true);
app.classList.toggle('open', s.sensor.lid === 'open');
text($('who'), s.name);
text($('head'), s.flag ? 'The mailbox was opened' : 'Nothing yet');
text($('sub'), s.flag ? (num(s.flagSecondsAgo) ? 'The flag is up since ' + clock(s.flagSecondsAgo) + '.' : 'The flag is up.') : 'The flag is down. It goes up when the mailbox opens.');
text($('n'), String(s.opened));
text($('nL'), s.opened === 1 ? 'opening' : 'openings');
if (!busy) $('clear').disabled = !s.flag;
text($('sName'), s.sensor.name);
text($('lid'), s.sensor.lid === 'open' ? 'Open' : s.sensor.lid === 'closed' ? 'Closed' : 'Not known');
text($('seen'), num(s.sensor.lastSeenSecondsAgo) ? ago(s.sensor.lastSeenSecondsAgo) : 'Never');
text($('sRssi'), num(s.sensor.rssi) ? Math.round(s.sensor.rssi) + ' dBm' : '–');
var late = $('late');
late.hidden = s.sensor.late !== true && num(s.sensor.lastSeenSecondsAgo);
if (!late.hidden) {
text(late, num(s.sensor.lastSeenSecondsAgo)
? 'The sensor has not called for ' + span(s.sensor.lastSeenSecondsAgo) + '. It calls every ' + span(s.sensor.checkInSeconds) + ' when all is well.'
: 'The sensor has not called since this device started.');
}
text($('rssi'), num(s.rssi) ? 'Wi-Fi ' + Math.round(s.rssi) + ' dBm' : 'Wi-Fi');
text($('up'), num(s.uptimeSeconds) ? 'running ' + span(s.uptimeSeconds) : '');
if (s.changes !== seenChanges) { seenChanges = s.changes; loadHistory(); }
}
function loadHistory() {
api('/history').then(function (h) {
var list = $('list');
list.textContent = '';
if (h.entries.length === 0) {
var none = document.createElement('li');
none.className = 'empty';
none.textContent = 'Nothing since this device started.';
list.appendChild(none);
}
h.entries.forEach(function (e) {
var li = document.createElement('li');
if (e.kind === 'cleared') li.className = 'cleared';
var t = document.createElement('time'); t.textContent = clock(e.secondsAgo);
var what = document.createElement('span');
what.textContent = e.kind === 'cleared' ? 'Flag cleared' : e.count > 1 ? 'Mailbox opened ' + e.count + ' times' : 'Mailbox opened';
var how = document.createElement('em');
how.textContent = e.via === 'button' ? 'button' : e.via === 'phone' ? 'phone' : 'sensor';
li.appendChild(t); li.appendChild(document.createElement('i')); li.appendChild(what); li.appendChild(how);
list.appendChild(li);
});
}).catch(function () { seenChanges = -1; });
}
$('clear').onclick = function () {
if (busy || !state || !state.flag) return;
busy = true;
$('clear').disabled = true;
api('/clear', { via: 'phone' }).then(function (s) { busy = false; show(s); }).catch(function (error) {
busy = false;
$('clear').disabled = false;
toast(error.message + '.');
});
};
var failures = 0;
function poll() {
api('/state').then(function (s) { failures = 0; show(s); }).catch(function () {
if (++failures >= 3) { text($('head'), 'No answer'); text($('sub'), 'The flag does not answer right now.'); $('clear').disabled = true; }
}).then(function () { setTimeout(poll, 1500); });
}
poll();
})();
</script>
</body>
</html>