Form1.cs 12 KB

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