Duas lâmpadas
demo-kit/ é o que simula o dispositivo para a demo na página inicial. O próprio dispositivo não precisa dele.
demo-kit/ é o que simula o dispositivo para a demo na página inicial. O próprio dispositivo não precisa dele.
// The pretend lamp for the demo of the lamps example (../../demo-kit/README.md): the same answers the program
// on the Raspberry Pi gives (../pi/lamp.py), with a pretend other lamp that sends a glow every 15 to 25
// seconds and takes every glow sent to it. A glow lasts 16 seconds here; the program's default is 60.
// The two names and places are made up.
(function () {
var ctx = czDemo.context;
var GLOW_SECONDS = 16;
var PATTERNS = ['glow', 'pulse', 'heartbeat', 'wave'];
var HERE = { name: 'lamp-north', label: 'Alex', place: 'Northhaven', color: '#ffb45e' };
var OTHER = { name: 'lamp-south', label: 'Sam', place: 'Southbrook', color: '#8fb8ff' };
// What the pretend other lamp sends, in turn: mostly its own colour by touch, now and then one picked on the 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 sockets = [];
var entries = []; // oldest first, as the program keeps them
var nextId = 1;
var hereGlow = null; // { color, pattern, seconds, ends } with `ends` in seconds of the demo
var otherGlow = null;
var path = null; // how the lamps reached each other; not known before the first glow has been sent
var turn = 0;
var nextFromOther = 6;
function unix() {
return Math.round(Date.now() / 1000);
}
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 state() {
return {
t: unix(),
here: { name: HERE.name, label: HERE.label, place: HERE.place, color: HERE.color, glow: glowNow(hereGlow) },
other: { name: OTHER.name, label: OTHER.label, place: OTHER.place, color: OTHER.color, glow: glowNow(otherGlow), reachable: true, path: path },
distanceKm: 620,
glowSeconds: GLOW_SECONDS,
patterns: PATTERNS,
hardware: 'simulator',
link: 'connectivitizer',
};
}
function history() {
var midnight = new Date();
midnight.setHours(0, 0, 0, 0);
var today = { received: 0, sent: 0, lastReceived: null, lastSent: null };
entries.forEach(function (entry) {
if (entry.direction === 'sent' && entry.delivered !== true) return;
var key = entry.direction === 'received' ? 'Received' : 'Sent';
if (entry.t * 1000 >= midnight.getTime()) today[key.toLowerCase()] += 1;
today['last' + key] = entry.t;
});
return { today: today, entries: entries.slice(-30).reverse() };
}
function add(direction, color, pattern, via, delivered, t) {
var entry = { id: nextId++, t: t || unix(), direction: direction, color: color, pattern: pattern, via: via, delivered: delivered };
entries.push(entry);
return entry;
}
function tell(type, entry, error) {
var message = { type: type };
if (entry) message.entry = entry;
if (error) message.error = error;
message.state = state();
sockets.forEach(function (socket) {
socket.send(message);
});
}
// Earlier today, 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, unix() - past[0] * 60);
});
function receive(from) {
var entry = add('received', from.color, from.pattern, from.via, true);
hereGlow = { color: from.color, pattern: from.pattern, seconds: GLOW_SECONDS, ends: ctx.seconds() + GLOW_SECONDS };
tell('glow', entry);
}
// The clock of the demo: the other lamp's glows, and the word that a glow has ended.
var shown = [false, false];
setInterval(function () {
if (ctx.seconds() >= nextFromOther) {
nextFromOther = ctx.seconds() + ctx.between(15, 25);
receive(FROM_OTHER[turn++ % FROM_OTHER.length]);
}
var glowing = [glowNow(hereGlow) !== null, glowNow(otherGlow) !== null];
if ((shown[0] && !glowing[0]) || (shown[1] && !glowing[1])) tell('state');
shown = glowing;
}, 250);
czDemo.define({
device: { name: HERE.name, title: 'Lamps' },
routes: {
'GET /state': state,
'GET /history': history,
'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 one of ' + PATTERNS.join(', ') + '.' });
// The page is the owner's phone: the glow goes to the other lamp, and arrives a moment later.
var entry = add('sent', color.toLowerCase(), pattern, 'phone', null);
tell('glow', entry);
setTimeout(function () {
entry.delivered = true;
path = 'direct';
otherGlow = { color: entry.color, pattern: pattern, seconds: GLOW_SECONDS, ends: ctx.seconds() + GLOW_SECONDS };
tell('delivered', entry);
}, ctx.between(700, 1300));
return { ok: true, entry: entry };
},
},
sockets: {
'/events': function (socket) {
sockets.push(socket);
socket.send({ type: 'state', state: state() });
socket.onclose = function () {
sockets.splice(sockets.indexOf(socket), 1);
};
},
},
});
})();