using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; 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 { public partial class Form1 : Form { Reader Reader = null; F1TVEmulator Emulator = null; private bool cancelRequested = false; private SemaphoreSlim semaphore = new SemaphoreSlim(1); string ConfigFile = ""; string GpUrl = ""; public Form1() { InitializeComponent(); } public async void RefreshUI() { } private async void btnSettings_Click(object sender, EventArgs e) { if(Emulator != null) Emulator.Stop(); Settings settingsForm = new Settings(); settingsForm.ShowDialog(); //MessageBox.Show(settingsForm.GrandPrixUrl + Environment.NewLine + settingsForm.GrandPrixName + Environment.NewLine + settingsForm.GrandPrixYear); if(settingsForm.GrandPrixUrl != "" && settingsForm.SelectedConfigFile != "") { 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"); } } 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) { //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) { cancelRequested = false; if (Emulator != null && Reader != null) { // Disable UI controls to prevent re-entrancy btnStartDecoding.Enabled = false; btnStopUpdating.Enabled = true; btnSettings.Enabled = false; while (!cancelRequested) { await semaphore.WaitAsync(); try { // Start the time-consuming task on a separate thread await Task.Run(async () => { 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; btnStopUpdating.Enabled = false; btnSettings.Enabled = true; } } private void Form1_FormClosing(object sender, FormClosingEventArgs e) { if (Emulator != null) { Emulator.Stop(); } } 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 = "Decoding done in :" + sw.ElapsedMilliseconds + "ms" + Environment.NewLine; tbxResult.Text += result; pbxResult.Image = screen; } } private void btnStopUpdating_Click(object sender, EventArgs e) { // 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; } } } }