Skip to content

Commit 1bd152a

Browse files
committed
Worked - need some changes like UI, Disconnect button
1 parent a2d87ab commit 1bd152a

File tree

3 files changed

+1093
-467
lines changed

3 files changed

+1093
-467
lines changed

app.py

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,17 @@
66
from pylsl import resolve_streams
77
import time
88
from chords_ble import Chords_BLE
9+
import subprocess
10+
import os
11+
import sys
912

1013
app = Flask(__name__)
1114
connection_manager = None
1215
ble_devices = []
1316
active_connection = None
1417
lsl_stream_active = False
1518
csv_logging_enabled = False # Global CSV logging state
19+
running_apps = {} # Dictionary to keep track of running applications
1620

1721
@app.route('/set_csv', methods=['POST'])
1822
def set_csv():
@@ -109,8 +113,85 @@ def connect_wifi():
109113

110114
def connect_ble(address):
111115
global active_connection, lsl_stream_active, csv_logging_enabled
116+
lsl_stream_active = False
117+
Thread(target=check_lsl_stream).start() # Add this line
112118
active_connection = Connection(csv_logging=csv_logging_enabled)
113119
active_connection.connect_ble(address) # No duplicate device selection
114120

121+
@app.route('/run_application', methods=['POST'])
122+
def run_application():
123+
global lsl_stream_active, running_apps
124+
125+
if not lsl_stream_active:
126+
return jsonify({'status': 'error', 'message': 'LSL stream is not active. Please connect first.'})
127+
128+
app_name = request.form.get('app_name')
129+
if not app_name:
130+
return jsonify({'status': 'error', 'message': 'No application specified'})
131+
132+
if app_name in running_apps:
133+
return jsonify({'status': 'error', 'message': f'{app_name} is already running'})
134+
135+
app_mapping = {
136+
'ECG with Heart Rate': 'heartbeat_ecg.py',
137+
'EMG with Envelope': 'emgenvelope.py',
138+
'EOG with Blinks': 'eog.py',
139+
'EEG with FFT': 'ffteeg.py',
140+
'EEG Tug of War' : 'game.py',
141+
'EEG Beetle Game': 'beetle.py',
142+
'EOG Keystroke Emulator': 'keystroke.py',
143+
'GUI Visualization': 'gui.py',
144+
'CSV Plotter': 'csvplotter.py'
145+
}
146+
147+
script_name = app_mapping.get(app_name)
148+
if not script_name:
149+
return jsonify({'status': 'error', 'message': 'Invalid application name'})
150+
151+
if not os.path.exists(script_name):
152+
return jsonify({'status': 'error', 'message': f'Script {script_name} not found'})
153+
154+
try:
155+
process = subprocess.Popen([sys.executable, script_name])
156+
running_apps[app_name] = process
157+
return jsonify({'status': 'success', 'message': f'{app_name} started successfully'})
158+
except Exception as e:
159+
return jsonify({'status': 'error', 'message': str(e)})
160+
161+
@app.route('/stop_application', methods=['POST'])
162+
def stop_application():
163+
global running_apps
164+
165+
app_name = request.form.get('app_name')
166+
if not app_name:
167+
return jsonify({'status': 'error', 'message': 'No application specified'})
168+
169+
if app_name not in running_apps:
170+
return jsonify({'status': 'error', 'message': f'{app_name} is not running'})
171+
172+
try:
173+
running_apps[app_name].terminate()
174+
del running_apps[app_name]
175+
return jsonify({'status': 'success', 'message': f'{app_name} stopped successfully'})
176+
except Exception as e:
177+
return jsonify({'status': 'error', 'message': str(e)})
178+
179+
@app.route('/check_app_status', methods=['GET'])
180+
def check_app_status():
181+
global running_apps
182+
183+
app_name = request.args.get('app_name')
184+
if not app_name:
185+
return jsonify({'status': 'error', 'message': 'No application specified'})
186+
187+
if app_name in running_apps:
188+
if running_apps[app_name].poll() is None:
189+
return jsonify({'status': 'running', 'message': f'{app_name} is running'})
190+
else:
191+
del running_apps[app_name]
192+
return jsonify({'status': 'stopped', 'message': f'{app_name} has stopped'})
193+
else:
194+
return jsonify({'status': 'stopped', 'message': f'{app_name} is not running'})
195+
115196
if __name__ == "__main__":
116197
app.run(debug=True)

0 commit comments

Comments
 (0)