2026-06-05 13:44:39 +09:00

89 lines
3.2 KiB
C#

using ILSOS_PrismaticGun.Process;
using ILSOS_PrismaticGun.Settings;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Text;
namespace ILSOS_PrismaticGun.Tools
{
public class CsvLogger
{
private readonly object _fileLock = new object();
private readonly string _dirPath; // 디렉터리 보관
private readonly string _fileNamePrefix; // 파일명 접두 (Default, 또는 인자)
private readonly string _dateFormat = "yyyyMMdd";
private string _currentDate; // 마지막으로 쓴 날짜(yyyyMMdd)
private string _filePath; // 현재 쓰는 파일 경로
public CsvLogger(string filepath = null, string fileName = null)
{
_dirPath = string.IsNullOrWhiteSpace(filepath) ? AppDomain.CurrentDomain.BaseDirectory : filepath;
Directory.CreateDirectory(_dirPath);
_fileNamePrefix = string.IsNullOrWhiteSpace(fileName) ? "Default" : fileName;
lock (_fileLock) {
UpdateFileDate();
}
}
private void UpdateFileDate()
{
string today = DateTime.Now.ToString(_dateFormat, CultureInfo.InvariantCulture);
if (_currentDate == today && !string.IsNullOrEmpty(_filePath))
return;
_currentDate = today;
string safeName = $"{_fileNamePrefix}_{_currentDate}.csv";
_filePath = Path.Combine(_dirPath, safeName);
if (!File.Exists(_filePath)) {
File.AppendAllText(_filePath, "Time,Lot_ID,Cell_ID,Ng,Judge,HeightMeas1,HeightMeas2,HeightMeas3,HeightMeas4,HeightMeas5,HeightMeas6,HeightMeas7,HeightMeas8\n", Encoding.UTF8);
}
}
public void WriteLine(string str)
{
lock (_fileLock) {
UpdateFileDate();
string time = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss", CultureInfo.InvariantCulture);
string line = $"{time},{str}";
File.AppendAllText(_filePath, line + Environment.NewLine, Encoding.UTF8);
}
}
public void WriteLineCellData(PalletInfo Cell)
{
var sb = new StringBuilder();
sb.Append(Cell.weldingData.LotId); sb.Append(",");
sb.Append(Cell.weldingData.CellId); sb.Append(",");
sb.Append(Cell.weldingData.NgJudge > 0); sb.Append(",");
sb.Append(Cell.weldingData.NgReason); sb.Append(",");
foreach (var heightMeas in Cell.weldingData.HeightMeasurements) {
sb.Append(heightMeas); sb.Append(",");
}
WriteLine(sb.ToString());
//var inv = CultureInfo.InvariantCulture;
//string data = string.Join(",", Cell.weldingData.HeightMeasurements.Select(d => d.ToString("G", inv)));
//string line = $"{Cell.weldingData.LotId},{Cell.weldingData.CellId},{(Cell.weldingData.NgJudge > 0)},{Cell.weldingData.NgReason},{data}";
//WriteLine(line);
}
public string GetFilePath()
{
lock (_fileLock) {
UpdateFileDate();
return _filePath;
}
}
}
}