namespace crusherScanner
{
public partial class Form1 : Form
{
private char PathSeparator = '\\';
private static ProgramFunctions? dataCore;
private static int counter = 0;
private bool flagINS = false;
///
/// Form 1 Initializer.
///
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
Logging.Append(LogLevel.Information,"Crusher Scanner Application " + 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 Application " + Application.ProductVersion + " has started.");
notifyIcon1.ShowBalloonTip(30, "Crusher Scanner Application", "Crusher Scanner Application, version " + Application.ProductVersion + " has started.", System.Windows.Forms.ToolTipIcon.Info);
Logging.Append(LogLevel.Debug, "Starting Reconciliation checks.");
backgroundWorker2.RunWorkerAsync();
}
private void TextBox1_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
{// test val RCA1275143
Scan scanned = new();
scanned.BufferCount = -1;
counter = 2;
scanned.barcode = TextBox1.Text.Trim().ToUpper();
if (e.KeyValue == 13 && dataCore != null)
{
Logging.Append(LogLevel.Information, "Barcode " + scanned.barcode + " has been scanned.");
if (flagINS)
{
scanned.Ins = true;
flagINS = false;
}
scanned = ProgramFunctions.StartChecks(scanned);
if(scanned.sampleData.Job != null)
{
TextBox1.Enabled = false;
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}")
{
label8.Text = $"Job in progress : {scanned.sampleData.Job}";
Logging.Append(LogLevel.Information, $"Job in progress : {scanned.sampleData.Job}");
}
else
{
label8.Text = "";
}
if (scanned.BufferCount != -1)
{
toolStripStatusLabel2.Text = scanned.BufferCount.ToString();
Logging.Append(LogLevel.Debug, $"{scanned.BufferCount} samples in the Ore Def buffer.");
}
if (!backgroundWorker2.IsBusy)
{
Logging.Append(LogLevel.Debug, "Starting Reconciliation checks.");
backgroundWorker2.RunWorkerAsync();
}
else
{
Logging.Append(LogLevel.Debug, "Did not start Reconciliation checks (Job already in progress).");
}
}
}
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();
Logging.Append(LogLevel.Information, bCode + " contains " + name);
}
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)
{
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;
TextBox1.Focus();
}
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();
}
private void notifyIcon1_DoubleClick(object sender, EventArgs e)
{
// Set the WindowState to normal if the form is minimized.
if (this.WindowState == FormWindowState.Minimized)
{
this.WindowState = FormWindowState.Normal;
// Activate the form.
this.Activate();
}
else
{
this.WindowState = FormWindowState.Minimized;
}
}
private void backgroundWorker2_DoWork(object sender, System.ComponentModel.DoWorkEventArgs e)
{
Reconciliation.CheckJobs(e);
}
private void backgroundWorker2_RunWorkerCompleted(object sender, System.ComponentModel.RunWorkerCompletedEventArgs e)
{
reconciliationToolStripMenuItem.DropDownItems.Clear();
if (e.Result != null)
{
var data = (HashSet)e.Result;
if(data.Count>=1)
{
foreach (string job in data)
{
string[] temp = job.Split(PathSeparator);
ToolStripMenuItem reconList = new ToolStripMenuItem();
reconList.Text = temp[temp.Length - 1];
reconList.Click += new System.EventHandler(this.FileMenuItemClick);
reconciliationToolStripMenuItem.DropDownItems.Add(reconList);
}
}
Logging.Append(LogLevel.Debug, $"{data.Count} job(s) pending Reconciliation.");
}
Logging.Append(LogLevel.Debug, "Reconciliation checks completed.");
}
private void FileMenuItemClick(object? sender, EventArgs e)
{
string path = Properties.Settings.Default.OreDefWorkFile;
if (sender!=null)
{
HashSet missingSamples = Reconciliation.CheckJobReconcilableDiscrepencies($"{path}\\{sender}");
if (missingSamples.Count >= 1)
{
int widthCounter = 0;
string messageReply = $"The following {missingSamples.Count} sample(s) are missing from the job.\n Are you sure you want to reconcile anyway?\n";
foreach (string sample in missingSamples)
{
if (missingSamples.Count >= 31)
{
if ((missingSamples.Count/31) == widthCounter)
{
messageReply += $"{sample}\n";
widthCounter = 0;
}
else
{
messageReply += $"{sample}, ";
widthCounter++;
}
}
else
{
messageReply += $"{sample}\n";
}
}
DialogResult result = MessageBox.Show(messageReply, "Missing Samples",MessageBoxButtons.YesNo, MessageBoxIcon.Question);
if (result == DialogResult.Yes)
{
backgroundWorker2.RunWorkerAsync(sender.ToString());
}
}
}
}
private void menuStrip1_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
{
}
private void flagINSSampleToolStripMenuItem_Click(object sender, EventArgs e)
{
flagINS = true;
}
}
}