Advertisement
Guest User

Caschetto

a guest
Feb 17th, 2020
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.98 KB | None | 0 0
  1. import asyncio
  2. import websockets
  3. import json
  4.  
  5.  
  6. # Funzione test
  7. async def Test():
  8.     async with websockets.connect('wss://localhost:6868') as ws:
  9.         await ws.send('{"id": 1,"jsonrpc": "2.0","method": "getCortexInfo"}')
  10.         response = await ws.recv()
  11.         print(response)
  12.         response = json.loads(response)
  13.  
  14.         print(response["result"]["buildDate"])
  15.  
  16.  
  17. async def Main():
  18.     # Connessione al server WebSocket
  19.     async with websockets.connect('wss://localhost:6868') as ws:
  20.         # Invio domanda CortexToken
  21.         await ws.send('''
  22.                    {
  23.                        "id": 1,
  24.                        "jsonrpc": "2.0",
  25.                        "method": "authorize",
  26.                        "params": {
  27.                            "clientId": "IwPGat6RobU0s9vumEEKjRMEjzgkFiGr7HxLGnWt",
  28.                            "clientSecret": "nhUxzMH4Pg2WHerDcg22mnj4AaPgDaX4hrJTv1W4OTk9WYycVYlmmyGde02BwY6VAOoc7f8akMEMFwAZQUJwP4OH5iJTCd6vkEIaD7JfwHbQ4LrObSog2Hp5il3QRzQ2"
  29.                        }
  30.                    }
  31.                    ''')
  32.  
  33.         # Ricevo la risposta
  34.         response = await ws.recv()
  35.         # Trasformo il JSON in un dizionario
  36.         response = json.loads(response)
  37.         # Token = CortexToken
  38.         Token = response['result']['cortexToken']
  39.  
  40.         # Invio domanda per l'id la creazione della sessione
  41.         await ws.send('''{
  42.                            "id": 1,
  43.                            "jsonrpc": "2.0",
  44.                            "method": "createSession",
  45.                            "params": {
  46.                                "cortexToken": "''' + str(Token) + '''",
  47.                                "headset": "EPOCPLUS-389AD52C",
  48.                                "status": "open"
  49.                            }
  50.                        }
  51.                        ''')
  52.  
  53.         # Ricevo la risposta
  54.         response = await ws.recv()
  55.         # Trasformo il JSON in un dizionario
  56.         response = json.loads(response)
  57.  
  58.         # Controllo la connessione al caschetto
  59.         try:
  60.             # Session = ID sessione
  61.             Session = response["result"]["id"]
  62.         except:
  63.             # Segnalazione errore
  64.             print("Caschetto scollegato")
  65.             # Termina l'esecuzione del programma
  66.             exit()
  67.  
  68.         # Sottoscrizione ad un determinato canale di dati ("streams": ["sys"]")
  69.         await ws.send('''{
  70.                            "id": 1,
  71.                            "jsonrpc": "2.0",
  72.                            "method": "subscribe",
  73.                            "params": {
  74.                                "cortexToken": "''' + str(Token) + '''",
  75.                                "session": "''' + str(Session) + '''",
  76.                                "streams": ["com", "sys"]
  77.                            }
  78.                        }
  79.                        ''')
  80.  
  81.         # Ricevo la risposta
  82.         response = await ws.recv()
  83.         # Trasformo il JSON in un dizionario
  84.         response = json.loads(response)
  85.         # Controllo della risposta
  86.         print(response) # Dati nel canale com e sys
  87.  
  88.         # Per continuare:
  89.         # Attivare il server locale da websocket.org
  90.         # Caricare il profilo di Giosuè (Prima della sottoscrizione degli stream com e sys)
  91.         # Prelevare i dati dallo stream sys e com (Dopo la loro sottoscrizione)
  92.         # Utilizzare i dati prelevati da response seguendo la doc. (advanced BCI - Mental command action sensitivy)
  93.  
  94.         #Link:
  95.         # Caricamento di un profilo: https://emotiv.gitbook.io/cortex-api/bci/setupprofile
  96.         # Controlli mentali: https://emotiv.gitbook.io/cortex-api/advanced-bci/mentalcommandactionsensitivity
  97.         # Sottoscrizione dei dati: https://emotiv.gitbook.io/cortex-api/data-subscription
  98.         # BCI (Brain Computer Interface): https://emotiv.gitbook.io/cortex-api/bci
  99.  
  100.  
  101. # Esempio - asyncio.get_event_loop().run_until_complete(Test())
  102.  
  103. asyncio.get_event_loop().run_until_complete(Main())
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement