Form1.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  1. namespace crusherScanner
  2. {
  3. public partial class Form1 : Form
  4. {
  5. private char PathSeparator = '\\';
  6. private static ProgramFunctions? dataCore;
  7. private static int counter = 0;
  8. private bool flagINS = false;
  9. /// <summary>
  10. /// Form 1 Initializer.
  11. /// </summary>
  12. public Form1()
  13. {
  14. InitializeComponent();
  15. }
  16. private void Form1_Load(object sender, EventArgs e)
  17. {
  18. Logging.Append(LogLevel.Information,"Crusher Scanner Application " + Application.ProductVersion + " is starting.");
  19. TextBox1.Enabled = false;
  20. toolStripStatusLabel5.Text = "";
  21. toolStripStatusLabel2.Text = "0";
  22. label8.Text = "";
  23. label7.Text = "";
  24. label6.Text = "";
  25. label5.Hide();
  26. label4.Hide();
  27. label3.Hide();
  28. label2.Hide();
  29. label1.Hide();
  30. WindowState = FormWindowState.Maximized;
  31. dataCore = new ProgramFunctions();
  32. TextBox1.Enabled = true;
  33. TextBox1.Focus();
  34. Logging.Append(LogLevel.Information,"Crusher Scanner Application " + Application.ProductVersion + " has started.");
  35. notifyIcon1.ShowBalloonTip(30, "Crusher Scanner Application", "Crusher Scanner Application, version " + Application.ProductVersion + " has started.", System.Windows.Forms.ToolTipIcon.Info);
  36. Logging.Append(LogLevel.Debug, "Starting Reconciliation checks.");
  37. backgroundWorker2.RunWorkerAsync();
  38. }
  39. private void TextBox1_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
  40. {// test val RCA1275143
  41. Scan scanned = new();
  42. scanned.BufferCount = -1;
  43. counter = 2;
  44. scanned.barcode = TextBox1.Text.Trim().ToUpper();
  45. if (e.KeyValue == 13 && dataCore != null)
  46. {
  47. Logging.Append(LogLevel.Information, "Barcode " + scanned.barcode + " has been scanned.");
  48. if (flagINS)
  49. {
  50. scanned.Ins = true;
  51. flagINS = false;
  52. }
  53. scanned = ProgramFunctions.StartChecks(scanned);
  54. if(scanned.sampleData.Job != null)
  55. {
  56. TextBox1.Enabled = false;
  57. backgroundWorker1.RunWorkerAsync(scanned);
  58. }
  59. TextBox1.Text = "";
  60. ContaminateAlert(scanned.sampleData.HammerOil, scanned.barcode, "Hammer Oil", label5, Color.Maroon);
  61. ContaminateAlert(scanned.sampleData.Manganese, scanned.barcode, "Manganese", label4, Color.Blue);
  62. ContaminateAlert(scanned.sampleData.Plastic, scanned.barcode, "Plastic", label3, Color.Orange);
  63. ContaminateAlert(scanned.sampleData.Pyrite, scanned.barcode, "Pyrite", label2, Color.Fuchsia);
  64. if (!scanned.Contaminated)
  65. {
  66. label1.Text = scanned.message;
  67. label1.Show();
  68. }
  69. else
  70. {
  71. label1.Hide();
  72. }
  73. if (scanned.sampleData.Job != null && label8.Text != $"Job in progress : {scanned.sampleData.Job}")
  74. {
  75. label8.Text = $"Job in progress : {scanned.sampleData.Job}";
  76. Logging.Append(LogLevel.Information, $"Job in progress : {scanned.sampleData.Job}");
  77. }
  78. else
  79. {
  80. label8.Text = "";
  81. }
  82. if (scanned.BufferCount != -1)
  83. {
  84. toolStripStatusLabel2.Text = scanned.BufferCount.ToString();
  85. Logging.Append(LogLevel.Debug, $"{scanned.BufferCount} samples in the Ore Def buffer.");
  86. }
  87. if (!backgroundWorker2.IsBusy)
  88. {
  89. Logging.Append(LogLevel.Debug, "Starting Reconciliation checks.");
  90. backgroundWorker2.RunWorkerAsync();
  91. }
  92. else
  93. {
  94. Logging.Append(LogLevel.Debug, "Did not start Reconciliation checks (Job already in progress).");
  95. }
  96. }
  97. }
  98. private void ContaminateAlert(bool check,string bCode, string name,Label rtLabel, Color bColor)
  99. {
  100. if (check)
  101. {
  102. rtLabel.Text = $"Sample {bCode} contains {name}";
  103. BackColor = bColor;
  104. rtLabel.Show();
  105. Logging.Append(LogLevel.Information, bCode + " contains " + name);
  106. }
  107. else
  108. {
  109. rtLabel.Hide();
  110. }
  111. }
  112. private void ExitToolStripMenuItem_Click(object sender, EventArgs e)
  113. {
  114. this.Close();
  115. }
  116. #region SettingsHandlers
  117. private void EditToolStripMenuItem_Click(object sender, EventArgs e)
  118. {
  119. SettingsDialog dlg = new();
  120. dlg.ShowDialog();
  121. dlg.Dispose();
  122. }
  123. private void PurgeToolStripMenuItem1_Click(object sender, EventArgs e)
  124. {
  125. DialogResult result = MessageBox.Show("Are you sure you would like to purge all setings?", "Clear settings.", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
  126. if(result == DialogResult.Yes)
  127. {
  128. Properties.Settings.Default.Reset();
  129. MessageBox.Show("Settings purged.", "Clear settings.");
  130. }
  131. }
  132. /// <summary>
  133. /// Import settings from JSON file.
  134. /// </summary>
  135. private void ImportToolStripMenuItem_Click(object sender, EventArgs e)
  136. {
  137. openFileDialog1.DefaultExt = "json";
  138. openFileDialog1.Filter = "Json files (*.json)|*.json";
  139. openFileDialog1.InitialDirectory = AppDomain.CurrentDomain.BaseDirectory;
  140. openFileDialog1.ShowDialog();
  141. string configFile = openFileDialog1.FileName;
  142. try
  143. {
  144. if(configFile=="" && File.Exists(configFile) && dataCore != null)
  145. {
  146. ProgramFunctions.ImportConfigFile();
  147. }
  148. else if(File.Exists(configFile) && dataCore != null)
  149. {
  150. ProgramFunctions.ImportConfigFile(configFile);
  151. }
  152. }
  153. catch (Exception ex)
  154. {
  155. MessageBox.Show("An error occurred." + Environment.NewLine + ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
  156. }
  157. }
  158. /// <summary>
  159. /// Export settings to JSON file.
  160. /// </summary>
  161. private void ExportToolStripMenuItem_Click(object sender, EventArgs e)
  162. {
  163. saveFileDialog1.DefaultExt = "json";
  164. saveFileDialog1.Filter = "Json files (*.json)|*.json";
  165. saveFileDialog1.InitialDirectory = AppDomain.CurrentDomain.BaseDirectory;
  166. saveFileDialog1.ShowDialog();
  167. string configFile = saveFileDialog1.FileName;
  168. try
  169. {
  170. if(configFile == "" && dataCore != null)
  171. {
  172. ProgramFunctions.ExportConfigFile();
  173. }
  174. else if (dataCore != null)
  175. {
  176. ProgramFunctions.ExportConfigFile(configFile);
  177. }
  178. }
  179. catch (Exception ex)
  180. {
  181. MessageBox.Show("An error occurred." + Environment.NewLine + ex.Message,"Error",MessageBoxButtons.OK,MessageBoxIcon.Error);
  182. }
  183. }
  184. #endregion
  185. private void ReinitializeSettingsToolStripMenuItem_Click(object sender, EventArgs e)
  186. {
  187. Application.Restart();
  188. }
  189. private void Timer1_Tick(object sender, EventArgs e)
  190. {
  191. DateTime now = DateTime.Now;
  192. label6.Text = now.ToShortTimeString();
  193. label7.Text = now.ToShortDateString();
  194. if (counter <= 0)
  195. {
  196. BackColor = SystemColors.Control;
  197. counter = 2;
  198. }
  199. else if(BackColor.Name != "Control")
  200. {
  201. counter--;
  202. }
  203. }
  204. private void backgroundWorker1_DoWork(object sender, System.ComponentModel.DoWorkEventArgs e)
  205. {
  206. if (e.Argument!=null)
  207. {
  208. WorkingDirControl.UpdateWorkingDir(e);
  209. }
  210. }
  211. private void backgroundWorker1_RunWorkerCompleted(object sender, System.ComponentModel.RunWorkerCompletedEventArgs e)
  212. {
  213. Scan scan;
  214. if (e.Result != null)
  215. {
  216. scan = (Scan)e.Result;
  217. toolStripProgressBar1.Maximum = scan.JobTotal;
  218. toolStripProgressBar1.Value = scan.JobCount;
  219. toolStripStatusLabel5.Text = (scan.JobTotal - (int)scan.JobCount).ToString();
  220. }
  221. TextBox1.Enabled = true;
  222. TextBox1.Focus();
  223. }
  224. private void openDirectoryToolStripMenuItem_Click(object sender, EventArgs e)
  225. {
  226. Logging.OpenLogDirectory();
  227. }
  228. private void openCurrentToolStripMenuItem_Click(object sender, EventArgs e)
  229. {
  230. Logging.OpenInTextEditor();
  231. }
  232. private void purgeToolStripMenuItem_Click(object sender, EventArgs e)
  233. {
  234. Logging.PurgeLogs();
  235. }
  236. private void notifyIcon1_DoubleClick(object sender, EventArgs e)
  237. {
  238. // Set the WindowState to normal if the form is minimized.
  239. if (this.WindowState == FormWindowState.Minimized)
  240. {
  241. this.WindowState = FormWindowState.Normal;
  242. // Activate the form.
  243. this.Activate();
  244. }
  245. else
  246. {
  247. this.WindowState = FormWindowState.Minimized;
  248. }
  249. }
  250. private void backgroundWorker2_DoWork(object sender, System.ComponentModel.DoWorkEventArgs e)
  251. {
  252. Reconciliation.CheckJobs(e);
  253. }
  254. private void backgroundWorker2_RunWorkerCompleted(object sender, System.ComponentModel.RunWorkerCompletedEventArgs e)
  255. {
  256. reconciliationToolStripMenuItem.DropDownItems.Clear();
  257. if (e.Result != null)
  258. {
  259. var data = (HashSet<string>)e.Result;
  260. if(data.Count>=1)
  261. {
  262. foreach (string job in data)
  263. {
  264. string[] temp = job.Split(PathSeparator);
  265. ToolStripMenuItem reconList = new ToolStripMenuItem();
  266. reconList.Text = temp[temp.Length - 1];
  267. reconList.Click += new System.EventHandler(this.FileMenuItemClick);
  268. reconciliationToolStripMenuItem.DropDownItems.Add(reconList);
  269. }
  270. }
  271. Logging.Append(LogLevel.Debug, $"{data.Count} job(s) pending Reconciliation.");
  272. }
  273. Logging.Append(LogLevel.Debug, "Reconciliation checks completed.");
  274. }
  275. private void FileMenuItemClick(object? sender, EventArgs e)
  276. {
  277. string path = Properties.Settings.Default.OreDefWorkFile;
  278. if (sender!=null)
  279. {
  280. HashSet<string> missingSamples = Reconciliation.CheckJobReconcilableDiscrepencies($"{path}\\{sender}");
  281. if (missingSamples.Count >= 1)
  282. {
  283. int widthCounter = 0;
  284. string messageReply = $"The following {missingSamples.Count} sample(s) are missing from the job.\n Are you sure you want to reconcile anyway?\n";
  285. foreach (string sample in missingSamples)
  286. {
  287. if (missingSamples.Count >= 31)
  288. {
  289. if ((missingSamples.Count/31) == widthCounter)
  290. {
  291. messageReply += $"{sample}\n";
  292. widthCounter = 0;
  293. }
  294. else
  295. {
  296. messageReply += $"{sample}, ";
  297. widthCounter++;
  298. }
  299. }
  300. else
  301. {
  302. messageReply += $"{sample}\n";
  303. }
  304. }
  305. DialogResult result = MessageBox.Show(messageReply, "Missing Samples",MessageBoxButtons.YesNo, MessageBoxIcon.Question);
  306. if (result == DialogResult.Yes)
  307. {
  308. backgroundWorker2.RunWorkerAsync(sender.ToString());
  309. }
  310. }
  311. }
  312. }
  313. private void menuStrip1_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
  314. {
  315. }
  316. private void flagINSSampleToolStripMenuItem_Click(object sender, EventArgs e)
  317. {
  318. flagINS = true;
  319. }
  320. }
  321. }