Caja de correo
demo-kit/ es lo que simula el dispositivo para la demostración en la página de inicio. El dispositivo en sí no lo necesita.
demo-kit/ es lo que simula el dispositivo para la demostración en la página de inicio. El dispositivo en sí no lo necesita.
// The pretend indicator for the demo of the mailbox example (../../demo-kit/README.md): the same answers the
// indicator's sketch gives (../esp32/indicator/indicator.ino), with a pretend sensor at the mailbox. The
// mailbox opens three seconds after the demo starts and again 20 to 30 seconds after the flag was cleared;
// the lid closes four seconds after it opened; the pretend sensor checks in every 20 seconds, where the
// sketch's sensor does so every 10 minutes.
(function () {
var ctx = czDemo.context;
var CHECK_IN_SECONDS = 600; // what the real sensor reports; the pretend one calls more often
var entries = []; // oldest first; `at` in seconds of the demo, negative for what happened before it opened
var nextId = 1;
var changes = 0;
var flag = false;
var flagAt = null;
var opened = 0; // openings since the flag was cleared
var lid = 'closed';
var sensorSeen = -140;
var sensorRssi = -72;
var rssi = -55;
var nextOpen = 3;
var closeAt = null;
var nextCheckIn = 12;
var uptimeAtStart = 6 * 86400 + 2 * 3600 + 40 * 60;
function add(kind, via, count, at) {
entries.push({ id: nextId++, at: at === undefined ? ctx.seconds() : at, kind: kind, via: via, count: count });
if (entries.length > 30) entries.shift();
changes++;
}
// Since the pretend indicator started, before the demo opened.
add('opened', 'sensor', 1, -26 * 3600 - 14 * 60);
add('cleared', 'button', 0, -25 * 3600 - 2 * 60);
add('opened', 'sensor', 2, -3 * 3600 - 37 * 60);
add('cleared', 'phone', 0, -3 * 3600 - 5 * 60);
// What the sensor's call does on the indicator.
function event(count, lidNow) {
sensorSeen = ctx.seconds();
sensorRssi = Math.max(-80, Math.min(-66, sensorRssi + ctx.between(-1.5, 1.5)));
if (lid !== lidNow) changes++;
lid = lidNow;
if (count > 0) {
if (!flag) flagAt = ctx.seconds();
flag = true;
opened += count;
add('opened', 'sensor', count);
}
}
function clear(via) {
if (!flag) return;
flag = false;
flagAt = null;
opened = 0;
add('cleared', via, 0);
nextOpen = ctx.seconds() + ctx.between(20, 30);
}
setInterval(function () {
var now = ctx.seconds();
if (nextOpen !== null && now >= nextOpen) {
nextOpen = null; // the next one comes after the flag was cleared
closeAt = now + 4;
event(1, 'open');
}
if (closeAt !== null && now >= closeAt) {
closeAt = null;
event(0, 'closed');
}
if (now >= nextCheckIn) {
nextCheckIn = now + 20;
event(0, lid);
}
rssi = Math.max(-62, Math.min(-50, rssi + ctx.between(-0.4, 0.4)));
}, 250);
function state() {
return {
name: 'mailbox-flag',
flag: flag,
flagSecondsAgo: flag ? Math.round(ctx.seconds() - flagAt) : null,
opened: opened,
sensor: {
name: 'mailbox',
lastSeenSecondsAgo: Math.round(ctx.seconds() - sensorSeen),
lid: lid,
rssi: Math.round(sensorRssi),
checkInSeconds: CHECK_IN_SECONDS,
late: false,
},
changes: changes,
rssi: Math.round(rssi),
uptimeSeconds: Math.round(uptimeAtStart + ctx.seconds()),
};
}
czDemo.define({
device: { name: 'mailbox-flag', title: 'Mailbox' },
routes: {
'GET /state': state,
'GET /history': function () {
return {
entries: entries
.slice()
.reverse()
.map(function (entry) {
return { id: entry.id, secondsAgo: Math.round(ctx.seconds() - entry.at), kind: entry.kind, via: entry.via, count: entry.count };
}),
};
},
'POST /clear': function () {
clear('phone');
return state();
},
'POST /event': function (request) {
var body = request.json();
if (!body || body.from !== 'mailbox') return ctx.respond(400, { error: 'from is not the sensor of this flag.' });
event(typeof body.opened === 'number' ? Math.max(0, Math.round(body.opened)) : 0, body.lid === 'open' ? 'open' : 'closed');
return { ok: true, flag: flag };
},
'GET /.cz/ui.json': function () {
return { name: 'Mailbox', entry: '/.cz/ui.html', version: '1', theme: { background: '#f4ecdc', foreground: '#9f251c' } };
},
},
});
})();