You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
169 lines
6.9 KiB
169 lines
6.9 KiB
|
3 months ago
|
using System;
|
||
|
|
using System.IO;
|
||
|
|
|
||
|
|
namespace Housing.Services
|
||
|
|
{
|
||
|
|
/// <summary>
|
||
|
|
/// Setting.ini (또는 Database.ini) 파일에서 바코드 관련 설정(BarcodeMapping)을 관리하는 클래스.
|
||
|
|
/// </summary>
|
||
|
|
public class BarcodeSettings
|
||
|
|
{
|
||
|
|
/// <summary>
|
||
|
|
/// 바코드 매핑 필수 여부.
|
||
|
|
/// true : 바코드 매핑 실패 시 검사 중단 (NG 처리)
|
||
|
|
/// false: 바코드 매핑 안 되어도 검사 계속 진행 (더미 데이터 생성 안함)
|
||
|
|
/// </summary>
|
||
|
|
public bool BarcodeMapping { get; set; } = true;
|
||
|
|
|
||
|
|
private static readonly object _barcodeLock = new object();
|
||
|
|
private static BarcodeSettings _cachedSettings;
|
||
|
|
private static DateTime _lastWriteTime;
|
||
|
|
private static string _cachedPath;
|
||
|
|
|
||
|
|
public static string GetDefaultFilePath()
|
||
|
|
{
|
||
|
|
string folder = AppDomain.CurrentDomain.BaseDirectory;
|
||
|
|
if (!Directory.Exists(folder))
|
||
|
|
{
|
||
|
|
Directory.CreateDirectory(folder);
|
||
|
|
}
|
||
|
|
|
||
|
|
string settingIni = Path.Combine(folder, "Setting.ini");
|
||
|
|
if (File.Exists(settingIni)) return settingIni;
|
||
|
|
|
||
|
|
string lowerSettingIni = Path.Combine(folder, "setting.ini");
|
||
|
|
if (File.Exists(lowerSettingIni)) return lowerSettingIni;
|
||
|
|
|
||
|
|
return settingIni;
|
||
|
|
}
|
||
|
|
|
||
|
|
public static BarcodeSettings Load(string path = null)
|
||
|
|
{
|
||
|
|
if (string.IsNullOrEmpty(path))
|
||
|
|
{
|
||
|
|
path = GetDefaultFilePath();
|
||
|
|
}
|
||
|
|
|
||
|
|
lock (_barcodeLock)
|
||
|
|
{
|
||
|
|
try
|
||
|
|
{
|
||
|
|
if (File.Exists(path))
|
||
|
|
{
|
||
|
|
var currentWriteTime = File.GetLastWriteTime(path);
|
||
|
|
if (_cachedSettings != null && _cachedPath == path && _lastWriteTime == currentWriteTime)
|
||
|
|
{
|
||
|
|
return _cachedSettings;
|
||
|
|
}
|
||
|
|
|
||
|
|
var settings = new BarcodeSettings();
|
||
|
|
var lines = File.ReadAllLines(path);
|
||
|
|
foreach (var line in lines)
|
||
|
|
{
|
||
|
|
var trimmed = line.Trim();
|
||
|
|
if (trimmed.StartsWith(";") || trimmed.StartsWith("#")) continue;
|
||
|
|
|
||
|
|
if (trimmed.StartsWith("BarcodeMapping", StringComparison.OrdinalIgnoreCase))
|
||
|
|
{
|
||
|
|
var parts = trimmed.Split(new[] { '=' }, 2);
|
||
|
|
if (parts.Length == 2)
|
||
|
|
{
|
||
|
|
if (bool.TryParse(parts[1].Trim(), out var val))
|
||
|
|
{
|
||
|
|
settings.BarcodeMapping = val;
|
||
|
|
}
|
||
|
|
else if (int.TryParse(parts[1].Trim(), out var intVal))
|
||
|
|
{
|
||
|
|
settings.BarcodeMapping = intVal != 0;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
_cachedSettings = settings;
|
||
|
|
_lastWriteTime = currentWriteTime;
|
||
|
|
_cachedPath = path;
|
||
|
|
return settings;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
catch (Exception ex)
|
||
|
|
{
|
||
|
|
System.Diagnostics.Debug.WriteLine($"[BarcodeSettings Load Error] {ex.Message}");
|
||
|
|
}
|
||
|
|
|
||
|
|
// 파일이 없을 경우 Setting.ini 자동 생성
|
||
|
|
EnsureDefaultFile(path);
|
||
|
|
return _cachedSettings ?? new BarcodeSettings();
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
public static void Save(BarcodeSettings settings, string path = null)
|
||
|
|
{
|
||
|
|
if (string.IsNullOrEmpty(path))
|
||
|
|
{
|
||
|
|
path = GetDefaultFilePath();
|
||
|
|
}
|
||
|
|
|
||
|
|
lock (_barcodeLock)
|
||
|
|
{
|
||
|
|
try
|
||
|
|
{
|
||
|
|
string dir = Path.GetDirectoryName(path);
|
||
|
|
if (!string.IsNullOrEmpty(dir) && !Directory.Exists(dir))
|
||
|
|
{
|
||
|
|
Directory.CreateDirectory(dir);
|
||
|
|
}
|
||
|
|
|
||
|
|
using (var writer = new StreamWriter(path, false, System.Text.Encoding.UTF8))
|
||
|
|
{
|
||
|
|
writer.WriteLine("; =====================================================================");
|
||
|
|
writer.WriteLine("; Leak Test System - 환경 및 바코드 설정 파일 (Setting.ini)");
|
||
|
|
writer.WriteLine("; =====================================================================");
|
||
|
|
writer.WriteLine();
|
||
|
|
writer.WriteLine("[Barcode]");
|
||
|
|
writer.WriteLine("; BarcodeMapping: 바코드 매핑 필수 여부 (true = 매핑 미될 시 검사 중단, false = 매핑 없어도 검사 진행)");
|
||
|
|
writer.WriteLine($"BarcodeMapping={(settings.BarcodeMapping ? "true" : "false")}");
|
||
|
|
}
|
||
|
|
|
||
|
|
_cachedSettings = settings;
|
||
|
|
_cachedPath = path;
|
||
|
|
_lastWriteTime = File.GetLastWriteTime(path);
|
||
|
|
}
|
||
|
|
catch (Exception ex)
|
||
|
|
{
|
||
|
|
System.Diagnostics.Debug.WriteLine($"[BarcodeSettings Save Error] {ex.Message}");
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
public static void EnsureDefaultFile(string path)
|
||
|
|
{
|
||
|
|
try
|
||
|
|
{
|
||
|
|
if (!File.Exists(path))
|
||
|
|
{
|
||
|
|
string dir = Path.GetDirectoryName(path);
|
||
|
|
if (!string.IsNullOrEmpty(dir) && !Directory.Exists(dir))
|
||
|
|
{
|
||
|
|
Directory.CreateDirectory(dir);
|
||
|
|
}
|
||
|
|
|
||
|
|
using (var writer = new StreamWriter(path, false, System.Text.Encoding.UTF8))
|
||
|
|
{
|
||
|
|
writer.WriteLine("; =====================================================================");
|
||
|
|
writer.WriteLine("; Leak Test System - 환경 및 바코드 설정 파일 (Setting.ini)");
|
||
|
|
writer.WriteLine("; =====================================================================");
|
||
|
|
writer.WriteLine();
|
||
|
|
writer.WriteLine("[Barcode]");
|
||
|
|
writer.WriteLine("; BarcodeMapping: 바코드 매핑 필수 여부 (true = 매핑 미될 시 검사 중단, false = 매핑 없어도 검사 진행)");
|
||
|
|
writer.WriteLine("BarcodeMapping=true");
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
catch (Exception ex)
|
||
|
|
{
|
||
|
|
System.Diagnostics.Debug.WriteLine($"[BarcodeSettings EnsureDefaultFile Error] {ex.Message}");
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|