Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions Src/Authorizer.cs
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we move these old files like Authorizer.cs into folder like __Internals__?

Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public string CurrentEmotivId

public Authorizer()
{
_ctxClient.WSConnectDone += OnWSConnectDone;
_ctxClient.CortexConnectionStared += OnCortexConnectionStared;
_ctxClient.GetUserLoginDone += OnGetUserLoginDone;
_ctxClient.UserLoginNotify += OnUserLoginNotify; // inform user loggin
_ctxClient.UserLogoutNotify += OnUserLogoutNotify; // inform user log out
Expand Down Expand Up @@ -222,10 +222,10 @@ private void OnRefreshTokenOK(object sender, string cortexToken)
SaveToken(tokenInfo);

// get license information
_ctxClient.GetLicenseInfo(cortexToken);
_ctxClient.GetLicenseInfo();
}

private void OnWSConnectDone(object sender, bool isConnected)
private void OnCortexConnectionStared(object sender, bool isConnected)
{
if (isConnected) {
#if UNITY_ANDROID || UNITY_IOS
Expand Down Expand Up @@ -271,7 +271,7 @@ private void OnAuthorizedOK(object sender, string cortexToken)
Authorizer.SaveToken(tokenInfo);

// get license information
_ctxClient.GetLicenseInfo(cortexToken);
_ctxClient.GetLicenseInfo();
} else {
AuthorizedFailed(this, cortexToken);
UnityEngine.Debug.Log("Invalid Token.");
Expand Down
30 changes: 0 additions & 30 deletions Src/BCIGameItf.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,6 @@ public void LoginWithAuthenticationCode(string code)
emotivUnityItf.LoginWithAuthenticationCode(code);
}

public void AcceptEulaAndPrivacyPolicy()
{
emotivUnityItf.AcceptEulaAndPrivacyPolicy();
}

/// <summary>
/// Get detected headsets. Returns a list of detected headsets.
/// </summary>
Expand Down Expand Up @@ -173,15 +168,6 @@ public async Task ProcessCallback(string args) {
}
#endif

#if USE_EMBEDDED_LIB || UNITY_ANDROID || UNITY_IOS
/// <summary>
/// Authenticate with Emotiv. It will open system browser to login and get the authentication code.
/// </summary>
public async Task AuthenticateAsync()
{
await emotivUnityItf.AuthenticateAsync();
}
#endif

/// <summary>
/// Initialize and start the application. It should be called when the app has granted permissions: bluetooth, location, write external storage.
Expand Down Expand Up @@ -390,12 +376,6 @@ public void EraseDataForMCTrainingAction(string action = DEFAULT_MC_ACTION)
emotivUnityItf.EraseMCTraining(action);
}

// logout
public void Logout()
{
emotivUnityItf.Logout();
}

/// <summary>
/// Query the dates having consumer data within a specified date range.
/// </summary>
Expand All @@ -414,16 +394,6 @@ public void QueryDayDetailOfConsumerData(DateTime date) {
emotivUnityItf.QueryDayDetailOfConsumerData(date);
}

public void OpenURL(string url)
{
emotivUnityItf.OpenURL(url);
}

public bool IsWebViewOpened()
{
return emotivUnityItf.IsWebViewOpened;
}

public string LoadedProfilePlayer() {
return emotivUnityItf.LoadedProfileName;
}
Expand Down
44 changes: 37 additions & 7 deletions Src/CortexClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
using System;
using System.Threading;
using Newtonsoft.Json.Linq;
using Emotiv.Cortex.Models;

using System.Collections.Generic;
using System.Collections;
Expand All @@ -49,7 +50,7 @@ public abstract class CortexClient
public AutoResetEvent m_MessageReceiveEvent = new AutoResetEvent(false);
public AutoResetEvent m_OpenedEvent = new AutoResetEvent(false);

public event EventHandler<bool> WSConnectDone;
public event EventHandler<bool> CortexConnectionStared;
public event EventHandler<ErrorMsgEventArgs> ErrorMsgReceived;
public event EventHandler<StreamDataEventArgs> StreamDataReceived;
public event EventHandler<List<Headset>> QueryHeadsetOK;
Expand All @@ -61,11 +62,13 @@ public abstract class CortexClient
public event EventHandler<bool> AccessRightGrantedDone;
public event EventHandler<string> AuthorizeOK;
public event EventHandler<UserDataInfo> GetUserLoginDone;
public event EventHandler<UserDataInfo> LoginDone;
public event EventHandler<string> EULAAccepted;
public event EventHandler<string> EULANotAccepted; // return cortexToken if user has not accept eula to proceed next step
public event EventHandler<string> UserLoginNotify;
public event EventHandler<string> UserLogoutNotify;
public event EventHandler<License> GetLicenseInfoDone;
public event EventHandler<(CortexErrorCode error, License data)> GetLicenseInfoResult;
public event EventHandler<SessionEventArgs> CreateSessionOK;
public event EventHandler<SessionEventArgs> UpdateSessionOK;
public event EventHandler<MultipleResultEventArgs> SubscribeDataDone;
Expand Down Expand Up @@ -103,6 +106,7 @@ public abstract class CortexClient
public event EventHandler<List<MentalStateModel>> QueryDayDetailOfConsumerDataDone;
public event EventHandler<MultipleResultEventArgs> ExportRecordsFinished;
public event EventHandler<string> DataPostProcessingFinished;
public string CurrentCortexToken { get; internal set; } = string.Empty;

public virtual void Init(object context = null) {}

Expand Down Expand Up @@ -148,9 +152,9 @@ protected string PrepareRequest(string method, JObject param, bool hasParam = tr
return request.ToString();
}

public void OnWSConnected(bool isConnected)
public void OnCortexConnectionStared(bool isConnected)
{
WSConnectDone(this, isConnected);
CortexConnectionStared(this, isConnected);
}

/// <summary>
Expand Down Expand Up @@ -185,6 +189,11 @@ public void OnMessageReceived(string receievedMsg)
UnityEngine.Debug.Log("An error received: " + messageError);
//Send Error message event
ErrorMsgReceived(this, new ErrorMsgEventArgs(code, messageError, method));

if (method == "getLicenseInfo")
{
GetLicenseInfoResult?.Invoke(this, (CortexErrorCode.UnknownError, null));
}

} else {
// handle response
Expand Down Expand Up @@ -302,11 +311,12 @@ private void HandleResponse(string method, JToken data)
loginData.EmotivId = data["username"].ToString();
String message = data["message"].ToString();
UnityEngine.Debug.Log("login message: " + message);
GetUserLoginDone(this, loginData);
LoginDone(this, loginData);
}
else if (method == "logout")
{
String message = data["message"].ToString();
CurrentCortexToken = string.Empty;
UserLogoutNotify(this, message);
// get user login info
GetUserLogin();
Expand All @@ -324,12 +334,14 @@ private void HandleResponse(string method, JToken data)
else if (method == "generateNewToken")
{
string cortexToken = data["cortexToken"].ToString();
CurrentCortexToken = cortexToken;
RefreshTokenOK(this, cortexToken);
}
else if (method == "getLicenseInfo")
{
License lic = new License(data["license"]);
GetLicenseInfoDone(this, lic);
// GetLicenseInfoDone(this, lic);
GetLicenseInfoResult?.Invoke(this, (CortexErrorCode.OK, lic));
}
else if (method == "getUserInformation")
{
Expand All @@ -338,6 +350,7 @@ private void HandleResponse(string method, JToken data)
else if (method == "authorize")
{
string token = (string)data["cortexToken"];
CurrentCortexToken = token;
if (data["warning"] != null)
{
JObject warning = (JObject)data["warning"];
Expand Down Expand Up @@ -658,6 +671,16 @@ public void LoginWithAuthenticationCode(string code)
}

// accept eula
public void AcceptEulaAndPrivacyPolicy()
{
if (string.IsNullOrEmpty(CurrentCortexToken))
{
UnityEngine.Debug.LogWarning("AcceptEulaAndPrivacyPolicy requested but no cortex token is available.");
return;
}
AcceptEulaAndPrivacyPolicy(CurrentCortexToken);
}

public void AcceptEulaAndPrivacyPolicy(string cortexToken)
{
JObject param = new JObject(
Expand All @@ -667,10 +690,17 @@ public void AcceptEulaAndPrivacyPolicy(string cortexToken)
}

// get license information
public void GetLicenseInfo(string cortexToken)
public void GetLicenseInfo()
{
if (string.IsNullOrEmpty(CurrentCortexToken))
{
UnityEngine.Debug.LogWarning("GetLicenseInfo requested but no cortex token is available.");
GetLicenseInfoResult?.Invoke(this, (CortexErrorCode.UnknownError, null));
return;
}

JObject param = new JObject(
new JProperty("cortexToken", cortexToken)
new JProperty("cortexToken", CurrentCortexToken)
);
SendTextMessage(param, "getLicenseInfo", true);
}
Expand Down
6 changes: 3 additions & 3 deletions Src/EmbeddedCortexClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public class CortexLibInterfaceProxy : AndroidJavaProxy
{
public CortexLibInterfaceProxy() : base("com.emotiv.unityplugin.CortexConnectionInterface") { }
void onReceivedMessage(String msg) => EmbeddedCortexClient.Instance.OnMessageReceived(msg);
void onCortexStarted() { Debug.Log("Cortex Lib Started"); EmbeddedCortexClient.Instance.OnWSConnected(true); }
void onCortexStarted() { Debug.Log("Cortex Lib Started"); EmbeddedCortexClient.Instance.OnCortexConnectionStared(true); }
}
public class CortexLogHandler : AndroidJavaProxy
{
Expand Down Expand Up @@ -75,7 +75,7 @@ public class CortexIOSHandler
[AOT.MonoPInvokeCallback(typeof(MessageCallback))]
private static void OnMessageReceived(string message) => EmbeddedCortexClient.Instance.OnMessageReceived(message);
[AOT.MonoPInvokeCallback(typeof(StartedCallback))]
private static void OnCortexLibIosStarted() { Debug.Log("OnCortexLibIosStarted"); EmbeddedCortexClient.Instance.OnWSConnected(true); }
private static void OnCortexLibIosStarted() { Debug.Log("OnCortexLibIosStarted"); EmbeddedCortexClient.Instance.OnCortexConnectionStared(true); }
public static void RegisterCallback() { RegisterUnityResponseCallback(OnMessageReceived); RegisterUnityStartedCallback(OnCortexLibIosStarted); }
}
#endif
Expand Down Expand Up @@ -124,7 +124,7 @@ public override void Init(object context = null)
}

private static void OnMessageReceivedStatic(string message) => EmbeddedCortexClient.Instance.OnMessageReceived(message);
private static void OnWSConnectedStatic() => EmbeddedCortexClient.Instance.OnWSConnected(true);
private static void OnCortexConnectionStaredStatic() => EmbeddedCortexClient.Instance.OnCortexConnectionStared(true);

#if USE_EMBEDDED_LIB
private void CortexStarted(object? sender, bool e)
Expand Down
Loading