From 737dec3c384f394fd6f9849b4480b697d1ba35d5 Mon Sep 17 00:00:00 2001
From: wanshenmean <cathay_xy@163.com>
Date: 星期二, 17 三月 2026 09:16:44 +0800
Subject: [PATCH] chore: 提交所有当前改动
---
Code/WCS/WIDESEAWCS_S7Simulator/WIDESEAWCS_S7Simulator.Core/Persistence/FilePersistenceService.cs | 111 +++++++++++++++++++++++++------------------------------
1 files changed, 50 insertions(+), 61 deletions(-)
diff --git a/Code/WCS/WIDESEAWCS_S7Simulator/WIDESEAWCS_S7Simulator.Core/Persistence/FilePersistenceService.cs b/Code/WCS/WIDESEAWCS_S7Simulator/WIDESEAWCS_S7Simulator.Core/Persistence/FilePersistenceService.cs
index 27002dd..8a6c694 100644
--- a/Code/WCS/WIDESEAWCS_S7Simulator/WIDESEAWCS_S7Simulator.Core/Persistence/FilePersistenceService.cs
+++ b/Code/WCS/WIDESEAWCS_S7Simulator/WIDESEAWCS_S7Simulator.Core/Persistence/FilePersistenceService.cs
@@ -1,4 +1,4 @@
-using System;
+锘縰sing System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
@@ -14,18 +14,18 @@
namespace WIDESEAWCS_S7Simulator.Core.Persistence
{
/// <summary>
- /// 鏂囦欢鎸佷箙鍖栨湇鍔″疄鐜�
- /// 灏嗗疄渚嬮厤缃拰鍐呭瓨鏁版嵁淇濆瓨鍒版湰鍦癑SON鏂囦欢
+ /// 閺傚洣娆㈤幐浣风畽閸栨牗婀囬崝鈥崇杽閻�?
+ /// 鐏忓棗鐤勬笟瀣帳缂冾喖鎷伴崘鍛摠閺佺増宓佹穱婵嗙摠閸掔増婀伴崷鐧慡ON閺傚洣娆�
/// </summary>
public class FilePersistenceService : IPersistenceService
{
/// <summary>
- /// 鏁版嵁鐩綍璺緞
+ /// 閺佺増宓侀惄顔肩秿鐠侯垰绶�
/// </summary>
private readonly string _dataPath;
/// <summary>
- /// JSON搴忓垪鍖栭�夐」锛堢嚎绋嬪畨鍏級
+ /// JSON鎼村繐鍨崠鏍偓澶愩�嶉敍鍫㈠殠缁嬪鐣ㄩ崗顭掔礆
/// </summary>
private static readonly JsonSerializerOptions _jsonOptions = new JsonSerializerOptions
{
@@ -34,21 +34,22 @@
};
/// <summary>
- /// 鏂囦欢鎿嶄綔閿侊紙绾跨▼瀹夊叏锛�
+ /// 閺傚洣娆㈤幙宥勭稊闁夸緤绱欑痪璺ㄢ柤鐎瑰鍙忛敍?
/// </summary>
private readonly SemaphoreSlim _fileLock = new SemaphoreSlim(1, 1);
/// <summary>
- /// 鏋勯�犲嚱鏁�
+ /// 閺嬪嫰鈧姴鍤遍弫?
/// </summary>
- /// <param name="dataPath">鏁版嵁鐩綍璺緞</param>
+ /// <param name="dataPath">閺佺増宓侀惄顔肩秿鐠侯垰绶�</param>
public FilePersistenceService(string dataPath = "Data")
{
- _dataPath = dataPath;
+ // 鏉烆剚宕叉稉铏圭卜鐎电鐭惧鍕剁礄閸╄桨绨ぐ鎾冲瀹搞儰缍旈惄顔肩秿閿�?
+ _dataPath = Path.GetFullPath(dataPath);
try
{
- // 纭繚鏁版嵁鐩綍瀛樺湪
+ // 绾喕绻氶弫鐗堝祦閻╊喖缍嶇�涙ê婀�
if (!Directory.Exists(_dataPath))
{
Directory.CreateDirectory(_dataPath);
@@ -62,7 +63,7 @@
}
/// <summary>
- /// 淇濆瓨瀹炰緥閰嶇疆
+ /// 娣囨繂鐡ㄧ�圭偘绶ラ柊宥囩枂
/// </summary>
public async Task SaveInstanceConfigAsync(InstanceConfig config)
{
@@ -96,12 +97,12 @@
}
/// <summary>
- /// 鍔犺浇瀹炰緥閰嶇疆
+ /// 閸旂姾娴囩�圭偘绶ラ柊宥囩枂
/// </summary>
public async Task<InstanceConfig> LoadInstanceConfigAsync(string instanceId)
{
if (string.IsNullOrWhiteSpace(instanceId))
- throw new ArgumentException("瀹炰緥ID涓嶈兘涓虹┖", nameof(instanceId));
+ throw new ArgumentException("鐎圭偘绶D娑撳秷鍏樻稉铏光敄", nameof(instanceId));
ValidateInstanceId(instanceId);
@@ -110,13 +111,13 @@
{
var configPath = Path.Combine(GetInstanceDirectory(instanceId), "config.json");
if (!File.Exists(configPath))
- throw new FileNotFoundException($"瀹炰緥閰嶇疆鏂囦欢涓嶅瓨鍦�: {configPath}");
+ throw new FileNotFoundException($"鐎圭偘绶ラ柊宥囩枂閺傚洣娆㈡稉宥呯摠閸�? {configPath}");
var json = await File.ReadAllTextAsync(configPath);
var model = JsonSerializer.Deserialize<InstanceDataModel>(json, _jsonOptions);
if (model == null)
- throw new InvalidOperationException("鏃犳硶鍙嶅簭鍒楀寲瀹炰緥閰嶇疆");
+ throw new InvalidOperationException("閺冪姵纭堕崣宥呯碍閸掓瀵茬�圭偘绶ラ柊宥囩枂");
return ToEntity(model);
}
@@ -132,7 +133,7 @@
}
/// <summary>
- /// 鍔犺浇鎵�鏈夊疄渚嬮厤缃�
+ /// 閸旂姾娴囬幍鈧張澶婄杽娓氬鍘ょ純?
/// </summary>
public async Task<List<InstanceConfig>> LoadAllInstanceConfigsAsync()
{
@@ -165,7 +166,7 @@
catch (Exception ex)
{
Console.WriteLine($"Error loading instance config from '{configPath}': {ex.Message}");
- // 璺宠繃鏃犳硶鍔犺浇鐨勯厤缃枃浠�
+ // 鐠哄疇绻冮弮鐘崇《閸旂姾娴囬惃鍕帳缂冾喗鏋冩禒?
continue;
}
}
@@ -180,12 +181,12 @@
}
/// <summary>
- /// 鍒犻櫎瀹炰緥閰嶇疆
+ /// 閸掔娀娅庣�圭偘绶ラ柊宥囩枂
/// </summary>
public async Task DeleteInstanceConfigAsync(string instanceId)
{
if (string.IsNullOrWhiteSpace(instanceId))
- throw new ArgumentException("瀹炰緥ID涓嶈兘涓虹┖", nameof(instanceId));
+ throw new ArgumentException("鐎圭偘绶D娑撳秷鍏樻稉铏光敄", nameof(instanceId));
ValidateInstanceId(instanceId);
@@ -210,12 +211,12 @@
}
/// <summary>
- /// 淇濆瓨鍐呭瓨鏁版嵁
+ /// 娣囨繂鐡ㄩ崘鍛摠閺佺増宓�
/// </summary>
public async Task SaveMemoryDataAsync(string instanceId, IMemoryStore memoryStore)
{
if (string.IsNullOrWhiteSpace(instanceId))
- throw new ArgumentException("瀹炰緥ID涓嶈兘涓虹┖", nameof(instanceId));
+ throw new ArgumentException("鐎圭偘绶D娑撳秷鍏樻稉铏光敄", nameof(instanceId));
ValidateInstanceId(instanceId);
@@ -234,7 +235,7 @@
var memoryPath = Path.Combine(instanceDir, "memory.json");
var exportedData = memoryStore.Export();
- // 灏嗗瓧鑺傛暟缁勮浆鎹负Base64瀛楃涓蹭互渚縅SON搴忓垪鍖�
+ // 鐏忓棗鐡ч懞鍌涙殶缂佸嫯娴嗛幑顫礋Base64鐎涙顑佹稉韫簰娓氱竻SON鎼村繐鍨崠?
var memoryDataModel = new MemoryDataModel
{
MemoryData = exportedData.ToDictionary(
@@ -258,12 +259,12 @@
}
/// <summary>
- /// 鍔犺浇鍐呭瓨鏁版嵁
+ /// 閸旂姾娴囬崘鍛摠閺佺増宓�
/// </summary>
public async Task LoadMemoryDataAsync(string instanceId, IMemoryStore memoryStore)
{
if (string.IsNullOrWhiteSpace(instanceId))
- throw new ArgumentException("瀹炰緥ID涓嶈兘涓虹┖", nameof(instanceId));
+ throw new ArgumentException("鐎圭偘绶D娑撳秷鍏樻稉铏光敄", nameof(instanceId));
ValidateInstanceId(instanceId);
@@ -275,7 +276,7 @@
{
var memoryPath = Path.Combine(GetInstanceDirectory(instanceId), "memory.json");
if (!File.Exists(memoryPath))
- return; // 鍐呭瓨鏂囦欢涓嶅瓨鍦紝璺宠繃鍔犺浇
+ return; // 閸愬懎鐡ㄩ弬鍥︽娑撳秴鐡ㄩ崷顭掔礉鐠哄疇绻冮崝鐘烘祰
var json = await File.ReadAllTextAsync(memoryPath);
var memoryDataModel = JsonSerializer.Deserialize<MemoryDataModel>(json, _jsonOptions);
@@ -283,7 +284,7 @@
if (memoryDataModel?.MemoryData == null)
return;
- // 灏咮ase64瀛楃涓茶浆鎹㈠洖瀛楄妭鏁扮粍
+ // 鐏忓挳ase64鐎涙顑佹稉鑼舵祮閹广垹娲栫�涙濡弫鎵矋
var importedData = new Dictionary<string, byte[]>();
foreach (var kvp in memoryDataModel.MemoryData)
{
@@ -294,7 +295,7 @@
catch (FormatException ex)
{
Console.WriteLine($"Warning: Invalid Base64 data for memory region '{kvp.Key}' in instance '{instanceId}': {ex.Message}");
- // 璺宠繃鏃犳晥鐨凚ase64鏁版嵁
+ // 鐠哄疇绻冮弮鐘虫櫏閻ㄥ嚉ase64閺佺増宓�
continue;
}
}
@@ -313,7 +314,7 @@
}
/// <summary>
- /// 鑾峰彇瀹炰緥鐩綍璺緞
+ /// 閼惧嘲褰囩�圭偘绶ラ惄顔肩秿鐠侯垰绶�
/// </summary>
private string GetInstanceDirectory(string instanceId)
{
@@ -321,58 +322,41 @@
}
/// <summary>
- /// 楠岃瘉瀹炰緥ID锛堥槻姝㈣矾寰勯亶鍘嗘敾鍑伙級
+ /// 妤犲矁鐦夌�圭偘绶D閿涘牓妲诲銏g熅瀵板嫰浜堕崢鍡樻暰閸戜紮绱�
/// </summary>
private void ValidateInstanceId(string instanceId)
{
if (string.IsNullOrWhiteSpace(instanceId))
- throw new ArgumentException("瀹炰緥ID涓嶈兘涓虹┖", nameof(instanceId));
+ throw new ArgumentException("鐎圭偘绶D娑撳秷鍏樻稉铏光敄", nameof(instanceId));
- // 妫�鏌ヨ矾寰勯亶鍘嗗瓧绗�
+ // 濡偓閺屻儴鐭惧鍕憾閸樺棗鐡х粭?
if (instanceId.Contains("..") || instanceId.Contains("/") || instanceId.Contains("\\"))
- throw new ArgumentException("瀹炰緥ID鍖呭惈闈炴硶瀛楃", nameof(instanceId));
+ throw new ArgumentException("鐎圭偘绶D閸栧懎鎯堥棃鐐寸《鐎涙顑�", nameof(instanceId));
- // 妫�鏌ユ棤鏁堣矾寰勫瓧绗�
+ // 濡偓閺屻儲妫ら弫鍫g熅瀵板嫬鐡х粭?
var invalidChars = Path.GetInvalidFileNameChars();
if (instanceId.IndexOfAny(invalidChars) >= 0)
- throw new ArgumentException("瀹炰緥ID鍖呭惈闈炴硶瀛楃", nameof(instanceId));
+ throw new ArgumentException("鐎圭偘绶D閸栧懎鎯堥棃鐐寸《鐎涙顑�", nameof(instanceId));
}
/// <summary>
- /// 鍘熷瓙鎬у啓鍏ユ枃浠讹紙浣跨敤涓存椂鏂囦欢+鏇挎崲妯″紡锛�
+ /// 閸愭瑥鍙嗛弬鍥︽閿涘牏娲块幒銉ュ晸閸忋儻绱濈粻鈧崠鏍閺堫剨绱�
/// </summary>
private async Task WriteFileAtomicAsync(string filePath, string content)
{
- var tempPath = filePath + ".tmp";
-
- try
+ // 绾喕绻氶惄顔界垼閺傚洣娆㈤惃鍕煑閻╊喖缍嶇�涙ê婀�
+ var directory = Path.GetDirectoryName(filePath);
+ if (!string.IsNullOrEmpty(directory) && !Directory.Exists(directory))
{
- // 鍐欏叆涓存椂鏂囦欢
- await File.WriteAllTextAsync(tempPath, content, Encoding.UTF8);
+ Directory.CreateDirectory(directory);
+ }
- // 鍘熷瓙鎬ф浛鎹㈢洰鏍囨枃浠�
- File.Replace(tempPath, filePath, null);
- }
- catch
- {
- // 娓呯悊涓存椂鏂囦欢
- if (File.Exists(tempPath))
- {
- try
- {
- File.Delete(tempPath);
- }
- catch
- {
- // 蹇界暐娓呯悊澶辫触
- }
- }
- throw;
- }
+ // 閻╁瓨甯撮崘娆忓弳閺傚洣娆�
+ await File.WriteAllTextAsync(filePath, content, Encoding.UTF8);
}
/// <summary>
- /// 灏嗗疄浣撹浆鎹负鏁版嵁妯″瀷
+ /// 鐏忓棗鐤勬担鎾规祮閹诡澀璐熼弫鐗堝祦濡�崇��
/// </summary>
private InstanceDataModel ToDataModel(InstanceConfig config)
{
@@ -384,10 +368,12 @@
Port = config.Port,
ActivationKey = config.ActivationKey,
AutoStart = config.AutoStart,
+ ProtocolTemplateId = config.ProtocolTemplateId,
MemoryConfig = new MemoryRegionConfigModel
{
MRegionSize = config.MemoryConfig.MRegionSize,
DBBlockCount = config.MemoryConfig.DBBlockCount,
+ DBBlockNumbers = config.MemoryConfig.DBBlockNumbers.ToList(),
DBBlockSize = config.MemoryConfig.DBBlockSize,
IRegionSize = config.MemoryConfig.IRegionSize,
QRegionSize = config.MemoryConfig.QRegionSize,
@@ -398,7 +384,7 @@
}
/// <summary>
- /// 灏嗘暟鎹ā鍨嬭浆鎹负瀹炰綋
+ /// 鐏忓棙鏆熼幑顔侥侀崹瀣祮閹诡澀璐熺�圭偘缍�
/// </summary>
private InstanceConfig ToEntity(InstanceDataModel model)
{
@@ -410,10 +396,12 @@
Port = model.Port,
ActivationKey = model.ActivationKey,
AutoStart = model.AutoStart,
+ ProtocolTemplateId = model.ProtocolTemplateId,
MemoryConfig = new MemoryRegionConfig
{
MRegionSize = model.MemoryConfig.MRegionSize,
DBBlockCount = model.MemoryConfig.DBBlockCount,
+ DBBlockNumbers = model.MemoryConfig.DBBlockNumbers?.ToList() ?? new List<int>(),
DBBlockSize = model.MemoryConfig.DBBlockSize,
IRegionSize = model.MemoryConfig.IRegionSize,
QRegionSize = model.MemoryConfig.QRegionSize,
@@ -424,3 +412,4 @@
}
}
}
+
--
Gitblit v1.9.3