Changes to be committed:

modified:   static/js/sensor-1.js
This commit is contained in:
IrisVega 2024-07-20 15:06:02 +08:00
parent 6db2ee1434
commit 13b080cc47

View File

@ -1,4 +1,3 @@
//折线图
document.addEventListener('DOMContentLoaded', function () {
var chartContainer = document.getElementById('chart-container');
var myChart = echarts.init(chartContainer);
@ -7,12 +6,12 @@ document.addEventListener('DOMContentLoaded', function () {
var temperatures = [];
function fetchAndUpdateData() {
fetchLatestSensorData()
fetch('http://111.230.197.156:8000/get_sensor_data')
.then(response => response.json())
.then(data => {
timestamps = data.timestamps.slice(-200); // 只保留最新的200个数据
temperatures = data.temperatures.slice(-200); // 只保留最新的200个数据
var option = {
title: {
text: '温度数据',
@ -52,28 +51,13 @@ document.addEventListener('DOMContentLoaded', function () {
};
myChart.setOption(option);
});
fetch('http://111.230.197.156:8000/get_latest_sensor_data')
.then(response => response.json())
.then(data => {
document.getElementById('temperature').textContent = `当前温度: ${temperatures.slice(-1)}°C`;
// 更新最新的温度
document.getElementById('temperature').textContent = `当前温度: ${temperatures.slice(-1)}`;
});
}
// 每5秒刷新一次数据
setInterval(fetchAndUpdateData, 1000);
fetchAndUpdateData(); // 初始加载数据
fetchAndUpdateData();
});
//获取当前的湿度
async function fetchLatestSensorData() {
const response = await fetch('http://111.230.197.156:8000/get_latest_sensor_data');
const data = await response.json();
document.getElementById('temperature').textContent = `当前温度: ${temperatures.slice(-1)}`;
}