Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import asyncio
- import websockets
- import json
- # Funzione test
- async def Test():
- async with websockets.connect('wss://localhost:6868') as ws:
- await ws.send('{"id": 1,"jsonrpc": "2.0","method": "getCortexInfo"}')
- response = await ws.recv()
- print(response)
- response = json.loads(response)
- print(response["result"]["buildDate"])
- async def Main():
- # Connessione al server WebSocket
- async with websockets.connect('wss://localhost:6868') as ws:
- # Invio domanda CortexToken
- await ws.send('''
- {
- "id": 1,
- "jsonrpc": "2.0",
- "method": "authorize",
- "params": {
- "clientId": "IwPGat6RobU0s9vumEEKjRMEjzgkFiGr7HxLGnWt",
- "clientSecret": "nhUxzMH4Pg2WHerDcg22mnj4AaPgDaX4hrJTv1W4OTk9WYycVYlmmyGde02BwY6VAOoc7f8akMEMFwAZQUJwP4OH5iJTCd6vkEIaD7JfwHbQ4LrObSog2Hp5il3QRzQ2"
- }
- }
- ''')
- # Ricevo la risposta
- response = await ws.recv()
- # Trasformo il JSON in un dizionario
- response = json.loads(response)
- # Token = CortexToken
- Token = response['result']['cortexToken']
- # Invio domanda per l'id la creazione della sessione
- await ws.send('''{
- "id": 1,
- "jsonrpc": "2.0",
- "method": "createSession",
- "params": {
- "cortexToken": "''' + str(Token) + '''",
- "headset": "EPOCPLUS-389AD52C",
- "status": "open"
- }
- }
- ''')
- # Ricevo la risposta
- response = await ws.recv()
- # Trasformo il JSON in un dizionario
- response = json.loads(response)
- # Controllo la connessione al caschetto
- try:
- # Session = ID sessione
- Session = response["result"]["id"]
- except:
- # Segnalazione errore
- print("Caschetto scollegato")
- # Termina l'esecuzione del programma
- exit()
- # Sottoscrizione ad un determinato canale di dati ("streams": ["sys"]")
- await ws.send('''{
- "id": 1,
- "jsonrpc": "2.0",
- "method": "subscribe",
- "params": {
- "cortexToken": "''' + str(Token) + '''",
- "session": "''' + str(Session) + '''",
- "streams": ["com", "sys"]
- }
- }
- ''')
- # Ricevo la risposta
- response = await ws.recv()
- # Trasformo il JSON in un dizionario
- response = json.loads(response)
- # Controllo della risposta
- print(response) # Dati nel canale com e sys
- # Per continuare:
- # Attivare il server locale da websocket.org
- # Caricare il profilo di Giosuè (Prima della sottoscrizione degli stream com e sys)
- # Prelevare i dati dallo stream sys e com (Dopo la loro sottoscrizione)
- # Utilizzare i dati prelevati da response seguendo la doc. (advanced BCI - Mental command action sensitivy)
- #Link:
- # Caricamento di un profilo: https://emotiv.gitbook.io/cortex-api/bci/setupprofile
- # Controlli mentali: https://emotiv.gitbook.io/cortex-api/advanced-bci/mentalcommandactionsensitivity
- # Sottoscrizione dei dati: https://emotiv.gitbook.io/cortex-api/data-subscription
- # BCI (Brain Computer Interface): https://emotiv.gitbook.io/cortex-api/bci
- # Esempio - asyncio.get_event_loop().run_until_complete(Test())
- asyncio.get_event_loop().run_until_complete(Main())
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement