Aquarium
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 aquarium for the demo (../../demo-kit/README.md): the same answers the sketch gives
// (../esp32/aquarium/aquarium.ino), with invented values. Its clock runs five hundred to a thousand times faster
// than a real one, so that a visitor sees a dusk, a night and a dawn within two minutes.
(function () {
var ctx = czDemo.context;
var S = ctx.state;
var INTERVAL_SECONDS = 900; // one history sample every 15 minutes of the tank's day, as on the device
var HISTORY_POINTS = 96; // 24 hours
var FEED_TURN_MS = 2400; // how long the feeder takes for one portion
var MAX_FEED_TIMES = 4;
var CLOCK_AT_START = 18 * 60 + 6; // the demo always starts at 18:06: a feeding and a dusk are close
// Minutes of the tank's day that pass in one real second.
var PACE_DAY = 8;
var PACE_RAMP = 6;
var PACE_NIGHT = 20;
function clamp(value, min, max) {
return Math.max(min, Math.min(max, value));
}
function round2(value) {
return Math.round(value * 100) / 100;
}
function round1(value) {
return Math.round(value * 10) / 10;
}
S.schedule = { sunrise: 480, sunset: 1200, ramp: 60, dayLevel: 85, moonLevel: 4 };
S.feedTimes = [450, 1110];
S.mode = 'auto';
S.manualLevel = 60;
S.minute = CLOCK_AT_START;
S.feedingUntil = 0;
S.lastStep = Date.now();
S.minutesSinceFed = CLOCK_AT_START - 450; // in the tank's time, like the clock
S.booted = Date.now() - (11 * 86400 + 7 * 3600 + 25 * 60) * 1000;
S.rssi = -61;
function phaseAt(minute) {
var p = S.schedule;
if (minute < p.sunrise || minute >= p.sunset + p.ramp) return 'moon';
if (minute < p.sunrise + p.ramp) return 'dawn';
if (minute < p.sunset) return 'day';
return 'dusk';
}
// The brightness the schedule asks for, in percent: moonlight, a ramp up from sunrise, the day, a ramp down from sunset.
function levelAt(minute) {
var p = S.schedule;
var phase = phaseAt(minute);
if (phase === 'moon') return p.moonLevel;
if (phase === 'day') return p.dayLevel;
if (phase === 'dawn') return p.moonLevel + ((p.dayLevel - p.moonLevel) * (minute - p.sunrise)) / p.ramp;
return p.dayLevel - ((p.dayLevel - p.moonLevel) * (minute - p.sunset)) / p.ramp;
}
// The water over a day: the lamp warms it a little, so it is warmest in the evening, and the heater's
// thermostat adds a small ripple.
function temperatureAt(minute) {
return 25.1 + 0.55 * Math.sin(((minute - 660) / 1440) * 2 * Math.PI) + 0.09 * Math.sin(minute / 21);
}
// The last 24 hours, oldest first.
var temperature = [];
var light = [];
for (var back = HISTORY_POINTS - 1; back >= 0; back--) {
var minute = (((CLOCK_AT_START - back * 15) % 1440) + 1440) % 1440;
temperature.push(round2(temperatureAt(minute) + ctx.between(-0.03, 0.03)));
light.push(Math.round(levelAt(minute)));
}
S.nextSample = Math.floor(CLOCK_AT_START / 15) * 15 + 15;
function currentLight() {
return S.mode === 'manual' ? S.manualLevel : levelAt(S.minute);
}
function currentTemperature() {
return round2(temperatureAt(S.minute) + 0.03 * Math.sin(ctx.seconds() / 3));
}
function feed(at) {
S.feedingUntil = at + FEED_TURN_MS;
S.minutesSinceFed = 0;
}
// Moves the pretend tank forward to this moment, in steps of a quarter of a second.
function advance() {
var at = Date.now();
var from = Math.max(S.lastStep, at - 4000); // a demo that was paused does not catch up
for (var t = from; t < at; t += 250) {
var dt = Math.min(250, at - t) / 1000;
var phase = phaseAt(S.minute);
var before = S.minute;
S.minute += dt * (phase === 'moon' ? PACE_NIGHT : phase === 'day' ? PACE_DAY : PACE_RAMP);
S.minutesSinceFed += S.minute - before;
for (var i = 0; i < S.feedTimes.length; i++) {
if (before < S.feedTimes[i] && S.minute >= S.feedTimes[i]) feed(t);
}
if (S.minute >= S.nextSample) {
S.nextSample += 15;
temperature.push(round2(temperatureAt(S.minute)));
light.push(Math.round(currentLight()));
if (temperature.length > HISTORY_POINTS) {
temperature.shift();
light.shift();
}
}
if (S.minute >= 1440) {
S.minute -= 1440;
S.nextSample -= 1440;
}
}
S.lastStep = at;
S.rssi = clamp(S.rssi + ctx.between(-0.6, 0.6), -68, -54);
}
function state() {
advance();
var at = Date.now();
var next = null;
for (var i = 0; i < S.feedTimes.length; i++) {
var wait = (S.feedTimes[i] - S.minute + 1440) % 1440;
if (next === null || wait < next) next = wait;
}
return {
temperature: currentTemperature(),
light: round1(currentLight()),
phase: phaseAt(S.minute),
mode: S.mode,
minuteOfDay: Math.floor(S.minute),
schedule: { sunrise: S.schedule.sunrise, sunset: S.schedule.sunset, ramp: S.schedule.ramp, dayLevel: S.schedule.dayLevel, moonLevel: S.schedule.moonLevel },
feeding: at < S.feedingUntil,
feedTimes: S.feedTimes.slice(),
nextFeedMinutes: next === null ? null : Math.ceil(next),
lastFedSecondsAgo: Math.round(S.minutesSinceFed * 60),
simulated: false,
rssi: Math.round(S.rssi),
uptimeSeconds: Math.round((at - S.booted) / 1000),
};
}
czDemo.define({
device: { name: 'aquarium', title: 'Aquarium' },
routes: {
'GET /state': state,
'GET /history': function () {
advance();
// As on the device: the stored samples, and the reading of this moment as the last point.
return {
intervalSeconds: INTERVAL_SECONDS,
temperature: temperature.slice(1).concat(currentTemperature()),
light: light.slice(1).concat(Math.round(currentLight())),
};
},
'POST /light': function (request, ctx) {
advance();
var body = request.json();
var hasLevel = body && typeof body.level === 'number';
if (!body || (body.mode !== 'auto' && body.mode !== 'manual' && !hasLevel)) {
return ctx.respond(400, { error: "Send { 'mode': 'auto' } or { 'mode': 'manual', 'level': percent }." });
}
if (body.mode === 'auto') {
S.mode = 'auto';
} else {
if (S.mode !== 'manual' && !hasLevel) S.manualLevel = Math.round(levelAt(S.minute)); // stays as bright as it is
S.mode = 'manual';
if (hasLevel) S.manualLevel = Math.round(clamp(body.level, 0, 100));
}
return state();
},
'POST /feed': function (request, ctx) {
advance();
if (Date.now() < S.feedingUntil) return ctx.respond(409, { error: 'The feeder is still turning.' });
feed(Date.now());
return state();
},
'POST /schedule': function (request, ctx) {
advance();
var body = request.json();
var p = S.schedule;
var known = body && (['sunrise', 'sunset', 'ramp', 'dayLevel', 'moonLevel'].some(function (key) { return typeof body[key] === 'number'; }) || Array.isArray(body.feedTimes));
if (!known) return ctx.respond(400, { error: 'Send sunrise, sunset, ramp, dayLevel, moonLevel or feedTimes.' });
if (typeof body.ramp === 'number') p.ramp = Math.round(clamp(body.ramp, 5, 120));
if (typeof body.sunrise === 'number') p.sunrise = Math.round(body.sunrise);
if (typeof body.sunset === 'number') p.sunset = Math.round(body.sunset);
// The day keeps its order: a dawn, at least an hour of day, a dusk that ends before midnight.
p.sunrise = clamp(p.sunrise, 0, 1425 - 2 * p.ramp - 60);
p.sunset = clamp(p.sunset, p.sunrise + p.ramp + 60, 1425 - p.ramp);
if (typeof body.dayLevel === 'number') p.dayLevel = Math.round(clamp(body.dayLevel, 10, 100));
if (typeof body.moonLevel === 'number') p.moonLevel = Math.round(clamp(body.moonLevel, 0, 10));
if (Array.isArray(body.feedTimes)) {
S.feedTimes = body.feedTimes
.filter(function (value) { return typeof value === 'number' && value >= 0 && value < 1440; })
.slice(0, MAX_FEED_TIMES)
.map(Math.round)
.sort(function (a, b) { return a - b; });
}
return state();
},
'GET /.cz/ui.json': function () {
return { name: 'Aquarium', entry: '/.cz/ui.html', version: '1', theme: { background: '#04121c', foreground: '#e6f6f8' } };
},
},
});
})();