From aa54652617aef33f0843214633bc3e879adb7dc2 Mon Sep 17 00:00:00 2001 From: Tongg Date: Thu, 10 Apr 2025 11:19:23 +0800 Subject: [PATCH] fix: play images optimize --- App.config | 5 ++++- Model/Helper/ConfigurationHelper.cs | 13 +++++++++++++ Model/Services/SOCClientService.cs | 21 ++++++++++++++------- ViewModel/Grading/DiamondSelectVM.cs | 4 ++-- ViewModel/Grading/GradingLoadingVM.cs | 7 ++++--- 5 files changed, 37 insertions(+), 13 deletions(-) diff --git a/App.config b/App.config index 2757a33..474d0eb 100644 --- a/App.config +++ b/App.config @@ -8,6 +8,8 @@ + + @@ -18,8 +20,9 @@ - + + diff --git a/Model/Helper/ConfigurationHelper.cs b/Model/Helper/ConfigurationHelper.cs index 9176078..e6763af 100644 --- a/Model/Helper/ConfigurationHelper.cs +++ b/Model/Helper/ConfigurationHelper.cs @@ -24,6 +24,19 @@ public class ConfigurationHelper return ""; } } + + public static int ReadConfigValueToInteger(string key, int defaultV = 0) + { + if (int.TryParse(ReadConfigValue(key), out int i)) + { + return i; + } + else + { + return defaultV; + } + } + /// /// 写入配置 /// diff --git a/Model/Services/SOCClientService.cs b/Model/Services/SOCClientService.cs index b633ae5..cdac309 100644 --- a/Model/Services/SOCClientService.cs +++ b/Model/Services/SOCClientService.cs @@ -96,9 +96,12 @@ namespace SparkClient.Model.Services }; } } - private async Task SendGetRequestImageAsync(string url) + private async Task SendGetRequestImageAsync(string url, string sendCode = "") { - string sendCode = url.GenerateSign(); + if (sendCode.IsNullOrEmpty()) + { + sendCode = url.GenerateSign(); + } using (var client = new HttpClient()) { Logger.Info($"[SendCode={sendCode}]Request Image Download URL: {url}"); @@ -249,12 +252,14 @@ namespace SparkClient.Model.Services List imageNames = new List(); // 读取图片接口 int imageIndex = 0; + int imageTotal = ConfigurationHelper.ReadConfigValueToInteger("DetectImageTotal", 100); while (true) { string url = $"{_baseUrl}/retrieve_image/{imageIndex}"; + string sendCode = url.GenerateSign(); try { - var response = await SendGetRequestImageAsync(url); + var response = await SendGetRequestImageAsync(url,sendCode); int status = (int)response.StatusCode; if(token.IsCancellationRequested) token.ThrowIfCancellationRequested(); @@ -285,10 +290,12 @@ namespace SparkClient.Model.Services case 404: // 资源未找到,结束循环 - return imageNames; + Logger.Info($"[SendCode={sendCode}] Image Not Found]"); + break; + //return imageNames; default: // 其他状态码,记录警告并继续 - Logger.Warn($"Unexpected status code: {status} for URL: {url}"); + Logger.Warn($"[SendCode={sendCode}]Unexpected status code: {status} for URL: {url}"); imageIndex++; break; } @@ -296,13 +303,13 @@ namespace SparkClient.Model.Services catch (HttpRequestException ex) { // 捕获HTTP请求异常并记录错误信息 - Logger.Error($"HTTP request failed for URL: {url}, Exception: {ex.Message}"); + Logger.Error($"[SendCode={sendCode}]HTTP request failed for URL: {url}, Exception: {ex.Message}"); return imageNames; } catch (Exception ex) { // 捕获其他异常并记录错误信息,结束循环 - Logger.Error($"An unexpected error occurred for URL: {url}, Exception: {ex.Message}"); + Logger.Error($"[SendCode={sendCode}]An unexpected error occurred for URL: {url}, Exception: {ex.Message}"); return imageNames; } } diff --git a/ViewModel/Grading/DiamondSelectVM.cs b/ViewModel/Grading/DiamondSelectVM.cs index 31d3799..a55a8f1 100644 --- a/ViewModel/Grading/DiamondSelectVM.cs +++ b/ViewModel/Grading/DiamondSelectVM.cs @@ -569,7 +569,7 @@ public class DiamondSelectVM : BaseViewModel /// private static readonly ILog Logger = LogManager.GetLogger(typeof(AlgorithmServer)); [Log] - public static void HandleAlgorithmFailure(string[] image_files, string dCode = "") + public static void HandleAlgorithmFailure(string[] image_files, string dCode = "", string dType = "") { // 从配置文件中读取 imageHistoryPath @@ -584,7 +584,7 @@ public class DiamondSelectVM : BaseViewModel // 生成时间戳,格式为 yyyyMMddHHmmss string timestamp = DateTime.Now.ToString("yyyyMMdd-HHmmss"); // 组合新的文件夹路径 - string newFolderPath = Path.Combine(imageHistoryPath, string.IsNullOrWhiteSpace(dCode) ? $"image-{timestamp}" : $"image-{dCode}-{timestamp}" ); + string newFolderPath = Path.Combine(imageHistoryPath, string.IsNullOrWhiteSpace(dCode) ? $"image-{timestamp}-{dType}" : $"image-{dCode}-{timestamp}-{dType}" ); // 检查 D 盘内存空间 string strSafeSpace = ConfigurationHelper.ReadConfigValue("SafeSpaceReservation"); diff --git a/ViewModel/Grading/GradingLoadingVM.cs b/ViewModel/Grading/GradingLoadingVM.cs index d177d1e..3175067 100644 --- a/ViewModel/Grading/GradingLoadingVM.cs +++ b/ViewModel/Grading/GradingLoadingVM.cs @@ -424,7 +424,7 @@ public class GradingLoadingVM : BaseViewModel,IDisposable string strSafeSpace = ConfigurationHelper.ReadConfigValue("SafeSpaceReservation"); if (hasErr || "0".Equals(strSafeSpace)) { - DiamondSelectVM.HandleAlgorithmFailure(ImagePaths, _diamondCode); + DiamondSelectVM.HandleAlgorithmFailure(ImagePaths, _diamondCode, _diamnondType); } } } @@ -554,8 +554,9 @@ public class GradingLoadingVM : BaseViewModel,IDisposable public void LoadDefaultImages() { List imagePaths = new List(); - string? savePath = ConfigurationManager.AppSettings["ImageFileBasePath"]; - for (int i = 0; i < 100; i++) + string? savePath = ConfigurationHelper.ReadConfigValue("ImageFileBasePath"); + int imageTotal = ConfigurationHelper.ReadConfigValueToInteger("DetectImageTotal", 100); + for (int i = 0; i < imageTotal; i++) { imagePaths.Add($"image_{i}.bmp"); }