Cleanup and fix #12

This commit is contained in:
EnderIce2 2024-01-23 21:03:05 +02:00
parent f2e866868a
commit 17a6a05739
5 changed files with 62 additions and 94 deletions

View File

@ -44,10 +44,6 @@ namespace EnderIce2.SDRSharpPlugin
_control = control;
if (Utils.GetBooleanSetting("EnableRPC", true))
{
if (Utils.GetStringSetting("ClientID").Replace(" ", "").Length != 18)
{
Utils.SaveSetting("ClientID", "765213507321856078");
}
client = new DiscordRpcClient(Utils.GetStringSetting("ClientID"), pipe: discordPipe)
{
Logger = new ConsoleLogger(logLevel, true)
@ -79,94 +75,73 @@ namespace EnderIce2.SDRSharpPlugin
private async Task MainLoop()
{
loop_start:
try
await Task.Delay(2000).ConfigureAwait(false);
LogWriter.WriteToFile($"MainLoop called {client.IsInitialized}");
while (true)
{
await Task.Delay(2000).ConfigureAwait(false);
LogWriter.WriteToFile($"MainLoop called {client.IsInitialized}");
while (true)
if (client != null && isConnected)
{
if (client != null && isConnected)
LogWriter.WriteToFile("Waiting 1000ms in loop...");
await Task.Delay(1000).ConfigureAwait(false); // 1 second delay
if (_control.IsPlaying)
{
LogWriter.WriteToFile("Waiting 1000ms in loop...");
await Task.Delay(1000).ConfigureAwait(false); // 1 second delay
if (_control.IsPlaying)
{
presence.Assets.SmallImageKey = "play";
playedBefore = true;
}
else if (!_control.IsPlaying && playedBefore)
{
presence.Assets.SmallImageKey = "pause";
}
presence.Assets.SmallImageKey = "play";
playedBefore = true;
}
else if (!_control.IsPlaying && playedBefore)
{
presence.Assets.SmallImageKey = "pause";
}
if (!playedBefore)
{
presence.Details = "Frequency: Not playing";
presence.State = "Not playing";
}
else
{
try
{
// TODO: Check BandPlan.xml file and set in the status
LogWriter.WriteToFile("Setting presence...");
string frequency_text = $"Frequency: {$"{_control.Frequency:#,0,,0 Hz}"} | Bandwidth: {$"{_control.FilterBandwidth:#,0,,0 Hz}"} | {Enum.GetName(typeof(DetectorType), _control.DetectorType)}";
presence.Details = frequency_text;
switch (_control.DetectorType)
{
case DetectorType.WFM:
if (!string.IsNullOrWhiteSpace(_control.RdsRadioText + _control.RdsProgramService))
{
string radio_text = string.IsNullOrWhiteSpace(_control.RdsRadioText) ? "" : $" - {_control.RdsRadioText}";
presence.State = _control.FmStereo
? $"RDS: ((( {_control.RdsProgramService} ))){radio_text}"
: $"RDS: {_control.RdsProgramService}{radio_text}";
}
else
{
presence.State = "RDS: unknown";
}
break;
default:
presence.State = ""; // TODO: implement for every type; right now I don't really know what to add
break;
}
}
catch (Exception ex)
{
LogWriter.WriteToFile("Set rpc exception\n" + ex);
}
}
if (!playedBefore)
{
presence.Details = "Frequency: Not playing";
presence.State = "Not playing";
}
else
{
try
{
client.SetPresence(presence);
// TODO: Check BandPlan.xml file and set in the status
LogWriter.WriteToFile("Setting presence...");
string frequency_text = $"Frequency: {$"{_control.Frequency:#,0,,0 Hz}"} | Bandwidth: {$"{_control.FilterBandwidth:#,0,,0 Hz}"} | {Enum.GetName(typeof(DetectorType), _control.DetectorType)}";
presence.Details = frequency_text;
switch (_control.DetectorType)
{
case DetectorType.WFM:
if (!string.IsNullOrWhiteSpace(_control.RdsRadioText + _control.RdsProgramService))
{
string radio_text = string.IsNullOrWhiteSpace(_control.RdsRadioText) ? "" : $" - {_control.RdsRadioText}";
presence.State = _control.FmStereo
? $"RDS: ((( {_control.RdsProgramService} ))){radio_text}"
: $"RDS: {_control.RdsProgramService}{radio_text}";
}
else
{
presence.State = "RDS: unknown";
}
break;
default:
presence.State = ""; // TODO: implement for every type; right now I don't really know what to add
break;
}
}
catch (ObjectDisposedException ex)
catch (Exception ex)
{
LogWriter.WriteToFile($"ObjectDisposedException exception for SetPresence\n{ex}");
LogWriter.WriteToFile("Set rpc exception\n" + ex);
}
LogWriter.WriteToFile("SetPresence");
_controlPanel.ChangeStatus = $"Presence Updated {DateTime.UtcNow}";
}
try
{
client.SetPresence(presence);
}
catch (ObjectDisposedException ex)
{
LogWriter.WriteToFile($"ObjectDisposedException exception for SetPresence\n{ex}");
}
LogWriter.WriteToFile("SetPresence");
_controlPanel.ChangeStatus = $"Presence Updated {DateTime.UtcNow}";
}
if (client == null)
{
_controlPanel.ChangeStatus = "Client is null";
}
else
{
_controlPanel.ChangeStatus = "Presence stopped";
}
}
catch (Exception ex)
{
if (ex.Message.Contains("The process cannot access the file")) // not important exception
{
goto loop_start;
}
_controlPanel.ChangeStatus = $"RPC Update Error\n{ex.Message}";
LogWriter.WriteToFile(ex.ToString());
}
}

View File

@ -84,7 +84,7 @@
this.checkBox1.TabIndex = 6;
this.checkBox1.Text = "Enable Logging (for debugging)";
this.checkBox1.UseVisualStyleBackColor = true;
this.checkBox1.CheckedChanged += new System.EventHandler(this.CheckBox1_CheckedChanged_1);
this.checkBox1.CheckedChanged += new System.EventHandler(this.CheckBox1_CheckedChanged);
//
// textBox1
//

View File

@ -22,23 +22,17 @@ namespace EnderIce2.SDRSharpPlugin
private void Button1_Click(object sender, EventArgs e) => Close();
private void CheckBox1_CheckedChanged(object sender, EventArgs e) => Utils.SaveSetting("ShowWelcomePage", !checkBox1.Checked);
private void Button5_Click(object sender, EventArgs e)
{
MessageBox.Show("MIT License\n\nCopyright (c) 2018 Lachee\n\nPermission is hereby granted, free of charge, to any person obtaining a copy\nof this software and associated documentation files (the \"Software\"), to deal\nin the Software without restriction, including without limitation the rights\nto use, copy, modify, merge, publish, distribute, sublicense, and/or sell\ncopies of the Software, and to permit persons to whom the Software is\nfurnished to do so, subject to the following conditions:\n\nThe above copyright notice and this permission notice shall be included in all\ncopies or substantial portions of the Software.\n\nTHE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\nIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\nFITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\nAUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\nLIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\nOUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\nSOFTWARE.", "discord-rpc-csharp");
MessageBox.Show("The MIT License (MIT)\n\nCopyright(c) 2007 James Newton-King\n\nPermission is hereby granted, free of charge, to any person obtaining a copy of\nthis software and associated documentation files (the \"Software\"), to deal in\nthe Software without restriction, including without limitation the rights to\nuse, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of\nthe Software, and to permit persons to whom the Software is furnished to do so,\nsubject to the following conditions:\n\nThe above copyright notice and this permission notice shall be included in all\ncopies or substantial portions of the Software.\n\nTHE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\nIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS\nFOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR\nCOPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER\nIN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN\nCONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.", "Newtonsoft.Json");
}
private void CheckBox1_CheckedChanged_1(object sender, EventArgs e) => Utils.SaveSetting("LogRPC", checkBox1.Checked);
private void CheckBox1_CheckedChanged(object sender, EventArgs e) => Utils.SaveSetting("LogRPC", checkBox1.Checked);
private void Button4_Click(object sender, EventArgs e)
{
textBox1.Text.Replace(" ", "").Replace("\n", "").Replace("\r", "");
if (!int.TryParse(textBox1.Text, out _) || textBox1.Text.Length != 18)
{
MessageBox.Show("Invalid Client ID!");
}
Utils.SaveSetting("ClientID", textBox1.Text);
label1.Text = $"Configuration Updated.\nNew ID: {Utils.GetStringSetting("ClientID")}";
}

View File

@ -47,7 +47,7 @@
this.checkBox1.TabIndex = 0;
this.checkBox1.Text = "Enable Discord RPC";
this.checkBox1.UseVisualStyleBackColor = true;
this.checkBox1.CheckedChanged += new System.EventHandler(this.CheckBox1_CheckedChanged);
this.checkBox1.CheckedChanged += new System.EventHandler(this.EnableRPC_CheckedChanged);
//
// label1
//
@ -78,7 +78,7 @@
this.button1.Size = new System.Drawing.Size(25, 25);
this.button1.TabIndex = 4;
this.button1.UseVisualStyleBackColor = true;
this.button1.Click += new System.EventHandler(this.Button1_Click);
this.button1.Click += new System.EventHandler(this.SettingsButton_Click);
//
// SettingsPanel
//

View File

@ -1,6 +1,5 @@
using SDRSharp.Radio;
using System;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace EnderIce2.SDRSharpPlugin
@ -25,9 +24,9 @@ namespace EnderIce2.SDRSharpPlugin
LogWriter.WriteToFile("User Control Loaded");
}
private void Button1_Click(object sender, EventArgs e) => new SettingsForm().Show();
private void SettingsButton_Click(object sender, EventArgs e) => new SettingsForm().Show();
private void CheckBox1_CheckedChanged(object sender, EventArgs e)
private void EnableRPC_CheckedChanged(object sender, EventArgs e)
{
Utils.SaveSetting("EnableRPC", checkBox1.Checked);
label1.Text = "Restart required";