干度和6个物性参数计算验算过的方法
This commit is contained in:
@@ -701,6 +701,114 @@ namespace CapMachine.Wpf.Services
|
||||
HasHeaderRecord = false,
|
||||
};
|
||||
|
||||
private static readonly HashSet<string> ExcludedCsvHeaderNames = new HashSet<string>(StringComparer.OrdinalIgnoreCase)
|
||||
{
|
||||
"EVAP出口温度[℃]",
|
||||
"EVAP出口温度[°C]",
|
||||
"EVAP出口压力[BarA]",
|
||||
"干度流量[kg/h]",
|
||||
"干度流量[kq/h]",
|
||||
"EVAP循环水温[℃]",
|
||||
"EVAP循坏水温[℃]",
|
||||
};
|
||||
|
||||
private static readonly object CsvSchemaRewriteLock = new object();
|
||||
private static readonly HashSet<string> CsvSchemaCheckedFiles = new HashSet<string>(StringComparer.OrdinalIgnoreCase);
|
||||
|
||||
private bool TryRewriteCsvWithoutExcludedColumnsIfNeeded(string filePath)
|
||||
{
|
||||
try
|
||||
{
|
||||
if (!File.Exists(filePath))
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
lock (CsvSchemaRewriteLock)
|
||||
{
|
||||
if (CsvSchemaCheckedFiles.Contains(filePath))
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
using (var reader = new StreamReader(filePath, Encoding.UTF8, true))
|
||||
using (var csv = new CsvReader(reader, CultureInfo.CurrentCulture))
|
||||
{
|
||||
if (!csv.Read())
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
csv.ReadHeader();
|
||||
var header = csv.HeaderRecord;
|
||||
if (header == null || header.Length == 0)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
bool containsExcluded = header.Any(h => !string.IsNullOrWhiteSpace(h) && ExcludedCsvHeaderNames.Contains(h.Trim()));
|
||||
if (!containsExcluded)
|
||||
{
|
||||
lock (CsvSchemaRewriteLock)
|
||||
{
|
||||
CsvSchemaCheckedFiles.Add(filePath);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
string tmpPath = filePath + ".tmp";
|
||||
using (var srcReader = new StreamReader(filePath, Encoding.UTF8, true))
|
||||
using (var srcCsv = new CsvReader(srcReader, CultureInfo.CurrentCulture))
|
||||
using (var dstWriter = new StreamWriter(tmpPath, false, Encoding.UTF8))
|
||||
using (var dstCsv = new CsvWriter(dstWriter, CultureInfo.CurrentCulture))
|
||||
{
|
||||
srcCsv.Context.RegisterClassMap<CsvRecordModelMap>();
|
||||
dstCsv.Context.RegisterClassMap<CsvRecordModelMap>();
|
||||
dstCsv.WriteRecords(srcCsv.GetRecords<CsvRecordModel>());
|
||||
}
|
||||
|
||||
string bakPath = filePath + ".bak";
|
||||
if (File.Exists(bakPath))
|
||||
{
|
||||
File.Delete(bakPath);
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
File.Replace(tmpPath, filePath, bakPath, true);
|
||||
if (File.Exists(bakPath))
|
||||
{
|
||||
File.Delete(bakPath);
|
||||
}
|
||||
}
|
||||
catch
|
||||
{
|
||||
if (File.Exists(filePath))
|
||||
{
|
||||
File.Delete(filePath);
|
||||
}
|
||||
File.Move(tmpPath, filePath);
|
||||
if (File.Exists(bakPath))
|
||||
{
|
||||
File.Delete(bakPath);
|
||||
}
|
||||
}
|
||||
|
||||
lock (CsvSchemaRewriteLock)
|
||||
{
|
||||
CsvSchemaCheckedFiles.Add(filePath);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
LogService?.Error($"重写CSV列失败: {ex.Message}");
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 保存到CSV文件
|
||||
/// </summary>
|
||||
@@ -737,6 +845,11 @@ namespace CapMachine.Wpf.Services
|
||||
{
|
||||
lock (ConfigService.CsvFileLock)
|
||||
{
|
||||
if (!TryRewriteCsvWithoutExcludedColumnsIfNeeded(FileFullInfo))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
//往已有的文件增加数据
|
||||
using (var stream = File.Open(FileFullInfo, FileMode.Append, FileAccess.Write, FileShare.ReadWrite))
|
||||
//using (var stream = new FileStream(FileFullInfo, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite))
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user