1
+ from flask import Flask , render_template , jsonify , request
2
+ import subprocess
3
+ import psutil
4
+ import os
5
+ import signal
6
+ import sys
7
+
8
+ app = Flask (__name__ )
9
+ lsl_process = None
10
+ app_processes = {}
11
+
12
+ def is_process_running (name ):
13
+ for proc in psutil .process_iter (['pid' , 'name' ]):
14
+ if name in proc .info ['name' ]:
15
+ return True
16
+ return False
17
+
18
+ @app .route ("/" )
19
+ def home ():
20
+ return render_template ("index.html" , lsl_started = False , lsl_status = "Stopped" , lsl_color = "red" )
21
+
22
+ @app .route ("/start_lsl" , methods = ["POST" ])
23
+ def start_lsl ():
24
+ global lsl_process
25
+ if lsl_process and lsl_process .poll () is None :
26
+ return jsonify ({"status" : "LSL stream already running" , "lsl_started" : True })
27
+ try :
28
+ # Start the LSL stream as a subprocess
29
+ if sys .platform == "win32" :
30
+ lsl_process = subprocess .Popen (["python" , "chords.py" , "--lsl" ], creationflags = subprocess .CREATE_NO_WINDOW )
31
+ else :
32
+ lsl_process = subprocess .Popen (["python" , "chords.py" , "--lsl" ])
33
+
34
+ if lsl_process .poll () is None :
35
+ return render_template ("index.html" , lsl_started = True , lsl_status = "Running" , lsl_color = "green" )
36
+ else :
37
+ return render_template ("index.html" , lsl_started = False , lsl_status = "Failed to Start" , lsl_color = "red" )
38
+ except Exception as e :
39
+ return render_template ("index.html" , lsl_started = False , lsl_status = f"Error: { e } " , lsl_color = "red" )
40
+
41
+ @app .route ("/run_app" , methods = ["POST" ])
42
+ def run_app ():
43
+ app_name = request .form .get ("app_name" )
44
+
45
+ # Check if the app is already running
46
+ if app_name in app_processes and app_processes [app_name ].poll () is None :
47
+ return jsonify ({"status" : f"{ app_name } is already running" })
48
+
49
+ try :
50
+ # Start the app subprocess
51
+ if sys .platform == "win32" :
52
+ process = subprocess .Popen (["python" , f"{ app_name } .py" ], creationflags = subprocess .CREATE_NO_WINDOW )
53
+ else :
54
+ process = subprocess .Popen (["python" , f"{ app_name } .py" ])
55
+
56
+ app_processes [app_name ] = process
57
+ return render_template ("index.html" , lsl_started = True , lsl_status = "Running" , lsl_color = "green" )
58
+ except Exception as e :
59
+ return jsonify ({"status" : f"Error starting { app_name } : { e } " })
60
+
61
+ @app .route ("/stop_lsl" , methods = ['POST' ])
62
+ def stop_lsl ():
63
+ # Terminate LSL process
64
+ if lsl_process and lsl_process .poll () is None :
65
+ lsl_process .terminate ()
66
+
67
+ # Terminate all app processes
68
+ for app_name , process in app_processes .items ():
69
+ if process .poll () is None :
70
+ process .terminate ()
71
+
72
+ # Shutdown the server gracefully
73
+ os ._exit (0 )
74
+ return jsonify ({'status' : 'LSL Stream and applications stopped and server is shutting down.' })
75
+
76
+ if __name__ == "__main__" :
77
+ app .run (debug = True )
0 commit comments