def get_project_infos(self, project_id): """ Get detailed project infos about the project Params: project_id: the id of the project Returns: project details """ project_infos = None # Callback function for the joinProject emitter def set_project_infos(a, project_infos_dict, c, d): # Set project_infos variable in outer scope nonlocal project_infos project_infos = project_infos_dict # Convert cookie from CookieJar to string cookie = "gke-route={}; overleaf_session2={}" \ .format( reqs.utils.dict_from_cookiejar(self._cookie)["gke-route"], reqs.utils.dict_from_cookiejar(self._cookie)["overleaf_session2"] ) # Connect to Overleaf Socket.IO, send a time parameter and the cookies socket_io = SocketIO( BASE_URL, params={'t': int(time.time())}, headers={'Cookie': cookie} ) # Wait until we connect to the socket socket_io.on('connect', lambda: None) socket_io.wait_for_callbacks() # Send the joinProject event and receive the project infos socket_io.emit('joinProject', {'project_id': project_id}, set_project_infos) socket_io.wait_for_callbacks() # Disconnect from the socket if still connected if socket_io.connected: socket_io.disconnect() return project_infos
class PublishMediaFrameworkMessages: def on_auth_r(self, *args): print('on_auth_r', args) def __init__(self): self.socketio = SocketIO(media_hub_url, 80, LoggingNamespace) self.socketio.emit('auth', {'password': password}, self.on_auth_r) self.socketio.wait_for_callbacks(seconds=10) self.receive_events_thread = Thread(target=self._receive_events_thread) self.receive_events_thread.daemon = True self.receive_events_thread.start() self.published_data = handle_data_for_osc("0", osc_chan_default) while True: reading = serial_port.readline() electret_peak_sample = reading publish_data = handle_data_for_osc(electret_peak_sample, osc_chan_default) if not self.published_data == publish_data: self.publish_osc(handle_data_for_osc(electret_peak_sample, osc_chan_401)) self.publish_osc(handle_data_for_osc(electret_peak_sample, osc_chan_402)) self.publish_osc(handle_data_for_osc(electret_peak_sample, osc_chan_403)) self.published_data = publish_data def _receive_events_thread(self): self.socketio.wait() def publish(self, score_to_play): if score_to_play: print("publish score") self.socketio.emit('sendCommand', 'electret', 'showScenesAndThemes', score_to_play) def publish_osc(self, osc): if osc: print("publish oscCommand") self.socketio.emit('sendCommand', 'lighting', 'oscCommand', osc)
class mySockIo(object): def __init__(self, host, port, router): self.sock = SocketIO(host, port) self.sock.on(router, self.on_message) def on_message(self, *args): return args[0] if isinstance(args[0], dict) else None def send(self, data): self.sock.send(data) result = self.sock.wait_for_callbacks(2) return result def close(self): self.sock.disconnect()
def callback(*args): print(args) # if not Worker, throw exception if (args[0] != 'Worker'): print('[ERROR] Handshake Unsuccessful. Killing process') raise ValueError('Server did not accept handshake') print('[INFO] Handshake Successful. Starting as Worker Node') socketIO = SocketIO('localhost', 8000) # Start handshake try: print('[INFO] Sending Handshake Packet') socketIO.emit('handShake', {"clientType": 1}, callback) socketIO.wait_for_callbacks(seconds=1) # wait for response packet except Exception as e: sys.exit(e) def on_start(*args): global step global loop if (loop == 0): random.shuffle(partList) step = 2 #Move to take pic imgloc = "G1 X" + str(xcam) + " Y" + str(ycam) + " F2500" socketIO.emit('chadMove', {"line": imgloc}) #time.sleep(1) socketIO.on('machineStatus', on_machine_status)
class TestSocketIO(TestCase): def setUp(self): self.socketIO = SocketIO(HOST, PORT) self.called_on_response = False def tearDown(self): del self.socketIO def on_response(self, *args): self.called_on_response = True for arg in args: if isinstance(arg, dict): self.assertEqual(arg, PAYLOAD) else: self.assertEqual(arg, DATA) def is_connected(self, socketIO, connected): childThreads = [ socketIO._rhythmicThread, socketIO._listenerThread, ] for childThread in childThreads: self.assertEqual(not connected, childThread.done.is_set()) self.assertEqual(connected, socketIO.connected) def test_disconnect(self): 'Terminate child threads after disconnect' self.is_connected(self.socketIO, True) self.socketIO.disconnect() self.is_connected(self.socketIO, False) # Use context manager with SocketIO(HOST, PORT) as self.socketIO: self.is_connected(self.socketIO, True) self.is_connected(self.socketIO, False) def test_message(self): 'Message' self.socketIO.define(Namespace) self.socketIO.message() self.socketIO.wait(0.1) namespace = self.socketIO.get_namespace() self.assertEqual(namespace.response, 'message_response') def test_message_with_data(self): 'Message with data' self.socketIO.define(Namespace) self.socketIO.message(DATA) self.socketIO.wait(0.1) namespace = self.socketIO.get_namespace() self.assertEqual(namespace.response, DATA) def test_message_with_payload(self): 'Message with payload' self.socketIO.define(Namespace) self.socketIO.message(PAYLOAD) self.socketIO.wait(0.1) namespace = self.socketIO.get_namespace() self.assertEqual(namespace.response, PAYLOAD) def test_message_with_callback(self): 'Message with callback' self.socketIO.message(callback=self.on_response) self.socketIO.wait_for_callbacks(seconds=0.1) self.assertEqual(self.called_on_response, True) def test_message_with_callback_with_data(self): 'Message with callback with data' self.socketIO.message(DATA, self.on_response) self.socketIO.wait_for_callbacks(seconds=0.1) self.assertEqual(self.called_on_response, True) def test_emit(self): 'Emit' self.socketIO.define(Namespace) self.socketIO.emit('emit') self.socketIO.wait(0.1) self.assertEqual(self.socketIO.get_namespace().argsByEvent, { 'emit_response': (), }) def test_emit_with_payload(self): 'Emit with payload' self.socketIO.define(Namespace) self.socketIO.emit('emit_with_payload', PAYLOAD) self.socketIO.wait(0.1) self.assertEqual(self.socketIO.get_namespace().argsByEvent, { 'emit_with_payload_response': (PAYLOAD, ), }) def test_emit_with_multiple_payloads(self): 'Emit with multiple payloads' self.socketIO.define(Namespace) self.socketIO.emit('emit_with_multiple_payloads', PAYLOAD, PAYLOAD) self.socketIO.wait(0.1) self.assertEqual(self.socketIO.get_namespace().argsByEvent, { 'emit_with_multiple_payloads_response': (PAYLOAD, PAYLOAD), }) def test_emit_with_callback(self): 'Emit with callback' self.socketIO.emit('emit_with_callback', self.on_response) self.socketIO.wait_for_callbacks(seconds=0.1) self.assertEqual(self.called_on_response, True) def test_emit_with_callback_with_payload(self): 'Emit with callback with payload' self.socketIO.emit('emit_with_callback_with_payload', self.on_response) self.socketIO.wait_for_callbacks(seconds=0.1) self.assertEqual(self.called_on_response, True) def test_emit_with_callback_with_multiple_payloads(self): 'Emit with callback with multiple payloads' self.socketIO.emit('emit_with_callback_with_multiple_payloads', self.on_response) self.socketIO.wait_for_callbacks(seconds=0.1) self.assertEqual(self.called_on_response, True) def test_emit_with_event(self): 'Emit to trigger an event' self.socketIO.on('emit_with_event_response', self.on_response) self.socketIO.emit('emit_with_event', PAYLOAD) self.socketIO.wait_for_callbacks(0.1) self.assertEqual(self.called_on_response, True) def test_ack(self): 'Trigger server callback' self.socketIO.define(Namespace) self.socketIO.emit('ack', PAYLOAD) self.socketIO.wait(0.1) self.assertEqual(self.socketIO.get_namespace().argsByEvent, { 'ack_response': (PAYLOAD, ), 'ack_callback_response': (PAYLOAD, ), }) def test_namespaces(self): 'Behave differently in different namespaces' mainNamespace = self.socketIO.define(Namespace) chatNamespace = self.socketIO.define(Namespace, '/chat') newsNamespace = self.socketIO.define(Namespace, '/news') newsNamespace.emit('emit_with_payload', PAYLOAD) self.socketIO.wait(0.1) self.assertEqual(mainNamespace.argsByEvent, {}) self.assertEqual(chatNamespace.argsByEvent, {}) self.assertEqual(newsNamespace.argsByEvent, { 'emit_with_payload_response': (PAYLOAD, ), })
class TestOnlineRecognition(unittest.TestCase): def setUp(self): self.socketIO = SocketIO('localhost', 8000) self.received_responses = 0 self.expected_responses = 0 time.sleep(1) def test_online_recognition(self): self.socketIO.on('result', self.assertMessageHasCorrectSchema) self.send_chunks() self.assertEquals(self.expected_responses + 2, self.received_responses) def send_chunks(self): self.socketIO.emit('begin', {'model': 'en-towninfo'}) self.socketIO.emit('change_lm', {'new_lm': 'new_lm'}) for chunk in self.chunks(): self.socketIO.emit('chunk', {'chunk': chunk, 'frame_rate': 16000}) self.socketIO.wait_for_callbacks() self.socketIO.emit('end', {}) self.socketIO.wait_for_callbacks() self.socketIO.wait_for_callbacks() self.socketIO.wait_for_callbacks() def chunks(self): basedir = os.path.dirname(os.path.realpath(__file__)) wav = wave.open("%s/../resources/test.wav" % basedir, "rb") while True: frames = wav.readframes(16384) if len(frames) == 0: break self.expected_responses += 1 yield self.frames_to_base64(frames) def frames_to_base64(self, frames): return base64.b64encode(frames) def assertMessageHasCorrectSchema(self, message): schema = { "type": "object", "properties": { "status": { "type": "number" }, "final": { "type": "boolean" }, "chunk_id": { "type": "string" }, "request_id": { "type": "string" }, "result": { "type": "object", "properties": { "hypotheses": { "type": "array", "items": { "type": "object", "properties": { "transcript": { "type": "string" }, "confidence": { "type": "number" } }, "required": ["transcript"], "additionalProperties": False, } } }, "required": ["hypotheses"], "additionalProperties": False, }, }, "required": ["status", "result", "final", "request_id"], "additionalProperties": False, } validationResult = validate(message, schema) self.assertIsNone(validationResult, msg="Message has invalid schema") self.received_responses += 1
class VolumioClient: """ Class for the websocket client to Volumio """ """https://github.com/foxey/volumio-buddy/blob/master/volumio_buddy/volumio_buddy.py""" def __init__(self, vu): HOSTNAME='localhost' PORT=3000 self._callback_function = False self._callback_args = False self.state = dict() self.state["status"] = "" self.prev_state = dict() self.prev_state["status"] = "" self.last_update_time = 0 self.vu = vu self.default_playlist = config.get("recorder", "default_playlist") def _on_pushState(*args): self.state = args[0] if self._callback_function: # call callback functions with arguments we need self._callback_function(*self._callback_args) self.prev_state = self.state logger.debug("VOLUMIO: %s" % self.state["status"]) self._client = SocketIO(HOSTNAME, PORT, LoggingNamespace) self._client.on('pushState', _on_pushState) self._client.emit('getState', _on_pushState) self._client.wait_for_callbacks(seconds=1) def set_volume(self): # amixer -c 0 sset PCM,0 96% # amixer -c 5 sset Mic,0 91% args = [ 'amixer', '-c', '0', 'sset', 'PCM,0', '96%' ] sub = subprocess.Popen(args) # args = [ # 'arecord', # '-D', self.SOUND_CARD_MIC, # '-f', 'S16_LE', # '-c', '1', # '-r', '44100', # '-V', 'mono', # '--process-id-file', self.RECORDING_PROCESS_ID_FILE, # self.filepath+".temp", # # '2>', os.path.join(HOME_DIR, "vumeter.tmp"), # ] # sub = subprocess.Popen(args, stderr=output) pass def set_callback(self, callback_function, *callback_args): self._callback_function = callback_function self._callback_args = callback_args def control_vu(self): """ Meant to run in a parallel thread. Will send player output to VU meter. """ #https://rvalbuena.blogspot.com/2014/04/led-vu-meter-using-mypishop-8x8-pimatrix.html # # This represents the sample (44100:16:2) that MPD is currently "playing" fifo = os.open('/tmp/snapfifo', os.O_RDONLY) # self.vu.set_percentage(100) while True: try: rawStream = os.read(fifo, 4096) except OSError as err: if err.errno == errno.EAGAIN or err.errno == errno.EWOULDBLOCK: rawStream = None logger.debug("no rawStream") else: raise # just_after_stream = False if rawStream: # TODO: Change to mono signal # TODO: Change dB scale to 0-100 scale # leftChannel = audioop.tomono(rawStream, 2, 1, 0) rightChannel = audioop.tomono(rawStream, 2, 0, 1) # stereoPeak = audioop.max(rawStream, 2) # leftPeak = audioop.max(leftChannel, 2) rightPeak = audioop.max(rightChannel, 2) # leftDB = 20 * math.log10(leftPeak) -74 rightDB = 20 * math.log10(rightPeak) -74 # logger.debug("RIGHT %s" % rightDB) # print(rightPeak, leftPeak, rightDB, leftDB) # out = 20 * math.log10(stereoPeak) -74 # out = stereoPeak/40 # if out > 90: # out = 90 # if out < 10: # out = 0 out = (rightDB + 20) * (70/23) logger.debug("MPD FIFO value %s" % out) self.vu.set_percentage(int(out)) time.sleep(0.01) # else: # self.vu.move_to_percentage(0) # # TODO: Test # # Turn meter down when stream stops # # if just_after_stream: # self.vu.move_to_percentage(0) # self.vu.stop() # # just_after_stream = False def start_vu_thread(self): self.vu.start() t = threading.Thread(target=self.control_vu, args=()) t.start() def play(self): self._client.emit('play') self.start_vu_thread() def pause(self): self._client.emit('pause') self.vu.move_to_percentage(0) def volume_up(self): self._client.emit('volume', '+') def volume_down(self): self._client.emit('volume', '-') def previous(self): self._client.emit('prev') def next(self): self._client.emit('next') def seek(self, seconds): self._client.emit('seek', int(seconds)) def create_playlist(self, name=None): if name==None: name=self.default_playlist self._client.emit('createPlaylist', {'name': name}) # def play_playlist(self, name=None): # if name==None: # name=self.default_playlist # self._client.emit('playPlaylist', {'name': name}) # def add_to_playlist(self, uri, playlist_name=None, service=None): # if playlist_name==None: # playlist_name=self.default_playlist # if service==None: # service="mpd" # # uri: mnt/INTERNAL/verhalenmachine_2017-12-01_20:04:45.wav # self._client.emit('addToPlaylist', {'name': playlist_name, 'service': service, 'uri': uri}) # def add_to_queue(self, uri): # self._client.emit('addToQueue', {'uri':uri}) # self._client.emit('addToQueue', "{uri:%s}" % uri) # self._client.emit('addToQueue', "{'uri':%s}" % uri) # self._client.emit('addToQueue', "{'uri':'%s'}" % uri) # # socketIO.emit('play',{"value":'5'}) # self._client.emit('addToQueue', {"uri":uri}) # self._client.emit('addToQueue', {"uri":'uri'}) # self._client.emit('pushQueue', '[{"uri":"%s","service":"mpd","name":"test.wav","tracknumber":0,"type":"track","trackType":"wav"}]' % uri) def add_to_queue_and_playlist(self, uri, sleep=5, update_database=True): # self._client.emit('addToQueue') # this triggers a database update? # arg = { "uri": uri, # # "service": "mpd", # # "name": "test.wav", # # "tracknumber": 0, # # "type":"track", # # "trackType":"wav" # } # self._client.emit('addToQueue', [arg]) # prefix_filename = config.get("recorder", "prefix_filename") # self.PLAYER_DIR = config.get("player", "player_dir") # self.playerpath = os.path.join(self.PLAYER_DIR+prefix_filename+uri) # uri = self.playerpath # self._client.emit('addToQueue', {'uri':uri}) # self._client.emit('addToQueue', "{uri:%s}" % uri) # self._client.emit('addToQueue', "{'uri':%s}" % uri) # self._client.emit('addToQueue', "{'uri':'%s'}" % uri) # # socketIO.emit('play',{"value":'5'}) # self._client.emit('addToQueue', {"uri":uri}) # self._client.emit('addToQueue', {"uri":'uri'}) time.sleep(sleep) self.update_database(uri="INTERNAL") time.sleep(sleep) # self._client.emit('addToQueue', [arg]) # time.sleep(5) logger.debug("Adding %s to queue" % uri) queuearg = { "uri": uri, # "service": "mpd", # "name": "test.wav", # "tracknumber": 0, # "type":"track", # "trackType":"wav" } self._client.emit('addToQueue', [queuearg]) playlistarg = { "name": self.default_playlist, "service":"mpd", "uri": uri } self._client.emit('addToPlaylist', playlistarg) # self._client.emit('pushQueue', '[{"uri":"%s","service":"mpd","name":"test.wav","tracknumber":0,"type":"track","trackType":"wav"}]' % uri) def enqueue_playlist(self, name=None): if name==None: name=self.default_playlist self._client.emit('enqueue', {'name': name}) def empty_queue(self): for x in range(100): self._client.emit('removeFromQueue', x) def empty_queue_and_enqueue_playlist(self, name=None): self.empty_queue() if name==None: name=self.default_playlist self._client.emit('playPlaylist', {'name': name}) # time.sleep(2) self.pause() def set_random(self): self._client.emit('setRandom', {'value': 'true'}) def set_repeat(self): self._client.emit('setRepeat', {'value': 'true'}) def is_playing(self): if self.state["status"] == "play": logger.debug("VOLUMIO IS PLAYING: %s" % self.state["status"]) return True logger.debug("VOLUMIO IS *NOT* PLAYING: %s" % self.state["status"]) return False def update_database(self, uri=[]): from mpd import MPDClient self.mpdclient = MPDClient() self.mpdclient.connect("localhost", 6600) self.mpdclient.update([uri]) self.mpdclient.close() self.mpdclient.disconnect() logger.debug("Updating MPD %s" %uri) def wait(self, **kwargs): self.wait_thread = threading.Thread(target=self._wait, args=(kwargs)) self.wait_thread.start() print "started websocket wait thread" return self.wait_thread def _wait(self, **kwargs): while True: self._client.wait(kwargs) print "websocket wait loop terminated, restarting"
import sys, getopt def getArgs(): argv = sys.argv[1:]; inputfile = '' outputfile = '' opts, args = getopt.getopt(argv,"heri:o:",["ifile=","ofile="]) optMap = {}; for opt, arg in opts: optMap[opt] = arg return optMap if __name__ == "__main__": words = [] for line in sys.stdin: for word in line.replace('\n', '').split(','): words.append(word) argMap = getArgs() address = 'https://ghcap-web.herokuapp.com' if argMap.has_key('-r') else 'http://localhost:9000' requests.packages.urllib3.disable_warnings() socket = SocketIO(address + '/socket.io-client', verify=True) def after(result): print result socket.emit('misc:save common words', words, after) socket.wait_for_callbacks(seconds=20)
class P300Client(object): def __init__(self): self.socket_client = None self.marker_outlet = None self.train_mode = True # True for training mode, False for prediction mode # will stay in training mode until first prediction self.train_results = [] self.pred_results = [] self.streams = {} self.register_results = None self.login_results = None self.logout_results = None self.time_diff = 0 # difference between unix and muse time self.sio = socketio.AsyncServer(async_mode='sanic') self.app = Sanic() self.sio.attach(self.app) def connect(self, ip, port): self.socket_client = SocketIO(ip, port) self.socket_client.connect() def disconnect(self): self.socket_client.disconnect() def create_streams(self): self.streams['eeg'] = self._create_eeg_stream() self.streams['marker'] = self._create_marker_stream() # TODO: some kind of switching between training and prediction modes data = { 'event_time': 0.4, # or 0.2? 'train_epochs': 120 } # 120 for 2 min, 240 for 4 min self.streams['ml'] = self._create_ml_stream(data) def start_streams(self): for stream in ['eeg', 'marker', 'ml']: self._start_stream(stream) while len(self.streams['eeg'].data) == 0: time.sleep(0.1) self.time_diff = time.time() - self.streams['eeg'].data[-1][-1] def change_mode(self, train_mode=False): """ train_mode=True for training mode train_mode=False for prediction mode """ if self.streams['ml'] is None: raise Exception(f"ml stream does is not running") curr_mode = self.streams['ml'].get_mode() if curr_mode is not train_mode: self.train_mode = train_mode self.streams['ml'].set_mode(train_mode) async def start_event_loop(self): """Continuously pulls data from ml_stream and sends to server based on whether we are training or predicting""" if self.streams.get('ml') is None: raise Exception(f"ml stream does not exist") data = None while data is None: # send training jobs to server if self.train_mode: data = self.streams['ml'].get_training_data() if data is not None: uuid = data['uuid'] train_data = data['train_data'] train_targets = data['train_targets'] self.train(uuid, train_data, train_targets) return # send prediction jobs to server else: data = self.streams['ml'].get_prediction_data() if data is not None: uuid = data['uuid'] eeg_data = data['eeg_data'] self.predict(uuid, eeg_data) return time.sleep(0.1) # # Callback funcs # def on_retrieve_prediction_results(self, *args): results = args[1] self.pred_results.append(results) def on_train_results(self, *args): results = args[1] self.train_results.append(results) def on_register_results(self, *args): self.register_results = args def on_login_results(self, *args): self.login_results = args def on_logout_results(self, *args): self.logout_results = args # # Server-side communication # def predict(self, uuid, eeg_data): data = (uuid, eeg_data) self.socket_client.emit("retrieve_prediction_results", data, self.on_retrieve_prediction_results) self.socket_client.wait_for_callbacks(seconds=1) def train(self, uuid, eeg_data, p300): data = (uuid, eeg_data, p300) self.socket_client.emit("train_classifier", data, self.on_train_results) self.socket_client.wait_for_callbacks(seconds=1) def register(self, username, password, email): data = (username, password, email) self.socket_client.emit("register", data, self.on_register_results) self.socket_client.wait_for_callbacks(seconds=1) while not self.register_results: time.sleep(.1) return self.register_results def login(self, username, password): data = (username, password) self.socket_client.emit("login", data, self.on_login_results) self.socket_client.wait_for_callbacks(seconds=1) while not self.login_results: time.sleep(.1) return self.login_results def logout(self): self.socket_client.emit("logout", None, self.on_logout_results) self.socket_client.wait_for_callbacks(seconds=1) while not self.logout_results: time.sleep(.1) return self.logout_results # # Private methods for creating and starting streams # @staticmethod def _create_eeg_stream(): return EEGStream(thread_name='EEG_data', event_channel_name='P300') def _create_marker_stream(self): info = pylsl.StreamInfo('Markers', 'Markers', 4, 0, 'string', 'mywid32') self.marker_outlet = pylsl.StreamOutlet(info) return MarkerStream(thread_name='Marker_stream') def _create_ml_stream(self, data): if self.streams.get('eeg') is None: raise Exception(f"EEG stream does not exist") if self.streams.get('marker') is None: raise Exception(f"Marker stream does not exist") return MLStream(m_stream=self.streams['marker'], eeg_stream=self.streams['eeg'], event_time=data['event_time'], train_epochs=data['train_epochs']) def _start_stream(self, stream): if self.streams.get(stream) is None: raise RuntimeError( "Cannot start {0} stream, stream does not exist".format( stream)) elif stream == 'ml': self.streams[stream].start(self.train_mode) else: self.streams[stream].lsl_connect() # # Handlers for communication with front end # def initialize_handlers(self): self.sio.on("train", self.train_handler) self.sio.on("predict", self.predict_handler) self.sio.on("register", self.register_handler) self.sio.on("login", self.login_handler) self.sio.on("logout", self.logout_handler) async def register_handler(self, sid, args): args = json.loads(args) username = args['username'] password = args['password'] email = args['email'] return self.register(username, password, email) async def login_handler(self, sid, args): args = json.loads(args) username = args['username'] password = args['password'] return self.login(username, password) async def logout_handler(self, sid): return self.logout() async def train_handler(self, sid, args): if not self.train_mode: self.change_mode(train_mode=True) time.sleep(.2) args = json.loads(args) uuid = args['uuid'] timestamp = args['timestamp'] p300 = args['p300'] timestamp -= self.time_diff package = [ str(timestamp), str(p300), # target str(1), # 1 event total str(uuid) # take uuid for epoch id ] self.marker_outlet.push_sample(package) await self.start_event_loop() while len(self.train_results) == 0: time.sleep(.1) score = self.train_results.pop(0) return sid, score async def predict_handler(self, sid, args): if self.train_mode: self.change_mode(train_mode=False) time.sleep(.2) args = json.loads(args) uuid = args['uuid'] timestamp = args['timestamp'] timestamp -= self.time_diff package = [ str(timestamp), str(0), # target str(1), # 1 event total str(uuid) # take uuid for epoch id ] self.marker_outlet.push_sample(package) await self.start_event_loop() while len(self.pred_results) == 0: time.sleep(.1) pred = self.pred_results.pop(0) uuid, p300, score = pred results = {'uuid': uuid, 'p300': p300, 'score': score} return sid, results
class Streaming(Q): stop_transmission = True def __init__(self, config: ConfigDTO = None, wait_for_connection = True): super().__init__(thread_status = True) self.__config = copy.deepcopy(config) self.wait_for_connection = wait_for_connection self.socketIO = None self.data_auth = {'username': self.__config.streaming.username, 'password': self.__config.streaming.password} def __connect(self, force = False): try: if self.socketIO is None: self.socketIO = SocketIO(host=self.__config.streaming.host, port=self.__config.streaming.port, Namespace=Namespace, wait_for_connection=self.wait_for_connection, params=self.data_auth) self.socketIO.wait(seconds=1) self.__subscribe() elif self.socketIO and self.socketIO.connected == False: self.socketIO.connect() self.socketIO.wait(seconds=1) self.__subscribe() if force == True: self.__subscribe() except Exception as e: raise e def __build_data(self, item: StreamDTO = None): try: jpeg = item.image.tobytes() jpeg = base64.b64encode(jpeg).decode('utf-8') image = "data:image/jpeg;base64,{}".format(jpeg) item = {'image': True, 'source': item.source, 'buff': image} return item except Exception as e: raise e def __on_response(self, *args): pass def __subscribe(self): if self.socketIO: self.socketIO.emit('subscriber', callback=self.__on_response) self.socketIO.wait_for_callbacks(seconds=1) def __unsuscriber(self): if self.socketIO: self.socketIO.emit('unsubscriber', callback=self.__on_response) self.socketIO.wait_for_callbacks(seconds=1) self.socketIO.disconnect() def process_item(self, item: StreamDTO = None) -> None: try: if Streaming.stop_transmission == True: return if item is None: return item = self.__build_data(item) self.__connect(force=False) self.socketIO.emit('cv-data', item, callback=self.__on_response) except Exception as e: print("Streaming__process_item", e) def empty_queue_for_lock(self)-> None: if self.__config.streaming.delay > 0: time.sleep(self.__config.streaming.delay) else: self.apply_lock() def process_status(self)-> None: try: self.__connect(force=True) if self.__config.streaming.delay_status > 0: time.sleep(self.__config.streaming.delay_status) except Exception as e: print("Streaming__process_status", e) def initialize(self): self.__connect(force=False) self.run_queue() def stop(self) -> None: self.stop_queue() self.__unsuscriber() Streaming.stop_transmission = True
class Volumio: def __init__(self, host="localhost", port=3000): from socketIO_client import SocketIO self._state = {} self._queue = list() self._radios = list() self._waiting = .1 print("Opening {}:{} ".format(host, port), end='') self._sock = SocketIO(host, port) # Callbacks self._sock.on('connect', self._on_connect) self._sock.on('pushState', self._on_push_state) self._sock.on('pushBrowseLibrary', self._on_push_browse_library) self._sock.on('pushQueue', self._on_push_queue) while len(self._state)==0: print('.', end='') self._sock.wait(seconds=0.3) def _on_connect(self, *args): print(' Connected!') self.get_state() def _on_push_state(self, *args): print("State updated") self._state = args[0] def _on_push_browse_library(self, *args): radios_list = args[0]['navigation']['lists'][0]['items'] self._radios = list() for radio in radios_list: self._radios.append({ 'title': radio['title'], 'uri': radio['uri'] }) def _on_push_queue(self, *args): print("Fetching queue") self._queue = list() for music in args[0]: self._queue.append({ "uri": music['uri'], "title": music.get('title', None), "name": music.get('name', None), }) def _send(self, command, args=None, callback=None): self._sock.emit(command, args, callback) self._sock.wait_for_callbacks(seconds=self._waiting) def get_state(self): self._send('getState', callback=self._on_push_state) def get_radios(self): self._send('browseLibrary', {"uri": "radio/myWebRadio"}, self._on_push_browse_library) def get_queue(self): self._send('getQueue', callback=self._on_push_queue) def radios(self): self.get_radios() return self._radios def state(self): return self._state def status(self): return self._state["status"] def playing(self): return Volumio.get_name(self._state) def playing_uri(self): self.get_state() return self._state["uri"] def stop(self): self._send('stop') self._send('clearQueue') def queue(self): self.get_queue() return self._queue def volume(self): return self._state["volume"] def set_volume(self, volume): """ Définit le volume de lecture :param volume: Volume souhaité [0-100] """ assert isinstance(volume, int), ":volume: doit être un entier (type : {})".format(type(volume)) assert 0 <= volume <= 100, ":volume: doit être compris entre 0 et 100 (valeur : {})".format(volume) self._send('volume', volume, callback=self._on_push_state) def play_radio(self, uri): """ Joue immédiatement la radio dont l'URI est passée en paramètre :param uri: URI de la radio à jouer """ # Méthode de force brute, on est obligé de vider la queue, et d'ajouter une musique, # Sinon elle s'ajoute à la fin de la queue. self._send('clearQueue') self._send('addPlay', {'status':'play', 'service':'webradio', 'uri':uri}) @staticmethod def get_name(music_as_dict): """ Permet d'extraire le nom (qui est soit le champ 'title' soit le champ 'name' d'une musique :param music_as_dict: le dictionnaire représentant la musique :return: le titre, ou le nom, ou bien None """ title = music_as_dict.get("title", None) name = music_as_dict.get("name", None) return title if title is not None else name
messageBox.box() messageTxt = curses.newwin(4, 29, sizeY - 5, sizeX - 30) message = "" # These functions are socket listeners. # The name prescribes the type of emit to listen for. class listeners: def userMessage(*args): messageList += ["Testing the userMessage listener"] # Sets all listeners. for function in listeners.__dict__: socket.on(function, listeners.__dict__[function]) socket.wait_for_callbacks() while message != "kill": for x in messageList: messages.addstr(0, 0, x) messageTxt.addstr(0, 0, message) screen.refresh() messages.refresh() messageBox.refresh() messageTxt.refresh() # Keypress handler. char = screen.getkey() if char == "\n": socket.emit('userMessage', message)
class Streaming: def __init__(self, config: ConfigDTO = None, wait_for_connection=True): super().__init__() self.__config = copy.deepcopy(config) self.wait_for_connection = wait_for_connection self.q = queue.Queue(maxsize=1000) self.condition = Condition(RLock()) self.started = False self.waiting = False self.socketIO = None def __build(self): #while True: data_auth = { 'username': self.__config.streaming.username, 'password': self.__config.streaming.password } self.socketIO = SocketIO(host=self.__config.streaming.host, port=self.__config.streaming.port, Namespace=Namespace, wait_for_connection=self.wait_for_connection, params=data_auth) self.socketIO.wait(seconds=1) def __check_socket(self): if self.socketIO and self.socketIO.connected == False: self.socketIO.connect() def initialize(self): try: self.__build() self.__check_socket() except Exception as e: print(e) def __on_bbb_response(self, *args): print('on_bbb_response', args) def __process(self, item: StreamDTO = None) -> None: try: if item is None: return jpeg = item.image.tobytes() jpeg = base64.b64encode(jpeg).decode('utf-8') image = "data:image/jpeg;base64,{}".format(jpeg) item = { 'image': True, 'source': item.source, 'buff': image, 'username': self.__config.streaming.username } self.__check_socket() if self.socketIO: self.socketIO.emit('handle_frame', item, callback=self.__on_bbb_response) except Exception as e: print(e) def __worker(self): while self.started == True: if self.q.empty() == True: with self.condition: self.waiting = True self.condition.wait() self.waiting = False else: item = self.q.get() self.__process(item) self.q.task_done() def put_nowait(self, item: StreamDTO = None) -> None: global start_broadcasting if start_broadcasting > 1: self.q.put_nowait(item) self.run() if self.waiting == True: with self.condition: self.condition.notify_all() def put(self, item: StreamDTO = None, block=True, timeout=None) -> None: global start_broadcasting if start_broadcasting > 1: self.q.put(item, block, timeout) self.run() if self.waiting == True: with self.condition: self.condition.notify_all() def run(self) -> None: if self.started == True: return self.started = True self.thr = Thread(target=self.__worker, daemon=True) self.thr.start() def join(self) -> None: self.q.join() def __unsuscriber(self): if self.socketIO: #self.socketIO.emit('on_unsubscriber', callback=self.__on_bbb_response) self.socketIO.emit('unsubscriber', callback=self.__on_bbb_response) self.socketIO.wait_for_callbacks(seconds=1) self.socketIO.disconnect() def stop(self): self.started = False self.__unsuscriber() def __del__(self): self.__config = None self.q = None self.started = None self.socketIO = None self.condition = None self.waiting = None self.wait_for_connection = None
class TestSocketIO(TestCase): def setUp(self): self.socketIO = SocketIO(HOST, PORT) self.called_on_response = False def tearDown(self): del self.socketIO def on_response(self, *args): self.called_on_response = True for arg in args: if isinstance(arg, dict): self.assertEqual(arg, PAYLOAD) else: self.assertEqual(arg, DATA) def is_connected(self, socketIO, connected): childThreads = [ socketIO._rhythmicThread, socketIO._listenerThread, ] for childThread in childThreads: self.assertEqual(not connected, childThread.done.is_set()) self.assertEqual(connected, socketIO.connected) def test_disconnect(self): 'Terminate child threads after disconnect' self.is_connected(self.socketIO, True) self.socketIO.disconnect() self.is_connected(self.socketIO, False) # Use context manager with SocketIO(HOST, PORT) as self.socketIO: self.is_connected(self.socketIO, True) self.is_connected(self.socketIO, False) def test_message(self): 'Message' self.socketIO.define(Namespace) self.socketIO.message() self.socketIO.wait(0.1) namespace = self.socketIO.get_namespace() self.assertEqual(namespace.response, 'message_response') def test_message_with_data(self): 'Message with data' self.socketIO.define(Namespace) self.socketIO.message(DATA) self.socketIO.wait(0.1) namespace = self.socketIO.get_namespace() self.assertEqual(namespace.response, DATA) def test_message_with_payload(self): 'Message with payload' self.socketIO.define(Namespace) self.socketIO.message(PAYLOAD) self.socketIO.wait(0.1) namespace = self.socketIO.get_namespace() self.assertEqual(namespace.response, PAYLOAD) def test_message_with_callback(self): 'Message with callback' self.socketIO.message(callback=self.on_response) self.socketIO.wait_for_callbacks(seconds=0.1) self.assertEqual(self.called_on_response, True) def test_message_with_callback_with_data(self): 'Message with callback with data' self.socketIO.message(DATA, self.on_response) self.socketIO.wait_for_callbacks(seconds=0.1) self.assertEqual(self.called_on_response, True) def test_emit(self): 'Emit' self.socketIO.define(Namespace) self.socketIO.emit('emit') self.socketIO.wait(0.1) self.assertEqual(self.socketIO.get_namespace().argsByEvent, { 'emit_response': (), }) def test_emit_with_payload(self): 'Emit with payload' self.socketIO.define(Namespace) self.socketIO.emit('emit_with_payload', PAYLOAD) self.socketIO.wait(0.1) self.assertEqual(self.socketIO.get_namespace().argsByEvent, { 'emit_with_payload_response': (PAYLOAD,), }) def test_emit_with_multiple_payloads(self): 'Emit with multiple payloads' self.socketIO.define(Namespace) self.socketIO.emit('emit_with_multiple_payloads', PAYLOAD, PAYLOAD) self.socketIO.wait(0.1) self.assertEqual(self.socketIO.get_namespace().argsByEvent, { 'emit_with_multiple_payloads_response': (PAYLOAD, PAYLOAD), }) def test_emit_with_callback(self): 'Emit with callback' self.socketIO.emit('emit_with_callback', self.on_response) self.socketIO.wait_for_callbacks(seconds=0.1) self.assertEqual(self.called_on_response, True) def test_emit_with_callback_with_payload(self): 'Emit with callback with payload' self.socketIO.emit('emit_with_callback_with_payload', self.on_response) self.socketIO.wait_for_callbacks(seconds=0.1) self.assertEqual(self.called_on_response, True) def test_emit_with_callback_with_multiple_payloads(self): 'Emit with callback with multiple payloads' self.socketIO.emit('emit_with_callback_with_multiple_payloads', self.on_response) self.socketIO.wait_for_callbacks(seconds=0.1) self.assertEqual(self.called_on_response, True) def test_emit_with_event(self): 'Emit to trigger an event' self.socketIO.on('emit_with_event_response', self.on_response) self.socketIO.emit('emit_with_event', PAYLOAD) self.socketIO.wait_for_callbacks(0.1) self.assertEqual(self.called_on_response, True) def test_ack(self): 'Trigger server callback' self.socketIO.define(Namespace) self.socketIO.emit('ack', PAYLOAD) self.socketIO.wait(0.1) self.assertEqual(self.socketIO.get_namespace().argsByEvent, { 'ack_response': (PAYLOAD,), 'ack_callback_response': (PAYLOAD,), }) def test_namespaces(self): 'Behave differently in different namespaces' mainNamespace = self.socketIO.define(Namespace) chatNamespace = self.socketIO.define(Namespace, '/chat') newsNamespace = self.socketIO.define(Namespace, '/news') newsNamespace.emit('emit_with_payload', PAYLOAD) self.socketIO.wait(0.1) self.assertEqual(mainNamespace.argsByEvent, {}) self.assertEqual(chatNamespace.argsByEvent, {}) self.assertEqual(newsNamespace.argsByEvent, { 'emit_with_payload_response': (PAYLOAD,), })
class BaseChallenge(object): def __init__(self, challenge_id, api_key, config, grader_id=False): # # challenge_id is present for backward compatibility reasons # a combination of challenge_client_name and grader_id should be # used for all the new challenges self.api_key = str(api_key).strip() self.challenge_id = challenge_id self.config = config self.session_key = None self.latest_response = False self.pbar = None self.PROGRESS_BAR=True self.AGGREGATED_PROGRESS_BAR=False self.SUPPRESS_LOGGING_HELPERS=False self.challenge_client_name = challenge_id self.grader_id = grader_id def on_connect(self): if not self.SUPPRESS_LOGGING_HELPERS: print(lh.success(CrowdAIEvents.Connection["CONNECTED"], "")) def on_disconnect(self): if not self.SUPPRESS_LOGGING_HELPERS: print(lh.error(CrowdAIEvents.Connection["DISCONNECTED"], "")) def on_reconnect(self): if not self.SUPPRESS_LOGGING_HELPERS: print(lh.success(CrowdAIEvents.Connection["RECONNECTED"], "")) def _connect(self): # TO-DO : Handle socketio connection and disconnection events self.socketio = SocketIO(self.config['remote_host'], self.config['remote_port'], LoggingNamespace) self.socketio.on('connect', self.on_connect) self.socketio.on('disconnect', self.on_disconnect) self.socketio.on('reconnect', self.on_reconnect) def disconnect(self): self.socketio.disconnect() def _authenticate_response(self, args): if args["response_type"] == CrowdAIEvents.Authentication["SUCCESS"]: self.session_key = args["session_token"] if not self.SUPPRESS_LOGGING_HELPERS: print(lh.success(CrowdAIEvents.Authentication["SUCCESS"], "Authentication Successful")) else: # TO-DO: Log authentication error if not self.SUPPRESS_LOGGING_HELPERS: print(lh.error(CrowdAIEvents.Authentication["ERROR"], args["message"])) self.disconnect() raise CrowdAIAuthenticationError(args["message"]) def _authenticate_response_placeholder(self, args): pass def _authenticate(self): if not self.SUPPRESS_LOGGING_HELPERS: print(lh.info(CrowdAIEvents.Authentication[""], "Authenticating for challenge = "+colored(self.challenge_id, "blue", attrs=['bold', 'underline']))) self.session_key = None self.socketio.on('authenticate_response', self._authenticate_response) self.socketio.emit('authenticate', {"API_KEY":self.api_key, "challenge_id": self.challenge_id, "client_version":pkg_resources.get_distribution("crowdai").version}, self._authenticate_response_placeholder) self.socketio.wait_for_callbacks(seconds=self.config['TIMEOUT_AUTHENTICATE']) if self.session_key == None: # TO-DO: Log authentication error self.disconnect() raise CrowdAIAuthenticationError("Authentication Timeout") def on_execute_function_response(self, channel_name, payload={}): if payload == {}:# TODO: Critical This is a hacky fix. We need to find the exact reason why post-submit events are not in this format. payload = channel_name payload = json.loads(payload) job_state = payload['response_type'] job_id = payload['job_id'] sequence_no = payload["data_sequence_no"] #Sequence No being the index of the corresponding input data point in the parallel execution array message = payload["message"] if job_state == CrowdAIEvents.Job["ERROR"]: self.disconnect() raise CrowdAIExecuteFunctionError(payload["message"]) elif job_state == CrowdAIEvents.Job["ENQUEUED"]: job_event_messsage = lh.info_yellow(job_state, job_id); if self.PROGRESS_BAR: self.write_above_single_progress_bar(sequence_no, job_event_messsage) elif job_state == CrowdAIEvents.Job["RUNNING"]: job_event_messsage = lh.info_blue(job_state, job_id) if self.PROGRESS_BAR: self.write_above_single_progress_bar(sequence_no, job_event_messsage) if not self.AGGREGATED_PROGRESS_BAR: self.update_single_progress_bar_description(sequence_no, colored(job_id, 'green', attrs=['bold'])) elif job_state == CrowdAIEvents.Job["PROGRESS_UPDATE"]: self.update_progress_tracker(sequence_no, payload["data"]["percent_complete"]) if self.PROGRESS_BAR: if not self.AGGREGATED_PROGRESS_BAR: self.update_single_progress_bar_description(sequence_no, colored(job_id, 'green', attrs=['bold'])) elif job_state == CrowdAIEvents.Job["COMPLETE"]: self.update_progress_tracker(sequence_no, 100) job_event_messsage = lh.success(job_state, job_id) safe_job_event_messsage = job_event_messsage # When sequence number is less than 0, it is a JOB_COMPLETE event which is not associated with any # current jobs if sequence_no >= 0: safe_job_event_messsage = job_event_messsage + "\t OK" job_event_messsage += u"\t \U0001F37A " else: safe_job_event_messsage = job_event_messsage + "\t OK" job_event_messsage += u"\t \U0001F37A \U0001F37A \U0001F37A" if self.PROGRESS_BAR: try: self.write_above_single_progress_bar(sequence_no, job_event_messsage) except UnicodeEncodeError: # If the client doesnt have the relevant codecs for rendering this, # Then dont make a whole mess about it, and instead print the safe_job_event_messsage. self.write_above_single_progress_bar(sequence_no, safe_job_event_messsage) if not self.AGGREGATED_PROGRESS_BAR: self.update_single_progress_bar_description(sequence_no, colored(job_id, 'green', attrs=['bold'])) elif job_state == CrowdAIEvents.Job["INFO"]: job_event_messsage = lh.info(job_state, "("+job_id+") "+payload["message"]) if self.PROGRESS_BAR: self.write_above_single_progress_bar(sequence_no, job_event_messsage) elif job_state == CrowdAIEvents.Job["TIMEOUT"]: job_event_messsage = lh.error(job_state, "("+job_id+") "+payload["message"]) if self.PROGRESS_BAR: self.write_above_single_progress_bar(sequence_no, job_event_messsage) else: job_event_messsage = lh.error(job_state, "("+job_id+") "+str(payload["message"])) if self.PROGRESS_BAR: self.write_above_single_progress_bar(sequence_no, job_event_messsage) raise CrowdAIExecuteFunctionError("Malformed response from server. \ Please update your crowdai package, and if the problem still persists contact the server admins.\n") def on_execute_function_response_complete(self, args): """ Placeholder function to be able to account for Timeout thresholds """ if self.PROGRESS_BAR: self.close_all_progress_bars() self._aggregated_responses = [] for _val in args: _item = _val['data'] _item['job_state'] = _val['response_type'] _item['message'] = _val['message'] self._aggregated_responses.append(_item) self.aggregated_responses = self._aggregated_responses return {} def execute_function(self, function_name, data, dry_run=False, parallel=False): #TO-DO : Validate if authenticated self.response_channel = self.challenge_id+"::"+str(uuid.uuid4()) #Prepare for response self.aggregated_responses = False self.instantiate_progress_bars(len(data)) if self.challenge_client_name and self.grader_id: payload = { "response_channel" : self.response_channel, "session_token": self.session_key, "api_key": self.api_key, "grader_id": self.grader_id, "challenge_client_name": self.challenge_client_name, "function_name": function_name, "data": data, "dry_run" : dry_run, "parallel" : parallel } else: """ This payload structure is deprecated. and is only added conditionally for backward compatibility. """ payload = { "response_channel" : self.response_channel, "session_token": self.session_key, "api_key": self.api_key, "challenge_id": self.challenge_id, "function_name": function_name, "data": data, "dry_run" : dry_run, "parallel" : parallel } self.socketio.on(self.response_channel, self.on_execute_function_response) self.execute_function_response = None self.socketio.emit('execute_function', payload, self.on_execute_function_response_complete) if self.challenge_id not in self.config['challenges'].keys(): timeout = self.config['challenges']["crowdAIGenericChallenge"]["TIMEOUT_EXECUTION"] else: timeout = self.config['challenges'][self.challenge_id]["TIMEOUT_EXECUTION"] self.socketio.wait_for_callbacks(seconds=timeout) """ This keeps checking until the aggregated_responses are prepared by the final on_execute_function_response_complete and then it returns the same to the calling function This is only to ensure that the function is blocking function """ #TODO Explore if we can come up with an event based mechanism for the same while True: if self.aggregated_responses: return self.aggregated_responses time.sleep(2) def instantiate_progress_trackers(self, number): if self.SUPPRESS_LOGGING_HELPERS: return self.last_reported_progress = [] for k in range(number): self.last_reported_progress.append(0) self.last_reported_mean_progress = 0 def update_progress_tracker(self, seq_no, value): if self.SUPPRESS_LOGGING_HELPERS: return diff = 0 if value > self.last_reported_progress[seq_no]: diff = value - self.last_reported_progress[seq_no] self.last_reported_progress[seq_no] = value if self.PROGRESS_BAR: if self.AGGREGATED_PROGRESS_BAR: _mean = sum(self.last_reported_progress)*1.0/len(self.last_reported_progress) if _mean > self.last_reported_mean_progress: diff = _mean - self.last_reported_mean_progress self.last_reported_mean_progress = _mean self.update_single_progress_bar(0, diff) else: self.update_single_progress_bar(seq_no, diff) def instantiate_progress_bars(self, number): if self.SUPPRESS_LOGGING_HELPERS: return self.instantiate_progress_trackers(number) if self.PROGRESS_BAR: """ Instantiate only of Progress Bars are enabled """ if self.AGGREGATED_PROGRESS_BAR: """ Force set numnber of progress bars to 1 if AGGREGATED_PROGRESS_BAR is enabled """ number = 1 else: return self.pbar = [] for k in range(number): self.pbar.append(tqdm(total=100, dynamic_ncols=True, unit="% ", \ bar_format="{desc}{percentage:3.0f}% "\ "|{bar}|" \ # "{n_fmt}/{total_fmt}" \ "[{elapsed}<{remaining}] "\ " {rate_fmt}] "\ ""\ )) def close_all_progress_bars(self): if self.SUPPRESS_LOGGING_HELPERS: return for _idx, _pbar in enumerate(self.pbar): _pbar.close() def update_single_progress_bar(self, seq_no, diff): if self.SUPPRESS_LOGGING_HELPERS: return try: if seq_no != -1: foo = self.last_reported_progress[seq_no] except Exception: return self.pbar[seq_no].update(diff) def write_above_single_progress_bar(self, seq_no, line): if self.SUPPRESS_LOGGING_HELPERS: return tqdm.write(line) def update_single_progress_bar_description(self, seq_no, line): if self.SUPPRESS_LOGGING_HELPERS: return try: if seq_no != -1: foo = self.last_reported_progress[seq_no] except Exception: return self.pbar[seq_no].set_description(line) def verbose(self, v): self.SUPPRESS_LOGGING_HELPERS = not v
class Muse(Device): def __init__(self, device_id=None): super().__init__(device_id) self.streams = {} self.pred_results = [] self.train_results = [] self.time_diff = 0 # difference between unix and muse time self.train_mode = True # True for training, False for prediction # socket for communicating with whatever wants to pull prediction # results from Muse self.sio = socketio.AsyncServer(async_mode='sanic') self.app = Sanic() self.sio.attach(self.app) @staticmethod def available_devices(): pass # # Callback functions # def on_retrieve_prediction_results(self, *args): """Callback function for saving prediction results""" results = args[0] self.pred_results.append(results) def on_train_results(self, *args): """Callback function for saving training results""" results = args[0] self.train_results.append(results) def print_results(self, *args): """Test callback function that simply prints out the results""" for arg in args: print(arg) # # Private device methods for handling data streams # def _create_eeg_stream(self): """Creates a stream that streams EEG data""" return EEGStream(thread_name='EEG_data', event_channel_name='P300') def _create_marker_stream(self): """Create a stream that streams marker data""" info = pylsl.StreamInfo('Markers', 'Markers', 4, 0, 'string', 'mywid32') self.marker_outlet = pylsl.StreamOutlet(info) return MarkerStream(thread_name='Marker_stream') def _create_ml_stream(self, data): """Creates a stream that combines the EEG and marker streams, and forms epochs based on timestamp""" if self.streams.get('eeg') is None: raise Exception(f"EEG stream does not exist") if self.streams.get('marker') is None: raise Exception(f"Marker stream does not exist") return MLStream(m_stream=self.streams['marker'], eeg_stream=self.streams['eeg'], event_time=data['event_time'], train_epochs=data['train_epochs']) def _start_stream(self, stream): """Starts stream given stream name (one of 'eeg', 'marker', or 'ml')""" if self.streams.get(stream) is None: raise RuntimeError( "Cannot start {0} stream, stream does not exist".format( stream)) elif stream == 'ml': self.streams[stream].start(self.train_mode) else: self.streams[stream].lsl_connect() def _stop_stream(self, stream): """Stops stream given stream name (one of 'eeg', 'marker', or 'ml')""" if self.streams.get(stream) is None: raise RuntimeError( "Cannot stop {0} stream, stream does not exist".format(stream)) else: self.streams[stream].stop() # # Methods for handling server communication # def neurostack_connect(self, ip='35.222.93.233', port=8001): """ Connects to neurostack server at ip:port and starts marker and ML streams. If no arguments for ip and port are given, then connects to the default hardcoded address for a server on the cloud. """ self.socket_client = SocketIO(ip, port) self.socket_client.connect() # assumes EEG stream has already been started for stream in ['marker', 'ml']: self._start_stream(stream) while len(self.streams['eeg'].data) == 0: time.sleep(0.1) self.time_diff = time.time() - self.streams['eeg'].data[-1][-1] def neurostack_disconnect(self): """Disconnects from neurostack server and stops marker and ML streams""" for stream in ['marker', 'ml']: self._stop_stream(stream) self.socket_client.disconnect() def send_train_data(self, uuid, eeg_data, p300): """ Sends training data to neurostack server :param uuid: client's UUID :param eeg_data: one sample of EEG data to be used for training :param p300: True if this data represents a p300 signal, else False :returns: None """ args = {'uuid': uuid, 'data': eeg_data, 'p300': p300} self.socket_client.emit("train_classifier", args, self.on_train_results) self.socket_client.wait_for_callbacks(seconds=1) def send_predict_data(self, uuid, eeg_data): """ Sneds prediction data to neurostack server :param uuid: client's UUID :param eeg_data: one sample of EEG data that we want to predict for :returns: None """ args = {'uuid': uuid, 'data': eeg_data} self.socket_client.emit("retrieve_prediction_results", args, self.on_retrieve_prediction_results) self.socket_client.wait_for_callbacks(seconds=1) def change_mode(self, train_mode=False): """ self.train_mode=True for training mode self.train_mode=False for prediction mode """ if self.streams['ml'] is None: raise Exception(f"ml stream does is not running") curr_mode = self.streams['ml'].get_mode() if curr_mode is not train_mode: self.train_mode = train_mode self.streams['ml'].set_mode(train_mode) def send_predict_data_test(self, uuid, eeg_data): """ Tests endpoint for sending prediction data to neurostack server :param uuid: client's UUID :param eeg_data: one sample of EEG data that we want to predict for :returns: None """ args = {'uuid': uuid, 'data': eeg_data} self.socket_client.emit("retrieve_prediction_results_test", args, self.print_results) self.socket_client.wait_for_callbacks(seconds=1) def send_train_data_test(self, uuid, eeg_data, p300): """ Tests endpoint for sending training data to neurostack server :param uuid: client's UUID :param eeg_data: one sample of EEG data to be used for training :param p300: True if this data represents a p300 signal, else False :returns: None """ args = {'uuid': uuid, 'data': eeg_data, 'p300': p300} self.socket_client.emit("train_classifier_test", args, self.print_results) self.socket_client.wait_for_callbacks(seconds=1) # # Methods for handling client-side communication # def initialize_handlers(self): """Initialize handlers for client-side communication""" self.sio.on("train", self.train_handler) self.sio.on("predict", self.predict_handler) self.sio.on("generate_uuid", self.generate_uuid_handler) async def train_handler(self, sid, args): """Handler for passing training data to Neurostack""" if not self.train_mode: self.change_mode(train_mode=True) time.sleep(.2) args = json.loads(args) uuid = args['uuid'] timestamp = args['timestamp'] p300 = args['p300'] timestamp -= self.time_diff package = [ str(timestamp), str(p300), # target str(1), # 1 event total str(uuid) # epoch ID ] self.marker_outlet.push_sample(package) await self.start_event_loop() while len(self.train_results) == 0: time.sleep(.1) return self.train_results.pop(0) async def predict_handler(self, sid, args): """Handler for passing prediction data to Neurostack""" if self.train_mode: self.change_mode(train_mode=False) time.sleep(.2) args = json.loads(args) uuid = args['uuid'] timestamp = args['timestamp'] timestamp -= self.time_diff package = [ str(timestamp), str(0), # target str(1), # 1 event total str(uuid) # epoch ID ] self.marker_outlet.push_sample(package) await self.start_event_loop() while len(self.pred_results) == 0: time.sleep(.1) return self.pred_results.pop(0) async def generate_uuid_handler(self, sid, args): """Handler for sending a request to the server to generate a UUID""" return generate_uuid() async def start_event_loop(self): """ Continuously pulls data from ml_stream and sends to server based on whether we are training or predicting """ if self.streams.get('ml') is None: raise Exception(f"ml stream does not exist") data = None while data is None: # send training jobs to server if self.train_mode: data = self.streams['ml'].get_training_data() if data is not None: uuid = data['uuid'] train_data = data['train_data'] train_targets = data['train_targets'] self.send_train_data(uuid, train_data, train_targets) return # send prediction jobs to server else: data = self.streams['ml'].get_prediction_data() if data is not None: uuid = data['uuid'] eeg_data = data['eeg_data'] self.send_predict_data(uuid, eeg_data) return time.sleep(0.1) # # Public device metods # def connect(self, device_id=None): """ Creates data streams if there are none and connects to EEG stream (since that is the one that is immediately needed for use) """ if self.streams.get('eeg') is None: self.streams['eeg'] = self._create_eeg_stream() if self.streams.get('marker') is None: self.streams['marker'] = self._create_marker_stream() if self.streams.get('ml') is None: data = { 'event_time': 0.4, 'train_epochs': 120 } # 120 for 2 min, 240 for 4 min self.streams['ml'] = self._create_ml_stream(data) self.streams['eeg'].lsl_connect() def start(self): """Start streaming EEG data""" self.streams['eeg'].start() while len(self.streams['eeg'].data) == 0: time.sleep(0.1) self.time_diff = time.time() - self.streams['eeg'].data[-1][-1] def stop(self): """Stop streaming EEG data""" self.streams['eeg'].stop() def shutdown(self): """Disconnect EEG stream (and stop streaming data)""" for stream_name in ['eeg', 'marker', 'ml']: self.streams[stream_name] = None def get_info(self): pass
def check(self): try: index = self.s.get('http://{}:{}/'.format(self.ip, self.flask_port), timeout=Config.timeout) except: self.errors.append('Host is down') self.put_status() return self.integrity += 5 try: res = self.s.post('http://{}:{}/register'.format(self.ip, self.flask_port), data={'email': self.email, 'login': self.username, 'about': self.flag}, timeout=Config.timeout) except: self.errors.append('Registration is unavailable') self.put_status() return self.integrity += 5 try: self.password = re.findall(r'\<p class="flow-text center-align"\>(.+?)\</p\>', res.text)[0].strip() self.idd = re.findall(r'Уже зарегистрировано\: (\d+?) \</p\>', res.text)[0].strip() except Exception as e: self.errors.append("Can't reach my password or id") self.put_status() self.integrity += 10 self.put_status() self.save_credentials() try: login = self.s.post('http://{}:{}/login'.format(self.ip, self.flask_port), data={'login': self.username, 'pass': self.password}, timeout=Config.timeout) if 'Выйти' not in login.text: self.errors.append("Can't log in") self.put_status() except: self.errors.append("Can't log in") self.put_status() try: profile = self.s.get('http://{}:{}/user/{}'.format(self.ip, self.flask_port, self.idd), timeout=Config.timeout) if self.flag not in profile.text: self.errors.append('Flag is unavailable') self.put_status() else: self.integrity += 30 self.put_status() except: self.errors.append('Flag is unavailable') self.put_status() possible = string.ascii_letters + string.digits + ' ' msg = ''.join([random.choice(possible) for i in range(random.randint(10, 15))]) # print('sending {} to chat'.format(msg)) try: socketIO = SocketIO(self.ip, self.sockets_port) chat_namespace = socketIO.define(ChatNamespace, '/chat') data = dict(data=dict(message=msg, author=self.username)) chat_namespace.emit('message', data) socketIO.wait_for_callbacks(seconds=1) chat = self.s.get('http://{}:{}/chat'.format(self.ip, self.flask_port), timeout=Config.timeout) if str(msg) not in chat.text: self.errors.append("Can't send messages to chat") self.put_status() else: self.integrity += 15 self.put_status() except: self.errors.append("Can't send messages to chat") self.put_status() if round != 1: s = requests.Session() try: login = s.post('http://{}:{}/login'.format(self.ip, self.flask_port), data={'login': self.old_username, 'pass': self.old_password}, timeout=Config.timeout) if 'Выйти' not in login.text: self.errors.append("Previous account is unavailable") self.put_status() old_profile = s.get('http://{}:{}/user/{}'.format(self.ip, self.flask_port, self.old_idd), timeout=Config.timeout) if self.old_flag not in old_profile.text: self.errors.append('Previous flag is unavailable') self.put_status() else: self.errors.append("Checked") self.integrity += 35 self.put_status() except: self.errors.append('Previous flag is unavailable') self.put_status() else: self.errors.append("Checked") self.integrity += 35 self.put_status()
class Volumio: def __init__(self, server='localhost', port=3000): # connection vers volumio self.socketIO = SocketIO(server, port) def __on_getQueue_response(self, *args): self.playlist = args[0] def __on_search_response(self, *args): search_result = args[0].get('navigation').get('lists')[0].get('items')[0].get('uri') print(search_result) self.clear_queue() self.add_to_queue(search_result) def __on_getState_response(self, *args): print(args[0]) def set_playlist(self, playlist): self.playlist_name = playlist self.socketIO.on('pushQueue', self.__on_getQueue_response) self.socketIO.emit('getQueue', '', self.__on_getQueue_response) self.socketIO.wait_for_callbacks(seconds=1) def play_playlist(self): self.socketIO.emit('playPlaylist', {'name': self.playlist_name}) def create_playlist(self): self.socketIO.emit('createPlaylist', {'name': self.playlist_name}) def remove_playlist(self): self.socketIO.emit('deletePlaylist', {'name': self.playlist_name}) def set_volume(self, volume): if volume > 0 or volume <100: self.socketIO.emit('volume', volume) def add_to_playlist(self, song): uri = song['uri'] self.socketIO.emit('addToPlaylist', {'name': self.playlist_name, 'service': song['service'], 'uri': uri}) def record_playlist(self): for song in self.playlist: self.add_to_playlist(song) time.sleep(0.7) def search(self, query): self.socketIO.on('pushBrowseLibrary', self.__on_search_response) self.socketIO.emit('search', {'value': query}, self.__on_search_response) self.socketIO.wait_for_callbacks(seconds=1) def getState(self): self.socketIO.on('pushState', self.__on_getState_response) self.socketIO.emit('getState', '', self.__on_getState_response) self.socketIO.wait_for_callbacks(seconds=1) def next_song(self): self.socketIO.emit('next') def previous_song(self): self.socketIO.emit('prev') def stop_song(self): self.socketIO.emit('stop') def pause_song(self): self.socketIO.emit('pause') def play_song(self): self.socketIO.emit('play') def clear_queue(self): self.socketIO.emit('clearQueue') def add_to_queue(self, uri): self.socketIO.emit('addToQueue', {'uri': uri}) @staticmethod def shutdown(): subprocess.call(['shutdown', '-h', 'now'], shell=False)
class VolumioClient: """ Class for the websocket client to Volumio """ def __init__(self): HOSTNAME = 'localhost' PORT = 3000 self._callback_function = False self._callback_args = False self.state = dict() self.state["status"] = "" self.prev_state = dict() self.prev_state["status"] = "" self.last_update_time = 0 def _on_pushState(*args): self.state = args[0] if self._callback_function: self._callback_function(*self._callback_args) self.prev_state = self.state self._client = SocketIO(HOSTNAME, PORT, LoggingNamespace) self._client.on('pushState', _on_pushState) self._client.emit('getState', _on_pushState) self._client.wait_for_callbacks(seconds=1) def set_callback(self, callback_function, *callback_args): self._callback_function = callback_function self._callback_args = callback_args def play(self): self._client.emit('play') def pause(self): self._client.emit('pause') def toggle_play(self): try: if self.state["status"] == "play": self._client.emit('pause') else: self._client.emit('play') except KeyError: self._client.emit('play') def volume_up(self): self._client.emit('volume', '+') def volume_down(self): self._client.emit('volume', '-') def previous(self): self._client.emit('prev') def next(self): self._client.emit('next') def seek(self, seconds): self._client.emit('seek', int(seconds)) def wait(self, **kwargs): wait_thread = Thread(target=self._client.wait, args=(kwargs)) wait_thread.start() print "started websocket wait thread" return wait_thread
# -*- coding: utf-8 -*- import types from socketIO_client import SocketIO hosts = 'http://websocke.server.com' port = 3000 # 收到message消息处理过程 def on_message(*args): if type(args[0]) is types.DictType: rp = args[0] print "recv:", rp sk = SocketIO(hosts, port=port, params={'token': 'ksdjfkjdf'}) # create connection with params # add lisenter for message response sk.on('message', on_message) data = {"sn": 0, "ver": 2} # send data to message sk.emit('message', data, on_message) sk.sendf(data, on_message) # default send data to message # send data to login sk.emit('login', data, on_message) sk.wait_for_callbacks(seconds=1)
class Neurostack: def __init__(self, devices=None): """ Initialize a connection with an EEG device, and sets up an asynchronous connection with subscribers passed in. :param device: [Devices] """ self.devices = devices # sanic server connects to app self.sio_app = socketio.AsyncServer(async_mode='sanic', cors_allowed_origins='*') self.sio_app_server = Sanic() self.sio_app.attach(self.sio_app_server) # socketIO client connects to neurostack server self.sio_neurostack = None self.train_results = {} self.predict_results = {} self.stream_raw_data = {} # # Methods for handling devices # def start(self, list_of_devices=None): """ Start streaming EEG from device, and publish data to subscribers. :param list_of_devices: [Device] List of devices to start streaming. If none, all devices will start streaming. :return: None """ if list_of_devices is None: devices_to_start = self.devices else: devices_to_start = list_of_devices for device in devices_to_start: device.start() def stop(self, list_of_devices=None): """ Stop streaming EEG data from device, and stop publishing data to subscribers. Connection to device remains intact, and device is not turned off. :param list_of_devices: [Device] List of devices to stop streaming. If none, all devices will stop streaming. :return: None """ if list_of_devices is None: devices_to_start = self.devices else: devices_to_start = list_of_devices for device in devices_to_start: device.stop() def shutdown(self, list_of_devices=None): """ Close connection to device, WebSocket connections to publishers, and tag sources. :return: None """ pass # # Methods for handling server-side communication # def neurostack_connect(self, ip='neurostack.neurotechuoft.com', port=8001): """ Connects to neurostack server at ip:port. If no arguments for ip and port are given, then connects to the default hardcoded address for a server on the cloud. """ self.sio_neurostack = SocketIO(ip, port) self.sio_neurostack.connect() def neurostack_disconnect(self): """Disconnects from neurostack server""" self.sio_neurostack.disconnect() def send_train_data(self, server_endpoint, uuid, eeg_data, label): """ Sends training data to neurostack server :param server_endpoint: server API's endpoint :param uuid: client's UUID :param eeg_data: one sample of EEG data to be used for training :param label: this data's label :returns: None """ args = {'uuid': uuid, 'data': eeg_data, 'label': label} self.sio_neurostack.emit(server_endpoint, args, self.on_train_results) self.sio_neurostack.wait_for_callbacks(seconds=1) def send_predict_data(self, server_endpoint, uuid, eeg_data): """ Sneds prediction data to neurostack server :param server_endpoint: server API's endpoint :param uuid: client's UUID :param eeg_data: one sample of EEG data that we want to predict for :returns: None """ args = {'uuid': uuid, 'data': eeg_data} self.sio_neurostack.emit(server_endpoint, args, self.on_predict_results) self.sio_neurostack.wait_for_callbacks(seconds=1) def send_train_data_test(self, uuid, eeg_data, label): """ Tests endpoint for sending training data to neurostack server :param uuid: client's UUID :param eeg_data: one sample of EEG data to be used for training :param label: this data's label :returns: None """ args = {'uuid': uuid, 'data': eeg_data, 'label': label} self.sio_neurostack.emit("test_train", args, self.print_results) self.sio_neurostack.wait_for_callbacks(seconds=1) def send_predict_data_test(self, uuid, eeg_data): """ Tests endpoint for sending prediction data to neurostack server :param uuid: client's UUID :param eeg_data: one sample of EEG data that we want to predict for :returns: None """ args = {'uuid': uuid, 'data': eeg_data} self.sio_neurostack.emit("test_predict", args, self.print_results) self.sio_neurostack.wait_for_callbacks(seconds=1) # # Methods for handling client-side communication # def initialize_handlers(self): """Initialize handlers for client-side communication""" # streaming raw data self.sio_app.on("start_streaming_raw_data", self.start_streaming_raw_data_handler) self.sio_app.on("stop_streaming_raw_data", self.stop_streaming_raw_data_handler) # training Neurostack model self.sio_app.on("p300_train", self.p300_train_handler) self.sio_app.on("p300_predict", self.p300_predict_handler) self.sio_app.on("left_right_train", self.left_right_train_handler) self.sio_app.on("left_right_predict", self.left_right_predict_handler) # misc self.sio_app.on("generate_uuid", self.generate_uuid_handler) def run(self, host='localhost', port=8002): """ Runs Neurostack on host:port. This is used as an endpoint for client-side communication. :param host: local address to Neurostack on :param port: port to run Neurostack on :return: None """ self.sio_app_server.run(host=host, port=port) async def start_streaming_raw_data_handler(self, sid, args): """ Handler for streaming raw data :param sid: session ID (not important) :param args: arguments passed to this function. This should include: uuid: universally unique ID of user who wants to stop streaming """ args = json.loads(args) uuid = args['uuid'] self.stream_raw_data[uuid] = True # keep track of data from previous while loop iteration, so that the # same data is not sent twice. prev_data = None while self.stream_raw_data[uuid]: # TODO: devices[0] is the Muse that we set at the bottom, but we # want to support multiple or different devices data_stream = self.devices[0].data_stream eeg_channel_names = data_stream.get_eeg_channels() raw_data = data_stream.get_latest_data(eeg_channel_names) # TODO: raw data can be either a list or a dict right now, should we # just stick with dict? # in case while loop is running faster than device streaming rate if raw_data != prev_data: prev_data = raw_data await self.sio_app.emit('raw_data', raw_data) async def stop_streaming_raw_data_handler(self, sid, args): """ Handler to tell neurostack to stop streaming raw data :param sid: session ID (not important) :param args: arguments passed to this function. This should include: uuid: universally unique ID of user who wants to stop streaming """ args = json.loads(args) uuid = args['uuid'] self.stream_raw_data[uuid] = False await self.sio_app.emit('raw_data', "streaming has stopped") async def p300_train_handler(self, sid, args): """P300 training handler""" args = json.loads(args) await self.train_handler(server_endpoint="p300_train", uuid=args['uuid'], timestamp=args['timestamp'], label=args['p300']) async def p300_predict_handler(self, sid, args): """P300 prediction handler""" args = json.loads(args) await self.predict_handler(server_endpoint="p300_predict", uuid=args['uuid'], timestamp=args['timestamp']) async def left_right_train_handler(self, sid, args): """Left-right training handler""" args = json.loads(args) await self.train_handler(server_endpoint="left_right_train", uuid=args['uuid'], timestamp=args['timestamp'], label=args['left']) async def left_right_predict_handler(self, sid, args): """Left-right prediction handler""" args = json.loads(args) await self.predict_handler(server_endpoint="left_right_predict", uuid=args['uuid'], timestamp=args['timestamp']) async def train_handler(self, server_endpoint, uuid, timestamp, label, window=0.75): """ Handler for passing training data to Neurostack TODO: something for sample rate :param server_endpoint: Neurostack server API endpoint :param uuid: client UUID :param timestamp: timestamp of data we are interested in, in unix time :param label: label for data :param window: window of data we are interested in, in seconds :return: None """ # create list for uuid if not done already self.train_results[uuid] = self.train_results.get(uuid, []) # TODO: change API to specify device device = self.devices[0] # Wait until the device has enough data (ie. the time slice is complete) # then take 100ms - 750ms window for training while time.time() < timestamp + window: time.sleep(.01) # TODO: num_samples = window * sample rate timestamp -= self.devices[0].get_time_diff() data_dict = device.data_stream.get_eeg_data(start_time=timestamp + .1, num_samples=128) data = list(data_dict.values()) self.send_train_data(server_endpoint=server_endpoint, uuid=uuid, eeg_data=data, label=label) # wait for results while len(self.train_results[uuid]) == 0: time.sleep(.01) result = self.train_results[uuid].pop(0) await self.sio_app.emit("train", result) async def predict_handler(self, server_endpoint, uuid, timestamp, window=0.75): """ Handler for passing prediction data to Neurostack TODO: something for sample rate :param server_endpoint: Neurostack server API endpoint :param uuid: client UUID :param timestamp: timestamp of data we are interested in, in unix time :param window: window of data we are interested in, in seconds :return: None """ # create list for uuid if not done already self.predict_results[uuid] = self.predict_results.get(uuid, []) # TODO: change API to specify device device = self.devices[0] # Wait until the device has enough data (ie. the time slice is complete) # then take 100ms - 750ms window for training. The window should # contain 0.65s * 256Hz = 166 samples. while time.time() < timestamp + window: time.sleep(.01) timestamp -= self.devices[0].get_time_diff() data_dict = device.data_stream.get_eeg_data(start_time=timestamp + .1, num_samples=128) data = list(data_dict.values()) self.send_predict_data(server_endpoint=server_endpoint, uuid=uuid, eeg_data=data) # wait for results while len(self.predict_results[uuid]) == 0: time.sleep(.01) result = self.predict_results[uuid].pop(0) await self.sio_app.emit("predict", result) async def generate_uuid_handler(self, sid, args): """Handler for sending a request to the server to generate a UUID""" uuid = generate_uuid() await self.sio_app.emit('generate_uuid', uuid) # # Callback functions # def on_train_results(self, *args): """Callback function for saving training results""" results = args[0] uuid = results['uuid'] self.train_results[uuid].append(results) def on_predict_results(self, *args): """Callback function for saving prediction results""" results = args[0] uuid = results['uuid'] self.predict_results[uuid].append(results) def print_results(self, *args): """Prints out results""" print(args) # # Other methods # def get_info(self, list_of_devices=None) -> []: """ Return list of string representations of device info for specified devices (by calling get_info of each device). By default lists info of all devices under Neurostack. :return: """ if list_of_devices is None: devices_to_start = self.devices else: devices_to_start = list_of_devices info = [device.get_info() for device in devices_to_start] return info
class BlueNode(QtCore.QThread): new_data = QtCore.Signal(object) def __init__(self, login, password): QtCore.QThread.__init__(self, parent=None) self.login = {'email': login, 'password': password} self.socketIO = None self.running = True self.connect_to_server() def connect_to_server(self): self.socketIO = SocketIO('http://arhiweb.pl', 80, LoggingNamespace) self.socketIO.on('auth_error', self.on_auth_error) self.socketIO.on('device_error', self.on_device_error) self.socketIO.on('device_type_error', self.on_device_type_error) self.socketIO.on('attribute_error', self.on_attribute_error) self.socketIO.on('attribute_error', self.on_attribute_error) self.start() login_data = json.dumps(self.login) print(login_data) self.socketIO.emit('authorization', login_data, self.callbacks_authorization) def callbacks_authorization(self, *args): print('callbacks_authorization', args) def send(self, data): self.socketIO.emit('my other event', data) def send_device(self, device): data = json.dumps(device) print(data) self.socketIO.emit('update_devices_list', data, self.callbacks_device) def callbacks_device(self, *args): print('callbacks_device ', args) def send_update_device_value(self, data): json_data = json.dumps(data) print(json_data) self.socketIO.emit('update_device_value', json_data, self.callback_update_device_value) def callback_update_device_value(self, *args): print('callback_update_device_value ', args) def send_update_devices_status(self, data): self.socketIO.emit('update_devices_status', json.dumps(data), self.callback_update_devices_status) def callback_update_devices_status(self, *args): print('update_devices_status ', args) def on_auth_error(self, *args): print('on auth ', args) def on_device_error(self, *args): print('on device error ', args) def on_device_type_error(self, *args): print('device_type_error ', args) def on_attribute_error(self, *args): print('attribute_error ', args) def run(self): self.socketIO.wait_for_callbacks(seconds=1) def stop(self): self.running = False
socket_client.emit('generate_uuid', None) socket_client.wait(seconds=1) # test training for i in range(10): timestamp = time.time() p300 = random.choice([0, 1]) args = json.dumps({'uuid': uuid, 'timestamp': timestamp, 'left': p300}) socket_client.emit("left_right_train", args) socket_client.wait(seconds=2) # test predictions for i in range(5): timestamp = time.time() args = json.dumps({'uuid': uuid, 'timestamp': timestamp}) socket_client.emit("left_right_predict", args, print_results) socket_client.wait_for_callbacks(seconds=1) # test streaming raw data args = json.dumps({'uuid': uuid}) socket_client.emit("start_streaming_raw_data", args) socket_client.wait() # # test stop streaming raw data socket_client.emit("stop_streaming_raw_data", args) socket_client.wait(seconds=5) socket_client.disconnect()
class TestOnlineRecognition(unittest.TestCase): def setUp(self): self.socketIO = SocketIO('localhost', 8000) self.received_responses = 0 self.expected_responses = 0 time.sleep(1) def test_online_recognition(self): self.socketIO.on('result', self.assertMessageHasCorrectSchema) self.send_chunks() self.assertEquals(self.expected_responses + 2, self.received_responses) def send_chunks(self): self.socketIO.emit('begin', {'model': 'en-towninfo'}) self.socketIO.emit('change_lm', {'new_lm': 'new_lm'}) for chunk in self.chunks(): self.socketIO.emit('chunk', {'chunk': chunk, 'frame_rate': 16000}) self.socketIO.wait_for_callbacks() self.socketIO.emit('end', {}) self.socketIO.wait_for_callbacks() self.socketIO.wait_for_callbacks() self.socketIO.wait_for_callbacks() def chunks(self): basedir = os.path.dirname(os.path.realpath(__file__)) wav = wave.open("%s/../resources/test.wav" % basedir, "rb") while True: frames = wav.readframes(16384) if len(frames) == 0: break self.expected_responses += 1 yield self.frames_to_base64(frames) def frames_to_base64(self, frames): return base64.b64encode(frames) def assertMessageHasCorrectSchema(self, message): schema = { "type": "object", "properties": { "status": {"type": "number"}, "final": {"type": "boolean"}, "chunk_id": {"type": "string"}, "request_id": {"type": "string"}, "result": { "type": "object", "properties": { "hypotheses": { "type": "array", "items": { "type": "object", "properties": { "transcript": {"type": "string"}, "confidence": {"type": "number"} }, "required": ["transcript"], "additionalProperties": False, } } }, "required": ["hypotheses"], "additionalProperties": False, }, }, "required": ["status", "result", "final", "request_id"], "additionalProperties": False, } validationResult = validate(message, schema) self.assertIsNone(validationResult, msg="Message has invalid schema") self.received_responses += 1
# encoding: utf-8 ''' @author:maidou @contact:QQ4113291000 @time:2018/5/14.下午4:30 ''' from socketIO_client import SocketIO,LoggingNamespace,BaseNamespace def on_aa(*args): print 'nihao ' print args socket = SocketIO('localhost',5000) # chat = socket.define(BaseNamespace, '/') # print 'hello' # socket.emit('test','hahao') socket.once('aa', on_aa) # socket.emit('test2') # socket.send('helloo', callback=on_aa) # socket.send({'helo':'helo'}) socket.wait_for_callbacks(seconds=20) socket.wait(10) # socket.once('aa', on_aa) # import time # time.sleep(20) if __name__ == '__main__': pass