Skip to content

Commit

Permalink
updaet scanning flow for csharp (#215)
Browse files Browse the repository at this point in the history
  • Loading branch information
tungntEmotiv authored Nov 15, 2023
1 parent 03d1948 commit 4228537
Show file tree
Hide file tree
Showing 8 changed files with 61 additions and 12 deletions.
1 change: 1 addition & 0 deletions csharp/CortexAccess/Config.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,5 +39,6 @@ public static class WarningCode
public const int EULAAccepted = 17;
public const int StreamWritingClosed = 18;
public const int HeadsetConnected = 104;
public const int HeadsetScanFinished = 142;
}
}
27 changes: 15 additions & 12 deletions csharp/CortexAccess/CtxClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ public sealed class CortexClient
public event EventHandler<JArray> OnQueryProfile;
public event EventHandler<double> OnGetTrainingTime;
public event EventHandler<JObject> OnTraining;
public event EventHandler<string> HeadsetScanFinished;

// Constructor
static CortexClient()
Expand All @@ -128,6 +129,9 @@ private CortexClient()
{
_nextRequestId = 1;
_wSC = new WebSocket(Url);
// Since Emotiv Cortex 3.7.0, the supported SSL Protocol will be TLS1.2 or later
_wSC.Security.EnabledSslProtocols = System.Security.Authentication.SslProtocols.Tls12;

_methodForRequestId = new Dictionary<int, string>();
_wSC.Opened += new EventHandler(WebSocketClient_Opened);

Expand Down Expand Up @@ -220,21 +224,13 @@ private void WebSocketClient_MessageReceived(object sender, MessageReceivedEvent
else if (response["warning"] != null)
{
JObject warning = (JObject)response["warning"];
string messageWarning = "";
int code = -1;
if (warning["code"] != null)
{
code = (int)warning["code"];
}
if (warning["message"].Type == JTokenType.String)
{
messageWarning = warning["message"].ToString();
}
else if (warning["message"].Type == JTokenType.Object)
{
Console.WriteLine("Received Warning Object");
}
HandleWarning(code, messageWarning);
JToken messageData = warning["message"];
HandleWarning(code, messageData);
}
}
// handle Response
Expand Down Expand Up @@ -441,9 +437,9 @@ private void HandleResponse(string method, JToken data)
}

// handle warning response
private void HandleWarning(int code, string message)
private void HandleWarning(int code, JToken messageData)
{
Console.WriteLine("handleWarning: " + code + " message: " + message);
Console.WriteLine("handleWarning: " + code);
if (code == WarningCode.AccessRightGranted)
{
// granted access right
Expand All @@ -459,12 +455,19 @@ private void HandleWarning(int code, string message)
}
else if (code == WarningCode.UserLogin)
{
string message = messageData.ToString();
OnUserLogin(this, message);
}
else if (code == WarningCode.UserLogout)
{
string message = messageData.ToString();
OnUserLogout(this, message);
}
else if (code == WarningCode.HeadsetScanFinished)
{
string message = messageData["behavior"].ToString();
HeadsetScanFinished(this, message);
}

}
private void WebSocketClient_Closed(object sender, EventArgs e)
Expand Down
6 changes: 6 additions & 0 deletions csharp/CortexAccess/DataStreamExample.cs
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,12 @@ private void AuthorizedOK(object sender, string cortexToken)
if (!String.IsNullOrEmpty(cortexToken))
{
_cortexToken = cortexToken;
if (!_headsetFinder.IsHeadsetScanning)
{
// Start scanning headset. It will call one time whole program.
// If you want re-scan, please check IsHeadsetScanning and call ScanHeadsets() again
_headsetFinder.ScanHeadsets();
}
// find headset
_headsetFinder.FindHeadset();
}
Expand Down
20 changes: 20 additions & 0 deletions csharp/CortexAccess/HeadsetFinder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,15 @@ public class HeadsetFinder
private Timer _aTimer;

private bool _isFoundHeadset;
private bool _isHeadsetScanning = false;

// Event
public event EventHandler<string> OnHeadsetConnected;
public event EventHandler<bool> OnHeadsetDisConnected;


public bool IsHeadsetScanning { get => _isHeadsetScanning; }

public HeadsetFinder()
{
_ctxClient = CortexClient.Instance;
Expand All @@ -26,13 +30,19 @@ public HeadsetFinder()
_ctxClient.OnQueryHeadset += QueryHeadsetOK;
_ctxClient.OnHeadsetConnected += HeadsetConnectedOK;
_ctxClient.OnHeadsetDisConnected += HeadsetDisconnectedOK;
_ctxClient.HeadsetScanFinished += OnHeadsetScanFinished;
}

private void HeadsetDisconnectedOK(object sender, bool e)
{
_headsetId = "";
OnHeadsetDisConnected(this, true);
}
private void OnHeadsetScanFinished(object sender, string message)
{
_isHeadsetScanning = false;
Console.WriteLine(message);
}

private void HeadsetConnectedOK(object sender, string headsetId)
{
Expand Down Expand Up @@ -99,6 +109,16 @@ public void FindHeadset()
}
}

/// <summary>
/// ScanHeadsets to trigger scan headsets from Cortex
/// </summary>
public void ScanHeadsets()
{
Console.WriteLine("Start scanning headset.");
_isHeadsetScanning = true;
_ctxClient.ControlDevice("refresh", "", new JObject());
}

// Create Timer for headset finding
private void SetTimer()
{
Expand Down
6 changes: 6 additions & 0 deletions csharp/CortexAccess/Training.cs
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,12 @@ private void QueryProfileOK(object sender, JArray profiles)
string name = (string)ele["name"];
_profileLists.Add(name);
}
if (!_headsetFinder.IsHeadsetScanning)
{
// Start scanning headset. It will call one time whole program.
// If you want re-scan, please check IsHeadsetScanning and call ScanHeadsets() again
_headsetFinder.ScanHeadsets();
}
// find headset
_headsetFinder.FindHeadset();
}
Expand Down
6 changes: 6 additions & 0 deletions csharp/InjectMarker/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,12 @@ private static void AuthorizedOK(object sender, string cortexToken)
if (!String.IsNullOrEmpty(cortexToken))
{
_cortexToken = cortexToken;
if (!_headsetFinder.IsHeadsetScanning)
{
// Start scanning headset. It will call one time whole program.
// If you want re-scan, please check IsHeadsetScanning and call ScanHeadsets() again
_headsetFinder.ScanHeadsets();
}
// find headset
_headsetFinder.FindHeadset();
}
Expand Down
1 change: 1 addition & 0 deletions csharp/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ This section describe structure overview, core classes and examples. The C# Cort
* You must login and logout via EMOTIV Launcher.
* You must use EMOTIV Launcher to grant AccessRight for the App one time for one emotiv user.
* You need a valid license to subscribe EEG data, Performance metrics data.
* From Emotiv Cortex 3.7, you need to call ScanHeadsets() at HeadsetFinder.cs to start headset scanning. Otherwise your headsets might not appeared in the headset list return from queryHeadsets(). If IsHeadsetScanning = false, you need re-call the ScanHeadsets() if want to re-scan headsets again.
* The Examples are only demo for some Apis not all apis and to be continue updating.


Expand Down
6 changes: 6 additions & 0 deletions csharp/RecordData/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,12 @@ private static void AuthorizedOK(object sender, string cortexToken)
if (!String.IsNullOrEmpty(cortexToken))
{
_cortexToken = cortexToken;
if (!_headsetFinder.IsHeadsetScanning)
{
// Start scanning headset. It will call one time whole program.
// If you want re-scan, please check IsHeadsetScanning and call ScanHeadsets() again
_headsetFinder.ScanHeadsets();
}
// find headset
_headsetFinder.FindHeadset();
}
Expand Down

0 comments on commit 4228537

Please sign in to comment.