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 { //private Reader Reader = null; private F1TVEmulator Emulator = null; private DataWrapper Wrapper = null; private bool cancelRequested = false; private SemaphoreSlim semaphore = new SemaphoreSlim(1); string ConfigFile = ""; string GpUrl = ""; public Form1() { InitializeComponent(); } public async void RefreshUI() { if (Directory.Exists(ConfigurationTool.CONFIGS_FOLDER_NAME)) { lsbPresets.DataSource = null; lsbPresets.DataSource = Directory.GetFiles(ConfigurationTool.CONFIGS_FOLDER_NAME); } } private async void btnSettings_Click(object sender, EventArgs e) { if (Emulator != null) Emulator.ResetDriver(); Emulator = null; Wrapper = null; GC.Collect(); 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"; textBox1.Text = GpUrl; RefreshUI(); } private async void btnUpdate_Click(object sender, EventArgs e) { cancelRequested = false; if (Emulator != null && Wrapper != null) { // Disable UI controls to prevent re-entrancy btnResetEmulator.Enabled = false; 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"); Invoke((MethodInvoker)delegate { pbxResult.Image = (Bitmap)screen.Clone(); }); Wrapper.ChangeImage(screen); int errorCode = Wrapper.Refresh(); sw.Stop(); // Task completed Invoke((MethodInvoker)delegate { DisplayResults(errorCode, sw, screen); DisplayBattles(); }); }); } finally { semaphore.Release(); } } // Re-enable UI controls btnStopUpdating.Text = "Stop"; btnStartDecoding.Enabled = true; btnStopUpdating.Enabled = false; btnResetEmulator.Enabled = true; btnSettings.Enabled = true; } } private void DisplayBattles() { Wrapper.DisplayBattles(pnlBattles,this); } private void Form1_FormClosing(object sender, FormClosingEventArgs e) { if (Emulator != null) { Emulator.Stop(); } } private void DisplayResults(int errorCode, Stopwatch sw, Bitmap screen) { if (errorCode != 0) { 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 { Wrapper.DisplayLiveRanking(pnlLiveRanking, this); } } private void btnStopUpdating_Click(object sender, EventArgs e) { // Set the cancellation flag cancelRequested = true; btnStopUpdating.Enabled = false; btnResetEmulator.Enabled = false; btnStopUpdating.Text = "Stopping"; } private async void button1_Click(object sender, EventArgs e) { btnResetEmulator.Text = "Restart driver"; btnResetEmulator.Enabled = false; btnSettings.Enabled = true; btnStartDecoding.Enabled = false; btnStopUpdating.Enabled = false; int errorCode = -1; await Task.Run(async () => { if (Emulator != null) Emulator.ResetDriver(); Emulator = null; Wrapper = null; GC.Collect(); 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 { Wrapper = new DataWrapper(ConfigFile, Emulator.Screenshot()); btnResetEmulator.Enabled = true; btnSettings.Enabled = true; btnStartDecoding.Enabled = true; } } private void lsbPresets_SelectedIndexChanged(object sender, EventArgs e) { if (lsbPresets.SelectedIndex >= 0) ConfigFile = lsbPresets.Items[lsbPresets.SelectedIndex].ToString(); } private void textBox1_TextChanged(object sender, EventArgs e) { if (textBox1.Text != "") GpUrl = textBox1.Text; } public void btnDriver_Click(object sender, EventArgs e) { //Happens when a driver button has been clicked //MessageBox.Show((sender as Button).Name + " has been selected"); Button btn = (sender as Button); string[] parts = btn.Name.Split('_'); DriverData driver = Wrapper.GetFullDriverData(parts[0], pnlCurrentDriverLapsHistory,this); lblCurrentDriverName.Text = driver.Name; lblCurrentDriverPosition.Text = driver.Position.ToString(); lblCurrentDriverGapToLeader.Text = Reader.ConvertMsToTime(driver.GapToLeader); lblCurrentDriverLapTime.Text = Reader.ConvertMsToTime(driver.LapTime); lblCurrentDriverTyreAge.Text = driver.CurrentTyre.NumberOfLaps.ToString(); if (driver.DRS) { lblCurrentDriverDRS.Text = "Open"; lblCurrentDriverDRS.ForeColor = Color.FromArgb(0, 164, 46); } else { lblCurrentDriverDRS.Text = "Closed"; lblCurrentDriverDRS.ForeColor = Color.Black; } switch (driver.CurrentTyre.Coumpound) { case Tyre.Type.Undefined: lblCurrentDriverTyreType.Text = "uuuuh..."; lblCurrentDriverTyreType.ForeColor = Color.Violet; break; case Tyre.Type.Hard: lblCurrentDriverTyreType.Text = "Hard"; lblCurrentDriverTyreType.ForeColor = Color.FromArgb(164, 165, 168); break; case Tyre.Type.Medium: lblCurrentDriverTyreType.Text = "Medium"; lblCurrentDriverTyreType.ForeColor = Color.FromArgb(245, 191, 0); break; case Tyre.Type.Soft: lblCurrentDriverTyreType.Text = "Soft"; lblCurrentDriverTyreType.ForeColor = Color.FromArgb(255, 0, 0); break; case Tyre.Type.Inter: lblCurrentDriverTyreType.Text = "Intermediate"; lblCurrentDriverTyreType.ForeColor = Color.FromArgb(0, 164, 46); break; case Tyre.Type.Wet: lblCurrentDriverTyreType.Text = "Wet"; lblCurrentDriverTyreType.ForeColor = Color.FromArgb(39, 96, 166); break; } } public void btnLapTime_Click(object sender, EventArgs e) { //Happens when a lapTime has been clicked Button btn = sender as Button; string[] parts = btn.Name.Split('_'); Wrapper.DisplayLapTimeInfos(parts[0], Convert.ToInt32(parts[1]),btn.Text); } } }