-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
131 lines (116 loc) · 3.47 KB
/
Copy pathmain.js
File metadata and controls
131 lines (116 loc) · 3.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
// ---------- Sensor Values ----------
let values = {
ph: null,
tds: null,
optical: null
};
// ---------- Fetch sensor data ----------
function fetchSensor(sensor) {
fetch(`/get/${sensor}`)
.then(res => res.json())
.then(data => {
const statusEl = document.getElementById(sensor + "_status");
if (data.error) {
statusEl.innerHTML = "❌ No data available";
statusEl.className = "status-fail";
values[sensor] = null;
} else {
values[sensor] = data.value;
statusEl.innerHTML = "✔ " + data.value;
statusEl.className = "status-ok";
}
checkFinish();
})
.catch(() => {
const statusEl = document.getElementById(sensor + "_status");
statusEl.innerHTML = "❌ Server error";
statusEl.className = "status-fail";
values[sensor] = null;
checkFinish();
});
}
// ---------- Enable Finish Button ----------
function checkFinish() {
const finishBtn = document.getElementById("finishBtn");
if (!finishBtn) return;
if (values.ph !== null && values.tds !== null && values.optical !== null) {
finishBtn.disabled = false;
} else {
finishBtn.disabled = true;
}
}
// ---------- Save Data ----------
function finishCollect() {
const herb = document.getElementById("herb").value.trim();
const purityEl = document.querySelector("input[name='purity']:checked");
if (!herb) {
alert("Please enter herb name");
return;
}
if (!purityEl) {
alert("Please select purity");
return;
}
fetch("/finish", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({
herb: herb,
purity: purityEl.value,
ph: values.ph,
tds: values.tds,
optical: values.optical
})
})
.then(res => res.json())
.then(data => {
if (data.status === "saved") {
alert("Data saved successfully 🌿");
clearSensors();
document.getElementById("herb").value = "";
}
})
.catch(() => {
alert("Error saving data. Try again.");
});
}
// ---------- Prediction ----------
function predict() {
if (values.ph === null || values.tds === null || values.optical === null) {
alert("Collect all sensor values before prediction");
return;
}
fetch("/predict_result", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify(values)
})
.then(res => res.json())
.then(data => {
const resultEl = document.getElementById("result");
resultEl.innerHTML = `
<h3>Prediction Result 🌿</h3>
<p><b>Herb:</b> ${data.herb}</p>
<p><b>Purity:</b> ${data.purity}</p>
<p><b>Confidence:</b> ${data.confidence}%</p>
`;
})
.catch(() => {
alert("Prediction failed. Make sure model is trained & sensors sent data.");
});
}
// ---------- Clear / Reset ----------
function clearSensors() {
values = { ph: null, tds: null, optical: null };
["ph", "tds", "optical"].forEach(sensor => {
const el = document.getElementById(sensor + "_status");
if (el) {
el.innerHTML = "❌ Not collected";
el.className = "status-fail";
}
});
const finishBtn = document.getElementById("finishBtn");
if (finishBtn) finishBtn.disabled = true;
const resultEl = document.getElementById("result");
if (resultEl) resultEl.innerHTML = "";
}