6
6
from pylsl import resolve_streams
7
7
import time
8
8
from chords_ble import Chords_BLE
9
+ import subprocess
10
+ import os
11
+ import sys
9
12
10
13
app = Flask (__name__ )
11
14
connection_manager = None
12
15
ble_devices = []
13
16
active_connection = None
14
17
lsl_stream_active = False
15
18
csv_logging_enabled = False # Global CSV logging state
19
+ running_apps = {} # Dictionary to keep track of running applications
16
20
17
21
@app .route ('/set_csv' , methods = ['POST' ])
18
22
def set_csv ():
@@ -109,8 +113,85 @@ def connect_wifi():
109
113
110
114
def connect_ble (address ):
111
115
global active_connection , lsl_stream_active , csv_logging_enabled
116
+ lsl_stream_active = False
117
+ Thread (target = check_lsl_stream ).start () # Add this line
112
118
active_connection = Connection (csv_logging = csv_logging_enabled )
113
119
active_connection .connect_ble (address ) # No duplicate device selection
114
120
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
+
115
196
if __name__ == "__main__" :
116
197
app .run (debug = True )
0 commit comments