ProgramFunctions.cs 10 KB

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