@@ -30,6 +30,7 @@ def handle_sample(sample):
3030import asyncore
3131import socket
3232import requests
33+ import json
3334
3435SAMPLE_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
404407class 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
412436class 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
0 commit comments