Skip to content

Commit

Permalink
Hotfix + Reconnect logic for pulsoid
Browse files Browse the repository at this point in the history
  • Loading branch information
VRCWizard authored Sep 13, 2023
1 parent f43709a commit 336a5e9
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 41 deletions.
2 changes: 1 addition & 1 deletion OSCVRCWiz/Resources/StartUp/Updater.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ namespace OSCVRCWiz.Resources.StartUp
public class Updater
{

public static string currentVersion = "1.5.6.5";
public static string currentVersion = "1.5.6.7";
public static string updateXMLName = "https://github.com/VRCWizard/TTS-Voice-Wizard/releases/latest/download/AutoUpdater-x64.xml";

public static async void getGithubInfo()
Expand Down
121 changes: 82 additions & 39 deletions OSCVRCWiz/Services/Integrations/Heartrate/HeartratePulsoid.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
using System.Net.Http.Headers;
using System.Net.WebSockets;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Markup;

Expand All @@ -23,7 +24,7 @@ public class HeartratePulsoid
static int currentHR = 0;
public static bool pulsoidEnabled = false;

static CancellationTokenSource PulsoidCt = new();
static CancellationTokenSource PulsoidCt = new();



Expand All @@ -35,7 +36,7 @@ public static void OnStartUp()
{
if (!HeartratePulsoid.pulsoidEnabled)
{
HeartratePulsoid.PulsoidHeartRate(VoiceWizardWindow.MainFormGlobal.pulsoidAuthToken.Text.ToString());
ConnectToPulsoid(VoiceWizardWindow.MainFormGlobal.pulsoidAuthToken.Text.ToString());
}

}
Expand Down Expand Up @@ -67,9 +68,11 @@ public static void PulsoidStop()

}

public static async Task PulsoidHeartRate(string accessToken)
public static async Task ConnectToPulsoid(string accessToken)
{




PulsoidCt = new();


Expand All @@ -79,53 +82,91 @@ public static async Task PulsoidHeartRate(string accessToken)
{
try
{
await clientWebSocket.ConnectAsync(uri, PulsoidCt.Token);
await clientWebSocket.ConnectAsync(uri, PulsoidCt.Token);//try to connect
await HeartrateConnected(clientWebSocket);//upon successful connection
}
catch (WebSocketException ex) when (ex.Message.Contains("403"))//incorrect accesstoken
{
// Handle URL/connection errors (e.g., incorrect access token) here
OutputText.outputLog($"Pulsoid WebSocketException: {ex.Message}", Color.Red);
OutputText.outputLog($"Your authorization token may be invalid, try re-copying it.", Color.Orange);

//make heart green
pulsoidEnabled = false;
VoiceWizardWindow.MainFormGlobal.Invoke((MethodInvoker)delegate ()
{
VoiceWizardWindow.MainFormGlobal.buttonPulsoidConnect.ForeColor = Color.Green;
if (VoiceWizardWindow.MainFormGlobal.groupBoxHeartrate.ForeColor != Color.Green)
{
VoiceWizardWindow.MainFormGlobal.groupBoxHeartrate.ForeColor = Color.Green;
VoiceWizardWindow.MainFormGlobal.HeartrateLabel.ForeColor = Color.Green;
}
VoiceWizardWindow.MainFormGlobal.buttonPulsoidConnect.ForeColor = Color.Red;
});
var message1 = new CoreOSC.OscMessage("/avatar/parameters/isHRConnected", true);
OSC.OSCSender.Send(message1);
}
catch (WebSocketException ex)// reconnection logic
{

OutputText.outputLog($"[Pulsoid WebSocket Activated]",Color.Green);
StartHeartTimer();
OutputText.outputLog($"Pulsoid WebSocketException: {ex.Message}", Color.Red);
OutputText.outputLog($"Attempting to reconnect...", Color.Orange);
pulsoidEnabled = true;

while (clientWebSocket.State == WebSocketState.Open)
VoiceWizardWindow.MainFormGlobal.Invoke((MethodInvoker)delegate ()
{
VoiceWizardWindow.MainFormGlobal.buttonPulsoidConnect.ForeColor = Color.Orange;
});
await Task.Delay(TimeSpan.FromSeconds(5));
if (PulsoidCt.Token.IsCancellationRequested)
{
return;
}
ConnectToPulsoid(accessToken);

if (PulsoidCt.Token.IsCancellationRequested)
{
// Perform cleanup and close the WebSocket gracefully
await clientWebSocket.CloseAsync(WebSocketCloseStatus.NormalClosure, "Cancellation requested", CancellationToken.None);
break; // Exit the loop
}

await ReceiveData(clientWebSocket);
}
}
catch (WebSocketException ex)

catch (Exception ex)
{
OutputText.outputLog($"Pulsoid WebSocketException: {ex.Message}",Color.Red);
OutputText.outputLog($"Try re-adding your Pulsoid authorization token", Color.Orange);
// Handle other exceptions
OutputText.outputLog($"Heartrate Error: {ex.Message}", Color.Red);
pulsoidEnabled = false;
VoiceWizardWindow.MainFormGlobal.Invoke((MethodInvoker)delegate ()
{
VoiceWizardWindow.MainFormGlobal.buttonPulsoidConnect.BackColor = Color.Red;
VoiceWizardWindow.MainFormGlobal.buttonPulsoidConnect.ForeColor = Color.Red;
});
}
}


}



private static async Task HeartrateConnected(ClientWebSocket clientWebSocket)
{
// Make the heart green
VoiceWizardWindow.MainFormGlobal.Invoke((MethodInvoker)delegate ()
{
VoiceWizardWindow.MainFormGlobal.buttonPulsoidConnect.ForeColor = Color.Green;
if (VoiceWizardWindow.MainFormGlobal.groupBoxHeartrate.ForeColor != Color.Green)
{
VoiceWizardWindow.MainFormGlobal.groupBoxHeartrate.ForeColor = Color.Green;
VoiceWizardWindow.MainFormGlobal.HeartrateLabel.ForeColor = Color.Green;
}
});

var message1 = new CoreOSC.OscMessage("/avatar/parameters/isHRConnected", true);
OSC.OSCSender.Send(message1);

OutputText.outputLog("[Pulsoid WebSocket Connected]", Color.Green);
StartHeartTimer();
pulsoidEnabled = true;

while (clientWebSocket.State == WebSocketState.Open)
{
if (PulsoidCt.Token.IsCancellationRequested)
{
// Perform cleanup and close the WebSocket gracefully
await clientWebSocket.CloseAsync(WebSocketCloseStatus.NormalClosure, "Cancellation requested", CancellationToken.None);
break; // Exit the loop
}

await ReceiveData(clientWebSocket);
}
}

static async Task ReceiveData(ClientWebSocket clientWebSocket)
{
var buffer = new byte[1024];
Expand All @@ -140,25 +181,27 @@ static async Task ReceiveData(ClientWebSocket clientWebSocket)
// Deserialize JSON and extract the heart rate value
HeartRateResponse heartRateResponse = Newtonsoft.Json.JsonConvert.DeserializeObject<HeartRateResponse>(jsonMessage);


currentHR = heartRateResponse.data.heart_rate;
OSCListener.globalBPM = currentHR.ToString();
}

}


}

}
public static void StartHeartTimer()
{
heartRateTimer = new System.Threading.Timer(heartratetimertick);
heartRateTimer.Change(Int32.Parse(heartrateIntervalPulsoid), 0);


}
public static void StopHeartTimer()
{
heartRateTimer.Change(Timeout.Infinite, Timeout.Infinite);
if (heartRateTimer != null)
{
heartRateTimer.Change(Timeout.Infinite, Timeout.Infinite);
}
}

public static void heartratetimertick(object sender)
Expand Down Expand Up @@ -186,7 +229,7 @@ private static async void doHeartrateTimerTick()



// Debug.WriteLine(currentHR + "--" + HRPrevious);
// Debug.WriteLine(currentHR + "--" + HRPrevious);

var labelBattery = $"❤️ {OSCListener.globalBPM}";

Expand Down Expand Up @@ -267,12 +310,12 @@ private static async void doHeartrateTimerTick()
}


heartRateTimer.Change(Int32.Parse(heartrateIntervalPulsoid), 0);
heartRateTimer.Change(Int32.Parse(heartrateIntervalPulsoid), 0);



}

}

class HeartRateResponse
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ public static async Task SynthesisGetAvailableVoicesAsync(string currentCategory
// Console.WriteLine("Voice " + voice.Key + " belongs to category " + categoryName);
}
VoiceWizardWindow.MainFormGlobal.comboBoxAccentSelect.SelectedIndex = 0;
VoiceWizardWindow.MainFormGlobal.comboBoxVoiceSelect.Items.Clear();
foreach (KeyValuePair<string, string> voice in UberNameAndCategory)
{
if (voice.Value == VoiceWizardWindow.MainFormGlobal.comboBoxAccentSelect.SelectedItem.ToString())
Expand Down
2 changes: 1 addition & 1 deletion OSCVRCWiz/VoiceWizardWindow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2132,7 +2132,7 @@ private void buttonPulsoidConnect_Click(object sender, EventArgs e)
{
if (!HeartratePulsoid.pulsoidEnabled)
{
HeartratePulsoid.PulsoidHeartRate(VoiceWizardWindow.MainFormGlobal.pulsoidAuthToken.Text.ToString());
HeartratePulsoid.ConnectToPulsoid(VoiceWizardWindow.MainFormGlobal.pulsoidAuthToken.Text.ToString());
}
else
{
Expand Down

0 comments on commit 336a5e9

Please sign in to comment.