Compare commits
4 Commits
c18514802c
...
f95e958ffb
| Author | SHA1 | Date | |
|---|---|---|---|
| f95e958ffb | |||
| 95fb5d6ab8 | |||
| b59d023887 | |||
| a2f86625be |
2
AiQ_GUI.Designer.cs
generated
2
AiQ_GUI.Designer.cs
generated
@@ -270,7 +270,7 @@ namespace AiQ_GUI
|
|||||||
CbBxUserName.Font = new Font("Segoe UI Semibold", 10F, FontStyle.Bold);
|
CbBxUserName.Font = new Font("Segoe UI Semibold", 10F, FontStyle.Bold);
|
||||||
CbBxUserName.ForeColor = SystemColors.Control;
|
CbBxUserName.ForeColor = SystemColors.Control;
|
||||||
CbBxUserName.FormattingEnabled = true;
|
CbBxUserName.FormattingEnabled = true;
|
||||||
CbBxUserName.Items.AddRange(new object[] { "Guest", "Clive", "Conor", "Charlie", "Henry", "Sam C", "Sam L", "Simon", "Toby", "Tom" });
|
CbBxUserName.Items.AddRange(new object[] { "Guest", "Clive", "Conor", "Charlie", "Henry", "Sam C", "Sam L", "Simon", "Sophie", "Toby", "Tom" });
|
||||||
CbBxUserName.Location = new Point(10, 262);
|
CbBxUserName.Location = new Point(10, 262);
|
||||||
CbBxUserName.Margin = new Padding(4, 3, 4, 3);
|
CbBxUserName.Margin = new Padding(4, 3, 4, 3);
|
||||||
CbBxUserName.MaxDropDownItems = 20;
|
CbBxUserName.MaxDropDownItems = 20;
|
||||||
|
|||||||
115
AiQ_GUI.cs
115
AiQ_GUI.cs
@@ -1,5 +1,6 @@
|
|||||||
using Newtonsoft.Json;
|
using Newtonsoft.Json;
|
||||||
using System.ComponentModel;
|
using System.ComponentModel;
|
||||||
|
using System.Data.OleDb;
|
||||||
using System.Diagnostics;
|
using System.Diagnostics;
|
||||||
using System.Reflection;
|
using System.Reflection;
|
||||||
|
|
||||||
@@ -103,7 +104,7 @@ namespace AiQ_GUI
|
|||||||
CbBxIris.SelectedIndex = lds.Iris;
|
CbBxIris.SelectedIndex = lds.Iris;
|
||||||
CbBxGain.SelectedIndex = lds.Gain;
|
CbBxGain.SelectedIndex = lds.Gain;
|
||||||
|
|
||||||
if (lds.User == "Bradley")
|
if (lds.User == "Bradley" || lds.User == "Sophie")
|
||||||
BtnTest.Visible = true;
|
BtnTest.Visible = true;
|
||||||
|
|
||||||
TxBxCheckValid(TxBxPsuIP); // Set save button color if valid
|
TxBxCheckValid(TxBxPsuIP); // Set save button color if valid
|
||||||
@@ -199,6 +200,7 @@ namespace AiQ_GUI
|
|||||||
if (!LEDreply.Contains("Power levels set successfully"))
|
if (!LEDreply.Contains("Power levels set successfully"))
|
||||||
AddToActionsList($"LED level could not be set: {LEDreply}");
|
AddToActionsList($"LED level could not be set: {LEDreply}");
|
||||||
}
|
}
|
||||||
|
await FlexiAPI.SetVaxtorMinMaxPlate(CamOnTest.IP);
|
||||||
|
|
||||||
DateTime PCTime = DateTime.Now; // Grab PC time as close to the API as possible to pass onto PDF later
|
DateTime PCTime = DateTime.Now; // Grab PC time as close to the API as possible to pass onto PDF later
|
||||||
|
|
||||||
@@ -225,6 +227,66 @@ namespace AiQ_GUI
|
|||||||
|
|
||||||
await TestPassed(PCTime);
|
await TestPassed(PCTime);
|
||||||
}
|
}
|
||||||
|
public void Stats(string TypeOfTest)
|
||||||
|
{
|
||||||
|
Stats([TypeOfTest]);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Stats(string[] TypeOfTest)
|
||||||
|
{
|
||||||
|
using OleDbConnection conn = new(Access.connString); // Opens connection to Access database
|
||||||
|
try
|
||||||
|
{
|
||||||
|
conn.Open(); // Opens DB
|
||||||
|
string modelNumber = CbBxCameraType.Text.Substring(0, 6); // Get model number from combobox and make sure it is only 6 characters.
|
||||||
|
|
||||||
|
foreach (string type in TypeOfTest)
|
||||||
|
{
|
||||||
|
string query = $"UPDATE AiQ SET [{type}] = [{type}] + 1 WHERE [ModelNumber] = ?"; // Add one for every test ran of this type for this model number
|
||||||
|
using OleDbCommand cmd = new(query, conn); // Create command
|
||||||
|
cmd.Parameters.AddWithValue("?", modelNumber); // Add model number to prevent injection
|
||||||
|
|
||||||
|
int rowsAffected = cmd.ExecuteNonQuery();
|
||||||
|
// Execute the command and get the number of rows affected
|
||||||
|
//if (rowsAffected > 0) // If one or more rows were updated
|
||||||
|
// AddToActionsList($"Updated {TypeOfTest} for {modelNumber}");
|
||||||
|
//else
|
||||||
|
// AddToActionsList($"No rows found for {modelNumber}");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch
|
||||||
|
{
|
||||||
|
AddToActionsList("Could not access Access in Google Drive. Is it running?");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void StatsDiags(string redDiagLabels, string RhTxBxActionsText, string IsRMA, int RMA)
|
||||||
|
{
|
||||||
|
using OleDbConnection conn = new(Access.connString);
|
||||||
|
conn.Open();
|
||||||
|
|
||||||
|
// Null checks
|
||||||
|
string redVal = string.IsNullOrWhiteSpace(redDiagLabels) ? "-" : redDiagLabels;
|
||||||
|
string actVal = string.IsNullOrWhiteSpace(RhTxBxActionsText) ? "-" : RhTxBxActionsText;
|
||||||
|
string model = string.IsNullOrWhiteSpace(CamOnTest?.Model) ? "-" : CamOnTest.Model;
|
||||||
|
|
||||||
|
string sql = @"
|
||||||
|
INSERT INTO DiagsStats ([Date], [Model], [Red Diags Labels], [RhTxBxActions Contents],[IsRMA],[RMA])
|
||||||
|
VALUES (?, ?, ?, ?, ?, ?)";
|
||||||
|
|
||||||
|
using OleDbCommand cmd = new(sql, conn);
|
||||||
|
|
||||||
|
// OleDb uses positional parameters — order must match the VALUES list above
|
||||||
|
cmd.Parameters.Add(new OleDbParameter { OleDbType = OleDbType.Date, Value = DateTime.Now });
|
||||||
|
cmd.Parameters.Add(new OleDbParameter { OleDbType = OleDbType.VarWChar, Value = model });
|
||||||
|
cmd.Parameters.Add(new OleDbParameter { OleDbType = OleDbType.VarWChar, Value = redVal });
|
||||||
|
cmd.Parameters.Add(new OleDbParameter { OleDbType = OleDbType.VarWChar, Value = actVal });
|
||||||
|
cmd.Parameters.Add(new OleDbParameter { OleDbType = OleDbType.Boolean, Value = IsRMA });
|
||||||
|
cmd.Parameters.Add(new OleDbParameter { OleDbType = OleDbType.VarWChar, Value = RMA });
|
||||||
|
|
||||||
|
cmd.ExecuteNonQuery();
|
||||||
|
}
|
||||||
|
|
||||||
private async void BtnPreTest_Click(object sender, EventArgs e)
|
private async void BtnPreTest_Click(object sender, EventArgs e)
|
||||||
{
|
{
|
||||||
@@ -352,6 +414,7 @@ namespace AiQ_GUI
|
|||||||
// Indicators to the user the test has failed
|
// Indicators to the user the test has failed
|
||||||
Btn.BackColor = Color.Maroon;
|
Btn.BackColor = Color.Maroon;
|
||||||
Btn.Text = "Test Failed";
|
Btn.Text = "Test Failed";
|
||||||
|
|
||||||
if (!(CamOnTest.RMANum != 0)) // Yap to check if it is not a RMA
|
if (!(CamOnTest.RMANum != 0)) // Yap to check if it is not a RMA
|
||||||
{
|
{
|
||||||
Access.Stats(["Final Tests Failed", ErrMssg], CamOnTest.Model);
|
Access.Stats(["Final Tests Failed", ErrMssg], CamOnTest.Model);
|
||||||
@@ -370,7 +433,6 @@ namespace AiQ_GUI
|
|||||||
|
|
||||||
Access.StatsDiags(RedLbls, RhTxBxActions.Text, CamOnTest.Model); // Log to Access database
|
Access.StatsDiags(RedLbls, RhTxBxActions.Text, CamOnTest.Model); // Log to Access database
|
||||||
|
|
||||||
|
|
||||||
if (await DisplayQuestion("Test failed, appeal?" + Environment.NewLine + "See Actions textbox for details."))
|
if (await DisplayQuestion("Test failed, appeal?" + Environment.NewLine + "See Actions textbox for details."))
|
||||||
{
|
{
|
||||||
if (CbBxUserName.Text == "Bradley")
|
if (CbBxUserName.Text == "Bradley")
|
||||||
@@ -387,9 +449,6 @@ namespace AiQ_GUI
|
|||||||
// Joins the actions box to any red labels to use as a full failed text
|
// Joins the actions box to any red labels to use as a full failed text
|
||||||
Logging.LogErrorMessage(FullFailureValues);
|
Logging.LogErrorMessage(FullFailureValues);
|
||||||
|
|
||||||
|
|
||||||
Logging.LogErrorMessage(FullFailureValues);
|
|
||||||
|
|
||||||
IList<IList<object>> values = GoogleAPI.service.Spreadsheets.Values.Get(GoogleAPI.spreadsheetId_ModelInfo, "'Approval'!A1:A").Execute().Values;
|
IList<IList<object>> values = GoogleAPI.service.Spreadsheets.Values.Get(GoogleAPI.spreadsheetId_ModelInfo, "'Approval'!A1:A").Execute().Values;
|
||||||
|
|
||||||
if (values?.Count > 0)
|
if (values?.Count > 0)
|
||||||
@@ -480,6 +539,17 @@ namespace AiQ_GUI
|
|||||||
|
|
||||||
Access.StatsDiags(RedLbls, RhTxBxActions.Text, CamOnTest.Model); // Log to Access database
|
Access.StatsDiags(RedLbls, RhTxBxActions.Text, CamOnTest.Model); // Log to Access database
|
||||||
|
|
||||||
|
if (CamOnTest.RMANum == 0) // Yap to check if it is not a RMA
|
||||||
|
{
|
||||||
|
Stats(["Pre Tests Failed", ErrMssg]);
|
||||||
|
StatsDiags(RedLbls, RhTxBxActions.Text, "FALSE", CamOnTest.RMANum);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
Stats("RMA Pre Tests Failed");
|
||||||
|
StatsDiags(RedLbls, RhTxBxActions.Text, "TRUE", CamOnTest.RMANum);
|
||||||
|
}
|
||||||
|
|
||||||
if (await DisplayQuestion("Test failed, restart?" + Environment.NewLine + "See Actions textbox for details."))
|
if (await DisplayQuestion("Test failed, restart?" + Environment.NewLine + "See Actions textbox for details."))
|
||||||
Helper.RestartApp();
|
Helper.RestartApp();
|
||||||
}
|
}
|
||||||
@@ -1718,39 +1788,8 @@ namespace AiQ_GUI
|
|||||||
{
|
{
|
||||||
Stopwatch stopWatchTest = Stopwatch.StartNew();
|
Stopwatch stopWatchTest = Stopwatch.StartNew();
|
||||||
|
|
||||||
//StatsExcel excelExporter = new();
|
StatsExcel excelExporter = new();
|
||||||
//excelExporter.ExportDatabaseToExcel();
|
excelExporter.ExportDatabaseToExcel();
|
||||||
|
|
||||||
|
|
||||||
//FakeCamera fakeCamera = new FakeCamera(80); // Create an instance of FakeCamera
|
|
||||||
|
|
||||||
////CamOnTest.IP = CbBxFoundCams.Text;
|
|
||||||
//_ = fakeCamera.StartAsync(CAMTYPE.GOOD).ContinueWith(task =>
|
|
||||||
//{
|
|
||||||
// //Network.Initialize("developer", "Pass123");
|
|
||||||
|
|
||||||
// if (task.IsFaulted)
|
|
||||||
// {
|
|
||||||
// AddToActionsList("Error starting FakeCamera: " + task.Exception?.Message);
|
|
||||||
// }
|
|
||||||
// else
|
|
||||||
// {
|
|
||||||
// AddToActionsList($"FakeCamera started successfully. IP: {fakeCamera}", false);
|
|
||||||
// }
|
|
||||||
//});
|
|
||||||
|
|
||||||
//await Task.Delay(3000); // Wait for server to start
|
|
||||||
//CbBxFoundCams.Text = "localhost"; // Should force update in creds an network reinit
|
|
||||||
//CmBoFoundCams_TextChanged(sender, e);
|
|
||||||
//CbBxCameraType.SelectedIndex = CbBxCameraType.Items.Count - 1; // Selects AB12CD as model number
|
|
||||||
|
|
||||||
//await Task.Delay(3000); // Wait for server to start
|
|
||||||
|
|
||||||
//BtnPreTest_Click(sender, e);
|
|
||||||
|
|
||||||
|
|
||||||
Teams.SendMssg("10", "Test User");
|
|
||||||
|
|
||||||
|
|
||||||
stopWatchTest.Stop();
|
stopWatchTest.Stop();
|
||||||
AddToActionsList("RunTime " + stopWatchTest.Elapsed.ToString(@"hh\:mm\:ss\.ff"));
|
AddToActionsList("RunTime " + stopWatchTest.Elapsed.ToString(@"hh\:mm\:ss\.ff"));
|
||||||
|
|||||||
@@ -161,6 +161,37 @@ namespace AiQ_GUI
|
|||||||
return null; // If it fails to parse the JSON
|
return null; // If it fails to parse the JSON
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
public static async Task<bool> SetVaxtorMinMaxPlate(string IP)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
// Build JSON array for Vaxtor min/max plate configuration
|
||||||
|
string[,] Vaxtor_JSON = { { "propMinCharHeight", "18" }, { "propMinGlobalConfidence", "30" } };
|
||||||
|
string response = await HTTP_Update("RaptorOCR".Trim(), IP, Vaxtor_JSON);
|
||||||
|
|
||||||
|
// Treat "operation was canceled" as a successful apply
|
||||||
|
if (response.Contains("The operation was canceled", StringComparison.OrdinalIgnoreCase))
|
||||||
|
{
|
||||||
|
Logging.LogMessage("SetVaxtorMinMaxPlate: Camera applied config but closed connection early (safe to ignore).");
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (response.Contains("error", StringComparison.OrdinalIgnoreCase))
|
||||||
|
{
|
||||||
|
MainForm.Instance.DisplayQuestion($"SetVaxtorMinMaxPlate: failed - Please set manually");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
MainForm.Instance.AddToActionsList($"Could not set Vaxtor Plate height and min confidence: {ex.Message}");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
public static async Task<bool> SetZoomLockOn(string IP)
|
public static async Task<bool> SetZoomLockOn(string IP)
|
||||||
{
|
{
|
||||||
@@ -406,5 +437,13 @@ namespace AiQ_GUI
|
|||||||
public class Property
|
public class Property
|
||||||
{
|
{
|
||||||
public string Value { get; set; } = string.Empty;
|
public string Value { get; set; } = string.Empty;
|
||||||
|
public string datatype { get; set; }
|
||||||
|
}
|
||||||
|
public class VaxtorConfig
|
||||||
|
{
|
||||||
|
public string id { get; set; }
|
||||||
|
public long configHash { get; set; }
|
||||||
|
public Property propMinCharHeight { get; set; }
|
||||||
|
public Property propMinGlobalConfidence { get; set; }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
2
LDS.cs
2
LDS.cs
@@ -35,7 +35,9 @@ namespace AiQ_GUI
|
|||||||
}
|
}
|
||||||
catch // If file can't deserialise
|
catch // If file can't deserialise
|
||||||
{
|
{
|
||||||
|
MainForm.Instance.AddToActionsList("Error loading Local Data Store");
|
||||||
return null; // Return null to indicate failure
|
return null; // Return null to indicate failure
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -14,154 +14,189 @@ namespace AiQ_GUI
|
|||||||
{
|
{
|
||||||
MainForm.Instance.AddToActionsList("=== ExportDatabaseToExcel START ===");
|
MainForm.Instance.AddToActionsList("=== ExportDatabaseToExcel START ===");
|
||||||
|
|
||||||
using (OleDbConnection conn = new(Access.connString))
|
using OleDbConnection conn = new(Access.connString);
|
||||||
|
conn.Open();
|
||||||
|
MainForm.Instance.AddToActionsList("Connected to Access database.");
|
||||||
|
DateTime now = DateTime.Now;
|
||||||
|
|
||||||
|
// Read LastStatsRun from UniversalData (if any)
|
||||||
|
DateTime? lastStatsRun = null;
|
||||||
|
try
|
||||||
{
|
{
|
||||||
conn.Open();
|
using (var cmdPeriod = new OleDbCommand("SELECT LastStatsRun FROM UniversalData", conn))
|
||||||
MainForm.Instance.AddToActionsList("Connected to Access database.");
|
|
||||||
DateTime now = DateTime.Now;
|
|
||||||
|
|
||||||
// ==================== MAIN STATS EXPORT ====================
|
|
||||||
string selectColumns = @"
|
|
||||||
ModelNumber,
|
|
||||||
[Total Tests Run],
|
|
||||||
[Pre Tests Passed],
|
|
||||||
[Pre Tests Failed],
|
|
||||||
[Final Tests Passed],
|
|
||||||
[Final Tests Failed],
|
|
||||||
[RMA Total Tests Run],
|
|
||||||
[RMA Pre Tests Passed],
|
|
||||||
[RMA Pre Tests Failed],
|
|
||||||
[RMA Final Tests Passed],
|
|
||||||
[RMA Final Tests Failed],
|
|
||||||
[Diagnostic Failure],
|
|
||||||
[Failed To Set Model Or Serial Number],
|
|
||||||
[Camera Not In Test Tube],
|
|
||||||
[Could Not Zoom Modules To 8000],
|
|
||||||
[Could Not Zoom Modules To Full Wide],
|
|
||||||
[Please Get RMA Number From Operations Team Before Continuing],
|
|
||||||
[Please Get A Valid Vaxtor Product Key Before Continuing],
|
|
||||||
[Visual Test Fail - Sleeve Not Aligned],
|
|
||||||
[Visual Test Fail - Not All Front Screws Fitted],
|
|
||||||
[Visual Test Fail - Not All Rear Screws Fitted],
|
|
||||||
[Visual Test Fail - Unit rattles]";
|
|
||||||
|
|
||||||
string query = $@"SELECT {selectColumns} FROM AiQ";
|
|
||||||
|
|
||||||
OleDbDataAdapter adapter = new(query, conn);
|
|
||||||
DataTable dataTable = new();
|
|
||||||
adapter.Fill(dataTable);
|
|
||||||
|
|
||||||
if (dataTable.Rows.Count == 0)
|
|
||||||
{
|
{
|
||||||
MainForm.Instance.AddToActionsList("No data found in AiQ table.");
|
object res = cmdPeriod.ExecuteScalar();
|
||||||
return;
|
if (res != null && res != DBNull.Value)
|
||||||
|
lastStatsRun = (DateTime)res;
|
||||||
}
|
}
|
||||||
|
MainForm.Instance.AddToActionsList($"Fetched LastStatsRun: {(lastStatsRun.HasValue ? lastStatsRun.Value.ToString("yyyy-MM-dd HH:mm:ss") : "none")}");
|
||||||
|
}
|
||||||
|
catch (Exception periodEx)
|
||||||
|
{
|
||||||
|
MainForm.Instance.AddToActionsList($"Warning: could not read LastStatsRun. Details: {periodEx.Message}");
|
||||||
|
}
|
||||||
|
|
||||||
string monthName = now.ToString("MMMM");
|
// ==================== MAIN STATS EXPORT ====================
|
||||||
string sheetName = $"{monthName} Stats";
|
string selectColumns = @"
|
||||||
|
ModelNumber,
|
||||||
|
[Total Tests Run],
|
||||||
|
[Pre Tests Passed],
|
||||||
|
[Pre Tests Failed],
|
||||||
|
[Final Tests Passed],
|
||||||
|
[Final Tests Failed],
|
||||||
|
[RMA Total Tests Run],
|
||||||
|
[RMA Pre Tests Passed],
|
||||||
|
[RMA Pre Tests Failed],
|
||||||
|
[RMA Final Tests Passed],
|
||||||
|
[RMA Final Tests Failed],
|
||||||
|
[Diagnostic Failure],
|
||||||
|
[Failed To Set Model Or Serial Number],
|
||||||
|
[Camera Not In Test Tube],
|
||||||
|
[Could Not Zoom Modules To 8000],
|
||||||
|
[Could Not Zoom Modules To Full Wide],
|
||||||
|
[Please Get RMA Number From Operations Team Before Continuing],
|
||||||
|
[Please Get A Valid Vaxtor Product Key Before Continuing],
|
||||||
|
[Visual Test Fail - Sleeve Not Aligned],
|
||||||
|
[Visual Test Fail - Not All Front Screws Fitted],
|
||||||
|
[Visual Test Fail - Not All Rear Screws Fitted],
|
||||||
|
[Visual Test Fail - Unit rattles]";
|
||||||
|
|
||||||
MainForm.Instance.AddToActionsList($"Adding sheet: {sheetName}");
|
string query = $@"
|
||||||
|
SELECT
|
||||||
|
{selectColumns}
|
||||||
|
FROM AiQ";
|
||||||
|
|
||||||
XLWorkbook workbook;
|
OleDbDataAdapter adapter = new(query, conn);
|
||||||
if (File.Exists(exportPath))
|
DataTable dataTable = new();
|
||||||
{
|
adapter.Fill(dataTable);
|
||||||
workbook = new XLWorkbook(exportPath);
|
|
||||||
MainForm.Instance.AddToActionsList("Opened existing workbook.");
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
workbook = new XLWorkbook();
|
|
||||||
MainForm.Instance.AddToActionsList("Created new workbook (file not found).");
|
|
||||||
}
|
|
||||||
|
|
||||||
if (workbook.Worksheets.Contains(sheetName))
|
if (dataTable.Rows.Count == 0)
|
||||||
{
|
{
|
||||||
workbook.Worksheet(sheetName).Delete();
|
MainForm.Instance.AddToActionsList("No data found in AiQ table.");
|
||||||
MainForm.Instance.AddToActionsList($"Deleted old sheet: {sheetName}");
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
IXLWorksheet ws = workbook.Worksheets.Add(sheetName);
|
string monthName = now.ToString("MMMM");
|
||||||
ws.Cell(1, 1).InsertTable(dataTable, "AiQ_Stats", true);
|
string sheetName = $"{monthName} Stats";
|
||||||
ws.Columns().AdjustToContents();
|
|
||||||
|
|
||||||
ws.Cell(1, dataTable.Columns.Count + 2).Value = "Exported:";
|
MainForm.Instance.AddToActionsList($"Adding sheet: {sheetName}");
|
||||||
ws.Cell(2, dataTable.Columns.Count + 2).Value = now.ToString("yyyy-MM-dd HH:mm:ss");
|
|
||||||
|
|
||||||
// ==================== DIAGS STATS EXPORT (CURRENT MONTH ONLY) ====================
|
XLWorkbook workbook;
|
||||||
MainForm.Instance.AddToActionsList("Exporting DiagsStats for current month...");
|
if (File.Exists(exportPath))
|
||||||
|
{
|
||||||
|
workbook = new XLWorkbook(exportPath);
|
||||||
|
MainForm.Instance.AddToActionsList("Opened existing workbook.");
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
workbook = new XLWorkbook();
|
||||||
|
MainForm.Instance.AddToActionsList("Created new workbook (file not found).");
|
||||||
|
}
|
||||||
|
|
||||||
DateTime monthStart = new(now.Year, now.Month, 1);
|
if (workbook.Worksheets.Contains(sheetName))
|
||||||
DateTime nextMonth = monthStart.AddMonths(1);
|
{
|
||||||
|
workbook.Worksheet(sheetName).Delete();
|
||||||
|
MainForm.Instance.AddToActionsList($"Deleted old sheet: {sheetName}");
|
||||||
|
}
|
||||||
|
|
||||||
string diagsQuery = @"
|
IXLWorksheet ws = workbook.Worksheets.Add(sheetName);
|
||||||
|
ws.Cell(1, 1).InsertTable(dataTable, "AiQ_Stats", true);
|
||||||
|
ws.Columns().AdjustToContents();
|
||||||
|
|
||||||
|
// Write Data Period label and computed period string to the right of the table
|
||||||
|
int infoCol = dataTable.Columns.Count + 2;
|
||||||
|
ws.Cell(1, infoCol).Value = "Data Period";
|
||||||
|
|
||||||
|
string periodText;
|
||||||
|
if (lastStatsRun.HasValue)
|
||||||
|
{
|
||||||
|
periodText = $"{lastStatsRun.Value:yyyy-MM-dd HH:mm:ss} - {now:yyyy-MM-dd HH:mm:ss}";
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
periodText = $"Up to {now:yyyy-MM-dd HH:mm:ss}";
|
||||||
|
}
|
||||||
|
|
||||||
|
ws.Cell(2, infoCol).Value = periodText;
|
||||||
|
|
||||||
|
// ==================== DIAGS STATS EXPORT (CURRENT MONTH ONLY) ====================
|
||||||
|
MainForm.Instance.AddToActionsList("Exporting DiagsStats for current month...");
|
||||||
|
|
||||||
|
DateTime monthStart = new(now.Year, now.Month, 1);
|
||||||
|
DateTime nextMonth = monthStart.AddMonths(1);
|
||||||
|
|
||||||
|
string diagsQuery = @"
|
||||||
SELECT
|
SELECT
|
||||||
[Date],
|
[Date],
|
||||||
[Model],
|
[Model],
|
||||||
[Red Diags Labels],
|
[Red Diags Labels],
|
||||||
[RhTxBxActions Contents]
|
[RhTxBxActions Contents],
|
||||||
|
[IsRma],
|
||||||
|
[RMA]
|
||||||
FROM DiagsStats
|
FROM DiagsStats
|
||||||
WHERE [Date] >= ? AND [Date] < ?";
|
WHERE [Date] >= ? AND [Date] < ?";
|
||||||
|
|
||||||
OleDbDataAdapter diagsAdapter = new(diagsQuery, conn);
|
using (var diagsCmd = new OleDbCommand(diagsQuery, conn))
|
||||||
diagsAdapter.SelectCommand.Parameters.Add(new OleDbParameter { OleDbType = OleDbType.Date, Value = monthStart });
|
{
|
||||||
diagsAdapter.SelectCommand.Parameters.Add(new OleDbParameter { OleDbType = OleDbType.Date, Value = nextMonth });
|
diagsCmd.Parameters.Add(new OleDbParameter { OleDbType = OleDbType.Date, Value = monthStart });
|
||||||
|
diagsCmd.Parameters.Add(new OleDbParameter { OleDbType = OleDbType.Date, Value = nextMonth });
|
||||||
|
|
||||||
DataTable diagsTable = new();
|
using (var diagsAdapter = new OleDbDataAdapter(diagsCmd))
|
||||||
diagsAdapter.Fill(diagsTable);
|
|
||||||
|
|
||||||
if (diagsTable.Rows.Count > 0)
|
|
||||||
{
|
{
|
||||||
int startRow = ws.LastRowUsed().RowNumber() + 3;
|
DataTable diagsTable = new();
|
||||||
|
diagsAdapter.Fill(diagsTable);
|
||||||
|
|
||||||
ws.Cell(startRow, 1).Value = "Diags Stats (Current Month)";
|
if (diagsTable.Rows.Count > 0)
|
||||||
ws.Cell(startRow, 1).Style.Font.Bold = true;
|
{
|
||||||
ws.Cell(startRow, 1).Style.Font.FontSize = 12;
|
int startRow = (ws.LastRowUsed()?.RowNumber() ?? 0) + 3;
|
||||||
|
|
||||||
ws.Cell(startRow + 1, 1).InsertTable(diagsTable, "Diags_Stats", true);
|
ws.Cell(startRow, 1).Value = "Diags Stats (Current Month)";
|
||||||
ws.Columns().AdjustToContents();
|
ws.Cell(startRow, 1).Style.Font.Bold = true;
|
||||||
|
ws.Cell(startRow, 1).Style.Font.FontSize = 12;
|
||||||
|
|
||||||
MainForm.Instance.AddToActionsList($"Added DiagsStats ({diagsTable.Rows.Count} rows) starting at row {startRow}.");
|
ws.Cell(startRow + 1, 1).InsertTable(diagsTable, "Diags_Stats", true);
|
||||||
}
|
ws.Columns().AdjustToContents();
|
||||||
else
|
|
||||||
{
|
MainForm.Instance.AddToActionsList($"Added DiagsStats ({diagsTable.Rows.Count} rows) starting at row {startRow}.");
|
||||||
MainForm.Instance.AddToActionsList("No DiagsStats data found for current month.");
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// ==================== SAVE MAIN SHEET ====================
|
// ==================== SAVE MAIN SHEET ====================
|
||||||
workbook.SaveAs(exportPath);
|
workbook.SaveAs(exportPath);
|
||||||
MainForm.Instance.AddToActionsList($"Added {sheetName} to workbook: {exportPath}");
|
MainForm.Instance.AddToActionsList($"Added {sheetName} to workbook: {exportPath}");
|
||||||
|
|
||||||
workbook.Dispose();
|
workbook.Dispose();
|
||||||
|
|
||||||
// ==================== BACKUP TABLE (ALWAYS, BEFORE RESET) ====================
|
// ==================== BACKUP TABLE (ALWAYS, BEFORE RESET) ====================
|
||||||
string ts = now.ToString("yyyyMMdd_HHmmss");
|
string ts = now.ToString("yyyyMMdd_HHmmss");
|
||||||
string backupTableName = $"Ztats_{ts}";
|
string backupTableName = $"Ztats_{ts}";
|
||||||
|
|
||||||
if (backupTableName.Length > 64)
|
if (backupTableName.Length > 64)
|
||||||
backupTableName = backupTableName.Substring(0, 64);
|
backupTableName = backupTableName.Substring(0, 64);
|
||||||
|
|
||||||
string backupSql = $@"
|
string backupSql = $@"
|
||||||
SELECT
|
SELECT
|
||||||
{selectColumns}
|
{selectColumns}
|
||||||
INTO [{backupTableName}]
|
INTO [{backupTableName}]
|
||||||
FROM AiQ";
|
FROM AiQ";
|
||||||
|
try
|
||||||
try
|
{
|
||||||
|
using (OleDbCommand cmdBackup = new(backupSql, conn))
|
||||||
{
|
{
|
||||||
using (OleDbCommand cmdBackup = new(backupSql, conn))
|
cmdBackup.ExecuteNonQuery();
|
||||||
{
|
|
||||||
cmdBackup.ExecuteNonQuery();
|
|
||||||
}
|
|
||||||
MainForm.Instance.AddToActionsList($"Created backup table: [{backupTableName}]");
|
|
||||||
}
|
|
||||||
catch (Exception backupEx)
|
|
||||||
{
|
|
||||||
MainForm.Instance.AddToActionsList($"ERROR creating backup table [{backupTableName}]. Aborting reset to protect data. Details: {backupEx.Message}");
|
|
||||||
MainForm.Instance.AddToActionsList("=== ExportDatabaseToExcel END (backup failed, no reset) ===");
|
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
|
MainForm.Instance.AddToActionsList($"Created backup table: [{backupTableName}]");
|
||||||
|
}
|
||||||
|
catch (Exception backupEx)
|
||||||
|
{
|
||||||
|
MainForm.Instance.AddToActionsList($"ERROR creating backup table [{backupTableName}]. Aborting reset to protect data. Details: {backupEx.Message}");
|
||||||
|
MainForm.Instance.AddToActionsList("=== ExportDatabaseToExcel END (backup failed, no reset) ===");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
using OleDbTransaction tx = conn.BeginTransaction();
|
using (var tx = conn.BeginTransaction())
|
||||||
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
string resetSql = @"
|
string resetSql = @"
|
||||||
|
|||||||
Reference in New Issue
Block a user