Form1.cs 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. namespace crusherScanner
  2. {
  3. public partial class Form1 : Form
  4. {
  5. private static ProgramFunctions? dataCore;
  6. private static int counter = 0;
  7. /// <summary>
  8. /// Form 1 Initializer.
  9. /// </summary>
  10. public Form1()
  11. {
  12. InitializeComponent();
  13. }
  14. private void Form1_Load(object sender, EventArgs e)
  15. {
  16. Logging.Append(LogLevel.Information,"Crusher Scanner Appliction " + Application.ProductVersion + " is starting.");
  17. TextBox1.Enabled = false;
  18. toolStripStatusLabel5.Text = "";
  19. toolStripStatusLabel2.Text = "0";
  20. label8.Text = "";
  21. label7.Text = "";
  22. label6.Text = "";
  23. label5.Hide();
  24. label4.Hide();
  25. label3.Hide();
  26. label2.Hide();
  27. label1.Hide();
  28. WindowState = FormWindowState.Maximized;
  29. dataCore = new ProgramFunctions();
  30. TextBox1.Enabled = true;
  31. TextBox1.Focus();
  32. Logging.Append(LogLevel.Information,"Crusher Scanner Appliction " + Application.ProductVersion + " has started.");
  33. }
  34. private void TextBox1_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
  35. {
  36. Scan scanned = new();
  37. scanned.BufferCount = -1;
  38. counter = 2;
  39. scanned.barcode = TextBox1.Text.Trim().ToUpper();
  40. if (e.KeyValue == 13 && dataCore != null)
  41. {
  42. scanned = ProgramFunctions.StartChecks(scanned);
  43. if(scanned.sampleData.Job != null)
  44. {
  45. backgroundWorker1.RunWorkerAsync(scanned);
  46. }
  47. TextBox1.Text = "";
  48. ContaminateAlert(scanned.sampleData.HammerOil, scanned.barcode, "Hammer Oil", label5, Color.Maroon);
  49. ContaminateAlert(scanned.sampleData.Manganese, scanned.barcode, "Manganese", label4, Color.Blue);
  50. ContaminateAlert(scanned.sampleData.Plastic, scanned.barcode, "Plastic", label3, Color.Orange);
  51. ContaminateAlert(scanned.sampleData.Pyrite, scanned.barcode, "Pyrite", label2, Color.Fuchsia);
  52. if (!scanned.Contaminated)
  53. {
  54. label1.Text = scanned.message;
  55. label1.Show();
  56. }
  57. else
  58. {
  59. label1.Hide();
  60. }
  61. if (scanned.sampleData.Job != null)
  62. {
  63. label8.Text = $"Job in progress : {scanned.sampleData.Job}";
  64. }
  65. else
  66. {
  67. label8.Text = "";
  68. }
  69. if (scanned.BufferCount != -1)
  70. {
  71. toolStripStatusLabel2.Text = scanned.BufferCount.ToString();
  72. }
  73. }
  74. }
  75. private void ContaminateAlert(bool check,string bCode, string name,Label rtLabel, Color bColor)
  76. {
  77. if (check)
  78. {
  79. rtLabel.Text = $"Sample {bCode} contains {name}";
  80. BackColor = bColor;
  81. rtLabel.Show();
  82. }
  83. else
  84. {
  85. rtLabel.Hide();
  86. }
  87. }
  88. private void ExitToolStripMenuItem_Click(object sender, EventArgs e)
  89. {
  90. this.Close();
  91. }
  92. #region SettingsHandlers
  93. private void EditToolStripMenuItem_Click(object sender, EventArgs e)
  94. {
  95. SettingsDialog dlg = new();
  96. dlg.ShowDialog();
  97. dlg.Dispose();
  98. }
  99. private void PurgeToolStripMenuItem1_Click(object sender, EventArgs e)
  100. {
  101. DialogResult result = MessageBox.Show("Are you sure you would like to purge all setings?", "Clear settings.", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
  102. if(result == DialogResult.Yes)
  103. {
  104. Properties.Settings.Default.Reset();
  105. MessageBox.Show("Settings purged.", "Clear settings.");
  106. }
  107. }
  108. /// <summary>
  109. /// Import settings from JSON file.
  110. /// </summary>
  111. private void ImportToolStripMenuItem_Click(object sender, EventArgs e)
  112. {
  113. openFileDialog1.DefaultExt = "json";
  114. openFileDialog1.Filter = "Json files (*.json)|*.json";
  115. openFileDialog1.InitialDirectory = AppDomain.CurrentDomain.BaseDirectory;
  116. openFileDialog1.ShowDialog();
  117. string configFile = openFileDialog1.FileName;
  118. try
  119. {
  120. if(configFile=="" && File.Exists(configFile) && dataCore != null)
  121. {
  122. ProgramFunctions.ImportConfigFile();
  123. }
  124. else if(File.Exists(configFile) && dataCore != null)
  125. {
  126. ProgramFunctions.ImportConfigFile(configFile);
  127. }
  128. }
  129. catch (Exception ex)
  130. {
  131. MessageBox.Show("An error occurred." + Environment.NewLine + ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
  132. }
  133. }
  134. /// <summary>
  135. /// Export settings to JSON file.
  136. /// </summary>
  137. private void ExportToolStripMenuItem_Click(object sender, EventArgs e)
  138. {
  139. saveFileDialog1.DefaultExt = "json";
  140. saveFileDialog1.Filter = "Json files (*.json)|*.json";
  141. saveFileDialog1.InitialDirectory = AppDomain.CurrentDomain.BaseDirectory;
  142. saveFileDialog1.ShowDialog();
  143. string configFile = saveFileDialog1.FileName;
  144. try
  145. {
  146. if(configFile == "" && dataCore != null)
  147. {
  148. ProgramFunctions.ExportConfigFile();
  149. }
  150. else if (dataCore != null)
  151. {
  152. ProgramFunctions.ExportConfigFile(configFile);
  153. }
  154. }
  155. catch (Exception ex)
  156. {
  157. MessageBox.Show("An error occurred." + Environment.NewLine + ex.Message,"Error",MessageBoxButtons.OK,MessageBoxIcon.Error);
  158. }
  159. }
  160. #endregion
  161. private void ReinitializeSettingsToolStripMenuItem_Click(object sender, EventArgs e)
  162. {
  163. Application.Restart();
  164. }
  165. private void Timer1_Tick(object sender, EventArgs e)
  166. {
  167. DateTime now = DateTime.Now;
  168. label6.Text = now.ToShortTimeString();
  169. label7.Text = now.ToShortDateString();
  170. if (counter <= 0)
  171. {
  172. BackColor = SystemColors.Control;
  173. counter = 2;
  174. }
  175. else if(BackColor.Name != "Control")
  176. {
  177. counter--;
  178. }
  179. }
  180. private void backgroundWorker1_DoWork(object sender, System.ComponentModel.DoWorkEventArgs e)
  181. {
  182. if (e.Argument!=null)
  183. {
  184. TextBox1.Enabled = false;
  185. WorkingDirControl.UpdateWorkingDir(e);
  186. }
  187. }
  188. private void backgroundWorker1_RunWorkerCompleted(object sender, System.ComponentModel.RunWorkerCompletedEventArgs e)
  189. {
  190. Scan scan;
  191. if (e.Result != null)
  192. {
  193. scan = (Scan)e.Result;
  194. toolStripProgressBar1.Maximum = scan.JobTotal;
  195. toolStripProgressBar1.Value = scan.JobCount;
  196. toolStripStatusLabel5.Text = (scan.JobTotal - (int)scan.JobCount).ToString();
  197. }
  198. TextBox1.Enabled = true;
  199. }
  200. private void openDirectoryToolStripMenuItem_Click(object sender, EventArgs e)
  201. {
  202. Logging.OpenLogDirectory();
  203. }
  204. private void openCurrentToolStripMenuItem_Click(object sender, EventArgs e)
  205. {
  206. Logging.OpenInTextEditor();
  207. }
  208. private void purgeToolStripMenuItem_Click(object sender, EventArgs e)
  209. {
  210. Logging.PurgeLogs();
  211. }
  212. }
  213. }