Skip to content

Commit 737c878

Browse files
committed
Connection is perfectly working for all 3 protocols
1 parent b75edbd commit 737c878

File tree

3 files changed

+141
-174
lines changed

3 files changed

+141
-174
lines changed

app.py

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
from flask import Response
88
import queue
99
import threading
10+
import time
1011

1112
console_queue = queue.Queue()
1213
app = Flask(__name__)
@@ -16,6 +17,7 @@
1617
connection_manager = None
1718
connection_thread = None
1819
ble_devices = []
20+
stream_active = False
1921

2022
def run_async(coro):
2123
def wrapper(*args, **kwargs):
@@ -46,16 +48,22 @@ async def scan_ble_devices():
4648

4749
@app.route('/check_stream')
4850
def check_stream():
49-
if connection_manager and connection_manager.lsl_connection:
51+
if connection_manager and connection_manager.stream_active:
5052
return jsonify({'connected': True})
5153
return jsonify({'connected': False})
5254

55+
@app.route('/check_connection')
56+
def check_connection():
57+
if connection_manager and connection_manager.stream_active:
58+
return jsonify({'status': 'connected'})
59+
return jsonify({'status': 'connecting'})
60+
5361
def post_console_message(message):
54-
if connection_manager:
55-
if "LSL stream started" in message:
56-
connection_manager.stream_active = True
57-
elif "Connection error" in message or "disconnected" in message:
58-
connection_manager.stream_active = False
62+
global stream_active
63+
if "LSL stream started" in message:
64+
stream_active = True
65+
elif "disconnected" in message:
66+
stream_active = False
5967
console_queue.put(message)
6068

6169
@app.route('/console_updates')
@@ -120,10 +128,10 @@ def run_connection():
120128

121129
@app.route('/disconnect', methods=['POST'])
122130
def disconnect_device():
123-
global connection_manager
131+
global connection_manager, stream_active
124132
if connection_manager:
125133
connection_manager.cleanup()
126-
connection_manager.stream_active = False
134+
stream_active = False
127135
post_console_message("disconnected")
128136
return jsonify({'status': 'disconnected'})
129137
return jsonify({'status': 'no active connection'})

connection.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ def __init__(self, csv_logging=False):
2626
self.csv_writer = None
2727
self.sample_counter = 0
2828
self.num_channels = 0
29+
self.stream_active = False
2930

3031
async def get_ble_device(self):
3132
devices = await Chords_BLE.scan_devices()
@@ -51,6 +52,8 @@ def setup_lsl(self, num_channels, sampling_rate):
5152
info = StreamInfo(self.stream_name, self.stream_type, num_channels, sampling_rate, self.stream_format, self.stream_id)
5253
self.lsl_connection = StreamOutlet(info)
5354
print(f"LSL stream started: {num_channels} channels at {sampling_rate}Hz")
55+
self.stream_active = True
56+
print("Flag is set to True")
5457
self.num_channels = num_channels
5558

5659
def setup_csv(self):

0 commit comments

Comments
 (0)