using AiQ_GUI.AiQ_Tests; using Newtonsoft.Json; using System.ComponentModel; using System.Diagnostics; using System.Net.Http.Json; using System.Reflection; using System.Text.Json; using System.Windows.Forms; namespace AiQ_GUI { public enum Level { ERROR, WARNING, LOG, Success } public partial class MainForm : Form { // Classes LocalDataStore localDataStore = new(); //public Diags DiagsAPI = new(); VaxtorLic VaxtorLicResp = new(); public Versions Vers = new(); public readonly Camera CamOnTest = new(); public SSHData sshData = new(); private List soakCameraList = []; private List soakCtsList = []; private List soakTasks = []; // Colours public static readonly Color BtnColour = Color.FromArgb(70, 65, 80); public static readonly Color TxBxColour = Color.FromArgb(53, 51, 64); [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)] public static MainForm? Instance { get; private set; } private TabPage CurrentTab; // remember what's currently inserted public MainForm() { InitializeComponent(); Instance = this; } private async void AiQGUI_Load(object sender, EventArgs e) { Task? closeProcessesTask = Windows.CloseProcesses(); // Fire and forget closing other apps Windows.UpdateFirewall(); Task UniDataTask = Task.Run(() => Access.ReadUniData()); // Get universal data Task LDSWAIT = Task.Run(() => LDS.GetLDS()); // Get and deserialise LDS.json Task guiVerTask = Task.Run(() => GUIUpdate.FindGUIVersion()); // Get GUI Version Network.Initialize("admin", "admin"); // Initialise HTTP client with basic auth creds. if (await Network.PingIP("8.8.8.8")) // Ping to check if we're online { if (!GoogleAPI.Setup()) AddToActionsList("Cannot setup Google API", Level.WARNING); } else { Flags.Offline = true; BtnStartTest.Text = "Offline Mode"; } // Hide on default so will only show based on the camera type selected later TabImagesandSettings.TabPages.Remove(TabSoak); TabImagesandSettings.TabPages.Remove(Mobile); GUIUpdate.GUIVerShort = await guiVerTask; // Guess the GUI version will be first to finish this.Name = "AiQ GUI V" + GUIUpdate.GUIVerShort; LblGUIVers.Text += GUIUpdate.GUIVerShort; await UniDataTask; // Have to wait for expected GUI version to compare to. GUIUpdate.UpdateGUI(); // Check if a GUI update is available // Load local data store localDataStore = await LDSWAIT; Logging.LogMessage("Opening GUI"); // Done after LDS to make sure directory exists. if (localDataStore == null) { AddToActionsList("Could not deserialise LDS.json please help!", Level.WARNING); return; } Task CheckHWOnline = PingCheck(); // Async check all hardware is online PopulateUIWithLDS(localDataStore); // Update fields that depend on LDS CbBxCameraModel.Text = localDataStore.LastModel; // Set last model that was tested into combobox BtnSave.BackColor = BtnSave.Enabled ? Color.ForestGreen : BtnSave.BackColor; // Sets the colour of the save button depending on if it is enabled. BtnRefreshUnix_Click(sender, e); // Reset timestamp if (RegexCache.FlexiVerRegex().IsMatch(UniversalData.ExpFlexiVer)) // Update Flexi version from universal data TxBxFlexiVer.Text = UniversalData.ExpFlexiVer; await CheckHWOnline; Flags.Start = false; } private void InsertCamTab(string camType) { // Remove previously inserted tab if present if (CurrentTab != null && TabImagesandSettings.TabPages.Contains(CurrentTab)) TabImagesandSettings.TabPages.Remove(CurrentTab); // Choose desired tab TabPage desired = null; if (camType == "Mobile") desired = Mobile; else if (camType == "AiQ") desired = TabSoak; // Insert desired tab if any if (desired != null && !TabImagesandSettings.TabPages.Contains(desired)) { int idx = Math.Min(3, TabImagesandSettings.TabPages.Count); TabImagesandSettings.TabPages.Insert(idx, desired); CurrentTab = desired; } else { CurrentTab = desired; // may be null } } private async void CbBxCamType_SelectedIndexChanged(object sender, EventArgs e) { CbBxCameraModel.Items.Clear(); var camType = CbBxCamType.Text; InsertCamTab(camType); var models = await Task.Run(() => Access.ReadCamTypes(camType)); if (models != null && models.Length > 0) CbBxCameraModel.Items.AddRange(models); } private void PopulateUIWithLDS(LocalDataStore lds) { CbBxUserName.Text = lds.User; TxBxPsuIP.Text = PSU.PSUIP = lds.PSUIP; TxBxEzIP.Text = Ez.PoEPowerIP = lds.EzIP; TxBxZebraIP.Text = Printer.ZebraIP = lds.ZebraIP; TxBxTestTubeIP.Text = TestTube.TTPiPicoIP = lds.TestTubeIP; CbBxShutter.SelectedIndex = lds.Shutter; CbBxIris.SelectedIndex = lds.Iris; CbBxGain.SelectedIndex = lds.Gain; if (lds.User == "Bradley" || lds.User == "Sophie") BtnTest.Visible = true; TxBxCheckValid(TxBxPsuIP); // Set save button color if valid } private async void MainForm_Shown(object sender, EventArgs e) // Done on show as all UI elements are loaded in properly for find cams to use { BtnFindCams_Click(sender, e); CheckPrintCapable(); // Check if the print buttons can be enabled TestStartConditions(); await Task.Delay(1000); // Delay for UI to catch up if (LblPSUPing.ForeColor == Color.ForestGreen) // Check state of PSU if it is connected. Task.Run(() => PSU.DisplayState(PSU.PSUIP)); } // ***** Test buttons ***** private async void BtnStartTest_Click(object sender, EventArgs e) { // Show user test has started BtnStartTest.BackColor = Color.Orange; BtnStartTest.Text = "Test underway"; BtnPreTest.Enabled = BtnStartTest.Enabled = false; // Disable buttons to stop user rnning multiple tests at the same time. Logging.LogMessage("Final Test Started"); if (CbBxCamType.Text == "AiQ") { await AiQTests.AiQFinalTest(); } else if (CbBxCamType.Text == "Mobile") { await PreTestPassed(); } } private async void BtnPreTest_Click(object sender, EventArgs e) { // Show user test has started BtnPreTest.BackColor = Color.Orange; BtnPreTest.Text = "Pre-Test underway"; BtnPreTest.Enabled = BtnStartTest.Enabled = false; // Disable buttons to stop user running multiple tests at the same time. Logging.LogMessage("Pre Test Started"); if (CbBxCamType.Text == "AiQ") { await AiQTests.AiQPreTest(); } else if (CbBxCamType.Text == "Mobile") { await PreTestPassed(); } } // ***** Pass/Fails ***** public async Task TestPassed(DateTime PCTime) { // Updates Vaxtor versions, licenses and unticks WIP string err = GoogleAPI.UpdateSpreadSheetFinalTest(CameraAccessInfo.SpreadsheetID, TestingFunctions.DiagsAPI, sshData, CamOnTest.RMANum); if (err != string.Empty) // If there is an error message, display it AddToActionsList("Failed to write to spreadsheet " + err, Level.ERROR); // Purge camera of all reads await FlexiAPI.APIHTTPRequest("/api/purge-all", CamOnTest.IP); if (await DisplayQuestion("Do you want to set this camera to 211 and God mode off?")) { // Turn off God mode string[,] GOD_JSON = { { "propGodMode", "false" } }; string IntConf = await FlexiAPI.HTTP_Update("GLOBAL--FlexiApplication", CamOnTest.IP, GOD_JSON); if (!IntConf.Contains("\"propGodMode\": {\"value\": \"false\", \"datatype\": \"boolean\"},")) AddToActionsList("Could not turn off God mode", Level.WARNING); Thread Thr211 = new(async () => { if (!await FlexiAPI.ChangeNetwork211(CamOnTest.IP)) // Change camera IP to 192.168.1.211. Waits for camera to come back. AddToActionsList("Could not find camera at 192.168.1.211. Please check manually", Level.WARNING); }); Thr211.IsBackground = true; Thr211.Start(); } this.Refresh(); // To make sure all labels are up to date before reading them string fulltestvalues = $"GUI Version = {GUIUpdate.GUIVerShort}\n" + string.Join("\n", PnlLbls.Controls .OfType