-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Please see the README for the latest release of HRtoVRChat!
- Loading branch information
1 parent
c411305
commit 5ae23b9
Showing
22 changed files
with
388 additions
and
5 deletions.
There are no files selected for viewing
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,168 @@ | ||
using System; | ||
using System.Threading; | ||
using HP.Omnicept; | ||
using HP.Omnicept.Messaging; | ||
using HP.Omnicept.Messaging.Messages; | ||
using UnhollowerBaseLib; | ||
|
||
namespace HRtoVRChat.HRManagers; | ||
|
||
public class OmniceptManager : HRManager | ||
{ | ||
public int HR { get; set; } | ||
|
||
private Glia m_gliaClient; | ||
private GliaValueCache m_gliaValCache; | ||
private bool m_isConnected; | ||
private HeartRate lastHeartRate; | ||
|
||
private Thread _worker; | ||
private CancellationTokenSource token; | ||
private void VerifyDeadThread() | ||
{ | ||
if (_worker != null) | ||
{ | ||
if(_worker.IsAlive) | ||
_worker.Abort(); | ||
_worker = null; | ||
} | ||
token = new CancellationTokenSource(); | ||
} | ||
|
||
private void StopGlia() | ||
{ | ||
// Verify Glia is Disposed | ||
if(m_gliaValCache != null) | ||
m_gliaValCache?.Stop(); | ||
if(m_gliaClient != null) | ||
m_gliaClient?.Dispose(); | ||
m_gliaValCache = null; | ||
m_gliaClient = null; | ||
m_isConnected = false; | ||
Glia.cleanupNetMQConfig(); | ||
} | ||
|
||
private bool StartGlia(bool isTesting = false) | ||
{ | ||
// Verify Glia is Disposed | ||
StopGlia(); | ||
bool ret; | ||
|
||
// Start Glia | ||
try | ||
{ | ||
m_gliaClient = new Glia("HRtoVRChat", | ||
new SessionLicense(String.Empty, String.Empty, LicensingModel.Core, false)); | ||
m_gliaValCache = new GliaValueCache(m_gliaClient.Connection); | ||
SubscriptionList sl = new SubscriptionList | ||
{ | ||
Subscriptions = | ||
{ | ||
new Subscription(MessageTypes.ABI_MESSAGE_HEART_RATE, String.Empty, String.Empty, | ||
String.Empty, String.Empty, new MessageVersionSemantic("1.0.0")) | ||
} | ||
}; | ||
m_gliaClient.setSubscriptions(sl); | ||
m_isConnected = true; | ||
ret = true; | ||
} | ||
catch (Exception) | ||
{ | ||
m_isConnected = false; | ||
ret = false; | ||
} | ||
if (!m_isConnected || isTesting) | ||
StopGlia(); | ||
if (isTesting) | ||
return ret; | ||
return m_isConnected; | ||
} | ||
|
||
void HandleMessage(ITransportMessage msg) | ||
{ | ||
switch (msg.Header.MessageType) | ||
{ | ||
case MessageTypes.ABI_MESSAGE_HEART_RATE: | ||
lastHeartRate = m_gliaClient.Connection.Build<HeartRate>(msg); | ||
break; | ||
} | ||
} | ||
|
||
ITransportMessage RetrieveMessage() | ||
{ | ||
ITransportMessage msg = null; | ||
if (m_gliaValCache != null) | ||
{ | ||
try | ||
{ | ||
msg = m_gliaValCache.GetNext(); | ||
} | ||
catch (HP.Omnicept.Errors.TransportError e) | ||
{ | ||
LogHelper.Error("OmniceptManager", e.Message); | ||
} | ||
} | ||
return msg; | ||
} | ||
|
||
public bool Init(string d1) | ||
{ | ||
bool status = StartGlia(true); | ||
if (status) | ||
{ | ||
LogHelper.Log("OmniceptManager", "Started Omnicept!"); | ||
StartThread(); | ||
} | ||
else | ||
LogHelper.Error("OmniceptManager", "Failed to start Omnicept!"); | ||
return status; | ||
} | ||
|
||
public void StartThread() | ||
{ | ||
VerifyDeadThread(); | ||
_worker = new Thread(() => | ||
{ | ||
IL2CPP.il2cpp_thread_attach(IL2CPP.il2cpp_domain_get()); | ||
LogHelper.Debug("OmniceptManager", "Omnicept Thread Started"); | ||
StartGlia(); | ||
while (!token.IsCancellationRequested) | ||
{ | ||
// Update message | ||
try | ||
{ | ||
if (m_isConnected) | ||
{ | ||
ITransportMessage msg = RetrieveMessage(); | ||
if(msg != null) | ||
HandleMessage(msg); | ||
} | ||
} | ||
catch (Exception e) | ||
{ | ||
LogHelper.Error("OmniceptManager" , "Failed to get message! " + e); | ||
} | ||
if (lastHeartRate != null) | ||
{ | ||
HR = (int)lastHeartRate.Rate; | ||
} | ||
Thread.Sleep(10); | ||
} | ||
// Thread-Safe; don't need to call it in this thread :) | ||
//StopGlia(); | ||
}); | ||
_worker.Start(); | ||
} | ||
|
||
public int GetHR() => HR; | ||
|
||
public void Stop() | ||
{ | ||
StopGlia(); | ||
token.Cancel(); | ||
} | ||
|
||
public bool IsOpen() => m_isConnected; | ||
|
||
public bool IsActive() => IsOpen(); | ||
} |
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file added
BIN
+176 KB
HRtoVRChat/HRtoVRChat/HRManagers/SDKLibs/System.Collections.Immutable.dll
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file added
BIN
+16.4 KB
HRtoVRChat/HRtoVRChat/HRManagers/SDKLibs/System.Runtime.CompilerServices.Unsafe.dll
Binary file not shown.
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
using System.Linq; | ||
using System.Threading; | ||
using HRtoVRChat_OSC_SDK; | ||
using SuperSimpleTcp; | ||
|
||
namespace HRtoVRChat.HRManagers; | ||
|
||
public class SDKManager : HRManager | ||
{ | ||
private Thread _worker; | ||
private CancellationTokenSource token; | ||
|
||
private SimpleTcpServer server; | ||
|
||
private int HR; | ||
private bool isActive; | ||
private bool isOpen; | ||
|
||
public bool Init(string d1) | ||
{ | ||
if (_worker != null) | ||
{ | ||
token.Cancel(); | ||
} | ||
|
||
token = new CancellationTokenSource(); | ||
_worker = new Thread(() => | ||
{ | ||
server = new SimpleTcpServer(d1); | ||
server.Events.ClientConnected += (sender, args) => LogHelper.Log("SDKManager", "SDK Connected!"); | ||
server.Events.ClientDisconnected += (sender, args) => LogHelper.Log("SDKManager", "SDK Disconnected!"); | ||
server.Events.DataReceived += (sender, args) => | ||
{ | ||
byte[] data = args.Data; | ||
object? fakeDeserialize = Messages.DeserializeMessage(data); | ||
string messageType = Messages.GetMessageType(fakeDeserialize); | ||
switch (messageType) | ||
{ | ||
case "HRMessage": | ||
Messages.HRMessage hrm = Messages.DeserializeMessage<Messages.HRMessage>(data); | ||
HR = hrm.HR; | ||
isActive = hrm.IsActive; | ||
isOpen = hrm.IsOpen; | ||
break; | ||
case "GetHRData": | ||
Messages.HRMessage hrm_ghrd = new Messages.HRMessage | ||
{ | ||
HR = HR, | ||
IsActive = isActive, | ||
IsOpen = isOpen | ||
}; | ||
server.Send(args.IpPort, hrm_ghrd.Serialize()); | ||
break; | ||
default: | ||
LogHelper.Warn("SDKManager", "Unknown Debug Message: " + messageType); | ||
break; | ||
} | ||
}; | ||
server.Start(); | ||
while (!token.IsCancellationRequested) | ||
{ | ||
int c = server.GetClients().ToList().Count; | ||
if (c <= 0) | ||
{ | ||
// reset all data | ||
HR = 0; | ||
isOpen = false; | ||
isActive = false; | ||
} | ||
Thread.Sleep(10); | ||
} | ||
server?.Stop(); | ||
}); | ||
_worker.Start(); | ||
return true; | ||
} | ||
|
||
public int GetHR() => HR; | ||
|
||
public void Stop() => token.Cancel(); | ||
|
||
public bool IsOpen() => isOpen; | ||
|
||
public bool IsActive() => isActive; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.