Skip to content

Commit 04296e6

Browse files
committed
when one application runs agiain, then it shows alert notification - already running
1 parent e0d255a commit 04296e6

File tree

2 files changed

+36
-5
lines changed

2 files changed

+36
-5
lines changed

app.py

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
connection_thread = None
2020
ble_devices = []
2121
stream_active = False
22+
running_apps = {} # Dictionary to track running apps
2223

2324
def run_async(coro):
2425
def wrapper(*args, **kwargs):
@@ -87,19 +88,33 @@ def launch_application():
8788
if not app_name:
8889
return jsonify({'status': 'error', 'message': 'No application specified'}), 400
8990

91+
# Check if app is already running
92+
if app_name in running_apps and running_apps[app_name].poll() is None:
93+
return jsonify({'status': 'error', 'message': f'{app_name} is already running','code': 'ALREADY_RUNNING'}), 400
94+
9095
try:
91-
# Here we'll use subprocess to launch the application script
9296
import subprocess
9397
import sys
9498

95-
python_exec = sys.executable # Determine the correct Python executable
96-
subprocess.Popen([python_exec, f"{app_name}.py"]) # Launch the application script in a separate process
99+
python_exec = sys.executable
100+
process = subprocess.Popen([python_exec, f"{app_name}.py"])
101+
running_apps[app_name] = process
97102

98103
return jsonify({'status': 'success', 'message': f'Launched {app_name}'})
99104
except Exception as e:
100105
logging.error(f"Error launching {app_name}: {str(e)}")
101106
return jsonify({'status': 'error', 'message': str(e)}), 500
102107

108+
@app.route('/check_app_status/<app_name>')
109+
def check_app_status(app_name):
110+
if app_name in running_apps:
111+
if running_apps[app_name].poll() is None: # Still running
112+
return jsonify({'status': 'running'})
113+
else: # Process has terminated
114+
del running_apps[app_name]
115+
return jsonify({'status': 'not_running'})
116+
return jsonify({'status': 'not_running'})
117+
103118
@app.route('/connect', methods=['POST'])
104119
def connect_device():
105120
global connection_manager, connection_thread, stream_active

templates/index.html

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -154,11 +154,26 @@ <h3 class="text-lg font-semibold text-gray-800 dark:text-gray-200 mb-1">${app.ti
154154
</div>
155155
`;
156156

157-
card.addEventListener('click', () => {
157+
card.addEventListener('click', async () => {
158158
if (!isConnected) {
159159
showAlert('Please start LSL Stream first by any protocol (USB/WIFI/BLE)');
160160
return;
161161
}
162+
163+
// First checking if app is already running
164+
try {
165+
const response = await fetch(`/check_app_status/${app.script}`);
166+
const data = await response.json();
167+
168+
if (data.status === 'running') {
169+
showAlert(`${app.title} is already running!`);
170+
return;
171+
}
172+
} catch (error) {
173+
console.error('Error checking app status:', error);
174+
}
175+
176+
// If not running, launch the app
162177
launchApplication(app.script);
163178
});
164179

@@ -523,7 +538,8 @@ <h3 class="text-lg font-semibold text-gray-800 dark:text-gray-200 mb-1">${app.ti
523538
.then(response => response.json())
524539
.then(data => {
525540
if (data.status === 'success') {
526-
// showStatus(`Launched ${appName} application`, 'fa-check-circle', 'text-green-500');
541+
} else if (data.code === 'ALREADY_RUNNING') {
542+
showAlert(data.message);
527543
} else {
528544
showAlert(`Failed to launch ${appName}: ${data.message}`);
529545
}

0 commit comments

Comments
 (0)