| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351 |
- namespace crusherScanner
- {
- public partial class Form1 : Form
- {
- private char PathSeparator = '\\';
- private static ProgramFunctions? dataCore;
- private static int counter = 0;
- private bool flagINS = false;
- /// <summary>
- /// Form 1 Initializer.
- /// </summary>
- 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.");
- }
- }
- /// <summary>
- /// Import settings from JSON file.
- /// </summary>
- 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);
- }
- }
- /// <summary>
- /// Export settings to JSON file.
- /// </summary>
- 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<string>)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<string> 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;
- }
- }
- }
|