Files

45 lines
1.7 KiB
JavaScript

(() => {
const { Gauge } = window.Lab;
const { RotaryValve } = window.MachineControls;
const M = window.CoffeeMachine;
const groups = [
{ key: "15", machine: { ...M.defaults, coldTemp: 15 } },
{ key: "20", machine: { ...M.defaults, coldTemp: 20 } }
];
const gauges = groups.map(group => new Gauge(document.getElementById(`temperatureGauge${group.key}`), {
min: 0, max: 100, unit: "°C", label: "T_s"
}));
const sensorTemperatures = groups.map(group => M.temperature(group.machine, 0.6));
let currentValve = 0.6;
let previousFrame = performance.now();
function synchronize(value) {
currentValve = value;
valves.forEach(valve => valve.setValue(value, false));
groups.forEach(group => {
document.getElementById(`hotValveValue${group.key}`).textContent = `${Math.round(value * 100)}%`;
document.getElementById(`hotFlowValue${group.key}`).textContent = M.hotFlow(group.machine, value).toFixed(3);
});
}
const valves = groups.map(group => new RotaryValve(document.getElementById(`hotValve${group.key}`), {
value: currentValve,
label: `${group.key}°C 冷水 · 60% 峰值沸水阀门角度`,
onChange: synchronize
}));
function frame(now) {
const dt = Math.min(0.1, Math.max(0, (now - previousFrame) / 1000));
previousFrame = now;
groups.forEach((group, index) => {
sensorTemperatures[index] = M.probeStep(group.machine, sensorTemperatures[index], currentValve, dt);
gauges[index].update(sensorTemperatures[index]);
});
requestAnimationFrame(frame);
}
document.addEventListener("visibilitychange", () => { previousFrame = performance.now(); });
synchronize(currentValve);
requestAnimationFrame(frame);
})();