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
28 changes: 24 additions & 4 deletions Src/CortexClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ public abstract class CortexClient
public event EventHandler<string> UserLogoutNotify;
public event EventHandler<License> GetLicenseInfoDone;
public event EventHandler<(CortexErrorCode error, License data)> GetLicenseInfoResult;
public event EventHandler<(CortexErrorCode error, string message)> RefreshHeadsetResult;
public event EventHandler<(CortexErrorCode error, List<Headset> data)> QueryHeadsetResult;
public event EventHandler<SessionEventArgs> CreateSessionOK;
public event EventHandler<SessionEventArgs> UpdateSessionOK;
public event EventHandler<MultipleResultEventArgs> SubscribeDataDone;
Expand Down Expand Up @@ -194,6 +196,14 @@ public void OnMessageReceived(string receievedMsg)
{
GetLicenseInfoResult?.Invoke(this, (CortexErrorCode.UnknownError, null));
}
else if (method == "queryHeadsets")
{
QueryHeadsetResult?.Invoke(this, (CortexErrorCode.UnknownError, null));
}
else if (method == "controlDevice")
{
RefreshHeadsetResult?.Invoke(this, (CortexErrorCode.UnknownError, messageError));
}

} else {
// handle response
Expand Down Expand Up @@ -278,7 +288,8 @@ private void HandleResponse(string method, JToken data)
foreach (JObject item in data) {
headsetLists.Add(new Headset(item));
}
QueryHeadsetOK(this, headsetLists);
// QueryHeadsetOK(this, headsetLists);
QueryHeadsetResult?.Invoke(this, (CortexErrorCode.OK, headsetLists));
}
else if (method == "controlDevice")
{
Expand All @@ -287,6 +298,10 @@ private void HandleResponse(string method, JToken data)
{
HeadsetDisConnectedOK(this, true);
}
else if (command == "refresh")
{
RefreshHeadsetResult?.Invoke(this, (CortexErrorCode.OK, command));
}
}
else if (method == "getUserLogin")
{
Expand Down Expand Up @@ -778,14 +793,19 @@ public void ControlDevice(string command, string headsetId, JObject mappings)
}

// CreateSession
// Required params: cortexToken, status
public void CreateSession(string cortexToken, string headsetId, string status)
// Required params: status
public void CreateSession(string headsetId, string status = "active")
{
JObject param = new JObject();
if (!String.IsNullOrEmpty(headsetId)) {
param.Add("headset", headsetId);
}
param.Add("cortexToken", cortexToken);
if (string.IsNullOrEmpty(CurrentCortexToken))
{
UnityEngine.Debug.LogWarning("CreateSession requested but no cortex token is available.");
return;
}
param.Add("cortexToken", CurrentCortexToken);
param.Add("status", status);
SendTextMessage(param, "createSession", true);
}
Expand Down
136 changes: 1 addition & 135 deletions Src/DataStreamManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -112,12 +112,10 @@ private void Init()
{
_dsProcess.ProcessInit();
_dsProcess.SubscribedOK += OnSubscribedOK;
_dsProcess.HeadsetConnectNotify += OnHeadsetConnectNotify;
_dsProcess.StreamStopNotify += OnStreamStopNotify;
_dsProcess.LicenseValidTo += OnLicenseValidTo;
_dsProcess.SessionActivedOK += OnSessionActivedOK;
_dsProcess.CreateSessionFail += OnCreateSessionFail;
_dsProcess.QueryHeadsetOK += OnQueryHeadsetOK;
_dsProcess.UserLogoutNotify += OnUserLogoutNotify;
_dsProcess.SessionClosedNotify += OnSessionClosedNotify;
_dsProcess.HeadsetScanFinished += OnHeadsetScanFinished;
Expand Down Expand Up @@ -174,8 +172,6 @@ private void OnSessionClosedNotify(object sender, string sessionId)
_wantedHeadsetId = "";
_connectHeadsetState = ConnectHeadsetStates.No_Connect;
_detectedHeadsets.Clear();
// start scanning headset again
_dsProcess.RefreshHeadset();
ResetDataBuffers();
}
}
Expand All @@ -198,34 +194,6 @@ private void OnUserLogoutNotify(object sender, string message)
}
}

private void OnQueryHeadsetOK(object sender, List<Headset> headsets)
{
lock(_locker)
{
_detectedHeadsets.Clear();
string strOut = "";
foreach(var headset in headsets) {
_detectedHeadsets.Add(headset);
string headsetId = headset.HeadsetID;
if (_readyCreateSession && headsetId == _wantedHeadsetId &&
(headset.Status == "connected")) {
UnityEngine.Debug.Log("The headset " + headsetId + " is connected. Start creating session.");

_readyCreateSession = false;
// create session
_dsProcess.CreateSession(headsetId, true);
}
strOut += headset.HeadsetID + "-" + headset.HeadsetConnection + "-" + headset.Status + "; ";
}
UnityEngine.Debug.Log("DataStreamManager-OnQueryHeadsetOK: " + strOut);
if (string.IsNullOrEmpty(strOut))
{
strOut = "No headset available at " + DateTime.Now.ToString("HH:mm:ss");
}
MessageQueryHeadsetOK(this, strOut);
}
}

private void OnCreateSessionFail(object sender, string errorMsg)
{
lock (_locker)
Expand All @@ -250,9 +218,6 @@ private void OnSessionActivedOK(object sender, SessionEventArgs sessionInfo)

_connectHeadsetState = ConnectHeadsetStates.Session_Created;

// stop query headset if session is created
_dsProcess.StopQueryHeadset();

// subscribe data
_dsProcess.SubscribeData();
}
Expand All @@ -265,10 +230,6 @@ private void OnSessionActivedOK(object sender, SessionEventArgs sessionInfo)

private void OnLicenseValidTo(object sender, DateTime validTo)
{
if (!_isSessActivated) {
// start scanning headset again
_dsProcess.RefreshHeadset();
}
LicenseValidTo(this, validTo);

}
Expand Down Expand Up @@ -341,33 +302,6 @@ private void OnStreamStopNotify(object sender, List<string> streams)
}
}

private void OnHeadsetConnectNotify(object sender, HeadsetConnectEventArgs e)
{
lock (_locker)
{
string headsetId = e.HeadsetId;
UnityEngine.Debug.Log("OnHeadsetConnectNotify for headset " + headsetId + " while wantedHeadset : " + _wantedHeadsetId + "_readyCreateSession" + _readyCreateSession);
if (e.IsSuccess && _readyCreateSession &&
(headsetId == _wantedHeadsetId)) {
UnityEngine.Debug.Log("Connect the headset " + headsetId + " successfully. Start creating session.");
_readyCreateSession = false;
// create session
_dsProcess.CreateSession(headsetId, true);
}
else if (!e.IsSuccess && headsetId == _wantedHeadsetId) {
UnityEngine.Debug.Log("Connect the headset " + headsetId + " unsuccessfully. Message : " + e.Message);
HeadsetConnectFail(this, headsetId);
_wantedHeadsetId = ""; // reset headsetId
_isSessActivated = false;

_connectHeadsetState = ConnectHeadsetStates.Session_Failed;
}
else {
UnityEngine.Debug.Log("OnHeadsetConnectNotify: " + headsetId + ". Message : " + e.Message);
}
}
}

private void OnSubscribedOK(object sender, Dictionary<string, JArray> e)
{
lock (_locker)
Expand Down Expand Up @@ -559,11 +493,6 @@ private void CloseSession()
private void OnHeadsetScanFinished(object sender, string message)
{
UnityEngine.Debug.Log(message);
if (!_isSessActivated) {
// start scanning headset again
UnityEngine.Debug.Log("Start scanning headset again.");
_dsProcess.RefreshHeadset();
}
}

/// <summary>
Expand All @@ -581,61 +510,7 @@ public void StartAuthorize(string licenseKey = "", object context = null)
/// <param name="headsetId">the id of headset you want to retrieve data.</param>
public void StartDataStream(List<string> streamNameList, string headsetId)
{
lock (_locker)
{
// if (!string.IsNullOrEmpty(_wantedHeadsetId)) {
// UnityEngine.Debug.Log("The data streams has already started for headset "
// + _wantedHeadsetId + ". Please wait...");
// return;
// }

if (string.IsNullOrEmpty(headsetId)) {
if (_detectedHeadsets.Count > 0) {
// get the first headset in the list
_wantedHeadsetId = _detectedHeadsets[0].HeadsetID;
}
else {
UnityEngine.Debug.LogError("No headset available.");
// query headset
// _dsProcess.QueryHeadsets("");
return;
}

}
else {
bool _foundHeadset = false;
foreach (var item in _detectedHeadsets) {
if (item.HeadsetID == headsetId){
_foundHeadset = true;
}
}
if (_foundHeadset) {
_wantedHeadsetId = headsetId;
}
else {
UnityEngine.Debug.LogError("The headset " + headsetId + " is not found. Please check the headset Id.");
return;
}
}

UnityEngine.Debug.Log("DataStreamManager-StartDataStream: " + _wantedHeadsetId);

_connectHeadsetState = ConnectHeadsetStates.Headset_Connecting;

foreach(var curStream in streamNameList) {
_dsProcess.AddStreams(curStream);
}
// check headset connected
if (!isConnectedHeadset(_wantedHeadsetId)) {
_readyCreateSession = true;
_dsProcess.StartConnectToDevice(_wantedHeadsetId);
}
else if (!_isSessActivated) {
UnityEngine.Debug.Log("The headset " + _wantedHeadsetId + " has already connected. Start creating session.");
_readyCreateSession = false;
_dsProcess.CreateSession(_wantedHeadsetId, true);
}
}

}

/// <summary>
Expand Down Expand Up @@ -990,13 +865,6 @@ public double GetPMData(string label)
return _pmBuff.GetData(label);
}

/// <summary>
/// Query headsets.
/// </summary>
public void QueryHeadsets(string headsetId = "") {
_dsProcess.QueryHeadsets(headsetId);
}

/// <summary>
/// Get detected headsets.
/// </summary>
Expand All @@ -1013,8 +881,6 @@ public List<Headset> GetDetectedHeadsets() {
public void Stop() {
// close data stream
CloseSession();
// stop query headset
_dsProcess.StopQueryHeadset();
_dsProcess.CloseCortexClient();
}

Expand Down
34 changes: 1 addition & 33 deletions Src/DataStreamProcess.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ public class DataStreamProcess
static readonly object _locker = new object();
private CortexClient _ctxClient = CortexClient.Instance;
private List<string> _streams;
private HeadsetFinder _headsetFinder = HeadsetFinder.Instance;
private Authorizer _authorizer = Authorizer.Instance;
private SessionHandler _sessionHandler = SessionHandler.Instance;

Expand Down Expand Up @@ -50,12 +49,6 @@ public event EventHandler<SessionEventArgs> SessionActivedOK
remove { _sessionHandler.SessionActived -= value; }
}
public event EventHandler<string> CreateSessionFail;

public event EventHandler<List<Headset>> QueryHeadsetOK
{
add { _headsetFinder.QueryHeadsetOK += value; }
remove { _headsetFinder.QueryHeadsetOK -= value; }
}
public event EventHandler<string> UserLogoutNotify; // inform license valid to date

// For test
Expand Down Expand Up @@ -117,7 +110,6 @@ public void ProcessInit()
private void OnUserLogoutNotify(object sender, string message)
{
// UnityEngine.Debug.Log("OnUserLogoutNotify: " + message);
StopQueryHeadset();
// Clear session data
Clear();
UserLogoutNotify(this, message);
Expand Down Expand Up @@ -351,9 +343,8 @@ public void CreateSession(string headsetId, bool isActiveSession)
// Wait a moment before creating session
System.Threading.Thread.Sleep(1000);
// CreateSession
string cortexToken = _authorizer.CortexToken;
UnityEngine.Debug.Log("Create Session with headset " + headsetId);
_sessionHandler.Create(cortexToken, headsetId, isActiveSession);
_sessionHandler.Create(headsetId, isActiveSession);
}

/// <summary>
Expand Down Expand Up @@ -424,22 +415,6 @@ public void CloseSession()
_sessionHandler.CloseSession(cortexToken);
}

/// <summary>
/// Start query headsets to get headsets information.
/// </summary>
public void QueryHeadsets(string headsetId = "")
{
_ctxClient.QueryHeadsets(headsetId);
}

/// <summary>
/// Stop query headsets.
/// </summary>
public void StopQueryHeadset()
{
_headsetFinder.StopQueryHeadset();
}

/// <summary>
/// Force close websocket client.
/// </summary>
Expand All @@ -448,13 +423,6 @@ public void CloseCortexClient()
_ctxClient.Close();
}

/// <summary>
/// Refresh headset to trigger scan btle devices from Cortex
/// </summary>
public void RefreshHeadset() {
_headsetFinder.RefreshHeadset();
}

// log out
public void Logout() {
_authorizer.Logout();
Expand Down
2 changes: 1 addition & 1 deletion Src/EmotivUnityItf.cs
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,7 @@ public void AcceptEulaAndPrivacyPolicy()
/// <param name="headsetId">The headset id of specific headset if want get headset information of a specific headset.
/// If use empty string it will query all headsets</param>
public void QueryHeadsets(string headsetId = "") {
_dsManager.QueryHeadsets(headsetId);
// _dsManager.QueryHeadsets(headsetId);
}
Comment on lines 314 to 316
Copy link

Copilot AI Mar 3, 2026

Choose a reason for hiding this comment

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

The body of QueryHeadsets has been replaced with a comment (// _dsManager.QueryHeadsets(headsetId);), making the public method a silent no-op. Any existing callers (e.g., BCIGameItf.QueryHeadsets which calls emotivUnityItf.QueryHeadsets(...)) will silently receive no results. If this method is intentionally deprecated in favour of the new IHeadsetService.ScanHeadsetAsync, this should be documented and callers should be notified or updated. If it is not intentionally deprecated, the implementation should be restored or replaced with the correct call.

Copilot uses AI. Check for mistakes.
Copy link
Contributor Author

Choose a reason for hiding this comment

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

the file will be not used in future when complete the change API of new unity-plugi. So i will revert the change


/// <summary>
Expand Down
Loading