| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223 |
- 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
- {
- /// <summary>
- /// Main entry point for sample checking functions.
- /// </summary>
- public class ProgramFunctions
- {
- private static string lastBarcodeScanned = "";
- public static bool ready = false;
- #region Contructor
- /// <summary>
- /// Constructor to load initial settings.
- /// </summary>
- public ProgramFunctions()
- {
-
- // no initial configuration present
- if (Properties.Settings.Default.OreDefInFile == "")
- {
- // Try to get settings from previous version of the app.
- Properties.Settings.Default.Upgrade();
- 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
- /// <summary>
- /// Check scanned sample ID in following order;
- /// - LIMS existence (active samples in the last 90 days). warn operator if not.
- /// - Is this an Ore def sample (RCA, RCB, RCC).
- /// - If not Ore def pass sample to crusher.
- /// - If Ore def without contamination send to crusher.
- /// - If sample has contamination not the ID and only send to crusher if scanned twice.
- /// - If sample could not be found in the Ore def lists. warn operator.
- /// </summary>
- /// <param name="scanned">Scaned sample details.</param>
- /// <returns></returns>
- 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
- /// <summary>
- /// Import app configuration from a JSON file.
- /// </summary>
- /// <param name="configFile">Path to file. (defaults to "config.json" in app directory)</param>
- public static void ImportConfigFile(string configFile = "config.json")
- {
- Settings tmpSettings = new();
- // Open JSON file to read
- string data = File.ReadAllText(configFile);
- using JsonDocument doc = JsonDocument.Parse(data);
- JsonElement root = doc.RootElement;
- var config = root;
- // Set config data from JSON into temp settings var for handling.
- tmpSettings.CrusherNo = (int)config.GetProperty("CrusherNo").GetInt32();
- tmpSettings.PrepmasterMagazineSerial = config.GetProperty("Serial").ToString();
- tmpSettings.OreDefInFile = config.GetProperty("OreDefInFile").ToString();
- tmpSettings.OreDefWorkFile = config.GetProperty("OreDefWorkFile").ToString();
- tmpSettings.OreDefOutFile = config.GetProperty("OreDefOutFile").ToString();
- tmpSettings.OutputFormat = config.GetProperty("OutputFormat").ToString();
- tmpSettings.OreDefTypes = config.GetProperty("OreDefTypes").ToString();
- tmpSettings.LimsConnection = config.GetProperty("LimsConnection").GetBoolean();
- tmpSettings.LimsConnString = config.GetProperty("LimsConnString").ToString();
- tmpSettings.PrepmasterConnection = config.GetProperty("PrepmasterConnection").GetBoolean();
- tmpSettings.LoggingDebug = config.GetProperty("LoggingDebug").GetBoolean();
- tmpSettings.LoggingInfo = config.GetProperty("LoggingInfo").GetBoolean();
- tmpSettings.LoggingWarning = config.GetProperty("LoggingWarning").GetBoolean();
- tmpSettings.LoggingError = config.GetProperty("LoggingError").GetBoolean();
- // Check settings, if good save to applications cache.
- if(SettingsHandler.CheckSettings(tmpSettings))
- {
- SaveSettings(tmpSettings);
- }
- }
- /// <summary>
- /// Save settings from temp var to app cache.
- /// </summary>
- /// <param name="configuration">Pre checked settings to save.</param>
- public static void SaveSettings(Settings configuration)
- {
- Properties.Settings.Default.CrusherNo = configuration.CrusherNo;
- Properties.Settings.Default.PrepmasterMagazineSerial = configuration.PrepmasterMagazineSerial;
- Properties.Settings.Default.OreDefInFile = configuration.OreDefInFile;
- Properties.Settings.Default.OreDefWorkFile = configuration.OreDefWorkFile;
- Properties.Settings.Default.OreDefOutFile = configuration.OreDefOutFile;
- Properties.Settings.Default.OutputFormat = configuration.OutputFormat;
- Properties.Settings.Default.OreDefTypes = configuration.OreDefTypes;
- Properties.Settings.Default.LimsConnection = configuration.LimsConnection;
- Properties.Settings.Default.LimsConnString = configuration.LimsConnString;
- Properties.Settings.Default.PrepmasterConnection = configuration.PrepmasterConnection;
- Properties.Settings.Default.LoggingDebug = configuration.LoggingDebug;
- Properties.Settings.Default.LoggingInfo = configuration.LoggingInfo;
- Properties.Settings.Default.LoggingWarning = configuration.LoggingWarning;
- Properties.Settings.Default.LoggingError = configuration.LoggingError;
- Properties.Settings.Default.Save();
- }
- /// <summary>
- /// Export a Json configuration file.
- /// </summary>
- /// <param name="configFile">File path and file name to save the config file to.</param>
- 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.WriteString("OreDefTypes", Properties.Settings.Default.OreDefTypes);
- writer.WriteBoolean("LimsConnection", Properties.Settings.Default.LimsConnection);
- writer.WriteString("LimsConnString", Properties.Settings.Default.LimsConnString);
- writer.WriteBoolean("PrepmasterConnection", Properties.Settings.Default.PrepmasterConnection);
- writer.WriteBoolean("LoggingDebug", Properties.Settings.Default.LoggingDebug);
- writer.WriteBoolean("LoggingInfo", Properties.Settings.Default.LoggingInfo);
- writer.WriteBoolean("LoggingWarning", Properties.Settings.Default.LoggingWarning);
- writer.WriteBoolean("LoggingError", Properties.Settings.Default.LoggingError);
- writer.WriteEndObject();
- writer.Flush();
- string json = Encoding.UTF8.GetString(ms.ToArray());
- File.WriteAllText(configFile, json);
- }
- #endregion
- }
- }
|