LimsAccess.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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. barcode.isInLims = true;
  32. return barcode;
  33. //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
  34. //string connetionString;
  35. //SqlConnection cnn;
  36. //connetionString = @"Data Source=WIN-50GP30FGO75;Initial Catalog=Demodb;User ID=sa;Password=demol23";
  37. //cnn = new SqlConnection(connetionString);
  38. //cnn.Open();
  39. //MessageBox.Show("Connection Open !");
  40. //cnn.Close();
  41. //or
  42. //https://dax.tips/2020/08/24/using-visual-studio-code-to-query-power-bi/
  43. }
  44. else // LIMS connection disabled. Check for a fall back file or return true.
  45. {
  46. if (CheckFileStore("sampleList.csv") && CheckFileStore(Properties.Settings.Default.OreDefWorkFile + "\\sampleList.csv") && CheckBuffer(barcode))
  47. {
  48. barcode.isInLims = true;
  49. return barcode;
  50. }
  51. else
  52. {
  53. barcode.isInLims = false;
  54. return barcode;
  55. }
  56. }
  57. }
  58. /// <summary>
  59. /// Check if the sample list file is available and load into the buffer.
  60. /// </summary>
  61. /// <returns></returns>
  62. private static bool CheckFileStore(string LimsList)
  63. {
  64. // No file list found, assume no checking and return true.
  65. if(!File.Exists(LimsList))
  66. {
  67. return true;
  68. }
  69. // File list found, load into buffer and check if barcode exists.
  70. using FileStream sampleList = new(LimsList, FileMode.Open, System.IO.FileAccess.Read, FileShare.None);
  71. {
  72. using StreamReader Reader = new(sampleList);
  73. {
  74. while (true)
  75. {
  76. var line = Reader.ReadLine();
  77. if (line != null && line != "")
  78. {
  79. SIDs.Add(line.Trim());
  80. }
  81. else
  82. {
  83. break;
  84. }
  85. }
  86. }
  87. }
  88. return false;
  89. }
  90. /// <summary>
  91. /// Check to see if the sample ID is in the buffer.
  92. /// </summary>
  93. /// <param name="barcode">barcode (sample ID)</param>
  94. /// <returns></returns>
  95. private static bool CheckBuffer(Scan barcode)
  96. {
  97. if(SIDs.Contains(barcode.barcode))
  98. {
  99. return true;
  100. }
  101. return false;
  102. }
  103. }
  104. }