LimsAccess.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. namespace crusherScanner
  7. {
  8. // TODO: Caution memory leak be here. Consider flushing buffer SIDs after a period of time.
  9. internal class LimsAccess
  10. {
  11. /// <summary>
  12. /// Buffer of sample ID's from LIMS or sample list file.
  13. /// </summary>
  14. private static HashSet<string> SIDs = new();
  15. /// <summary>
  16. /// Check if barcode is found in LIMS samples.
  17. /// </summary>
  18. /// <param name="barcode">Barcode as string to check LIMS for.</param>
  19. public static Scan IsInLims(Scan barcode)
  20. {
  21. // Is sample ID in buffer
  22. if(CheckBuffer(barcode))
  23. {
  24. barcode.isInLims = true;
  25. return barcode;
  26. }
  27. // LIMS connection enabled, ask LIMS for sample ID.
  28. if (Properties.Settings.Default.LimsConnection)
  29. {
  30. //TODO: Actully check LIMS for sample ID. And fill buffer.
  31. Logging.Append(LogLevel.Information, $"Unable to contact the LIMS database : {barcode.barcode}");
  32. MessageBox.Show($"Unable to contact the LIMS database.", "Crusher Input");
  33. barcode.isInLims = false;
  34. return barcode;
  35. //https://www.guru99.com/c-sharp-access-database.html#:~:text=Code%20Explanation%3A-%201%20The%20first%20step%20is%20to,connection%20to%20the%20database.%20...%20More%20items...%20
  36. //string connetionString;
  37. //SqlConnection cnn;
  38. //connetionString = @"Data Source=WIN-50GP30FGO75;Initial Catalog=Demodb;User ID=sa;Password=demol23";
  39. //cnn = new SqlConnection(connetionString);
  40. //cnn.Open();
  41. //MessageBox.Show("Connection Open !");
  42. //cnn.Close();
  43. //or
  44. //https://dax.tips/2020/08/24/using-visual-studio-code-to-query-power-bi/
  45. }
  46. else // LIMS connection disabled. Check for a fall back file or return true.
  47. {
  48. if ((CheckFileStore("sampleList.csv") && CheckFileStore(Properties.Settings.Default.OreDefWorkFile + "\\sampleList.csv")) || CheckBuffer(barcode))
  49. {
  50. barcode.isInLims = true;
  51. return barcode;
  52. }
  53. else
  54. {
  55. barcode.isInLims = false;
  56. return barcode;
  57. }
  58. }
  59. }
  60. /// <summary>
  61. /// Check if the sample list file is available and load into the buffer.
  62. /// </summary>
  63. /// <returns></returns>
  64. private static bool CheckFileStore(string LimsList)
  65. {
  66. // No file list found, assume no checking and return true.
  67. if(!File.Exists(LimsList))
  68. {
  69. return true;
  70. }
  71. // File list found, load into buffer and check if barcode exists.
  72. using FileStream sampleList = new(LimsList, FileMode.Open, System.IO.FileAccess.Read, FileShare.None);
  73. {
  74. using StreamReader Reader = new(sampleList);
  75. {
  76. while (true)
  77. {
  78. var line = Reader.ReadLine();
  79. if (line != null && line != "")
  80. {
  81. SIDs.Add(line.Trim());
  82. }
  83. else
  84. {
  85. break;
  86. }
  87. }
  88. }
  89. }
  90. return false;
  91. }
  92. /// <summary>
  93. /// Check to see if the sample ID is in the buffer.
  94. /// </summary>
  95. /// <param name="barcode">barcode (sample ID)</param>
  96. /// <returns></returns>
  97. private static bool CheckBuffer(Scan barcode)
  98. {
  99. if(SIDs.Contains(barcode.barcode))
  100. {
  101. return true;
  102. }
  103. return false;
  104. }
  105. }
  106. }