using System.IO; namespace Housing.Services; public sealed class BarcodeScannerSettings { public bool Enabled { get; set; } public string PortName { get; set; } = "Auto"; public int BaudRate { get; set; } = 9600; public int ReadTimeout { get; set; } = 500; public int IdleCommitMilliseconds { get; set; } = 250; public bool DtrEnable { get; set; } = true; public bool RtsEnable { get; set; } = true; public static BarcodeScannerSettings Load(string filePath) { if (!File.Exists(filePath)) { throw new FileNotFoundException("Hardware.ini file was not found.", filePath); } var values = IniFile.LoadSection(filePath, "BarcodeScanner"); return new BarcodeScannerSettings { Enabled = GetBool(values, "Enabled", false), PortName = GetString(values, "PortName", "Auto"), BaudRate = GetInt(values, "BaudRate", 9600), ReadTimeout = GetInt(values, "ReadTimeout", 500), IdleCommitMilliseconds = GetInt(values, "IdleCommitMilliseconds", 250), DtrEnable = GetBool(values, "DtrEnable", true), RtsEnable = GetBool(values, "RtsEnable", true) }; } public static bool IsAutoPort(string portName) { return string.IsNullOrWhiteSpace(portName) || string.Equals(portName.Trim(), "Auto", StringComparison.OrdinalIgnoreCase); } private static string GetString(Dictionary values, string key, string defaultValue) { return values.TryGetValue(key, out var value) && !string.IsNullOrWhiteSpace(value) ? value.Trim() : defaultValue; } private static int GetInt(Dictionary values, string key, int defaultValue) { return values.TryGetValue(key, out var value) && int.TryParse(value, out var result) ? result : defaultValue; } private static bool GetBool(Dictionary values, string key, bool defaultValue) { if (!values.TryGetValue(key, out var value) || string.IsNullOrWhiteSpace(value)) { return defaultValue; } return value.Trim().ToUpperInvariant() switch { "1" or "TRUE" or "YES" or "ON" => true, "0" or "FALSE" or "NO" or "OFF" => false, _ => defaultValue }; } }