2025-12-22 14:37:43 +00:00
using Renci.SshNet.Common ;
using System ;
using System.Collections.Generic ;
using System.ComponentModel ;
using System.Text ;
namespace AiQ_GUI.AiQ_Tests
{
public class TestingFunctions
{
public static Diags DiagsAPI = new ( ) ;
// Colours
public static readonly Color BtnColour = Color . FromArgb ( 70 , 65 , 80 ) ;
public static readonly Color TxBxColour = Color . FromArgb ( 53 , 51 , 64 ) ;
// ***** Testing functions *****
public static async Task CheckDiagsAPIPt1 ( ) // Parts done on pre and final test
{
DiagsAPI = await FlexiAPI . GetDiagnostics ( MainForm . Instance . CamOnTest . IP ) ; // Diagnostic API request
2025-12-23 13:01:15 +00:00
// Check Flexi Version
string flexiVerText = "Flexi Version = " + DiagsAPI . FlexiVersion ;
bool flexiVerGreen = DiagsAPI . FlexiVersion = = UniversalData . ExpFlexiVer ;
if ( ! flexiVerGreen )
flexiVerText + = " Expected = " + UniversalData . ExpFlexiVer ;
MainForm . Instance . AddLabelToPanel ( flexiVerText , ! flexiVerGreen ) ;
// Check Flexi Revision
string flexiRevText = "Flexi Revision = " + DiagsAPI . FlexiRevision ;
bool flexiRevGreen = DiagsAPI . FlexiRevision = = UniversalData . ExpFlexiRev ;
if ( ! flexiRevGreen )
flexiRevText + = " Expected = " + UniversalData . ExpFlexiRev ;
MainForm . Instance . AddLabelToPanel ( flexiRevText , ! flexiRevGreen ) ;
// Display MAC
string macText = "MAC = " + DiagsAPI . MAC ;
bool macIsRed = false ;
2025-12-22 14:37:43 +00:00
if ( RegexCache . MACRegexNVIDIA ( ) . IsMatch ( DiagsAPI . MAC ) ) // Checks it is in the right format and is a NVIDIA registered MAC address
{
2025-12-23 13:01:15 +00:00
// Valid NVIDIA MAC
2025-12-22 14:37:43 +00:00
}
else if ( RegexCache . MACRegex ( ) . IsMatch ( DiagsAPI . MAC ) ) // Is a valid MAC, but not NVIDIA
{
2025-12-23 13:01:15 +00:00
macIsRed = true ;
2025-12-22 14:37:43 +00:00
MainForm . Instance . AddToActionsList ( $"{DiagsAPI.MAC} not recognised as NVIDIA MAC address" , Level . ERROR ) ;
}
else
{
2025-12-23 13:01:15 +00:00
macIsRed = true ;
2025-12-22 14:37:43 +00:00
MainForm . Instance . AddToActionsList ( $"{DiagsAPI.MAC} not recognised as a MAC address" , Level . ERROR ) ;
}
2025-12-23 13:01:15 +00:00
MainForm . Instance . AddLabelToPanel ( macText , macIsRed ) ;
2025-12-22 14:37:43 +00:00
// Check timestamp
DateTime dateTime = new ( 1970 , 1 , 1 , 0 , 0 , 0 , 0 , DateTimeKind . Utc ) ;
dateTime = dateTime . AddSeconds ( DiagsAPI . timeStamp ) . ToLocalTime ( ) ;
long timediff = DateTimeOffset . UtcNow . ToUnixTimeSeconds ( ) - DiagsAPI . timeStamp ;
2025-12-23 13:01:15 +00:00
string timestampText = "Timestamp = " + dateTime ;
bool timestampIsRed = false ;
2025-12-22 14:37:43 +00:00
if ( timediff > 10 ) // Over 10 seconds ago
{
2025-12-23 13:01:15 +00:00
timestampText + = $" Time behind by {timediff}s" ;
timestampIsRed = true ;
2025-12-22 14:37:43 +00:00
}
else if ( timediff < 0 ) // Time is in the future.
{
2025-12-23 13:01:15 +00:00
timestampText + = $" Time is in the future by {Math.Abs(timediff)}s" ;
timestampIsRed = true ;
2025-12-22 14:37:43 +00:00
}
2025-12-23 13:01:15 +00:00
MainForm . Instance . AddLabelToPanel ( timestampText , timestampIsRed ) ;
2025-12-22 14:37:43 +00:00
2025-12-23 13:01:15 +00:00
// Check Temperature
string tempText = "Temperature = " + DiagsAPI . IntTemperature + "°C" ;
bool tempIsRed = DiagsAPI . IntTemperature < = 20 | | DiagsAPI . IntTemperature > = 70 ;
MainForm . Instance . AddLabelToPanel ( tempText , tempIsRed ) ;
2025-12-22 14:37:43 +00:00
2025-12-23 13:01:15 +00:00
// Check Zoom Lock
string zoomLockText = "Zoom Lock = " + DiagsAPI . zoomLock ;
bool zoomLockIsRed = false ;
2025-12-22 14:37:43 +00:00
if ( DiagsAPI . zoomLock = = true )
{
2025-12-23 13:01:15 +00:00
if ( DiagsAPI . IRmodule . zoom ! = DiagsAPI . OVmodule . zoom ) // Checks if zoomlock is doing what is says it should
2025-12-22 14:37:43 +00:00
{
2025-12-23 13:01:15 +00:00
zoomLockText + = $" Zoomlock on but {DiagsAPI.IRmodule.zoom}≠{DiagsAPI.OVmodule.zoom}" ;
zoomLockIsRed = true ;
2025-12-22 14:37:43 +00:00
}
}
else
2025-12-23 13:01:15 +00:00
zoomLockIsRed = true ;
MainForm . Instance . AddLabelToPanel ( zoomLockText , zoomLockIsRed ) ;
2025-12-22 14:37:43 +00:00
}
public static async Task CheckDiagsAPIPt2 ( ) // Parts only done on final test
{
if ( MainForm . Instance . RhTxBxActions . Text . Contains ( "Error reading JSON" ) ) // If failed to deserialise in part 1
return ;
try // In case serial or model are blank
{
2025-12-23 13:01:15 +00:00
string serialText = "Serial Number = " + DiagsAPI . serialNumber ;
bool serialIsRed = ! RegexCache . SerialRegex ( ) . IsMatch ( DiagsAPI . serialNumber ) ;
MainForm . Instance . AddLabelToPanel ( serialText , serialIsRed ) ;
string modelText = "Model Number = " + DiagsAPI . modelNumber ;
bool modelIsRed = ! RegexCache . ModelRegex ( ) . IsMatch ( DiagsAPI . modelNumber ) ;
MainForm . Instance . AddLabelToPanel ( modelText , modelIsRed ) ;
2025-12-22 14:37:43 +00:00
if ( RegexCache . SerialRegex ( ) . IsMatch ( DiagsAPI . serialNumber ) & & RegexCache . ModelRegex ( ) . IsMatch ( DiagsAPI . modelNumber ) )
{
if ( DiagsAPI . modelNumber ! = MainForm . Instance . CamOnTest . Model )
{
MainForm . Instance . AddToActionsList ( "Model number in camera doesn't match what has been selected" , Level . WARNING ) ;
}
else if ( ! GoogleAPI . CheckWIP ( DiagsAPI . serialNumber , CameraAccessInfo . SpreadsheetID ) ) // Check WIP column in serial number register, if not ticked then RMA
{
MainForm . Instance . CamOnTest . RMANum = GoogleAPI . CheckRMANum ( DiagsAPI . serialNumber , DiagsAPI . modelNumber ) ; // Corrected by qualifying with the type name
if ( MainForm . Instance . CamOnTest . RMANum = = 0 ) // Couldn't find RMA num in spreadsheet
{
MainForm . Instance . CamOnTest . RMANum = Convert . ToInt32 ( await MainForm . Instance . DisplayInput ( "What is the RMA number?" ) ) ;
if ( MainForm . Instance . CamOnTest . RMANum = = - 1 ) // Means they chose the 'I don't know' option
await MainForm . Instance . TestFailed ( MainForm . Instance . BtnStartTest , "Please get RMA number from operations team before continuing" ) ;
}
// Found RMA num and want to verify it with user
else if ( ! await MainForm . Instance . DisplayQuestion ( $"Is {MainForm.Instance.CamOnTest.RMANum} the RMA Number?" ) ) // '!' because if its not the right RMA number let the user to it manually
{
MainForm . Instance . CamOnTest . RMANum = Convert . ToInt32 ( await MainForm . Instance . DisplayInput ( "What is the RMA number?" ) ) ;
if ( MainForm . Instance . CamOnTest . RMANum = = - 1 ) // Means they chose the 'I don't know' option
await MainForm . Instance . TestFailed ( MainForm . Instance . BtnStartTest , "Please get RMA number from operations team before continuing" ) ;
}
}
}
else
{
MainForm . Instance . AddToActionsList ( "Camera has not been given a valid serial and model number, suggest you run through pre test again and check serial number register for any issues." , Level . ERROR ) ;
}
}
2025-12-23 13:01:15 +00:00
catch ( Exception ex )
{
MainForm . Instance . AddToActionsList ( $"Error in CheckDiagsAPIPt2 serial/model: {ex.Message}" , Level . ERROR ) ;
}
2025-12-22 14:37:43 +00:00
// Check licenses
List < string > licensesOnCam = [ ] ; // Temporary list for licenses on camera
if ( DiagsAPI . licenses . saf1 )
licensesOnCam . Add ( "SAF1" ) ;
if ( DiagsAPI . licenses . saf2 )
licensesOnCam . Add ( "SAF2" ) ;
if ( DiagsAPI . licenses . saf3 )
licensesOnCam . Add ( "SAF3" ) ;
if ( DiagsAPI . licenses . saf4 )
licensesOnCam . Add ( "SAF4" ) ;
if ( DiagsAPI . licenses . audit )
licensesOnCam . Add ( "Audit" ) ;
if ( DiagsAPI . licenses . stream )
licensesOnCam . Add ( "Stream" ) ;
2025-12-23 13:01:15 +00:00
if ( MainForm . Instance . sshData ! = null & & MainForm . Instance . sshData . tailscale )
2025-12-22 14:37:43 +00:00
licensesOnCam . Add ( "Tailscale" ) ;
2025-12-23 13:01:15 +00:00
string licText = "Licenses = " + ( licensesOnCam . Count = = 0 ? "None" : string . Join ( ", " , licensesOnCam ) ) ;
MainForm . Instance . AddLabelToPanel ( licText , false ) ;
2025-12-22 14:37:43 +00:00
double CPUround = Math . Round ( DiagsAPI . CPUusage ) ; // Check CPU usage isn't near max
2025-12-23 13:01:15 +00:00
string cpuText = "CPU Usage = " + CPUround + "%" ;
bool cpuIsRed = CPUround < = 50 | | CPUround > = 98 ;
MainForm . Instance . AddLabelToPanel ( cpuText , cpuIsRed ) ;
2025-12-22 14:37:43 +00:00
// Check Vaxtor if it doesn't need or have license OR has and wants one then pass
2025-12-23 13:01:15 +00:00
bool vaxtorIsRed = false ;
string vaxtorText = "Vaxtor Key ID = " + DiagsAPI . licenses . raptorKeyID ;
2025-12-22 14:37:43 +00:00
if ( CameraAccessInfo . VaxtorLic = = false & & DiagsAPI . licenses . raptorKeyID = = "Not Licensed" | | CameraAccessInfo . VaxtorLic = = true & & DiagsAPI . licenses . raptorKeyID ! = "Not Licensed" )
{
2025-12-23 13:01:15 +00:00
// OK - passes condition
vaxtorIsRed = false ;
2025-12-22 14:37:43 +00:00
}
else if ( await MainForm . Instance . DisplayQuestion ( "Was this camera licensed manually?" ) )
{
string ProdcutKeyID = await MainForm . Instance . DisplayInput ( "What is the Key ID?" , false ) ;
2025-12-23 13:01:15 +00:00
if ( ! RegexCache . VaxtorRegex ( ) . IsMatch ( ProdcutKeyID ) ) // Means they chose valid Key ID
{
DiagsAPI . licenses . raptorKeyID = MainForm . Instance . TxBxProductKey . Text ;
vaxtorText = "Vaxtor Key ID = " + DiagsAPI . licenses . raptorKeyID ;
vaxtorIsRed = false ;
}
else
2025-12-22 14:37:43 +00:00
{
Access . Stats ( "Please Get A Valid Vaxtor Product Key Before Continuing" , MainForm . Instance . CamOnTest . Model ) ;
await MainForm . Instance . TestFailed ( MainForm . Instance . BtnStartTest , "Please get a valid Vaxtor Product Key before continuing" ) ;
2025-12-23 13:01:15 +00:00
vaxtorIsRed = true ;
2025-12-22 14:37:43 +00:00
}
}
2025-12-23 13:01:15 +00:00
else // Should have license but doesn't OR shouldn't have but does + any other unforeseen circumstance then fail
2025-12-22 14:37:43 +00:00
{
2025-12-23 13:01:15 +00:00
vaxtorIsRed = true ;
2025-12-22 14:37:43 +00:00
}
2025-12-23 13:01:15 +00:00
MainForm . Instance . AddLabelToPanel ( vaxtorText , vaxtorIsRed ) ;
2025-12-22 14:37:43 +00:00
// Check trim is within 10% both horizontally and vertically, from auto set done earlier in the test
2025-12-23 13:01:15 +00:00
string trimText = "Trim = H: " + DiagsAPI . trim [ 0 ] + " V: " + DiagsAPI . trim [ 1 ] ;
2025-12-22 14:37:43 +00:00
// Offset accounted for in the SetTrim function, so value should be close to 0,0.
const int HMax = 96 ; // 5% of 1920 each way = ±96
const int VMax = 54 ; // 5% of 1080 each way = ±54
2025-12-23 13:01:15 +00:00
bool trimIsRed = Math . Abs ( DiagsAPI . trim [ 0 ] ) > HMax | | Math . Abs ( DiagsAPI . trim [ 1 ] ) > VMax ;
MainForm . Instance . AddLabelToPanel ( trimText , trimIsRed ) ;
2025-12-22 14:37:43 +00:00
}
}
}