using System; using System.Collections.Generic; using System.Data.SqlClient; using System.Linq; using System.Text; using System.Text.Json; using System.Threading.Tasks; namespace crusherScanner { public class ProgramFunctions { private static string lastBarcodeScanned = ""; public static bool ready = false; #region Contructor public ProgramFunctions() { Settings tmp = new(); tmp.CrusherNo = 1; tmp.OreDefInFile = "te"; SettingsHandler.CheckSettings(tmp); // no initial configuration present if (Properties.Settings.Default.OreDefInFile == "") { if (File.Exists("config.json")) { MessageBox.Show("No configuration stored,\nloading configuration from 'config.json'","Settings",MessageBoxButtons.OK,MessageBoxIcon.Exclamation); JsonHandler conf = new("config.json", true); //ImportConfigFile("config.json"); } else { MessageBox.Show("No configuration stored,\nYou will need to enter settings manually.\nThe settings will open now.", "Settings", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); SettingsDialog dlg = new(); dlg.ShowDialog(); dlg.Dispose(); } } //TODO check if settings are ok to use before setting ready. } #endregion #region Main logic public static Scan StartChecks(Scan scanned) { scanned.messageState = true; scanned = LimsAccess.IsInLims(scanned); if (!scanned.isInLims) // Check if this sample exists in LIMS { scanned.messageState = false; scanned.message = $"Sample {scanned.barcode} does not seem to be in LIMS."; return scanned; } scanned = OreDefData.CheckSampleType(scanned); if (scanned.sampleType == "Error!") // Check if sample prefix needs checking { scanned.messageState = false; scanned.message = "Unable to check sample due to a configuration issue."; return scanned; } if(scanned.sampleType == null) //sample is NOT an OreDef sample { scanned.message = $"Sample {scanned.barcode} is not an OreDef sample."; SerialAccess.SendToPrepmaster(scanned.barcode); lastBarcodeScanned = scanned.barcode; return scanned; } scanned = OreDefData.CheckContaminates(scanned); if (!scanned.Contaminated && !scanned.SampleNotFound) // No contamnination and SID found { scanned.message = $"Sample {scanned.barcode} is good to go."; SerialAccess.SendToPrepmaster(scanned.barcode); lastBarcodeScanned = scanned.barcode; return scanned; } else if (scanned.Contaminated && !scanned.SampleNotFound) // Contaminated sample found { scanned.message = ""; if (lastBarcodeScanned == scanned.barcode) { SerialAccess.SendToPrepmaster(scanned.barcode); } lastBarcodeScanned = scanned.barcode; return scanned; } else // Sample not found in job lists. { scanned.messageState = false; scanned.message = $"Sample {scanned.barcode} is not found in job lists."; return scanned; } } #endregion #region Settings functions public static void ImportConfigFile(string configFile = "config.json") { string data = File.ReadAllText(configFile); using JsonDocument doc = JsonDocument.Parse(data); JsonElement root = doc.RootElement; var config = root; Properties.Settings.Default.CrusherNo = (int)config.GetProperty("CrusherNo").GetInt32(); Properties.Settings.Default.PrepmasterMagazineSerial = config.GetProperty("Serial").ToString(); Properties.Settings.Default.OreDefInFile = config.GetProperty("OreDefInFile").ToString(); Properties.Settings.Default.OreDefWorkFile = config.GetProperty("OreDefWorkFile").ToString(); Properties.Settings.Default.OreDefOutFile = config.GetProperty("OreDefOutFile").ToString(); Properties.Settings.Default.OutputFormat = config.GetProperty("OutputFormat").ToString(); Properties.Settings.Default.Save(); } /// /// Export a Json configuration file. /// /// File path and file name to save the config file to. public static void ExportConfigFile(string configFile = "config.json") { using var ms = new MemoryStream(); using var writer = new Utf8JsonWriter(ms); writer.WriteStartObject(); writer.WriteNumber("CrusherNo", Properties.Settings.Default.CrusherNo); writer.WriteString("Serial", Properties.Settings.Default.PrepmasterMagazineSerial); writer.WriteString("OreDefInFile", Properties.Settings.Default.OreDefInFile); writer.WriteString("OreDefWorkFile", Properties.Settings.Default.OreDefWorkFile); writer.WriteString("OreDefOutFile", Properties.Settings.Default.OreDefOutFile); writer.WriteString("OutputFormat", Properties.Settings.Default.OutputFormat); writer.WriteEndObject(); writer.Flush(); string json = Encoding.UTF8.GetString(ms.ToArray()); File.WriteAllText(configFile, json); } #endregion } }