Spaces:
Sleeping
Sleeping
Update templates/predict.html
Browse files- templates/predict.html +80 -29
templates/predict.html
CHANGED
|
@@ -23,7 +23,6 @@
|
|
| 23 |
background-color: white;
|
| 24 |
border-radius: 15px;
|
| 25 |
box-shadow: 0 4px 12px rgba(0,0,0,0.1);
|
| 26 |
-
/* FIX 3: Start with content hidden and add a smooth fade-in transition */
|
| 27 |
opacity: 0;
|
| 28 |
transition: opacity 0.5s ease-in-out;
|
| 29 |
}
|
|
@@ -63,24 +62,21 @@
|
|
| 63 |
.btn-start-motor:hover {
|
| 64 |
background-color: #218838;
|
| 65 |
}
|
| 66 |
-
|
| 67 |
-
/* --- FIX 1 & 2: Camera Container, Video, and Timer Styles --- */
|
| 68 |
.camera-container {
|
| 69 |
margin-top: 20px;
|
| 70 |
-
display: none;
|
| 71 |
-
/* Use Flexbox to stack and center children */
|
| 72 |
display: flex;
|
| 73 |
-
flex-direction: column;
|
| 74 |
-
align-items: center;
|
| 75 |
-
gap: 20px;
|
| 76 |
}
|
| 77 |
video {
|
| 78 |
-
width: 100%;
|
| 79 |
-
max-width: 720px;
|
| 80 |
height: auto;
|
| 81 |
border-radius: 10px;
|
| 82 |
border: 2px solid #ddd;
|
| 83 |
-
background-color: #000;
|
| 84 |
}
|
| 85 |
.timer {
|
| 86 |
font-size: 2rem;
|
|
@@ -90,16 +86,14 @@
|
|
| 90 |
padding: 10px 25px;
|
| 91 |
border-radius: 10px;
|
| 92 |
display: inline-block;
|
| 93 |
-
min-width: 120px;
|
| 94 |
text-align: center;
|
| 95 |
}
|
| 96 |
-
/* Style for the "Finished" button state */
|
| 97 |
.timer-finished {
|
| 98 |
font-size: 1.5rem;
|
| 99 |
padding: 15px 40px;
|
| 100 |
-
background-color: #28a745;
|
| 101 |
}
|
| 102 |
-
|
| 103 |
.alert-message {
|
| 104 |
font-size: 1.5rem;
|
| 105 |
color: #dc3545;
|
|
@@ -107,6 +101,12 @@
|
|
| 107 |
margin-top: 20px;
|
| 108 |
text-align: center;
|
| 109 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 110 |
</style>
|
| 111 |
</head>
|
| 112 |
<body>
|
|
@@ -143,7 +143,7 @@
|
|
| 143 |
<button id="startMonitoring" class="btn btn-start-motor">Start AI Monitoring</button>
|
| 144 |
</div>
|
| 145 |
|
| 146 |
-
<!-- The camera and timer
|
| 147 |
<div class="camera-container" id="cameraContainer">
|
| 148 |
<h4 class="text-center mb-0">Live Irrigation Feed</h4>
|
| 149 |
<video id="videoElement" autoplay playsinline></video>
|
|
@@ -153,15 +153,56 @@
|
|
| 153 |
<div class="alert-message" id="alertMessage"></div>
|
| 154 |
</div>
|
| 155 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 156 |
<script>
|
| 157 |
-
//
|
| 158 |
window.onload = function() {
|
| 159 |
document.querySelector('.container').style.opacity = 1;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 160 |
};
|
| 161 |
-
|
| 162 |
-
document.getElementById('startMonitoring').addEventListener('click', function() {
|
| 163 |
-
document.getElementById('cameraContainer').style.display = 'flex'; // Use flex to show it
|
| 164 |
|
|
|
|
|
|
|
| 165 |
if (navigator.mediaDevices && navigator.mediaDevices.getUserMedia) {
|
| 166 |
navigator.mediaDevices.getUserMedia({ video: { facingMode: 'environment' } })
|
| 167 |
.then(function(stream) {
|
|
@@ -172,23 +213,19 @@
|
|
| 172 |
alert("Could not access the camera. Please ensure you have given permission.");
|
| 173 |
});
|
| 174 |
}
|
| 175 |
-
|
| 176 |
let estimatedTime = parseFloat("{{ estimated_time_duration }}");
|
| 177 |
let timeUnit = "{{ time_unit }}";
|
| 178 |
let estimatedTimeSeconds = timeUnit === "minutes" ? estimatedTime * 60 : estimatedTime;
|
| 179 |
-
|
| 180 |
let timerElem = document.getElementById('timer');
|
| 181 |
let timeRemaining = Math.floor(estimatedTimeSeconds);
|
| 182 |
|
| 183 |
-
|
| 184 |
-
|
| 185 |
let countdown = setInterval(function() {
|
| 186 |
if (timeRemaining <= 0) {
|
| 187 |
clearInterval(countdown);
|
| 188 |
-
// Update timer to look like a finished button
|
| 189 |
timerElem.textContent = "Finished";
|
| 190 |
-
timerElem.classList.add('timer-finished');
|
| 191 |
-
|
| 192 |
let beep = new Audio('/static/alarn_tune.mp3');
|
| 193 |
beep.play();
|
| 194 |
document.getElementById('alertMessage').textContent = "Irrigation time is over. Motor is off automatically.";
|
|
@@ -198,10 +235,24 @@
|
|
| 198 |
timeRemaining--;
|
| 199 |
}, 1000);
|
| 200 |
|
|
|
|
| 201 |
fetch('/start_motor', { method: 'POST' })
|
| 202 |
.then(response => response.json())
|
| 203 |
-
.then(data =>
|
| 204 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 205 |
});
|
| 206 |
</script>
|
| 207 |
</body>
|
|
|
|
| 23 |
background-color: white;
|
| 24 |
border-radius: 15px;
|
| 25 |
box-shadow: 0 4px 12px rgba(0,0,0,0.1);
|
|
|
|
| 26 |
opacity: 0;
|
| 27 |
transition: opacity 0.5s ease-in-out;
|
| 28 |
}
|
|
|
|
| 62 |
.btn-start-motor:hover {
|
| 63 |
background-color: #218838;
|
| 64 |
}
|
|
|
|
|
|
|
| 65 |
.camera-container {
|
| 66 |
margin-top: 20px;
|
| 67 |
+
display: none;
|
|
|
|
| 68 |
display: flex;
|
| 69 |
+
flex-direction: column;
|
| 70 |
+
align-items: center;
|
| 71 |
+
gap: 20px;
|
| 72 |
}
|
| 73 |
video {
|
| 74 |
+
width: 100%;
|
| 75 |
+
max-width: 720px;
|
| 76 |
height: auto;
|
| 77 |
border-radius: 10px;
|
| 78 |
border: 2px solid #ddd;
|
| 79 |
+
background-color: #000;
|
| 80 |
}
|
| 81 |
.timer {
|
| 82 |
font-size: 2rem;
|
|
|
|
| 86 |
padding: 10px 25px;
|
| 87 |
border-radius: 10px;
|
| 88 |
display: inline-block;
|
| 89 |
+
min-width: 120px;
|
| 90 |
text-align: center;
|
| 91 |
}
|
|
|
|
| 92 |
.timer-finished {
|
| 93 |
font-size: 1.5rem;
|
| 94 |
padding: 15px 40px;
|
| 95 |
+
background-color: #28a745;
|
| 96 |
}
|
|
|
|
| 97 |
.alert-message {
|
| 98 |
font-size: 1.5rem;
|
| 99 |
color: #dc3545;
|
|
|
|
| 101 |
margin-top: 20px;
|
| 102 |
text-align: center;
|
| 103 |
}
|
| 104 |
+
|
| 105 |
+
/* Modal small style tweak */
|
| 106 |
+
.modal-header {
|
| 107 |
+
background-color: #28a745;
|
| 108 |
+
color: white;
|
| 109 |
+
}
|
| 110 |
</style>
|
| 111 |
</head>
|
| 112 |
<body>
|
|
|
|
| 143 |
<button id="startMonitoring" class="btn btn-start-motor">Start AI Monitoring</button>
|
| 144 |
</div>
|
| 145 |
|
| 146 |
+
<!-- The camera and timer -->
|
| 147 |
<div class="camera-container" id="cameraContainer">
|
| 148 |
<h4 class="text-center mb-0">Live Irrigation Feed</h4>
|
| 149 |
<video id="videoElement" autoplay playsinline></video>
|
|
|
|
| 153 |
<div class="alert-message" id="alertMessage"></div>
|
| 154 |
</div>
|
| 155 |
|
| 156 |
+
<!-- --- START: Modal asking 1 or 0 --- -->
|
| 157 |
+
<div class="modal fade" id="startModal" tabindex="-1" aria-labelledby="startModalLabel" aria-hidden="true">
|
| 158 |
+
<div class="modal-dialog modal-dialog-centered">
|
| 159 |
+
<div class="modal-content">
|
| 160 |
+
<div class="modal-header">
|
| 161 |
+
<h5 class="modal-title" id="startModalLabel">Start Irrigation</h5>
|
| 162 |
+
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
|
| 163 |
+
</div>
|
| 164 |
+
<div class="modal-body">
|
| 165 |
+
<p>Would you like to start the motor now?</p>
|
| 166 |
+
<p class="mb-0"><strong>Reply 1</strong> = Start <strong>Reply 0</strong> = Cancel</p>
|
| 167 |
+
</div>
|
| 168 |
+
<div class="modal-footer">
|
| 169 |
+
<button id="modalCancelBtn" type="button" class="btn btn-secondary" data-bs-dismiss="modal">0 — Don't Start</button>
|
| 170 |
+
<button id="modalStartBtn" type="button" class="btn btn-success">1 — Start</button>
|
| 171 |
+
</div>
|
| 172 |
+
</div>
|
| 173 |
+
</div>
|
| 174 |
+
</div>
|
| 175 |
+
<!-- --- END: Modal --- -->
|
| 176 |
+
|
| 177 |
+
<!-- Bootstrap bundle (includes Popper) -->
|
| 178 |
+
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha1/dist/js/bootstrap.bundle.min.js"></script>
|
| 179 |
+
|
| 180 |
<script>
|
| 181 |
+
// Fade in container
|
| 182 |
window.onload = function() {
|
| 183 |
document.querySelector('.container').style.opacity = 1;
|
| 184 |
+
|
| 185 |
+
// Show the modal automatically on load
|
| 186 |
+
var startModalEl = document.getElementById('startModal');
|
| 187 |
+
var bsModal = new bootstrap.Modal(startModalEl, { backdrop: 'static', keyboard: false });
|
| 188 |
+
bsModal.show();
|
| 189 |
+
|
| 190 |
+
// Button handlers for modal
|
| 191 |
+
document.getElementById('modalStartBtn').addEventListener('click', function() {
|
| 192 |
+
bsModal.hide();
|
| 193 |
+
// Programmatically click the Start Monitoring button so existing logic executes (camera + client timer + server POST)
|
| 194 |
+
document.getElementById('startMonitoring').click();
|
| 195 |
+
});
|
| 196 |
+
|
| 197 |
+
document.getElementById('modalCancelBtn').addEventListener('click', function() {
|
| 198 |
+
// User chose "0" - do not start monitoring
|
| 199 |
+
bsModal.hide();
|
| 200 |
+
document.getElementById('alertMessage').textContent = "Motor was not started.";
|
| 201 |
+
});
|
| 202 |
};
|
|
|
|
|
|
|
|
|
|
| 203 |
|
| 204 |
+
document.getElementById('startMonitoring').addEventListener('click', function() {
|
| 205 |
+
document.getElementById('cameraContainer').style.display = 'flex';
|
| 206 |
if (navigator.mediaDevices && navigator.mediaDevices.getUserMedia) {
|
| 207 |
navigator.mediaDevices.getUserMedia({ video: { facingMode: 'environment' } })
|
| 208 |
.then(function(stream) {
|
|
|
|
| 213 |
alert("Could not access the camera. Please ensure you have given permission.");
|
| 214 |
});
|
| 215 |
}
|
| 216 |
+
|
| 217 |
let estimatedTime = parseFloat("{{ estimated_time_duration }}");
|
| 218 |
let timeUnit = "{{ time_unit }}";
|
| 219 |
let estimatedTimeSeconds = timeUnit === "minutes" ? estimatedTime * 60 : estimatedTime;
|
|
|
|
| 220 |
let timerElem = document.getElementById('timer');
|
| 221 |
let timeRemaining = Math.floor(estimatedTimeSeconds);
|
| 222 |
|
| 223 |
+
// Note: start_motor fetch is already below. We keep it so server-side timer + completion messages work.
|
|
|
|
| 224 |
let countdown = setInterval(function() {
|
| 225 |
if (timeRemaining <= 0) {
|
| 226 |
clearInterval(countdown);
|
|
|
|
| 227 |
timerElem.textContent = "Finished";
|
| 228 |
+
timerElem.classList.add('timer-finished');
|
|
|
|
| 229 |
let beep = new Audio('/static/alarn_tune.mp3');
|
| 230 |
beep.play();
|
| 231 |
document.getElementById('alertMessage').textContent = "Irrigation time is over. Motor is off automatically.";
|
|
|
|
| 235 |
timeRemaining--;
|
| 236 |
}, 1000);
|
| 237 |
|
| 238 |
+
// Call the server endpoint which now starts the server-side timer (so finalization/WhatsApp behavior remains consistent)
|
| 239 |
fetch('/start_motor', { method: 'POST' })
|
| 240 |
.then(response => response.json())
|
| 241 |
+
.then(data => {
|
| 242 |
+
console.log("Monitoring request sent:", data);
|
| 243 |
+
if (data && data.status === "motor_started") {
|
| 244 |
+
// optionally show a confirmation to user
|
| 245 |
+
document.getElementById('alertMessage').textContent = `Motor started for ${data.estimated_time_duration} ${data.time_unit}.`;
|
| 246 |
+
} else {
|
| 247 |
+
if (data && data.status === "no_pending_task") {
|
| 248 |
+
document.getElementById('alertMessage').textContent = "No pending irrigation task found. Please submit a new prediction.";
|
| 249 |
+
}
|
| 250 |
+
}
|
| 251 |
+
})
|
| 252 |
+
.catch(error => {
|
| 253 |
+
console.error("Error sending monitoring request:", error);
|
| 254 |
+
document.getElementById('alertMessage').textContent = "Error starting motor on server. Check logs.";
|
| 255 |
+
});
|
| 256 |
});
|
| 257 |
</script>
|
| 258 |
</body>
|