Skip to content

Commit 2ae596a

Browse files
author
AJ Keller
committed
FIX: Streaming seems to be working well - #51
1 parent fb54d1a commit 2ae596a

File tree

2 files changed

+41
-17
lines changed

2 files changed

+41
-17
lines changed

open_bci_wifi.py

Lines changed: 40 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ def handle_sample(sample):
3030
import asyncore
3131
import socket
3232
import requests
33+
import json
3334

3435
SAMPLE_RATE = 0 # Hz
3536

@@ -250,14 +251,16 @@ def start_streaming(self, callback, lapse=-1):
250251
callback: A callback function -- or a list of functions -- that will receive a single argument of the
251252
OpenBCISample object captured.
252253
"""
253-
if not self.streaming:
254-
self.init_streaming()
255-
256254
start_time = timeit.default_timer()
257255

258256
# Enclose callback funtion in a list if it comes alone
259257
if not isinstance(callback, list):
260-
callback = [callback]
258+
self.local_wifi_server.set_callback(callback)
259+
else:
260+
self.local_wifi_server.set_callback(callback[0])
261+
262+
if not self.streaming:
263+
self.init_streaming()
261264

262265
# while self.streaming:
263266
# # should the board get disconnected and we could not wait for notification anymore, a reco should be attempted through timeout mechanism
@@ -402,25 +405,53 @@ def __init__(self, packet_id, channel_data, aux_data, imp_data):
402405

403406

404407
class WiFiShieldHandler(asyncore.dispatcher_with_send):
408+
def __init__(self, sock, callback=None):
409+
asyncore.dispatcher_with_send.__init__(self, sock)
410+
411+
self.callback = callback
405412

406413
def handle_read(self):
407-
data = self.recv(8192)
408-
if data:
409-
print(data)
414+
data = self.recv(3000) # 3000 is the max data the WiFi shield is allowed to send over TCP
415+
if len(data) > 2:
416+
try:
417+
possible_chunks = data.split('\r\n')
418+
if len(possible_chunks) > 1:
419+
possible_chunks = possible_chunks[:-1]
420+
for possible_chunk in possible_chunks:
421+
if len(possible_chunk) > 2:
422+
chunk_dict = json.loads(possible_chunk)
423+
if 'chunk' in chunk_dict:
424+
for sample in chunk_dict['chunk']:
425+
if self.callback is not None:
426+
self.callback(sample)
427+
else:
428+
print("not a sample packet")
429+
except ValueError as e:
430+
print("failed to parse: %s" % data)
431+
print e
432+
except BaseException as e:
433+
print e
410434

411435

412436
class WiFiShieldServer(asyncore.dispatcher):
413437

414-
def __init__(self, host, port):
438+
def __init__(self, host, port, callback=None):
415439
asyncore.dispatcher.__init__(self)
416440
self.create_socket(socket.AF_INET, socket.SOCK_STREAM)
417441
self.set_reuse_addr()
418442
self.bind((host, port))
419443
self.listen(5)
444+
self.callback = None
445+
self.handler = None
420446

421447
def handle_accept(self):
422448
pair = self.accept()
423449
if pair is not None:
424450
sock, addr = pair
425451
print 'Incoming connection from %s' % repr(addr)
426-
handler = WiFiShieldHandler(sock)
452+
self.handler = WiFiShieldHandler(sock, self.callback)
453+
454+
def set_callback(self, callback):
455+
self.callback = callback
456+
if self.handler is not None:
457+
self.handler.callback = callback

scripts/stream_data_wifi.py

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,10 @@
11
import sys; sys.path.append('..') # help python find open_bci_v3.py relative to scripts folder
22
import open_bci_wifi as bci
3-
import os
43
import logging
5-
import time
64

75

86
def printData(sample):
9-
#os.system('clear')
10-
print "----------------"
11-
print("%f" %(sample.id))
12-
print sample.channel_data
13-
# print sample.aux_data
14-
print "----------------"
7+
print sample
158

169

1710
if __name__ == '__main__':

0 commit comments

Comments
 (0)