From 336a5e9b1c3a88fd4a6b471d814e73b4882d9cb6 Mon Sep 17 00:00:00 2001 From: VRCWizard <101527472+VRCWizard@users.noreply.github.com> Date: Wed, 13 Sep 2023 10:35:51 -0500 Subject: [PATCH] Hotfix + Reconnect logic for pulsoid --- OSCVRCWiz/Resources/StartUp/Updater.cs | 2 +- .../Heartrate/HeartratePulsoid.cs | 121 ++++++++++++------ .../TextToSpeech/TTSEngines/UberDuckTTS.cs | 1 + OSCVRCWiz/VoiceWizardWindow.cs | 2 +- 4 files changed, 85 insertions(+), 41 deletions(-) diff --git a/OSCVRCWiz/Resources/StartUp/Updater.cs b/OSCVRCWiz/Resources/StartUp/Updater.cs index a1ad1c3e..66c03c4f 100644 --- a/OSCVRCWiz/Resources/StartUp/Updater.cs +++ b/OSCVRCWiz/Resources/StartUp/Updater.cs @@ -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() diff --git a/OSCVRCWiz/Services/Integrations/Heartrate/HeartratePulsoid.cs b/OSCVRCWiz/Services/Integrations/Heartrate/HeartratePulsoid.cs index dba59d5a..2fa7fd8f 100644 --- a/OSCVRCWiz/Services/Integrations/Heartrate/HeartratePulsoid.cs +++ b/OSCVRCWiz/Services/Integrations/Heartrate/HeartratePulsoid.cs @@ -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; @@ -23,7 +24,7 @@ public class HeartratePulsoid static int currentHR = 0; public static bool pulsoidEnabled = false; - static CancellationTokenSource PulsoidCt = new(); + static CancellationTokenSource PulsoidCt = new(); @@ -35,7 +36,7 @@ public static void OnStartUp() { if (!HeartratePulsoid.pulsoidEnabled) { - HeartratePulsoid.PulsoidHeartRate(VoiceWizardWindow.MainFormGlobal.pulsoidAuthToken.Text.ToString()); + ConnectToPulsoid(VoiceWizardWindow.MainFormGlobal.pulsoidAuthToken.Text.ToString()); } } @@ -67,9 +68,11 @@ public static void PulsoidStop() } - public static async Task PulsoidHeartRate(string accessToken) + public static async Task ConnectToPulsoid(string accessToken) { - + + + PulsoidCt = new(); @@ -79,46 +82,49 @@ 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; }); } } @@ -126,6 +132,41 @@ public static async Task PulsoidHeartRate(string accessToken) } + + + 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]; @@ -140,25 +181,27 @@ static async Task ReceiveData(ClientWebSocket clientWebSocket) // Deserialize JSON and extract the heart rate value HeartRateResponse heartRateResponse = Newtonsoft.Json.JsonConvert.DeserializeObject(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) @@ -186,7 +229,7 @@ private static async void doHeartrateTimerTick() - // Debug.WriteLine(currentHR + "--" + HRPrevious); + // Debug.WriteLine(currentHR + "--" + HRPrevious); var labelBattery = $"❤️ {OSCListener.globalBPM}"; @@ -267,12 +310,12 @@ private static async void doHeartrateTimerTick() } - heartRateTimer.Change(Int32.Parse(heartrateIntervalPulsoid), 0); - + heartRateTimer.Change(Int32.Parse(heartrateIntervalPulsoid), 0); + } - + } class HeartRateResponse diff --git a/OSCVRCWiz/Services/Speech/TextToSpeech/TTSEngines/UberDuckTTS.cs b/OSCVRCWiz/Services/Speech/TextToSpeech/TTSEngines/UberDuckTTS.cs index 30d5305d..1d5bc9a8 100644 --- a/OSCVRCWiz/Services/Speech/TextToSpeech/TTSEngines/UberDuckTTS.cs +++ b/OSCVRCWiz/Services/Speech/TextToSpeech/TTSEngines/UberDuckTTS.cs @@ -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 voice in UberNameAndCategory) { if (voice.Value == VoiceWizardWindow.MainFormGlobal.comboBoxAccentSelect.SelectedItem.ToString()) diff --git a/OSCVRCWiz/VoiceWizardWindow.cs b/OSCVRCWiz/VoiceWizardWindow.cs index 5a04a65b..0fc1b6ac 100644 --- a/OSCVRCWiz/VoiceWizardWindow.cs +++ b/OSCVRCWiz/VoiceWizardWindow.cs @@ -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 {