Improved functionnality of the main form

This commit is contained in:
2023-05-22 10:20:54 +02:00
parent 96de5485fe
commit e650072213
5 changed files with 128 additions and 47 deletions
+93 -35
View File
@@ -6,8 +6,10 @@ using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
namespace Test_Merge
{
@@ -16,6 +18,10 @@ namespace Test_Merge
Reader Reader = null;
F1TVEmulator Emulator = null;
private bool cancelRequested = false;
private SemaphoreSlim semaphore = new SemaphoreSlim(1);
string ConfigFile = "";
string GpUrl = "";
public Form1()
{
@@ -23,7 +29,7 @@ namespace Test_Merge
}
public async void RefreshUI()
{
}
private async void btnSettings_Click(object sender, EventArgs e)
{
@@ -31,28 +37,29 @@ namespace Test_Merge
Settings settingsForm = new Settings();
settingsForm.ShowDialog();
//MessageBox.Show(settingsForm.GrandPrixUrl + Environment.NewLine + settingsForm.GrandPrixName + Environment.NewLine + settingsForm.GrandPrixYear);
Emulator = new F1TVEmulator(settingsForm.GrandPrixUrl);
try
if(settingsForm.GrandPrixUrl != "" && settingsForm.SelectedConfigFile != "")
{
await Emulator.Start();
Reader = new Reader(settingsForm.SelectedConfigFile, Emulator.Screenshot(), true);
GpUrl = settingsForm.GrandPrixUrl;
if (File.Exists(settingsForm.SelectedConfigFile))
{
ConfigFile = settingsForm.SelectedConfigFile;
}
else
{
MessageBox.Show("The config file has not been found please return to the config and change it");
}
}
catch
else
{
//Could not start the driver even with the new config
MessageBox.Show("The config is wrong or incomplete please try again");
MessageBox.Show("There is no URL for the Grand Prix you want to decode. Please return to the config and add a valid one");
}
}
private async void Form1_Load(object sender, EventArgs e)
{
string configFile = "./Presets/Clean_4K_2023.json";
string gpUrl = "https://f1tv.formula1.com/detail/1000006688/2023-azerbaijan-grand-prix?action=play";
Emulator = new F1TVEmulator(gpUrl);
await Emulator.Start();
Reader = new Reader(configFile, Emulator.Screenshot(), true);
//Those are the default values but they will need to be changed later when the configuration has been done
ConfigFile = "./Presets/Clean_4K_2023.json";
GpUrl = "https://f1tv.formula1.com/detail/1000006688/2023-azerbaijan-grand-prix?action=play";
}
private async void btnUpdate_Click(object sender, EventArgs e)
@@ -66,24 +73,33 @@ namespace Test_Merge
btnSettings.Enabled = false;
while (!cancelRequested)
{
// Start the time-consuming task on a separate thread
await Task.Run(async () =>
await semaphore.WaitAsync();
try
{
Stopwatch sw = new Stopwatch();
sw.Start();
Bitmap screen = Emulator.Screenshot();
screen.Save("SCREEEEEEEEN.png");
Reader.ChangeImage(screen);
string result = await Reader.Decode(Reader.MainZones, Reader.Drivers);
sw.Stop();
// Task completed
Invoke((MethodInvoker)delegate
// Start the time-consuming task on a separate thread
await Task.Run(async () =>
{
DisplayResults(result, sw, screen);
Stopwatch sw = new Stopwatch();
sw.Start();
Bitmap screen = Emulator.Screenshot();
screen.Save("HopefullyDataScreenshot.png");
Reader.ChangeImage(screen);
string result = await Reader.Decode(Reader.MainZones, Reader.Drivers);
sw.Stop();
// Task completed
Invoke((MethodInvoker)delegate
{
DisplayResults(result, sw, screen);
});
});
});
}
finally
{
semaphore.Release();
}
}
// Re-enable UI controls
btnStartDecoding.Enabled = true;
@@ -98,12 +114,20 @@ namespace Test_Merge
Emulator.Stop();
}
}
private void DisplayResults(string result,Stopwatch sw,Bitmap screen)
private void DisplayResults(string result, Stopwatch sw, Bitmap screen)
{
tbxResult.Text = "";
tbxResult.Text = "Decoding done in :" + sw.ElapsedMilliseconds + "ms" + Environment.NewLine;
tbxResult.Text += result;
pbxResult.Image = screen;
if (result == "")
{
cancelRequested = true;
MessageBox.Show("An error has occured while trying to recover data from live feed. This can happen sometimes. I would advise you to restart a few times. If the problem persists check your configuration.");
}
else
{
tbxResult.Text = "";
tbxResult.Text = "Decoding done in :" + sw.ElapsedMilliseconds + "ms" + Environment.NewLine;
tbxResult.Text += result;
pbxResult.Image = screen;
}
}
private void btnStopUpdating_Click(object sender, EventArgs e)
@@ -111,5 +135,39 @@ namespace Test_Merge
// Set the cancellation flag
cancelRequested = true;
}
private async void button1_Click(object sender, EventArgs e)
{
btnResetEmulator.Enabled = false;
btnSettings.Enabled = false;
btnStartDecoding.Enabled = false;
btnStopUpdating.Enabled = false;
int errorCode = -1;
await Task.Run(async () =>
{
if (Emulator != null)
Emulator.ResetDriver();
Emulator = null;
Reader = null;
Emulator = new F1TVEmulator(GpUrl);
errorCode = await Emulator.Start();
});
if (errorCode != 0)
{
//IMPLEMENT MORE SPECIFIC ERROR CODES !!
MessageBox.Show("An error has occured while trying to start the driver.");
btnResetEmulator.Enabled = true;
}
else
{
Reader = new Reader(ConfigFile, Emulator.Screenshot(), true);
btnResetEmulator.Enabled = true;
btnSettings.Enabled = true;
btnStartDecoding.Enabled = true;
}
}
}
}