mirror of
https://github.com/xiaozhou233/RiverWaterQualityMonitoring.git
synced 2026-12-05 13:05:53 +08:00
重构数据加载逻辑,更新API地址,优化数据处理和错误处理
This commit is contained in:
1 parent
4a87a9869d
commit
59a7f85293
1 file changed
+45
-46
+45
-46
@@ -1,5 +1,5 @@
|
|||||||
const SCORE_API = "http://127.0.0.1:8000/data/score?token=mE7yG0kI";
|
const API_BASE = "http://127.0.0.1:8000";
|
||||||
const HISTORY_API = "http://127.0.0.1:8000/data/all?token=mE7yG0kI";
|
const DATA_API = API_BASE + "/data/integration";
|
||||||
|
|
||||||
let phChart = null;
|
let phChart = null;
|
||||||
let tdsChart = null;
|
let tdsChart = null;
|
||||||
@@ -49,19 +49,24 @@ function createChart(chart, title, color) {
|
|||||||
},
|
},
|
||||||
|
|
||||||
series: [{
|
series: [{
|
||||||
|
|
||||||
name: title,
|
name: title,
|
||||||
type: "line",
|
type: "line",
|
||||||
smooth: true,
|
smooth: true,
|
||||||
symbol: "none",
|
symbol: "none",
|
||||||
|
|
||||||
lineStyle: {
|
lineStyle: {
|
||||||
width: 3,
|
width: 3,
|
||||||
color: color
|
color: color
|
||||||
},
|
},
|
||||||
|
|
||||||
areaStyle: {
|
areaStyle: {
|
||||||
opacity: 0.15,
|
opacity: 0.15,
|
||||||
color: color
|
color: color
|
||||||
},
|
},
|
||||||
|
|
||||||
data: []
|
data: []
|
||||||
|
|
||||||
}]
|
}]
|
||||||
|
|
||||||
});
|
});
|
||||||
@@ -85,7 +90,7 @@ function initCharts() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/* ===========================
|
/* ===========================
|
||||||
Update Item
|
Update Card
|
||||||
=========================== */
|
=========================== */
|
||||||
|
|
||||||
function setItem(name, data) {
|
function setItem(name, data) {
|
||||||
@@ -102,63 +107,58 @@ function setItem(name, data) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/* ===========================
|
/* ===========================
|
||||||
Load Score
|
Load Data
|
||||||
=========================== */
|
=========================== */
|
||||||
|
|
||||||
async function loadData() {
|
async function loadData() {
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
|
||||||
const res = await fetch(SCORE_API);
|
const res = await fetch(DATA_API);
|
||||||
|
|
||||||
const data = await res.json();
|
const result = await res.json();
|
||||||
|
|
||||||
document.getElementById("score").innerHTML = data.score;
|
if (result.code !== 200) {
|
||||||
|
|
||||||
document.getElementById("level").innerHTML = data.level;
|
console.error(result.message);
|
||||||
|
return;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
/* ===========================
|
||||||
|
Score
|
||||||
|
=========================== */
|
||||||
|
|
||||||
|
const score = result.score;
|
||||||
|
|
||||||
|
document.getElementById("score").innerHTML =
|
||||||
|
score.score;
|
||||||
|
|
||||||
|
document.getElementById("level").innerHTML =
|
||||||
|
score.level;
|
||||||
|
|
||||||
document.getElementById("time").innerHTML =
|
document.getElementById("time").innerHTML =
|
||||||
data.time.substring(11, 19);
|
result.time.substring(11, 19);
|
||||||
|
|
||||||
const abnormal =
|
const abnormal =
|
||||||
document.getElementById("abnormal");
|
document.getElementById("abnormal");
|
||||||
|
|
||||||
abnormal.innerHTML =
|
abnormal.innerHTML =
|
||||||
data.abnormal ? "异常" : "正常";
|
score.abnormal ? "异常" : "正常";
|
||||||
|
|
||||||
abnormal.className =
|
abnormal.className =
|
||||||
"card-value " +
|
"card-value " +
|
||||||
(data.abnormal ? "abnormal" : "normal");
|
(score.abnormal ? "abnormal" : "normal");
|
||||||
|
|
||||||
setItem("ph", data.ph);
|
setItem("ph", score.ph);
|
||||||
setItem("tds", data.tds);
|
setItem("tds", score.tds);
|
||||||
setItem("turbidity", data.turbidity);
|
setItem("turbidity", score.turbidity);
|
||||||
|
|
||||||
}
|
/* ===========================
|
||||||
catch (e) {
|
History
|
||||||
|
=========================== */
|
||||||
|
|
||||||
console.error("Load Score Error:", e);
|
const history = result.data.slice(-100);
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
/* ===========================
|
|
||||||
Load History
|
|
||||||
=========================== */
|
|
||||||
|
|
||||||
async function loadHistory() {
|
|
||||||
|
|
||||||
try {
|
|
||||||
|
|
||||||
const res = await fetch(HISTORY_API);
|
|
||||||
|
|
||||||
const all = await res.json();
|
|
||||||
|
|
||||||
if (!Array.isArray(all) || all.length === 0)
|
|
||||||
return;
|
|
||||||
|
|
||||||
const history = all.slice(-100);
|
|
||||||
|
|
||||||
const time = history.map(item =>
|
const time = history.map(item =>
|
||||||
item.time.substring(11, 19));
|
item.time.substring(11, 19));
|
||||||
@@ -208,7 +208,7 @@ async function loadHistory() {
|
|||||||
}
|
}
|
||||||
catch (e) {
|
catch (e) {
|
||||||
|
|
||||||
console.error("Load History Error:", e);
|
console.error("Load Data Error:", e);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -233,11 +233,14 @@ window.addEventListener("resize", () => {
|
|||||||
|
|
||||||
requestAnimationFrame(() => {
|
requestAnimationFrame(() => {
|
||||||
|
|
||||||
if (phChart) phChart.resize();
|
if (phChart)
|
||||||
|
phChart.resize();
|
||||||
|
|
||||||
if (tdsChart) tdsChart.resize();
|
if (tdsChart)
|
||||||
|
tdsChart.resize();
|
||||||
|
|
||||||
if (turbidityChart) turbidityChart.resize();
|
if (turbidityChart)
|
||||||
|
turbidityChart.resize();
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -263,16 +266,12 @@ window.addEventListener("load", async () => {
|
|||||||
|
|
||||||
await loadData();
|
await loadData();
|
||||||
|
|
||||||
await loadHistory();
|
|
||||||
|
|
||||||
setInterval(updateClock, 1000);
|
setInterval(updateClock, 1000);
|
||||||
|
|
||||||
setInterval(async () => {
|
setInterval(async () => {
|
||||||
|
|
||||||
await loadData();
|
await loadData();
|
||||||
|
|
||||||
await loadHistory();
|
|
||||||
|
|
||||||
}, 3000);
|
}, 3000);
|
||||||
|
|
||||||
});
|
});
|
||||||
Reference in new issue
Block a user