Boîte à lettres
demo-kit/ est ce qui simule l’appareil pour la démo sur la page de démarrage. L’appareil lui-même n’en a pas besoin.
demo-kit/ est ce qui simule l’appareil pour la démo sur la page de démarrage. L’appareil lui-même n’en a pas besoin.
<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 sensor</title>
<style>
:root { --paper: #f4ecdc; --card: #fbf6ea; --ink: #2b2420; --soft: #6d6157; --faint: #948779; --line: rgba(43,36,32,.13); --red: #c8362b; --blue: #2f4a6b; --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); -webkit-font-smoothing: antialiased; font-variant-numeric: tabular-nums; }
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)); }
.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); }
h1.open { color: var(--red); }
.sub { margin: 6px 0 0; color: var(--soft); min-height: 20px; }
.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; }
dl { display: grid; grid-template-columns: 1fr auto; gap: 8px 12px; margin: 10px 0 0; }
dt { color: var(--soft); } dd { margin: 0; font-weight: 600; text-align: right; 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; }
footer { display: flex; justify-content: center; gap: 6px 16px; flex-wrap: wrap; margin-top: 14px; color: var(--faint); font-size: 12px; }
</style>
</head>
<body>
<main>
<div class="eyebrow" id="who">Mailbox sensor</div>
<h1 id="head">Asking the sensor</h1>
<p class="sub" id="sub"></p>
<section class="card" aria-label="Calls to the flag">
<h2>Calls to <span id="ind"></span></h2>
<dl>
<dt>Openings since the sensor started</dt><dd id="opens">–</dd>
<dt>Openings not delivered yet</dt><dd id="unsent">–</dd>
<dt>Last call that was answered</dt><dd id="last">–</dd>
<dt>The flag, as last answered</dt><dd id="flag">–</dd>
</dl>
<p class="warn" id="warn" hidden></p>
</section>
<footer><span id="rssi">Wi-Fi</span><span id="up"></span></footer>
</main>
<script>
(function () {
'use strict';
var $ = function (id) { return document.getElementById(id); };
var num = function (v) { return typeof v === 'number' && isFinite(v); };
function text(node, value) { if (node.textContent !== value) node.textContent = value; }
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';
}
function show(s) {
text($('who'), s.name);
text($('head'), s.lid === 'open' ? 'The lid is open' : 'The lid is closed');
$('head').className = s.lid === 'open' ? 'open' : '';
text($('sub'), s.calling ? 'Calling ' + s.indicator + '.' : 'It calls ' + s.indicator + ' when the lid moves, and every ' + span(s.checkInSeconds) + '.');
text($('ind'), s.indicator);
text($('opens'), String(s.opens));
text($('unsent'), String(s.unsent));
text($('last'), num(s.lastDeliveredSecondsAgo) ? ago(s.lastDeliveredSecondsAgo) : 'None yet');
text($('flag'), s.indicatorFlag === true ? 'Up' : s.indicatorFlag === false ? 'Down' : 'Not known');
var failed = num(s.lastStatus) && s.lastStatus !== 200;
$('warn').hidden = !failed;
if (failed) text($('warn'), s.lastStatus < 0 ? 'The last call got no answer. The sensor tries again.' : 'The last call was answered with ' + s.lastStatus + '. The sensor tries again.');
text($('rssi'), num(s.rssi) ? 'Wi-Fi ' + Math.round(s.rssi) + ' dBm' : 'Wi-Fi');
text($('up'), num(s.uptimeSeconds) ? 'running ' + span(s.uptimeSeconds) : '');
}
function poll() {
fetch('/state').then(function (r) { return r.json(); }).then(show).catch(function () { text($('sub'), 'The sensor does not answer right now.'); })
.then(function () { setTimeout(poll, 2000); });
}
poll();
})();
</script>
</body>
</html>