예제 #1
0
    /// <summary>
    /// This thread creates a connection with the cortex service through the websocket and then calls the login, createSession and subscribe methods.
    /// </summary>
    private IEnumerator Initialize()
    {
        w = new WebSocket(new Uri(EMOTIV_URL));
        yield return(StartCoroutine(w.Connect()));

        StartCoroutine(GetReply());

        Dictionary <string, string> loginDictionary = new Dictionary <string, string>();

        loginDictionary.Add("username", username);
        loginDictionary.Add("password", userPassword);
        loginDictionary.Add("client_id", CLIENT_ID);
        loginDictionary.Add("client_secret", CLIENT_SECRET);
        w.SendString(
            CortexJsonUtility.GetMethodJSON
            (
                "login",
                (int)MethodsID.Login,
                loginDictionary
            ));

        Authorize();
        StartCoroutine(CreateSession());
        StartCoroutine(SubscribeStreams());
    }
예제 #2
0
 /// <summary>
 /// Calls a training method with some given parameters.
 /// </summary>
 /// <param name="parameters"></param>
 public void SendTrainMessage(Dictionary <string, string> parameters)
 {
     w.SendString(
         CortexJsonUtility.GetMethodJSON
         (
             "training",
             (int)MethodsID.Training,
             parameters
         ));
 }
예제 #3
0
    /// <summary>
    /// Calls the logout method
    /// </summary>
    public void Logout()
    {
        Dictionary <string, string> logoutDictionary = new Dictionary <string, string>();

        logoutDictionary.Add("username", username);
        w.SendString(
            CortexJsonUtility.GetMethodJSON
            (
                "logout",
                (int)MethodsID.Logout,
                logoutDictionary
            ));
    }
예제 #4
0
 /// <summary>
 /// Waits until the _auth token is gathered, then calls the subscribe method with the "sys" and "com" streams as parameters.
 /// </summary>
 IEnumerator SubscribeStreams()
 {
     while (_auth == null)
     {
         yield return(0);
     }
     w.SendString(
         CortexJsonUtility.GetSuscribtionJson
         (
             (int)MethodsID.Suscribe,
             _auth,
             new string[] { "sys", "com" }
         ));
 }
예제 #5
0
    /// <summary>
    /// Calls the authorize method with the corresponding client id and secret.
    /// </summary>
    void Authorize()
    {
        Dictionary <string, string> authorizeDictionary = new Dictionary <string, string>();

        authorizeDictionary.Add("client_id", CLIENT_ID);
        authorizeDictionary.Add("client_secret", CLIENT_SECRET);

        w.SendString(
            CortexJsonUtility.GetMethodJSON
            (
                "authorize",
                (int)MethodsID.Authorize,
                authorizeDictionary
            ));
    }
예제 #6
0
    /// <summary>
    /// Waits until the _auth token is gathered, then it calls the createSession method.
    /// </summary>
    IEnumerator CreateSession()
    {
        while (_auth == null)
        {
            yield return(0);
        }

        Dictionary <string, string> sessionDictionary = new Dictionary <string, string>();

        sessionDictionary.Add("_auth", _auth);
        sessionDictionary.Add("status", "open");

        w.SendString(
            CortexJsonUtility.GetMethodJSON
            (
                "createSession",
                (int)MethodsID.CreateSession,
                sessionDictionary
            ));
    }
예제 #7
0
    /// <summary>
    /// Handles the cortex responses.
    /// </summary>
    IEnumerator GetReply()
    {
        while (true)
        {
            string reply = w.RecvString();
            if (reply != null)
            {
                Debug.Log(reply);

                JSONObject replyObj = new JSONObject(reply);

                // If there´s a field named "result", it is the actual response to the sent method
                JSONObject resultObject = replyObj.GetField("result");
                if (resultObject)
                {
                    JSONObject idObject = replyObj.GetField("id");

                    switch ((MethodsID)(int)idObject.n)
                    {
                    case MethodsID.Login:
                        HandleLoginSuccess();
                        break;

                    case MethodsID.Logout:
                        HandleLogoutSuccess();
                        break;

                    case MethodsID.Authorize:
                        _auth = CortexJsonUtility.GetFieldFromJSONObject(replyObj, "_auth");
                        break;

                    case MethodsID.CreateSession:
                        trainingCanvas.SetActive(true);
                        break;
                    }
                }

                // If there´s a field named "sys", it is an event triggered from a training process
                JSONObject sysArray = replyObj.GetField("sys");
                if (sysArray)
                {
                    switch (sysArray[1].str)
                    {
                    case "MC_Succeeded":
                        isTrainingDone = true;
                        break;

                    case "MC_Completed":
                        isTrainingComplete = true;
                        break;

                    case "MC_Rejected":
                        isTrainingComplete = true;
                        break;

                    case "MC_Failed":
                        isTrainingComplete = true;
                        break;
                    }
                }

                // If there´s a field named "com", it is an event triggered from a mental command detection
                JSONObject comArray = replyObj.GetField("com");
                if (comArray)
                {
                    if (OnMentalCommandEvent != null)
                    {
                        // Call functions assigned to the OnMentalCommandEvent delegate with the action received from the Cortex as the parameter
                        OnMentalCommandEvent(comArray[0].str);
                    }
                }
            }
            yield return(0);
        }
    }