Skip to content

Commit 4677d16

Browse files
committed
Add Comments in index.html
1 parent f0f74e7 commit 4677d16

File tree

1 file changed

+35
-14
lines changed

1 file changed

+35
-14
lines changed

templates/index.html

Lines changed: 35 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,10 @@ <h1>Chords-Python Applications</h1>
1919
<p>{{ message }}</p>
2020
</div>
2121
{% endif %}
22-
22+
<!-- Container for LSL and NPG stream control buttons -->
2323
<div class="controls-container">
2424
<div class="controls">
25+
<!-- Button to start LSL Stream, disabled if already running or if NPG is running -->
2526
<button id="start_lsl_button"
2627
class="{% if lsl_started %}running{% else %}not-running{% endif %}"
2728
onclick="showCSVConfirmation()"
@@ -30,6 +31,7 @@ <h1>Chords-Python Applications</h1>
3031
</button>
3132
</div>
3233
<div class="controls">
34+
<!-- Button to start NPG Stream, disabled if already running or if LSL is running -->
3335
<button id="start_npg_button"
3436
class="{% if npg_started %}running{% else %}not-running{% endif %}"
3537
onclick="showNPGPopup()"
@@ -50,7 +52,7 @@ <h1>Chords-Python Applications</h1>
5052
</form>
5153
</div>
5254
</div>
53-
55+
<!-- App buttons (disabled until either one of stream is active) -->
5456
<div class="app-buttons {% if not (lsl_started or npg_started) %}disabled-apps{% endif %}">
5557
<!-- Row 1: ECG, EMG, EOG, EEG -->
5658
<div class="row">
@@ -78,7 +80,7 @@ <h1>Chords-Python Applications</h1>
7880
</form>
7981
</div>
8082

81-
<!-- Row 2: Game, GUI, Keystroke, CSVPlotter -->
83+
<!-- Row 2: Tug of War Game,EEG Beetle Game, GUI, Keystroke, CSVPlotter -->
8284
<div class="row">
8385
<form action="/run_app" method="POST">
8486
<button type="submit" name="app_name" value="game"
@@ -114,10 +116,13 @@ <h1>Chords-Python Applications</h1>
114116
<!-- NPG Device Selection Popup -->
115117
<div id="npgDevicePopup">
116118
<h3>Select NPG Device</h3>
119+
<!-- Status message for scanning status or connection updates -->
117120
<div id="npgPopupStatus">Ready to scan devices</div>
121+
<!-- Device list area where detected devices will be listed -->
118122
<div id="npgDeviceList">
119123
<p>Click "Scan Devices" to begin</p>
120124
</div>
125+
<!-- Buttons for scanning, connecting, or canceling the device selection -->
121126
<div class="npg-popup-buttons">
122127
<button class="npg-popup-btn" id="scanDevicesBtn" onclick="scanNPGDevices()">Scan Devices</button>
123128
<button class="npg-popup-btn npg-connect-btn" id="npgConnectBtn" disabled onclick="connectToDevice()">Connect</button>
@@ -126,10 +131,11 @@ <h3>Select NPG Device</h3>
126131
</div>
127132

128133
<script>
129-
let selectedDevice = null;
130-
let connectionCheckInterval = null;
131-
let eventSource;
134+
let selectedDevice = null; // Store the selected device address
135+
let connectionCheckInterval = null; // Used to repeatedly check connection status
136+
let eventSource; // EventSource for server-sent events for real-time updates
132137

138+
// Function to display the NPG device selection popup
133139
function showNPGPopup() {
134140
document.getElementById('npgDevicePopup').style.display = 'block';
135141
document.getElementById('csvConfirmationPopup').style.display = 'none';
@@ -139,6 +145,7 @@ <h3>Select NPG Device</h3>
139145
selectedDevice = null;
140146
}
141147

148+
// Function to hide the NPG popup and stop connection checks
142149
function hideNPGPopup() {
143150
document.getElementById('npgDevicePopup').style.display = 'none';
144151
if (connectionCheckInterval) {
@@ -147,11 +154,13 @@ <h3>Select NPG Device</h3>
147154
}
148155
}
149156

157+
// Function to fetch and list available NPG devices from backend
150158
async function scanNPGDevices() {
151159
const statusDiv = document.getElementById('npgPopupStatus');
152160
const deviceList = document.getElementById('npgDeviceList');
153161
const scanBtn = document.getElementById('scanDevicesBtn');
154162

163+
// Show scanning status and disable scan button
155164
scanBtn.disabled = true;
156165
statusDiv.textContent = 'Scanning for devices...';
157166
statusDiv.className = 'scanning-status';
@@ -161,15 +170,18 @@ <h3>Select NPG Device</h3>
161170
const response = await fetch('/scan_devices', { method: 'POST' });
162171
const data = await response.json();
163172

173+
// If devices are found
164174
if (data.status === 'success' && data.devices.length > 0) {
165175
deviceList.innerHTML = '';
166176

177+
// Display each device to UI and make them selectable
167178
data.devices.forEach(device => {
168179
const div = document.createElement('div');
169180
div.className = 'npg-device-item';
170181
div.textContent = `${device.name || 'Unknown'} (${device.address})`;
171182
div.dataset.address = device.address;
172183

184+
// Add click event to highlight selected device
173185
div.addEventListener('click', () => {
174186
document.querySelectorAll('.npg-device-item').forEach(item => {
175187
item.classList.remove('selected');
@@ -189,23 +201,25 @@ <h3>Select NPG Device</h3>
189201
statusDiv.textContent = data.message || 'No devices found';
190202
statusDiv.className = 'error-status';
191203
}
192-
} catch (error) {
204+
} catch (error) { // Handle scan error
193205
console.error('Scan error:', error);
194206
deviceList.innerHTML = '<p>Scan failed</p>';
195207
statusDiv.textContent = 'Scan failed - please try again';
196208
statusDiv.className = 'error-status';
197209
} finally {
198-
scanBtn.disabled = false;
210+
scanBtn.disabled = false; // Re-enable scan button
199211
}
200212
}
201213

214+
// Function to connect to the selected NPG device
202215
async function connectToDevice() {
203216
if (!selectedDevice) return;
204217

205218
const statusDiv = document.getElementById('npgPopupStatus');
206219
const connectBtn = document.getElementById('npgConnectBtn');
207220
const scanBtn = document.getElementById('scanDevicesBtn');
208221

222+
// Show connecting state
209223
connectBtn.disabled = true;
210224
scanBtn.disabled = true;
211225
statusDiv.textContent = 'Connecting to device...';
@@ -222,17 +236,20 @@ <h3>Select NPG Device</h3>
222236

223237
const data = await response.json();
224238

239+
// If connection is pending, start checking status
225240
if (data.status === 'pending') {
226241
const checkStatus = async () => {
227242
const statusResponse = await fetch('/check_connection');
228243
const statusData = await statusResponse.json();
229244

245+
// Log connection status for debugging
230246
console.log("Connection status:", statusData);
247+
// Check if connected or if there was an error
231248
if (statusData.connected) {
232249
clearInterval(connectionCheckInterval);
233250
statusDiv.textContent = statusData.message;
234251
statusDiv.className = 'connected-status';
235-
setTimeout(() => {
252+
setTimeout(() => { // Hide popup after 1 second
236253
hideNPGPopup();
237254
window.location.reload(); // Force refresh to sync state
238255
}, 1000);
@@ -265,21 +282,24 @@ <h3>Select NPG Device</h3>
265282
}
266283
}
267284

285+
// Function to show the CSV confirmation popup when the LSL button is clicked
268286
function showCSVConfirmation() {
269287
document.getElementById('csvConfirmationPopup').style.display = 'block';
270288
document.getElementById('npgDevicePopup').style.display = 'none';
271289
}
272290

291+
// Function to set the CSV choice and submit the form. This is called when the user clicks YES or NO in the CSV Popup.
273292
function setCSVChoice(saveAsCSV) {
274293
document.getElementById('csvInput').value = saveAsCSV ? 'true' : 'false';
275294
document.getElementById('csvForm').submit();
276295
document.getElementById('csvConfirmationPopup').style.display = 'none';
277296
}
278297

298+
// Function to update the state of app buttons based on running applications. This function is called when the server sends updates about running applications.
279299
function updateAppButtons(runningApps) {
280300
// Get all app buttons
281301
const appButtons = document.querySelectorAll('.app-buttons button[type="submit"]');
282-
302+
// Loop through each button and check if the corresponding app is running
283303
appButtons.forEach(button => {
284304
const appName = button.value;
285305
const isRunning = runningApps.includes(appName);
@@ -296,6 +316,7 @@ <h3>Select NPG Device</h3>
296316
});
297317
}
298318

319+
// Function to set up the EventSource for receiving real-time updates from the server. This function is called when the page loads and whenever the connection is re-established.
299320
function setupEventSource() {
300321
if (eventSource) eventSource.close();
301322

@@ -364,13 +385,13 @@ <h3>Select NPG Device</h3>
364385
}
365386

366387
document.addEventListener('DOMContentLoaded', function() {
367-
setupEventSource();
388+
setupEventSource(); // Initialize the SSE connection to receive real-time updates from the server
368389

369-
// Initial state setup
390+
// Fetch the current status of running applications when the page loads
370391
fetch('/check_app_status')
371-
.then(response => response.json())
392+
.then(response => response.json()) // Parse the JSON response from the server
372393
.then(data => {
373-
if (data.running_apps) {
394+
if (data.running_apps) { // If there are running applications, update the UI to show their current state
374395
updateAppButtons(data.running_apps);
375396
}
376397
});

0 commit comments

Comments
 (0)