if (chart) chart.destroy(); const ctx = document.getElementById('tempChart').getContext('2d'); chart = new Chart(ctx, type: 'line', data: labels: times, datasets: [ t < 0) ? 'red' : 'green'), pointRadius: 4, fill: true ] , options: responsive: true, plugins: tooltip: callbacks: label: (ctx) => `$ctx.raw°C` , annotation: annotations: safeZone: type: 'box', yMin: 0, yMax: 4, backgroundColor: 'rgba(0,255,0,0.1)' , scales: y: title: display: true, text: 'Temperature (°C)' , min: -2, max: 8, grid: , x: title: display: true, text: 'Time' ); // Count violations const violations = temps.filter(t => t > 4 );
@app.route('/api/temperatures') def get_temps(): fridge_id = request.args.get('fridge_id', 1) hours = int(request.args.get('hours', 24)) conn = sqlite3.connect('fridge.db') c = conn.cursor() since = datetime.now() - timedelta(hours=hours) c.execute('''SELECT recorded_at, temp FROM temperature_logs WHERE fridge_id = ? AND recorded_at > ? ORDER BY recorded_at''', (fridge_id, since)) data = ['time': row[0], 'temp': row[1] for row in c.fetchall()] conn.close() return jsonify(data) grafic temperatura frigider magazin alimentar
@app.route('/api/log', methods=['POST']) def log_temp(): fridge_id = request.json['fridge_id'] temp = request.json['temperature'] conn = sqlite3.connect('fridge.db') c = conn.cursor() c.execute("INSERT INTO temperature_logs (fridge_id, temp, recorded_at) VALUES (?,?,?)", (fridge_id, temp, datetime.now().isoformat())) conn.commit() conn.close() return jsonify('status': 'ok') if (chart) chart