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
+4
View File
@@ -237,6 +237,10 @@ namespace Test_Merge
Driver.Dispose(); Driver.Dispose();
return 105; return 105;
} }
else
{
Thread.Sleep(3000);
}
Screenshot("BEFOREFULLSCREEN"); Screenshot("BEFOREFULLSCREEN");
+14 -1
View File
@@ -33,6 +33,7 @@
this.btnStartDecoding = new System.Windows.Forms.Button(); this.btnStartDecoding = new System.Windows.Forms.Button();
this.pbxResult = new System.Windows.Forms.PictureBox(); this.pbxResult = new System.Windows.Forms.PictureBox();
this.btnStopUpdating = new System.Windows.Forms.Button(); this.btnStopUpdating = new System.Windows.Forms.Button();
this.btnResetEmulator = new System.Windows.Forms.Button();
((System.ComponentModel.ISupportInitialize)(this.pbxResult)).BeginInit(); ((System.ComponentModel.ISupportInitialize)(this.pbxResult)).BeginInit();
this.SuspendLayout(); this.SuspendLayout();
// //
@@ -84,11 +85,22 @@
this.btnStopUpdating.UseVisualStyleBackColor = true; this.btnStopUpdating.UseVisualStyleBackColor = true;
this.btnStopUpdating.Click += new System.EventHandler(this.btnStopUpdating_Click); this.btnStopUpdating.Click += new System.EventHandler(this.btnStopUpdating_Click);
// //
// btnResetEmulator
//
this.btnResetEmulator.Location = new System.Drawing.Point(336, 12);
this.btnResetEmulator.Name = "btnResetEmulator";
this.btnResetEmulator.Size = new System.Drawing.Size(162, 53);
this.btnResetEmulator.TabIndex = 5;
this.btnResetEmulator.Text = "Reset emulator";
this.btnResetEmulator.UseVisualStyleBackColor = true;
this.btnResetEmulator.Click += new System.EventHandler(this.button1_Click);
//
// Form1 // Form1
// //
this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 16F); this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 16F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(800, 335); this.ClientSize = new System.Drawing.Size(799, 335);
this.Controls.Add(this.btnResetEmulator);
this.Controls.Add(this.btnStopUpdating); this.Controls.Add(this.btnStopUpdating);
this.Controls.Add(this.pbxResult); this.Controls.Add(this.pbxResult);
this.Controls.Add(this.btnStartDecoding); this.Controls.Add(this.btnStartDecoding);
@@ -111,6 +123,7 @@
private System.Windows.Forms.Button btnStartDecoding; private System.Windows.Forms.Button btnStartDecoding;
private System.Windows.Forms.PictureBox pbxResult; private System.Windows.Forms.PictureBox pbxResult;
private System.Windows.Forms.Button btnStopUpdating; private System.Windows.Forms.Button btnStopUpdating;
private System.Windows.Forms.Button btnResetEmulator;
} }
} }
+74 -16
View File
@@ -6,8 +6,10 @@ using System.Diagnostics;
using System.Drawing; using System.Drawing;
using System.Linq; using System.Linq;
using System.Text; using System.Text;
using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
using System.Windows.Forms; using System.Windows.Forms;
using System.IO;
namespace Test_Merge namespace Test_Merge
{ {
@@ -16,6 +18,10 @@ namespace Test_Merge
Reader Reader = null; Reader Reader = null;
F1TVEmulator Emulator = null; F1TVEmulator Emulator = null;
private bool cancelRequested = false; private bool cancelRequested = false;
private SemaphoreSlim semaphore = new SemaphoreSlim(1);
string ConfigFile = "";
string GpUrl = "";
public Form1() public Form1()
{ {
@@ -31,28 +37,29 @@ namespace Test_Merge
Settings settingsForm = new Settings(); Settings settingsForm = new Settings();
settingsForm.ShowDialog(); settingsForm.ShowDialog();
//MessageBox.Show(settingsForm.GrandPrixUrl + Environment.NewLine + settingsForm.GrandPrixName + Environment.NewLine + settingsForm.GrandPrixYear); //MessageBox.Show(settingsForm.GrandPrixUrl + Environment.NewLine + settingsForm.GrandPrixName + Environment.NewLine + settingsForm.GrandPrixYear);
Emulator = new F1TVEmulator(settingsForm.GrandPrixUrl); if(settingsForm.GrandPrixUrl != "" && settingsForm.SelectedConfigFile != "")
try
{ {
await Emulator.Start(); GpUrl = settingsForm.GrandPrixUrl;
Reader = new Reader(settingsForm.SelectedConfigFile, Emulator.Screenshot(), true); if (File.Exists(settingsForm.SelectedConfigFile))
}
catch
{ {
//Could not start the driver even with the new config ConfigFile = settingsForm.SelectedConfigFile;
MessageBox.Show("The config is wrong or incomplete please try again"); }
else
{
MessageBox.Show("The config file has not been found please return to the config and change it");
}
}
else
{
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) private async void Form1_Load(object sender, EventArgs e)
{ {
string configFile = "./Presets/Clean_4K_2023.json"; //Those are the default values but they will need to be changed later when the configuration has been done
string gpUrl = "https://f1tv.formula1.com/detail/1000006688/2023-azerbaijan-grand-prix?action=play"; ConfigFile = "./Presets/Clean_4K_2023.json";
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);
} }
private async void btnUpdate_Click(object sender, EventArgs e) private async void btnUpdate_Click(object sender, EventArgs e)
@@ -65,6 +72,10 @@ namespace Test_Merge
btnStopUpdating.Enabled = true; btnStopUpdating.Enabled = true;
btnSettings.Enabled = false; btnSettings.Enabled = false;
while (!cancelRequested) while (!cancelRequested)
{
await semaphore.WaitAsync();
try
{ {
// Start the time-consuming task on a separate thread // Start the time-consuming task on a separate thread
await Task.Run(async () => await Task.Run(async () =>
@@ -73,7 +84,7 @@ namespace Test_Merge
sw.Start(); sw.Start();
Bitmap screen = Emulator.Screenshot(); Bitmap screen = Emulator.Screenshot();
screen.Save("SCREEEEEEEEN.png"); screen.Save("HopefullyDataScreenshot.png");
Reader.ChangeImage(screen); Reader.ChangeImage(screen);
string result = await Reader.Decode(Reader.MainZones, Reader.Drivers); string result = await Reader.Decode(Reader.MainZones, Reader.Drivers);
@@ -85,6 +96,11 @@ namespace Test_Merge
}); });
}); });
} }
finally
{
semaphore.Release();
}
}
// Re-enable UI controls // Re-enable UI controls
btnStartDecoding.Enabled = true; btnStartDecoding.Enabled = true;
btnStopUpdating.Enabled = false; btnStopUpdating.Enabled = false;
@@ -99,17 +115,59 @@ namespace Test_Merge
} }
} }
private void DisplayResults(string result, Stopwatch sw, Bitmap screen) private void DisplayResults(string result, Stopwatch sw, Bitmap 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 = "";
tbxResult.Text = "Decoding done in :" + sw.ElapsedMilliseconds + "ms" + Environment.NewLine; tbxResult.Text = "Decoding done in :" + sw.ElapsedMilliseconds + "ms" + Environment.NewLine;
tbxResult.Text += result; tbxResult.Text += result;
pbxResult.Image = screen; pbxResult.Image = screen;
} }
}
private void btnStopUpdating_Click(object sender, EventArgs e) private void btnStopUpdating_Click(object sender, EventArgs e)
{ {
// Set the cancellation flag // Set the cancellation flag
cancelRequested = true; 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;
}
}
} }
} }
+1 -1
View File
@@ -83,7 +83,7 @@ namespace Test_Merge
//outputBitmap.Save(Window.STRING_DEBUG_FOLDER + @"\Final_treshold_" + id + ".png"); //outputBitmap.Save(Window.STRING_DEBUG_FOLDER + @"\Final_treshold_" + id + ".png");
break; break;
case WindowType.Tyre: case WindowType.Tyre:
outputBitmap.Save(Window.TYRE_DEBUG_FOLDER + @"\raw_" + id + ".png"); //outputBitmap.Save(Window.TYRE_DEBUG_FOLDER + @"\raw_" + id + ".png");
outputBitmap = RemoveUseless(outputBitmap); outputBitmap = RemoveUseless(outputBitmap);
//outputBitmap.Save(Window.TYRE_DEBUG_FOLDER + @"\uselessRemoved_" + id + ".png"); //outputBitmap.Save(Window.TYRE_DEBUG_FOLDER + @"\uselessRemoved_" + id + ".png");
+14 -8
View File
@@ -236,6 +236,7 @@ namespace Test_Merge
{ {
case 0: case 0:
//Main Zone //Main Zone
//Parallel.For(0, mainZones[mainZoneId].Zones.Count,async i =>
for (int i = 0; i < mainZones[mainZoneId].Zones.Count; i++) for (int i = 0; i < mainZones[mainZoneId].Zones.Count; i++)
{ {
DriverData data = await mainZones[mainZoneId].Zones[i].Decode(new List<string>(drivers)); DriverData data = await mainZones[mainZoneId].Zones[i].Decode(new List<string>(drivers));
@@ -259,12 +260,6 @@ namespace Test_Merge
Storage.AddDriverStat(stats, DriverLaps[i]); Storage.AddDriverStat(stats, DriverLaps[i]);
} }
//Checking if its a pitstop //Checking if its a pitstop
//If the tyre is a different counpound or if the tyre is much fresher than the older one then it must be because the driver had a pitstop
//BE CAREFULL the last if is here because the OCR is quite weak with the number 4
//if (data.CurrentTyre.Coumpound != DriverDataLogs[i][DriverDataLogs[i].Count - 2].CurrentTyre.Coumpound
//|| data.CurrentTyre.NumberOfLaps != DriverDataLogs[i][DriverDataLogs[i].Count - 2].CurrentTyre.NumberOfLaps
//&& data.CurrentTyre.NumberOfLaps < DriverDataLogs[i][DriverDataLogs[i].Count - 2].CurrentTyre.NumberOfLaps
//&& DriverDataLogs[i][DriverDataLogs[i].Count - 2].CurrentTyre.NumberOfLaps != 3)
//Forget this the best way to know if a tyre has been changed is if the number of laps is zero //Forget this the best way to know if a tyre has been changed is if the number of laps is zero
if (data.CurrentTyre.Coumpound != Tyre.Type.Undefined && data.CurrentTyre.NumberOfLaps == 0 && DriverDataLogs[i][DriverDataLogs[i].Count - 2].CurrentTyre.NumberOfLaps != 0) if (data.CurrentTyre.Coumpound != Tyre.Type.Undefined && data.CurrentTyre.NumberOfLaps == 0 && DriverDataLogs[i][DriverDataLogs[i].Count - 2].CurrentTyre.NumberOfLaps != 0)
{ {
@@ -273,21 +268,32 @@ namespace Test_Merge
} }
} }
DriverDataLogs[i].Add(data); DriverDataLogs[i].Add(data);
} }//);
break; break;
//Next there could be a Title Zone and TrackInfoZone //Next there could be a Title Zone and TrackInfoZone
} }
} }
//Display //Display
int nullDrivers = 0;
foreach (DriverData driver in mainResults) foreach (DriverData driver in mainResults)
{ {
if (driver.Position == -1 && driver.LapTime == 0)
nullDrivers++;
result += driver.ToString(); result += driver.ToString();
result += Environment.NewLine; result += Environment.NewLine;
} }
//If 75% or more of the drivers cannot be decoded there must be a problem somewhere.
//Some can be because if they are out of the race usually the OCR returns -1 as the driver position
if (nullDrivers >= Convert.ToInt32((float)NUMBER_OF_DRIVERS / 4.0f * 3.0f))
{
return "";
}
else
{
return result; return result;
} }
}
public void ChangeImage(Bitmap Image) public void ChangeImage(Bitmap Image)
{ {