From 8ead72772d21189f4d32a028db2feb5edf6e4c39 Mon Sep 17 00:00:00 2001 From: Tongg Date: Thu, 13 Mar 2025 16:53:48 +0800 Subject: [PATCH] fix: 20250313 add history images config --- App.config | 2 + ViewModel/Grading/DiamondSelectVM.cs | 16 +-- ViewModel/Grading/GradingLoadingVM.cs | 138 ++++++++++++++------------ 3 files changed, 89 insertions(+), 67 deletions(-) diff --git a/App.config b/App.config index 9cb7d5e..cb1b6f9 100644 --- a/App.config +++ b/App.config @@ -8,6 +8,8 @@ + + diff --git a/ViewModel/Grading/DiamondSelectVM.cs b/ViewModel/Grading/DiamondSelectVM.cs index 8376e22..b883c2b 100644 --- a/ViewModel/Grading/DiamondSelectVM.cs +++ b/ViewModel/Grading/DiamondSelectVM.cs @@ -536,7 +536,7 @@ public class DiamondSelectVM : BaseViewModel /// private static readonly ILog Logger = LogManager.GetLogger(typeof(AlgorithmServer)); [Log] - private void HandleAlgorithmFailure(string image_files, string dCode = "") + public static void HandleAlgorithmFailure(string[] image_files, string dCode = "") { // 从配置文件中读取 imageHistoryPath @@ -552,10 +552,13 @@ public class DiamondSelectVM : BaseViewModel string timestamp = DateTime.Now.ToString("yyyyMMdd-HHmmss"); // 组合新的文件夹路径 string newFolderPath = Path.Combine(imageHistoryPath, string.IsNullOrWhiteSpace(dCode) ? $"image-{timestamp}" : $"image-{dCode}-{timestamp}" ); - + // 检查 D 盘内存空间 - DriveInfo dDrive = new DriveInfo("D"); - long requiredSpace = 10L * 1024 * 1024 * 1024; // 10GB + string strSafeSpace = ConfigurationHelper.ReadConfigValue("SafeSpaceReservation"); + long safeSpace = 10L; + long.TryParse(strSafeSpace, out safeSpace); + DriveInfo dDrive = new DriveInfo(imageHistoryPath.Substring(0,1)); + long requiredSpace = safeSpace * 1024 * 1024 * 1024; if (dDrive.TotalFreeSpace < requiredSpace) { // 如果 D 盘空间不足 10GB,删除最早创建的10个文件夹 @@ -582,8 +585,9 @@ public class DiamondSelectVM : BaseViewModel try { // 解析 image_files JSON 数组 - JArray imageFilesArray = JArray.Parse(image_files); - string[] imageFiles = imageFilesArray.Select(token => token.ToString()).ToArray(); + // JArray imageFilesArray = JArray.Parse(image_files); + // string[] imageFiles = imageFilesArray.Select(token => token.ToString()).ToArray(); + string[] imageFiles = image_files; string? imageBasePath = ConfigurationManager.AppSettings["ImageFileBasePath"]; // 图片根目录 if (string.IsNullOrEmpty(imageBasePath)) diff --git a/ViewModel/Grading/GradingLoadingVM.cs b/ViewModel/Grading/GradingLoadingVM.cs index 67d5d09..11ee063 100644 --- a/ViewModel/Grading/GradingLoadingVM.cs +++ b/ViewModel/Grading/GradingLoadingVM.cs @@ -222,7 +222,7 @@ public class GradingLoadingVM : BaseViewModel,IDisposable SOCClientService.Service.OpenPump(false); return -100; } - + Diamond diamond = new Diamond(); string circleSql = $"SELECT VALUE FROM CUTTER_CONFIG WHERE KEY = 'half_circle'"; DataTable circleTable = DataBaseHelper.ExecuteQuery(circleSql); @@ -295,82 +295,98 @@ public class GradingLoadingVM : BaseViewModel,IDisposable private async Task ReslutGen(Task detectTask) { - - CompleteProgressQuicklyAsync(); - - if (detectTask.Result != null) + bool hasErr = false; + try { - switch (detectTask.Result.Status) + CompleteProgressQuicklyAsync(); + + if (detectTask.Result != null) { - case StatusCodes.AlgorithmFailed: - new MessageBox().Show(MultilingualHelper.getString("AlgorithmFailed")); - return -1; - case StatusCodes.ImageFileReadFailure: - new MessageBox().Show(MultilingualHelper.getString("ImageFileReadFailure")); - return -1; - case StatusCodes.JsonParseFailure: + switch (detectTask.Result.Status) + { + case StatusCodes.AlgorithmFailed: + new MessageBox().Show(MultilingualHelper.getString("AlgorithmFailed")); + hasErr = true; + return -1; + case StatusCodes.ImageFileReadFailure: + new MessageBox().Show(MultilingualHelper.getString("ImageFileReadFailure")); + hasErr = true; + return -1; + case StatusCodes.JsonParseFailure: + new MessageBox().Show(MultilingualHelper.getString("JsonParseFailure")); + hasErr = true; + return -1; + case StatusCodes.NoDiamond: + new MessageBox().Show(MultilingualHelper.getString("NoDiamond")); + hasErr = true; + return -1; + } + + Progress = (100.00); + + string strParam = JsonConvert.SerializeObject(detectTask.Result); + //Logger.Info("序列化字符串:" + strParam); + AlgorithmResultEntity parameter = JsonConvert.DeserializeObject(strParam); + if (parameter == null && _isCancel == false) + { new MessageBox().Show(MultilingualHelper.getString("JsonParseFailure")); + hasErr = true; return -1; - case StatusCodes.NoDiamond: - new MessageBox().Show(MultilingualHelper.getString("NoDiamond")); - return -1; - } + } - Progress = (100.00); + parameter.DiamondCode = _diamondCode; + parameter.Standard = getStandardName(); + parameter.Shape = _diamnondType.Split(" ")[0]; + parameter.CrownType = _diamnondType.Split(" ")[1]; + parameter.PavType = _diamnondType.Split(" ")[2]; - string strParam = JsonConvert.SerializeObject(detectTask.Result); - //Logger.Info("序列化字符串:" + strParam); - AlgorithmResultEntity parameter = JsonConvert.DeserializeObject(strParam); - if (parameter == null && _isCancel == false) - { - new MessageBox().Show(MultilingualHelper.getString("JsonParseFailure")); - return -1; - } + try + { + string parameterJson = JsonConvert.SerializeObject(parameter); + parameterJson = JToken.Parse(parameterJson).ToString(); + string outputPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "logs", "result"); + if (!Directory.Exists(outputPath)) + Directory.CreateDirectory(outputPath); + string outputFilePath = $"{outputPath}/{_diamondCode}-{DateTime.Now:yyyyMMdd_HHmmss}.json"; + using (var file = File.Create(outputFilePath)) + using (StreamWriter stream = new StreamWriter(file)) + { + stream.Write(parameterJson); + } + } + catch (Exception ex) + { + Logger.Error("output输出失败:" + ex.Message); + } - parameter.DiamondCode = _diamondCode; - parameter.Standard = getStandardName(); - parameter.Shape = _diamnondType.Split(" ")[0]; - parameter.CrownType = _diamnondType.Split(" ")[1]; - parameter.PavType = _diamnondType.Split(" ")[2]; + Parameter = parameter; + if (_isCancel == true) + { + SOCClientService.Service.OpenPump(false); + return -100; + } - try - { - string parameterJson = JsonConvert.SerializeObject(parameter); - parameterJson = JToken.Parse(parameterJson).ToString(); - string outputPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "logs", "result"); - if (!Directory.Exists(outputPath)) - Directory.CreateDirectory(outputPath); - string outputFilePath = $"{outputPath}/{_diamondCode}-{DateTime.Now:yyyyMMdd_HHmmss}.json"; - using (var file = File.Create(outputFilePath)) - using (StreamWriter stream = new StreamWriter(file)) + ; + + if (parameter.status == StatusCodes.Recheck) { - stream.Write(parameterJson); + new MessageBox().Show(MultilingualHelper.getString("Recheck")); } - } - catch (Exception ex) - { - Logger.Error("output输出失败:" + ex.Message); - } - Parameter = parameter; - if (_isCancel == true) - { - SOCClientService.Service.OpenPump(false); - return -100; + return 0; } - ; - - if (parameter.status == StatusCodes.Recheck) + + return -10; + } + finally + { + string strSafeSpace = ConfigurationHelper.ReadConfigValue("SafeSpaceReservation"); + if (hasErr || "0".Equals(strSafeSpace)) { - new MessageBox().Show(MultilingualHelper.getString("Recheck")); + DiamondSelectVM.HandleAlgorithmFailure(ImagePaths, _diamondCode); } - - return 0; } - - - return -10; } private async Task CompleteProgressSlowlyAsync()