ProgramFunctions.cs 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Data.SqlClient;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Text.Json;
  7. using System.Threading.Tasks;
  8. namespace crusherScanner
  9. {
  10. /// <summary>
  11. /// Main entry point for sample checking functions.
  12. /// </summary>
  13. public class ProgramFunctions
  14. {
  15. private static string lastBarcodeScanned = "";
  16. public static bool ready = false;
  17. #region Contructor
  18. /// <summary>
  19. /// Constructor to load initial settings.
  20. /// </summary>
  21. public ProgramFunctions()
  22. {
  23. // no initial configuration present
  24. if (Properties.Settings.Default.OreDefInFile == "")
  25. {
  26. if (File.Exists("config.json"))
  27. {
  28. MessageBox.Show("No configuration stored,\nloading configuration from 'config.json'","Settings",MessageBoxButtons.OK,MessageBoxIcon.Exclamation);
  29. JsonHandler conf = new("config.json", true);
  30. ImportConfigFile("config.json");
  31. }
  32. else
  33. {
  34. MessageBox.Show("No configuration stored,\nYou will need to enter settings manually.\nThe settings will open now.", "Settings", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
  35. SettingsDialog dlg = new();
  36. dlg.ShowDialog();
  37. dlg.Dispose();
  38. }
  39. }
  40. //TODO check if settings are ok to use before setting ready.
  41. }
  42. #endregion
  43. #region Main logic
  44. /// <summary>
  45. /// Check scanned sample ID in following order;
  46. /// - LIMS existence (active samples in the last 90 days). warn operator if not.
  47. /// - Is this an Ore def sample (RCA, RCB, RCC).
  48. /// - If not Ore def pass sample to crusher.
  49. /// - If Ore def without contamination send to crusher.
  50. /// - If sample has contamination not the ID and only send to crusher if scanned twice.
  51. /// - If sample could not be found in the Ore def lists. warn operator.
  52. /// </summary>
  53. /// <param name="scanned">Scaned sample details.</param>
  54. /// <returns></returns>
  55. public static Scan StartChecks(Scan scanned)
  56. {
  57. scanned.messageState = true;
  58. scanned = LimsAccess.IsInLims(scanned);
  59. if (!scanned.isInLims) // Check if this sample exists in LIMS
  60. {
  61. scanned.messageState = false;
  62. scanned.message = $"Sample {scanned.barcode} does not seem to be in LIMS.";
  63. return scanned;
  64. }
  65. scanned = OreDefData.CheckSampleType(scanned);
  66. if (scanned.sampleType == "Error!") // Check if sample prefix needs checking
  67. {
  68. scanned.messageState = false;
  69. scanned.message = "Unable to check sample due to a configuration issue.";
  70. return scanned;
  71. }
  72. if(scanned.sampleType == null) //sample is NOT an OreDef sample
  73. {
  74. scanned.message = $"Sample {scanned.barcode} is not an OreDef sample.";
  75. SerialAccess.SendToPrepmaster(scanned.barcode);
  76. lastBarcodeScanned = scanned.barcode;
  77. return scanned;
  78. }
  79. scanned = OreDefData.CheckContaminates(scanned);
  80. if (!scanned.Contaminated && !scanned.SampleNotFound) // No contamnination and SID found
  81. {
  82. scanned.message = $"Sample {scanned.barcode} is good to go.";
  83. SerialAccess.SendToPrepmaster(scanned.barcode);
  84. lastBarcodeScanned = scanned.barcode;
  85. return scanned;
  86. }
  87. else if (scanned.Contaminated && !scanned.SampleNotFound) // Contaminated sample found
  88. {
  89. scanned.message = "";
  90. if (lastBarcodeScanned == scanned.barcode)
  91. {
  92. SerialAccess.SendToPrepmaster(scanned.barcode);
  93. }
  94. lastBarcodeScanned = scanned.barcode;
  95. return scanned;
  96. }
  97. else // Sample not found in job lists.
  98. {
  99. scanned.messageState = false;
  100. scanned.message = $"Sample {scanned.barcode} is not found in job lists.";
  101. return scanned;
  102. }
  103. }
  104. #endregion
  105. #region Settings functions
  106. /// <summary>
  107. /// Import app configuration from a JSON file.
  108. /// </summary>
  109. /// <param name="configFile">Path to file. (defaults to "config.json" in app directory)</param>
  110. public static void ImportConfigFile(string configFile = "config.json")
  111. {
  112. Settings tmpSettings = new();
  113. // Open JSON file to read
  114. string data = File.ReadAllText(configFile);
  115. using JsonDocument doc = JsonDocument.Parse(data);
  116. JsonElement root = doc.RootElement;
  117. var config = root;
  118. // Set config data from JSON into temp settings var for handling.
  119. tmpSettings.CrusherNo = (int)config.GetProperty("CrusherNo").GetInt32();
  120. tmpSettings.PrepmasterMagazineSerial = config.GetProperty("Serial").ToString();
  121. tmpSettings.OreDefInFile = config.GetProperty("OreDefInFile").ToString();
  122. tmpSettings.OreDefWorkFile = config.GetProperty("OreDefWorkFile").ToString();
  123. tmpSettings.OreDefOutFile = config.GetProperty("OreDefOutFile").ToString();
  124. tmpSettings.OutputFormat = config.GetProperty("OutputFormat").ToString();
  125. tmpSettings.OreDefTypes = config.GetProperty("OreDefTypes").ToString();
  126. tmpSettings.LimsConnection = config.GetProperty("LimsConnection").GetBoolean();
  127. tmpSettings.LimsConnString = config.GetProperty("LimsConnString").ToString();
  128. tmpSettings.PrepmasterConnection = config.GetProperty("PrepmasterConnection").GetBoolean();
  129. tmpSettings.LoggingDebug = config.GetProperty("LoggingDebug").GetBoolean();
  130. tmpSettings.LoggingInfo = config.GetProperty("LoggingInfo").GetBoolean();
  131. tmpSettings.LoggingWarning = config.GetProperty("LoggingWarning").GetBoolean();
  132. tmpSettings.LoggingError = config.GetProperty("LoggingError").GetBoolean();
  133. // Check settings, if good save to applications cache.
  134. if(SettingsHandler.CheckSettings(tmpSettings))
  135. {
  136. SaveSettings(tmpSettings);
  137. }
  138. }
  139. /// <summary>
  140. /// Save settings from temp var to app cache.
  141. /// </summary>
  142. /// <param name="configuration">Pre checked settings to save.</param>
  143. public static void SaveSettings(Settings configuration)
  144. {
  145. Properties.Settings.Default.CrusherNo = configuration.CrusherNo;
  146. Properties.Settings.Default.PrepmasterMagazineSerial = configuration.PrepmasterMagazineSerial;
  147. Properties.Settings.Default.OreDefInFile = configuration.OreDefInFile;
  148. Properties.Settings.Default.OreDefWorkFile = configuration.OreDefWorkFile;
  149. Properties.Settings.Default.OreDefOutFile = configuration.OreDefOutFile;
  150. Properties.Settings.Default.OutputFormat = configuration.OutputFormat;
  151. Properties.Settings.Default.OreDefTypes = configuration.OreDefTypes;
  152. Properties.Settings.Default.LimsConnection = configuration.LimsConnection;
  153. Properties.Settings.Default.LimsConnString = configuration.LimsConnString;
  154. Properties.Settings.Default.PrepmasterConnection = configuration.PrepmasterConnection;
  155. Properties.Settings.Default.LoggingDebug = configuration.LoggingDebug;
  156. Properties.Settings.Default.LoggingInfo = configuration.LoggingInfo;
  157. Properties.Settings.Default.LoggingWarning = configuration.LoggingWarning;
  158. Properties.Settings.Default.LoggingError = configuration.LoggingError;
  159. Properties.Settings.Default.Save();
  160. }
  161. /// <summary>
  162. /// Export a Json configuration file.
  163. /// </summary>
  164. /// <param name="configFile">File path and file name to save the config file to.</param>
  165. public static void ExportConfigFile(string configFile = "config.json")
  166. {
  167. using var ms = new MemoryStream();
  168. using var writer = new Utf8JsonWriter(ms);
  169. writer.WriteStartObject();
  170. writer.WriteNumber("CrusherNo", Properties.Settings.Default.CrusherNo);
  171. writer.WriteString("Serial", Properties.Settings.Default.PrepmasterMagazineSerial);
  172. writer.WriteString("OreDefInFile", Properties.Settings.Default.OreDefInFile);
  173. writer.WriteString("OreDefWorkFile", Properties.Settings.Default.OreDefWorkFile);
  174. writer.WriteString("OreDefOutFile", Properties.Settings.Default.OreDefOutFile);
  175. writer.WriteString("OutputFormat", Properties.Settings.Default.OutputFormat);
  176. writer.WriteString("OreDefTypes", Properties.Settings.Default.OreDefTypes);
  177. writer.WriteBoolean("LimsConnection", Properties.Settings.Default.LimsConnection);
  178. writer.WriteString("LimsConnString", Properties.Settings.Default.LimsConnString);
  179. writer.WriteBoolean("PrepmasterConnection", Properties.Settings.Default.PrepmasterConnection);
  180. writer.WriteBoolean("LoggingDebug", Properties.Settings.Default.LoggingDebug);
  181. writer.WriteBoolean("LoggingInfo", Properties.Settings.Default.LoggingInfo);
  182. writer.WriteBoolean("LoggingWarning", Properties.Settings.Default.LoggingWarning);
  183. writer.WriteBoolean("LoggingError", Properties.Settings.Default.LoggingError);
  184. writer.WriteEndObject();
  185. writer.Flush();
  186. string json = Encoding.UTF8.GetString(ms.ToArray());
  187. File.WriteAllText(configFile, json);
  188. }
  189. #endregion
  190. }
  191. }