Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 3ab2a15

Browse files
authoredNov 14, 2024··
COR-5444: update csharp examples to handle headset warnings and impro… (#220)
* COR-5444: update csharp examples to handle headset warnings and improve examples * update rework
1 parent f24ebc2 commit 3ab2a15

File tree

17 files changed

+540
-320
lines changed

17 files changed

+540
-320
lines changed
 

‎csharp/BandPowerLogger/Program.cs

+4-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,10 @@ namespace BandPowerLogger
1111
{
1212
class Program
1313
{
14+
// init constants before running
1415
const string OutFilePath = @"BandPowerLogger.csv";
16+
const string WantedHeadsetId = ""; // if you want to connect to specific headset, put headset id here. For example: "EPOCX-71D833AC"
17+
1518
private static FileStream OutFileStream;
1619

1720
static void Main(string[] args)
@@ -31,7 +34,7 @@ static void Main(string[] args)
3134
dse.AddStreams("pow");
3235
dse.OnSubscribed += SubscribedOK;
3336
dse.OnBandPowerDataReceived += OnBandPowerOK;
34-
dse.Start();
37+
dse.Start("", false, WantedHeadsetId);
3538

3639
Console.WriteLine("Press Esc to flush data to file and exit");
3740
while (Console.ReadKey().Key != ConsoleKey.Escape) { }

‎csharp/CortexAccess/Config.cs

+4
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,10 @@ public static class WarningCode
3838
public const int UserLoginOnAnotherOsUser = 16;
3939
public const int EULAAccepted = 17;
4040
public const int StreamWritingClosed = 18;
41+
public const int HeadsetWrongInformation = 100;
42+
public const int HeadsetCannotConnected = 101;
43+
public const int HeadsetConnectingTimeout = 102;
44+
public const int HeadsetDataTimeOut = 103;
4145
public const int HeadsetConnected = 104;
4246
public const int HeadsetScanFinished = 142;
4347
}

‎csharp/CortexAccess/CortexAccess.csproj

+1
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@
8282
<Compile Include="HeadsetFinder.cs" />
8383
<Compile Include="Properties\AssemblyInfo.cs" />
8484
<Compile Include="Record.cs" />
85+
<Compile Include="RecordManager.cs" />
8586
<Compile Include="SessionCreator.cs" />
8687
<Compile Include="Training.cs" />
8788
<Compile Include="Utils.cs" />

‎csharp/CortexAccess/CtxClient.cs

+50-27
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,20 @@ public ErrorMsgEventArgs(int code, string messageError)
6868
public string MessageError { get; set; }
6969
}
7070

71+
// event to inform about headset connect
72+
public class HeadsetConnectEventArgs
73+
{
74+
public HeadsetConnectEventArgs(bool isSuccess, string message, string headsetId)
75+
{
76+
IsSuccess = isSuccess;
77+
Message = message;
78+
HeadsetId = headsetId;
79+
}
80+
public bool IsSuccess { get; set; }
81+
public string Message { get; set; }
82+
public string HeadsetId { get; set; }
83+
}
84+
7185
public sealed class CortexClient
7286
{
7387
const string Url = "wss://localhost:6868";
@@ -86,8 +100,6 @@ public sealed class CortexClient
86100
public event EventHandler<ErrorMsgEventArgs> OnErrorMsgReceived;
87101
public event EventHandler<StreamDataEventArgs> OnStreamDataReceived;
88102
public event EventHandler<List<Headset>> OnQueryHeadset;
89-
public event EventHandler<string> OnHeadsetConnected;
90-
public event EventHandler<bool> OnHeadsetDisConnected;
91103
public event EventHandler<bool> OnHasAccessRight;
92104
public event EventHandler<bool> OnRequestAccessDone;
93105
public event EventHandler<bool> OnAccessRightGranted;
@@ -118,6 +130,8 @@ public sealed class CortexClient
118130
public event EventHandler<JArray> OnQueryProfile;
119131
public event EventHandler<double> OnGetTrainingTime;
120132
public event EventHandler<JObject> OnTraining;
133+
public event EventHandler<string> SessionClosedNotify;
134+
public event EventHandler<HeadsetConnectEventArgs> HeadsetConnectNotify;
121135
public event EventHandler<string> HeadsetScanFinished;
122136

123137
// Constructor
@@ -250,29 +264,7 @@ private void HandleResponse(string method, JToken data)
250264
else if (method == "controlDevice")
251265
{
252266
string command = (string)data["command"];
253-
if (command == "connect")
254-
{
255-
string message = (string)data["message"];
256-
string headsetId = "";
257-
258-
Console.WriteLine("ConnectHeadset " + message);
259-
if (message.Contains("Start connecting to device"))
260-
{
261-
//"Start connecting to device " + headsetId
262-
headsetId = message.Substring(27);
263-
}
264-
else if (message.Contains("The device"))
265-
{
266-
//"The device " + headsetId + " has been connected or is connecting";
267-
string tmp = message.Replace(" has been connected or is connecting", "");
268-
headsetId = tmp.Substring(11);
269-
}
270-
OnHeadsetConnected(this, headsetId);
271-
}
272-
else if (command == "disconnect")
273-
{
274-
OnHeadsetDisConnected(this, true);
275-
}
267+
Console.WriteLine("controlDevice response for command " + command);
276268
}
277269
else if (method == "getUserLogin")
278270
{
@@ -468,6 +460,36 @@ private void HandleWarning(int code, JToken messageData)
468460
string message = messageData["behavior"].ToString();
469461
HeadsetScanFinished(this, message);
470462
}
463+
else if (code == WarningCode.StreamStop)
464+
{
465+
string sessionId = messageData["sessionId"].ToString();
466+
SessionClosedNotify(this, sessionId);
467+
}
468+
else if (code == WarningCode.SessionAutoClosed)
469+
{
470+
string sessionId = messageData["sessionId"].ToString();
471+
SessionClosedNotify(this, sessionId);
472+
}
473+
else if (code == WarningCode.HeadsetConnected)
474+
{
475+
string headsetId = messageData["headsetId"].ToString();
476+
string message = messageData["behavior"].ToString();
477+
Console.WriteLine("handleWarning:" + message);
478+
HeadsetConnectNotify(this, new HeadsetConnectEventArgs(true, message, headsetId));
479+
}
480+
else if (code == WarningCode.HeadsetWrongInformation ||
481+
code == WarningCode.HeadsetCannotConnected ||
482+
code == WarningCode.HeadsetConnectingTimeout)
483+
{
484+
string headsetId = messageData["headsetId"].ToString();
485+
string message = messageData["behavior"].ToString();
486+
HeadsetConnectNotify(this, new HeadsetConnectEventArgs(false, message, headsetId));
487+
}
488+
else if (code == WarningCode.CortexAutoUnloadProfile)
489+
{
490+
// the current profile is unloaded automatically
491+
OnUnloadProfile(this, true);
492+
}
471493

472494
}
473495
private void WebSocketClient_Closed(object sender, EventArgs e)
@@ -580,6 +602,7 @@ public void QueryHeadsets(string headsetId)
580602
// mappings is required if connect to epoc flex
581603
public void ControlDevice(string command, string headsetId, JObject mappings)
582604
{
605+
Console.WriteLine("ControlDevice " + command + " headsetId:" + headsetId);
583606
JObject param = new JObject();
584607
param.Add("command", command);
585608
if (!String.IsNullOrEmpty(headsetId))
@@ -622,7 +645,7 @@ public void UpdateSession(string cortexToken, string sessionId, string status)
622645
// CreateRecord
623646
// Required params: session, title, cortexToken
624647
public void CreateRecord(string cortexToken, string sessionId, string title,
625-
JToken description = null, JToken subjectName = null, JToken tags= null)
648+
JToken description = null, JToken subjectName = null, List<string> tags = null)
626649
{
627650
JObject param = new JObject();
628651
param.Add("session", sessionId);
@@ -638,7 +661,7 @@ public void CreateRecord(string cortexToken, string sessionId, string title,
638661
}
639662
if (tags != null)
640663
{
641-
param.Add("tags", tags);
664+
param.Add("tags", JArray.FromObject(tags));
642665
}
643666
SendTextMessage(param, "createRecord", true);
644667
}

‎csharp/CortexAccess/DataStreamExample.cs

+15-5
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ public class DataStreamExample
1616
private HeadsetFinder _headsetFinder;
1717
private Authorizer _authorizer;
1818
private SessionCreator _sessionCreator;
19+
private string _wantedHeadsetId;
1920

2021
public List<string> Streams
2122
{
@@ -63,6 +64,7 @@ public DataStreamExample() {
6364
_ctxClient.OnStreamDataReceived += StreamDataReceived;
6465
_ctxClient.OnSubscribeData += SubscribeDataOK;
6566
_ctxClient.OnUnSubscribeData += UnSubscribeDataOK;
67+
_ctxClient.SessionClosedNotify += SessionClosedOK;
6668

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

7375
private void SessionClosedOK(object sender, string sessionId)
7476
{
75-
Console.WriteLine("The Session " + sessionId + " has closed successfully.");
77+
if (sessionId == _sessionId)
78+
{
79+
Console.WriteLine("The Session " + sessionId + " has closed successfully.");
80+
_sessionId = "";
81+
_headsetFinder.HasHeadsetConnected = false;
82+
}
7683
}
7784

7885
private void UnSubscribeDataOK(object sender, MultipleResultEventArgs e)
@@ -126,14 +133,16 @@ private void SubscribeDataOK(object sender, MultipleResultEventArgs e)
126133

127134
private void SessionCreatedOk(object sender, string sessionId)
128135
{
129-
// subscribe
130136
_sessionId = sessionId;
137+
// subscribe
138+
string streamsString = string.Join(", ", Streams);
139+
Console.WriteLine("Session is created successfully. Subscribe for Streams: " + streamsString);
131140
_ctxClient.Subscribe(_cortexToken, _sessionId, Streams);
132141
}
133142

134143
private void HeadsetConnectedOK(object sender, string headsetId)
135144
{
136-
//Console.WriteLine("HeadsetConnectedOK " + headsetId);
145+
Console.WriteLine("HeadsetConnectedOK " + headsetId);
137146
// Wait a moment before creating session
138147
System.Threading.Thread.Sleep(1500);
139148
// CreateSession
@@ -152,7 +161,7 @@ private void AuthorizedOK(object sender, string cortexToken)
152161
_headsetFinder.ScanHeadsets();
153162
}
154163
// find headset
155-
_headsetFinder.FindHeadset();
164+
_headsetFinder.FindHeadset(_wantedHeadsetId);
156165
}
157166
}
158167

@@ -194,8 +203,9 @@ public void AddStreams(string stream)
194203
}
195204
}
196205
// start
197-
public void Start(string licenseID="", bool activeSession = false)
206+
public void Start(string licenseID="", bool activeSession = false, string wantedHeadsetId = "")
198207
{
208+
_wantedHeadsetId = wantedHeadsetId;
199209
_isActiveSession = activeSession;
200210
_authorizer.Start(licenseID);
201211
}
There was a problem loading the remainder of the diff.

0 commit comments

Comments
 (0)
Please sign in to comment.