namespace crusherScanner
{
public partial class Form1 : Form
{
private static ProgramFunctions? dataCore;
private static int counter = 0;
///
/// Form 1 Initializer.
///
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
Logging.Append(LogLevel.Information,"Crusher Scanner Appliction " + Application.ProductVersion + " is starting.");
TextBox1.Enabled = false;
toolStripStatusLabel5.Text = "";
toolStripStatusLabel2.Text = "0";
label8.Text = "";
label7.Text = "";
label6.Text = "";
label5.Hide();
label4.Hide();
label3.Hide();
label2.Hide();
label1.Hide();
WindowState = FormWindowState.Maximized;
dataCore = new ProgramFunctions();
TextBox1.Enabled = true;
TextBox1.Focus();
Logging.Append(LogLevel.Information,"Crusher Scanner Appliction " + Application.ProductVersion + " has started.");
}
private void TextBox1_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
{
Scan scanned = new();
scanned.BufferCount = -1;
counter = 2;
scanned.barcode = TextBox1.Text.Trim().ToUpper();
if (e.KeyValue == 13 && dataCore != null)
{
scanned = ProgramFunctions.StartChecks(scanned);
if(scanned.sampleData.Job != null)
{
backgroundWorker1.RunWorkerAsync(scanned);
}
TextBox1.Text = "";
ContaminateAlert(scanned.sampleData.HammerOil, scanned.barcode, "Hammer Oil", label5, Color.Maroon);
ContaminateAlert(scanned.sampleData.Manganese, scanned.barcode, "Manganese", label4, Color.Blue);
ContaminateAlert(scanned.sampleData.Plastic, scanned.barcode, "Plastic", label3, Color.Orange);
ContaminateAlert(scanned.sampleData.Pyrite, scanned.barcode, "Pyrite", label2, Color.Fuchsia);
if (!scanned.Contaminated)
{
label1.Text = scanned.message;
label1.Show();
}
else
{
label1.Hide();
}
if (scanned.sampleData.Job != null)
{
label8.Text = $"Job in progress : {scanned.sampleData.Job}";
}
else
{
label8.Text = "";
}
if (scanned.BufferCount != -1)
{
toolStripStatusLabel2.Text = scanned.BufferCount.ToString();
}
}
}
private void ContaminateAlert(bool check,string bCode, string name,Label rtLabel, Color bColor)
{
if (check)
{
rtLabel.Text = $"Sample {bCode} contains {name}";
BackColor = bColor;
rtLabel.Show();
}
else
{
rtLabel.Hide();
}
}
private void ExitToolStripMenuItem_Click(object sender, EventArgs e)
{
this.Close();
}
#region SettingsHandlers
private void EditToolStripMenuItem_Click(object sender, EventArgs e)
{
SettingsDialog dlg = new();
dlg.ShowDialog();
dlg.Dispose();
}
private void PurgeToolStripMenuItem1_Click(object sender, EventArgs e)
{
DialogResult result = MessageBox.Show("Are you sure you would like to purge all setings?", "Clear settings.", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
if(result == DialogResult.Yes)
{
Properties.Settings.Default.Reset();
MessageBox.Show("Settings purged.", "Clear settings.");
}
}
///
/// Import settings from JSON file.
///
private void ImportToolStripMenuItem_Click(object sender, EventArgs e)
{
openFileDialog1.DefaultExt = "json";
openFileDialog1.Filter = "Json files (*.json)|*.json";
openFileDialog1.InitialDirectory = AppDomain.CurrentDomain.BaseDirectory;
openFileDialog1.ShowDialog();
string configFile = openFileDialog1.FileName;
try
{
if(configFile=="" && File.Exists(configFile) && dataCore != null)
{
ProgramFunctions.ImportConfigFile();
}
else if(File.Exists(configFile) && dataCore != null)
{
ProgramFunctions.ImportConfigFile(configFile);
}
}
catch (Exception ex)
{
MessageBox.Show("An error occurred." + Environment.NewLine + ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
///
/// Export settings to JSON file.
///
private void ExportToolStripMenuItem_Click(object sender, EventArgs e)
{
saveFileDialog1.DefaultExt = "json";
saveFileDialog1.Filter = "Json files (*.json)|*.json";
saveFileDialog1.InitialDirectory = AppDomain.CurrentDomain.BaseDirectory;
saveFileDialog1.ShowDialog();
string configFile = saveFileDialog1.FileName;
try
{
if(configFile == "" && dataCore != null)
{
ProgramFunctions.ExportConfigFile();
}
else if (dataCore != null)
{
ProgramFunctions.ExportConfigFile(configFile);
}
}
catch (Exception ex)
{
MessageBox.Show("An error occurred." + Environment.NewLine + ex.Message,"Error",MessageBoxButtons.OK,MessageBoxIcon.Error);
}
}
#endregion
private void ReinitializeSettingsToolStripMenuItem_Click(object sender, EventArgs e)
{
Application.Restart();
}
private void Timer1_Tick(object sender, EventArgs e)
{
DateTime now = DateTime.Now;
label6.Text = now.ToShortTimeString();
label7.Text = now.ToShortDateString();
if (counter <= 0)
{
BackColor = SystemColors.Control;
counter = 2;
}
else if(BackColor.Name != "Control")
{
counter--;
}
}
private void backgroundWorker1_DoWork(object sender, System.ComponentModel.DoWorkEventArgs e)
{
if (e.Argument!=null)
{
TextBox1.Enabled = false;
WorkingDirControl.UpdateWorkingDir(e);
}
}
private void backgroundWorker1_RunWorkerCompleted(object sender, System.ComponentModel.RunWorkerCompletedEventArgs e)
{
Scan scan;
if (e.Result != null)
{
scan = (Scan)e.Result;
toolStripProgressBar1.Maximum = scan.JobTotal;
toolStripProgressBar1.Value = scan.JobCount;
toolStripStatusLabel5.Text = (scan.JobTotal - (int)scan.JobCount).ToString();
}
TextBox1.Enabled = true;
}
private void openDirectoryToolStripMenuItem_Click(object sender, EventArgs e)
{
Logging.OpenLogDirectory();
}
private void openCurrentToolStripMenuItem_Click(object sender, EventArgs e)
{
Logging.OpenInTextEditor();
}
private void purgeToolStripMenuItem_Click(object sender, EventArgs e)
{
Logging.PurgeLogs();
}
}
}