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
{
///
/// Main entry point for sample checking functions.
///
public class ProgramFunctions
{
private static string lastBarcodeScanned = "";
public static bool ready = false;
#region Contructor
///
/// Constructor to load initial settings.
///
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
///
/// 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.
///
/// Scaned sample details.
///
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
///
/// Import app configuration from a JSON file.
///
/// Path to file. (defaults to "config.json" in app directory)
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);
}
}
///
/// Save settings from temp var to app cache.
///
/// Pre checked settings to save.
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();
}
///
/// 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.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
}
}