using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace crusherScanner
{
// TODO: Caution memory leak be here. Consider flushing buffer SIDs after a period of time.
internal class LimsAccess
{
///
/// Buffer of sample ID's from LIMS or sample list file.
///
private static HashSet SIDs = new();
///
/// Check if barcode is found in LIMS samples.
///
/// Barcode as string to check LIMS for.
public static Scan IsInLims(Scan barcode)
{
// Is sample ID in buffer
if(CheckBuffer(barcode))
{
barcode.isInLims = true;
return barcode;
}
// LIMS connection enabled, ask LIMS for sample ID.
if (Properties.Settings.Default.LimsConnection)
{
//TODO: Actully check LIMS for sample ID. And fill buffer.
barcode.isInLims = true;
return barcode;
//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
//string connetionString;
//SqlConnection cnn;
//connetionString = @"Data Source=WIN-50GP30FGO75;Initial Catalog=Demodb;User ID=sa;Password=demol23";
//cnn = new SqlConnection(connetionString);
//cnn.Open();
//MessageBox.Show("Connection Open !");
//cnn.Close();
//or
//https://dax.tips/2020/08/24/using-visual-studio-code-to-query-power-bi/
}
else // LIMS connection disabled. Check for a fall back file or return true.
{
if (CheckFileStore("sampleList.csv") && CheckFileStore(Properties.Settings.Default.OreDefWorkFile + "\\sampleList.csv") && CheckBuffer(barcode))
{
barcode.isInLims = true;
return barcode;
}
else
{
barcode.isInLims = false;
return barcode;
}
}
}
///
/// Check if the sample list file is available and load into the buffer.
///
///
private static bool CheckFileStore(string LimsList)
{
// No file list found, assume no checking and return true.
if(!File.Exists(LimsList))
{
return true;
}
// File list found, load into buffer and check if barcode exists.
using FileStream sampleList = new(LimsList, FileMode.Open, System.IO.FileAccess.Read, FileShare.None);
{
using StreamReader Reader = new(sampleList);
{
while (true)
{
var line = Reader.ReadLine();
if (line != null && line != "")
{
SIDs.Add(line.Trim());
}
else
{
break;
}
}
}
}
return false;
}
///
/// Check to see if the sample ID is in the buffer.
///
/// barcode (sample ID)
///
private static bool CheckBuffer(Scan barcode)
{
if(SIDs.Contains(barcode.barcode))
{
return true;
}
return false;
}
}
}