Skip to content

Commit

Permalink
Cleanup and fix #12
Browse files Browse the repository at this point in the history
  • Loading branch information
EnderIce2 committed Jan 23, 2024
1 parent f2e8668 commit 17a6a05
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 93 deletions.
135 changes: 55 additions & 80 deletions MainPlugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,6 @@ public void Initialize(ISharpControl control)
_control = control;
if (Utils.GetBooleanSetting("EnableRPC", true))
{
if (Utils.GetStringSetting("ClientID").Replace(" ", "").Length != 18)

This comment has been minimized.

Copy link
@JaimeHW

JaimeHW Jan 23, 2024

h

{
Utils.SaveSetting("ClientID", "765213507321856078");
}
client = new DiscordRpcClient(Utils.GetStringSetting("ClientID"), pipe: discordPipe)
{
Logger = new ConsoleLogger(logLevel, true)
Expand Down Expand Up @@ -79,94 +75,73 @@ public void Initialize(ISharpControl control)

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
if (!playedBefore)
{
presence.Details = "Frequency: Not playing";
presence.State = "Not playing";
}
else
{
try
{
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)
// 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)
{
LogWriter.WriteToFile("Set rpc exception\n" + ex);
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;
}
}
try
{
client.SetPresence(presence);
}
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());
}
}

Expand Down
2 changes: 1 addition & 1 deletion SettingsForm.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 1 addition & 7 deletions SettingsForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,23 +22,17 @@ private void Button2_Click(object sender, EventArgs e)

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")}";
}
Expand Down
4 changes: 2 additions & 2 deletions SettingsPanel.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 2 additions & 3 deletions SettingsPanel.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using SDRSharp.Radio;
using System;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace EnderIce2.SDRSharpPlugin
Expand All @@ -25,9 +24,9 @@ public SettingsPanel()
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";
Expand Down

0 comments on commit 17a6a05

Please sign in to comment.