2025-09-02 15:32:24 +01:00
using System.Data.OleDb ;
namespace AiQ_GUI
{
class Access
{
2025-12-19 16:14:13 +00:00
public const string connString =
@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=G:\Shared drives\MAV Production GUI's\AiQ\GUI's\AiQ_Final_Test.accdb;Persist Security Info=False;OLE DB Services=-1;" ;
2025-09-02 15:32:24 +01:00
2025-12-19 16:14:13 +00:00
// Allows Different Cam Types with different schemas to be handled safely
private static bool HasColumn ( OleDbDataReader reader , string columnName )
2025-09-02 15:32:24 +01:00
{
2025-12-19 16:14:13 +00:00
for ( int i = 0 ; i < reader . FieldCount ; i + + )
if ( reader . GetName ( i ) . Equals ( columnName , StringComparison . OrdinalIgnoreCase ) )
return true ;
return false ;
}
public static string [ ] ReadCamTypes ( string camType )
{
// No camera type selected
if ( string . IsNullOrWhiteSpace ( camType ) )
return null ;
// Preallocate list to reduce resizing
List < Tuple < string , string > > modelTuples = new ( 30 ) ;
2025-09-02 15:32:24 +01:00
using OleDbConnection conn = new ( connString ) ;
2025-12-19 16:14:13 +00:00
// Attempt to open the database
2025-09-02 15:32:24 +01:00
try
{
conn . Open ( ) ;
}
catch
{
MessageBox . Show ( "Could not access Access in google drive. Is it running?" ) ;
return null ;
}
2025-12-19 16:14:13 +00:00
// Mobile table does not have MarkNumber
// AiQ and others might do ????? - TODO ask
string query = camType = = "Mobile"
? "SELECT ModelNumber, Description FROM [Mobile]"
: $"SELECT ModelNumber, Description FROM [{camType}] WHERE MarkNumber > 1" ;
2025-09-02 15:32:24 +01:00
using OleDbCommand cmd = new ( query , conn ) ;
using OleDbDataReader reader = cmd . ExecuteReader ( ) ;
2025-12-19 16:14:13 +00:00
// Read all models from the selected table
2025-09-02 15:32:24 +01:00
while ( reader . Read ( ) )
{
string modelNumber = reader [ "ModelNumber" ] as string ? ? string . Empty ;
string description = reader [ "Description" ] as string ? ? string . Empty ;
2025-12-19 16:14:13 +00:00
2025-09-02 15:32:24 +01:00
modelTuples . Add ( Tuple . Create ( modelNumber . Trim ( ) , description . Trim ( ) ) ) ;
}
2025-12-19 16:14:13 +00:00
// Sort models, pushing AB12CD to the bottom
var sorted = modelTuples
. OrderBy ( t = > t . Item1 . Equals ( "AB12CD" , StringComparison . OrdinalIgnoreCase ) ? 1 : 0 )
. ThenBy ( t = > t . Item1 , StringComparer . OrdinalIgnoreCase ) ;
2025-09-02 15:32:24 +01:00
2025-12-19 16:14:13 +00:00
// Format for combo box display
2025-09-02 15:32:24 +01:00
return sorted . Select ( t = > $"{t.Item1} - {t.Item2}" ) . ToArray ( ) ;
}
public static void ReadUniData ( )
{
using OleDbConnection conn = new ( connString ) ;
2025-12-19 16:14:13 +00:00
2025-09-02 15:32:24 +01:00
try
{
conn . Open ( ) ;
}
catch
{
MessageBox . Show ( "Could not access Access in google drive. Is it running?" ) ;
return ;
}
2025-12-19 16:14:13 +00:00
const string query =
2026-01-05 12:35:28 +00:00
"SELECT FlexiVersion, FlexiRevision, WonwooFirmware, AiQGUIVersion, PowerConsumption, LicencingServerURL, SRZFirmware FROM UniversalData" ;
2025-09-02 15:32:24 +01:00
using OleDbCommand cmd = new ( query , conn ) ;
using OleDbDataReader reader = cmd . ExecuteReader ( ) ;
2025-12-19 16:14:13 +00:00
// UniversalData is expected to contain a single row
2025-09-02 15:32:24 +01:00
reader . Read ( ) ;
2025-12-19 16:14:13 +00:00
2025-09-02 15:32:24 +01:00
UniversalData . ExpFlexiVer = Convert . ToString ( reader [ "FlexiVersion" ] ) ;
UniversalData . ExpFlexiRev = Convert . ToString ( reader [ "FlexiRevision" ] ) ;
UniversalData . WonwooFirmware = Convert . ToString ( reader [ "WonwooFirmware" ] ) ;
UniversalData . LatestVersion = Convert . ToString ( reader [ "AiQGUIVersion" ] ) ;
UniversalData . PowerConsumption = Convert . ToInt16 ( reader [ "PowerConsumption" ] ) ;
UniversalData . LicencingServerURL = Convert . ToString ( reader [ "LicencingServerURL" ] ) ;
2026-01-05 12:35:28 +00:00
UniversalData . SRZFirmware = Convert . ToString ( reader [ "SRZFirmware" ] ) ;
2025-09-02 15:32:24 +01:00
}
2025-12-19 16:14:13 +00:00
// Populates CameraAccessInfo dynamically based on available columns
public static void ReadModelRow ( string camType , string ModelOnTest )
2025-09-02 15:32:24 +01:00
{
using OleDbConnection conn = new ( connString ) ;
2025-12-19 16:14:13 +00:00
2025-09-02 15:32:24 +01:00
try
{
conn . Open ( ) ;
}
catch
{
MessageBox . Show ( "Could not access Access in google drive. Is it running?" ) ;
return ;
}
2025-12-19 16:14:13 +00:00
// Parameterised query to prevent injection
string query = $"SELECT * FROM [{camType}] WHERE ModelNumber = ?" ;
2025-09-02 15:32:24 +01:00
using OleDbCommand cmd = new ( query , conn ) ;
2025-12-19 16:14:13 +00:00
cmd . Parameters . AddWithValue ( "?" , ModelOnTest ) ;
2025-09-02 15:32:24 +01:00
using OleDbDataReader reader = cmd . ExecuteReader ( ) ;
2025-12-19 16:14:13 +00:00
// No matching model found
if ( ! reader . Read ( ) )
return ;
2025-09-02 15:32:24 +01:00
2025-12-19 16:14:13 +00:00
// Populate CameraAccessInfo only if columns exist
CameraAccessInfo . Processor =
HasColumn ( reader , "Processor" ) ? Convert . ToString ( reader [ "Processor" ] ) : string . Empty ;
CameraAccessInfo . VaxtorLic =
HasColumn ( reader , "Vaxtor" ) & & Convert . ToString ( reader [ "Vaxtor" ] ) = = "Yes" ;
CameraAccessInfo . HardwareExtras =
HasColumn ( reader , "HardwareExtras" ) ? Convert . ToString ( reader [ "HardwareExtras" ] ) : string . Empty ;
CameraAccessInfo . PowerType =
HasColumn ( reader , "PowerType" ) ? Convert . ToString ( reader [ "PowerType" ] ) : string . Empty ;
CameraAccessInfo . LED_V =
HasColumn ( reader , "LEDVoltage" ) ? Convert . ToDouble ( reader [ "LEDVoltage" ] ) : 0 ;
CameraAccessInfo . LED_I =
HasColumn ( reader , "LEDCurrent" ) ? Convert . ToInt32 ( reader [ "LEDCurrent" ] ) : 0 ;
CameraAccessInfo . SpreadsheetID =
HasColumn ( reader , "SSID" ) ? Convert . ToString ( reader [ "SSID" ] ) : string . Empty ;
}
2025-10-21 15:46:28 +01:00
public static void Stats ( string TypeOfTest , string modelNumber )
{
Stats ( [ TypeOfTest ] , modelNumber ) ;
}
public static void Stats ( string [ ] TypeOfTest , string modelNumber )
{
2025-12-19 16:14:13 +00:00
using OleDbConnection conn = new ( connString ) ;
2025-10-21 15:46:28 +01:00
try
{
2025-12-19 16:14:13 +00:00
conn . Open ( ) ;
2025-10-21 15:46:28 +01:00
foreach ( string type in TypeOfTest )
{
2025-12-19 16:14:13 +00:00
string query =
$"UPDATE AiQ SET [{type}] = [{type}] + 1 WHERE [ModelNumber] = ?" ;
using OleDbCommand cmd = new ( query , conn ) ;
cmd . Parameters . AddWithValue ( "?" , modelNumber ) ;
2025-10-21 15:46:28 +01:00
int rowsAffected = cmd . ExecuteNonQuery ( ) ;
2025-12-19 16:14:13 +00:00
if ( rowsAffected = = 0 )
MainForm . Instance . AddToActionsList ( $"No rows affected for {modelNumber}" , Level . ERROR ) ;
2025-10-21 15:46:28 +01:00
}
}
catch
{
2025-12-19 16:14:13 +00:00
MainForm . Instance . AddToActionsList (
"Could not access Access in Google Drive. Is it running?" , Level . WARNING ) ;
2025-10-21 15:46:28 +01:00
}
}
public static void StatsDiags ( string redDiagLabels , string RhTxBxActionsText , string ModelNumber )
{
using OleDbConnection conn = new ( connString ) ;
conn . Open ( ) ;
2025-12-19 16:14:13 +00:00
// Replace null or empty values
2025-10-21 15:46:28 +01:00
string redVal = string . IsNullOrWhiteSpace ( redDiagLabels ) ? "-" : redDiagLabels ;
string actVal = string . IsNullOrWhiteSpace ( RhTxBxActionsText ) ? "-" : RhTxBxActionsText ;
string model = string . IsNullOrWhiteSpace ( ModelNumber ) ? "-" : ModelNumber ;
2025-12-19 16:14:13 +00:00
const string sql =
@ "INSERT INTO DiagsStats ([Date], [Model], [Red Diags Labels], [RhTxBxActions Contents])
VALUES ( ? , ? , ? , ? ) ";
2025-10-21 15:46:28 +01:00
using OleDbCommand cmd = new ( sql , conn ) ;
cmd . Parameters . Add ( new OleDbParameter
{
OleDbType = OleDbType . Date ,
Value = DateTime . Now
} ) ;
2025-12-19 16:14:13 +00:00
2025-10-21 15:46:28 +01:00
cmd . Parameters . AddWithValue ( "?" , model ) ;
cmd . Parameters . AddWithValue ( "?" , redVal ) ;
cmd . Parameters . AddWithValue ( "?" , actVal ) ;
int rows = cmd . ExecuteNonQuery ( ) ;
2025-11-26 14:39:07 +00:00
if ( rows = = 0 )
2025-12-19 16:14:13 +00:00
MainForm . Instance . AddToActionsList (
"No rows inserted into DiagsStats (unexpected)." , Level . ERROR ) ;
2025-10-21 15:46:28 +01:00
}
}
2025-09-02 15:32:24 +01:00
public class UniversalData
{
public static string ExpFlexiVer { get ; set ; } = string . Empty ;
public static string ExpFlexiRev { get ; set ; } = string . Empty ;
public static string WonwooFirmware { get ; set ; } = string . Empty ;
public static string LatestVersion { get ; set ; } = string . Empty ;
public static int PowerConsumption { get ; set ; } = 0 ;
public static string LicencingServerURL { get ; set ; } = string . Empty ;
2025-12-22 14:37:43 +00:00
public static string SRZFirmware { get ; set ; } = string . Empty ;
2025-09-02 15:32:24 +01:00
}
public class CameraAccessInfo
{
public static string Processor { get ; set ; } = string . Empty ;
public static bool VaxtorLic { get ; set ; } = false ;
public static string HardwareExtras { get ; set ; } = string . Empty ;
public static string PowerType { get ; set ; } = string . Empty ;
public static double LED_V { get ; set ; } = 0 ;
public static int LED_I { get ; set ; } = 0 ;
public static string SpreadsheetID { get ; set ; } = string . Empty ;
}
}