Deux feux, ESP32
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.
// The pretend lamp for the demo of the lamps-esp32 example (../../demo-kit/README.md): the same answers the
// sketch gives (../esp32/lamp/lamp.ino), with a pretend other lamp that sends a glow every 15 to 25 seconds
// and takes every glow sent to it after about a second. A glow lasts 16 seconds here; the sketch's is 60.
(function () {
var ctx = czDemo.context;
var GLOW_SECONDS = 16;
var PATTERNS = ['glow', 'pulse', 'heartbeat', 'wave'];
var HERE = { name: 'lamp-east', color: '#ffb45e' };
var OTHER = { name: 'lamp-west', color: '#8fb8ff' };
// What the pretend other lamp sends, in turn: mostly its own colour by touch, at times one picked on its phone.
var FROM_OTHER = [
{ color: '#8fb8ff', pattern: 'glow', via: 'touch' },
{ color: '#ff8fa3', pattern: 'heartbeat', via: 'phone' },
{ color: '#8fb8ff', pattern: 'wave', via: 'touch' },
{ color: '#86e0c2', pattern: 'pulse', via: 'phone' },
];
var entries = []; // oldest first; `at` in seconds of the demo, negative for what happened before it opened
var nextId = 1;
var received = 0;
var sent = 0;
var changes = 0;
var hereGlow = null; // { color, pattern, seconds, ends }
var otherGlow = null;
var sending = false;
var lastAnswer = null; // seconds of the demo at which the other lamp last answered
var turn = 0;
var nextFromOther = 5;
var uptimeAtStart = 3 * 86400 + 5 * 3600 + 12 * 60;
var rssi = -58;
function glowNow(glow) {
if (!glow) return null;
var remaining = glow.ends - ctx.seconds();
if (remaining <= 0) return null;
return { color: glow.color, pattern: glow.pattern, seconds: glow.seconds, remainingSeconds: Math.round(remaining * 10) / 10 };
}
function add(direction, color, pattern, via, delivered, at) {
var entry = { id: nextId++, at: at === undefined ? ctx.seconds() : at, direction: direction, color: color, pattern: pattern, via: via, delivered: delivered };
entries.push(entry);
if (entries.length > 30) entries.shift();
changes++;
return entry;
}
function shown(entry) {
return { id: entry.id, secondsAgo: Math.round(ctx.seconds() - entry.at), direction: entry.direction, color: entry.color, pattern: entry.pattern, via: entry.via, delivered: entry.delivered };
}
// Since the pretend lamp started, before the demo opened.
[
[312, 'sent', '#ffb45e', 'glow', 'touch'],
[305, 'received', '#8fb8ff', 'glow', 'touch'],
[188, 'received', '#ff8fa3', 'heartbeat', 'phone'],
[97, 'sent', '#b9a0ff', 'wave', 'phone'],
[41, 'received', '#8fb8ff', 'pulse', 'touch'],
].forEach(function (past) {
add(past[1], past[2], past[3], past[4], true, -past[0] * 60);
if (past[1] === 'received') received++;
else sent++;
});
lastAnswer = -97 * 60;
function step() {
if (ctx.seconds() >= nextFromOther) {
nextFromOther = ctx.seconds() + ctx.between(15, 25);
var from = FROM_OTHER[turn++ % FROM_OTHER.length];
add('received', from.color, from.pattern, from.via, true);
received++;
hereGlow = { color: from.color, pattern: from.pattern, seconds: GLOW_SECONDS, ends: ctx.seconds() + GLOW_SECONDS };
}
rssi = Math.max(-66, Math.min(-52, rssi + ctx.between(-0.5, 0.5)));
}
setInterval(step, 250);
function state() {
return {
here: { name: HERE.name, color: HERE.color, glow: glowNow(hereGlow) },
other: { name: OTHER.name, glow: glowNow(otherGlow), reachable: true, lastAnswerSecondsAgo: Math.round(ctx.seconds() - lastAnswer), lastStatus: 200 },
glowSeconds: GLOW_SECONDS,
patterns: PATTERNS,
sending: sending,
changes: changes,
simulated: false,
rssi: Math.round(rssi),
uptimeSeconds: Math.round(uptimeAtStart + ctx.seconds()),
};
}
czDemo.define({
device: { name: HERE.name, title: 'Lamp' },
routes: {
'GET /state': state,
'GET /history': function () {
return { received: received, sent: sent, entries: entries.slice().reverse().map(shown) };
},
'POST /glow': function (request) {
var asked = request.json();
if (!asked || typeof asked !== 'object') return ctx.respond(400, { error: 'The body is a JSON object.' });
var color = asked.color || HERE.color;
var pattern = asked.pattern || 'glow';
if (!/^#[0-9a-f]{6}$/i.test(color)) return ctx.respond(400, { error: 'color is #rrggbb.' });
if (PATTERNS.indexOf(pattern) < 0) return ctx.respond(400, { error: 'pattern is glow, pulse, heartbeat or wave.' });
if (sending) return ctx.respond(409, { error: 'A glow is on its way.' });
// The page is the owner's phone: the lamp calls the other lamp, which answers a moment later.
var entry = add('sent', color.toLowerCase(), pattern, 'phone', null);
sending = true;
setTimeout(function () {
entry.delivered = true;
sending = false;
sent++;
changes++;
lastAnswer = ctx.seconds();
otherGlow = { color: entry.color, pattern: pattern, seconds: GLOW_SECONDS, ends: ctx.seconds() + GLOW_SECONDS };
}, ctx.between(700, 1300));
return { ok: true, entry: shown(entry) };
},
'GET /.cz/ui.json': function () {
return { name: 'Lamp', entry: '/.cz/ui.html', version: '1', theme: { background: '#15100d', foreground: '#ffc98a' } };
},
},
});
})();