Skip to content

Commit

Permalink
COR-5444: update csharp examples to handle headset warnings and impro… (
Browse files Browse the repository at this point in the history
#220)

* COR-5444: update csharp examples to handle headset warnings and improve examples

* update rework
  • Loading branch information
tungntEmotiv authored Nov 14, 2024
1 parent f24ebc2 commit 3ab2a15
Show file tree
Hide file tree
Showing 17 changed files with 540 additions and 320 deletions.
5 changes: 4 additions & 1 deletion csharp/BandPowerLogger/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@ namespace BandPowerLogger
{
class Program
{
// init constants before running
const string OutFilePath = @"BandPowerLogger.csv";
const string WantedHeadsetId = ""; // if you want to connect to specific headset, put headset id here. For example: "EPOCX-71D833AC"

private static FileStream OutFileStream;

static void Main(string[] args)
Expand All @@ -31,7 +34,7 @@ static void Main(string[] args)
dse.AddStreams("pow");
dse.OnSubscribed += SubscribedOK;
dse.OnBandPowerDataReceived += OnBandPowerOK;
dse.Start();
dse.Start("", false, WantedHeadsetId);

Console.WriteLine("Press Esc to flush data to file and exit");
while (Console.ReadKey().Key != ConsoleKey.Escape) { }
Expand Down
4 changes: 4 additions & 0 deletions csharp/CortexAccess/Config.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ public static class WarningCode
public const int UserLoginOnAnotherOsUser = 16;
public const int EULAAccepted = 17;
public const int StreamWritingClosed = 18;
public const int HeadsetWrongInformation = 100;
public const int HeadsetCannotConnected = 101;
public const int HeadsetConnectingTimeout = 102;
public const int HeadsetDataTimeOut = 103;
public const int HeadsetConnected = 104;
public const int HeadsetScanFinished = 142;
}
Expand Down
1 change: 1 addition & 0 deletions csharp/CortexAccess/CortexAccess.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@
<Compile Include="HeadsetFinder.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Record.cs" />
<Compile Include="RecordManager.cs" />
<Compile Include="SessionCreator.cs" />
<Compile Include="Training.cs" />
<Compile Include="Utils.cs" />
Expand Down
77 changes: 50 additions & 27 deletions csharp/CortexAccess/CtxClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,20 @@ public ErrorMsgEventArgs(int code, string messageError)
public string MessageError { get; set; }
}

// event to inform about headset connect
public class HeadsetConnectEventArgs
{
public HeadsetConnectEventArgs(bool isSuccess, string message, string headsetId)
{
IsSuccess = isSuccess;
Message = message;
HeadsetId = headsetId;
}
public bool IsSuccess { get; set; }
public string Message { get; set; }
public string HeadsetId { get; set; }
}

public sealed class CortexClient
{
const string Url = "wss://localhost:6868";
Expand All @@ -86,8 +100,6 @@ public sealed class CortexClient
public event EventHandler<ErrorMsgEventArgs> OnErrorMsgReceived;
public event EventHandler<StreamDataEventArgs> OnStreamDataReceived;
public event EventHandler<List<Headset>> OnQueryHeadset;
public event EventHandler<string> OnHeadsetConnected;
public event EventHandler<bool> OnHeadsetDisConnected;
public event EventHandler<bool> OnHasAccessRight;
public event EventHandler<bool> OnRequestAccessDone;
public event EventHandler<bool> OnAccessRightGranted;
Expand Down Expand Up @@ -118,6 +130,8 @@ public sealed class CortexClient
public event EventHandler<JArray> OnQueryProfile;
public event EventHandler<double> OnGetTrainingTime;
public event EventHandler<JObject> OnTraining;
public event EventHandler<string> SessionClosedNotify;
public event EventHandler<HeadsetConnectEventArgs> HeadsetConnectNotify;
public event EventHandler<string> HeadsetScanFinished;

// Constructor
Expand Down Expand Up @@ -250,29 +264,7 @@ private void HandleResponse(string method, JToken data)
else if (method == "controlDevice")
{
string command = (string)data["command"];
if (command == "connect")
{
string message = (string)data["message"];
string headsetId = "";

Console.WriteLine("ConnectHeadset " + message);
if (message.Contains("Start connecting to device"))
{
//"Start connecting to device " + headsetId
headsetId = message.Substring(27);
}
else if (message.Contains("The device"))
{
//"The device " + headsetId + " has been connected or is connecting";
string tmp = message.Replace(" has been connected or is connecting", "");
headsetId = tmp.Substring(11);
}
OnHeadsetConnected(this, headsetId);
}
else if (command == "disconnect")
{
OnHeadsetDisConnected(this, true);
}
Console.WriteLine("controlDevice response for command " + command);
}
else if (method == "getUserLogin")
{
Expand Down Expand Up @@ -468,6 +460,36 @@ private void HandleWarning(int code, JToken messageData)
string message = messageData["behavior"].ToString();
HeadsetScanFinished(this, message);
}
else if (code == WarningCode.StreamStop)
{
string sessionId = messageData["sessionId"].ToString();
SessionClosedNotify(this, sessionId);
}
else if (code == WarningCode.SessionAutoClosed)
{
string sessionId = messageData["sessionId"].ToString();
SessionClosedNotify(this, sessionId);
}
else if (code == WarningCode.HeadsetConnected)
{
string headsetId = messageData["headsetId"].ToString();
string message = messageData["behavior"].ToString();
Console.WriteLine("handleWarning:" + message);
HeadsetConnectNotify(this, new HeadsetConnectEventArgs(true, message, headsetId));
}
else if (code == WarningCode.HeadsetWrongInformation ||
code == WarningCode.HeadsetCannotConnected ||
code == WarningCode.HeadsetConnectingTimeout)
{
string headsetId = messageData["headsetId"].ToString();
string message = messageData["behavior"].ToString();
HeadsetConnectNotify(this, new HeadsetConnectEventArgs(false, message, headsetId));
}
else if (code == WarningCode.CortexAutoUnloadProfile)
{
// the current profile is unloaded automatically
OnUnloadProfile(this, true);
}

}
private void WebSocketClient_Closed(object sender, EventArgs e)
Expand Down Expand Up @@ -580,6 +602,7 @@ public void QueryHeadsets(string headsetId)
// mappings is required if connect to epoc flex
public void ControlDevice(string command, string headsetId, JObject mappings)
{
Console.WriteLine("ControlDevice " + command + " headsetId:" + headsetId);
JObject param = new JObject();
param.Add("command", command);
if (!String.IsNullOrEmpty(headsetId))
Expand Down Expand Up @@ -622,7 +645,7 @@ public void UpdateSession(string cortexToken, string sessionId, string status)
// CreateRecord
// Required params: session, title, cortexToken
public void CreateRecord(string cortexToken, string sessionId, string title,
JToken description = null, JToken subjectName = null, JToken tags= null)
JToken description = null, JToken subjectName = null, List<string> tags = null)
{
JObject param = new JObject();
param.Add("session", sessionId);
Expand All @@ -638,7 +661,7 @@ public void CreateRecord(string cortexToken, string sessionId, string title,
}
if (tags != null)
{
param.Add("tags", tags);
param.Add("tags", JArray.FromObject(tags));
}
SendTextMessage(param, "createRecord", true);
}
Expand Down
20 changes: 15 additions & 5 deletions csharp/CortexAccess/DataStreamExample.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ public class DataStreamExample
private HeadsetFinder _headsetFinder;
private Authorizer _authorizer;
private SessionCreator _sessionCreator;
private string _wantedHeadsetId;

public List<string> Streams
{
Expand Down Expand Up @@ -63,6 +64,7 @@ public DataStreamExample() {
_ctxClient.OnStreamDataReceived += StreamDataReceived;
_ctxClient.OnSubscribeData += SubscribeDataOK;
_ctxClient.OnUnSubscribeData += UnSubscribeDataOK;
_ctxClient.SessionClosedNotify += SessionClosedOK;

_authorizer.OnAuthorized += AuthorizedOK;
_headsetFinder.OnHeadsetConnected += HeadsetConnectedOK;
Expand All @@ -72,7 +74,12 @@ public DataStreamExample() {

private void SessionClosedOK(object sender, string sessionId)
{
Console.WriteLine("The Session " + sessionId + " has closed successfully.");
if (sessionId == _sessionId)
{
Console.WriteLine("The Session " + sessionId + " has closed successfully.");
_sessionId = "";
_headsetFinder.HasHeadsetConnected = false;
}
}

private void UnSubscribeDataOK(object sender, MultipleResultEventArgs e)
Expand Down Expand Up @@ -126,14 +133,16 @@ private void SubscribeDataOK(object sender, MultipleResultEventArgs e)

private void SessionCreatedOk(object sender, string sessionId)
{
// subscribe
_sessionId = sessionId;
// subscribe
string streamsString = string.Join(", ", Streams);
Console.WriteLine("Session is created successfully. Subscribe for Streams: " + streamsString);
_ctxClient.Subscribe(_cortexToken, _sessionId, Streams);
}

private void HeadsetConnectedOK(object sender, string headsetId)
{
//Console.WriteLine("HeadsetConnectedOK " + headsetId);
Console.WriteLine("HeadsetConnectedOK " + headsetId);
// Wait a moment before creating session
System.Threading.Thread.Sleep(1500);
// CreateSession
Expand All @@ -152,7 +161,7 @@ private void AuthorizedOK(object sender, string cortexToken)
_headsetFinder.ScanHeadsets();
}
// find headset
_headsetFinder.FindHeadset();
_headsetFinder.FindHeadset(_wantedHeadsetId);
}
}

Expand Down Expand Up @@ -194,8 +203,9 @@ public void AddStreams(string stream)
}
}
// start
public void Start(string licenseID="", bool activeSession = false)
public void Start(string licenseID="", bool activeSession = false, string wantedHeadsetId = "")
{
_wantedHeadsetId = wantedHeadsetId;
_isActiveSession = activeSession;
_authorizer.Start(licenseID);
}
Expand Down
Loading

0 comments on commit 3ab2a15

Please sign in to comment.