From b11144891aacfb2976057ee3ed8906028a028aae Mon Sep 17 00:00:00 2001 From: Tongg Date: Mon, 13 Jan 2025 10:57:19 +0800 Subject: [PATCH 1/2] =?UTF-8?q?fix:=20=E6=B6=88=E6=81=AF=E5=BC=B9=E7=AA=97?= =?UTF-8?q?=E4=BC=98=E5=8C=96=EF=BC=8C=E8=BE=93=E5=85=A5=E6=A1=86=E8=87=AA?= =?UTF-8?q?=E5=8A=A8=E8=8E=B7=E7=84=A6=EF=BC=8C=E5=9B=9E=E8=BD=A6=E7=A1=AE?= =?UTF-8?q?=E8=AE=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Views/Dialog/MessageBox.xaml.cs | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/Views/Dialog/MessageBox.xaml.cs b/Views/Dialog/MessageBox.xaml.cs index 6ad6bf1..6c167bb 100644 --- a/Views/Dialog/MessageBox.xaml.cs +++ b/Views/Dialog/MessageBox.xaml.cs @@ -69,11 +69,17 @@ public partial class MessageBox : Window return _result; } // - public MessageBoxResult ShowInput(String message, out String inputStr , String confirmButtonTitle = "", String cancelButtonTitle ="") + public MessageBoxResult ShowInput(String message, out String inputStr , String confirmButtonTitle = "", String cancelButtonTitle ="", bool EnterConfirm = true) { _showType = 2; this.Height = 300; TextBlockCenterBox.Text = message; + TextBoxInput.Focus(); + if (EnterConfirm) + { + TextBoxInput.KeyDown += TextBoxInput_OnKeyDown; + } + if (!string.IsNullOrWhiteSpace(confirmButtonTitle) && !string.IsNullOrWhiteSpace(cancelButtonTitle)) { ConfirmButtonText.Text = confirmButtonTitle; @@ -136,4 +142,12 @@ public partial class MessageBox : Window } } + + private void TextBoxInput_OnKeyDown(object sender, KeyEventArgs e) + { + if (e.Key == Key.Enter) // 检测是否按下回车键 + { + ConfirmButton_OnClick(sender, e); // 调用按钮的点击事件 + } + } } \ No newline at end of file From d27e407c79f4cf5964098cc51c16475cac082c68 Mon Sep 17 00:00:00 2001 From: handefeng <1030428966@qq.com> Date: Tue, 14 Jan 2025 08:53:12 +0800 Subject: [PATCH 2/2] =?UTF-8?q?fix=20:=E7=AE=97=E6=B3=95=E9=9A=94=E7=A6=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Model/Services/AlgorithmServer.cs | 173 +++++++++++++++++++++++------- SparkClient.sln.DotSettings.user | 4 + 2 files changed, 138 insertions(+), 39 deletions(-) diff --git a/Model/Services/AlgorithmServer.cs b/Model/Services/AlgorithmServer.cs index 8d2569c..074d888 100644 --- a/Model/Services/AlgorithmServer.cs +++ b/Model/Services/AlgorithmServer.cs @@ -8,6 +8,9 @@ using Newtonsoft.Json.Linq; using SparkClient.Model.Entity.ApiEntity; using SparkClient.Model.Helper; using System.Configuration; +using System.Diagnostics; +using System.Text; +using System.Text.RegularExpressions; using SparkClient.Model.Common; namespace SparkClient.Model.Services @@ -24,7 +27,7 @@ namespace SparkClient.Model.Services // 添加公共方法 public AlgorithmResultEntity CallParseJsonAndReturnActions(string shape, string shape_mode, string image_files) - { + { try { //半圆 @@ -33,18 +36,23 @@ namespace SparkClient.Model.Services object halfCircleValue = circleTable.Rows[0][0]; bool.TryParse(halfCircleValue.ToString(), out bool boolResult); bool half_circle = boolResult; - + //算法配置参数 string sql = $"SELECT JSON FROM ALGORITHM_CONFIG ORDER BY JSON_ORDER ASC"; DataTable table = DataBaseHelper.ExecuteQuery(sql); object lightLevelValue = table.Rows[0][0]; string algo_config = lightLevelValue.ToString() ?? throw new InvalidOperationException(); - + //图片根目录 string? image_file_base_path = ConfigurationManager.AppSettings["ImageFileBasePath"]; + if (string.IsNullOrEmpty(image_file_base_path)) + { + throw new InvalidOperationException("ImageFileBasePath is not configured in AppSettings."); + } // 获取 log4net 日志文件所在的目录 - string? log4NetLogDirectory = Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location); + string? log4NetLogDirectory = + Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location); // 构建C++ DLL 日志文件路径 string algorithm_log_path = Path.Combine(log4NetLogDirectory, "logs"); @@ -55,51 +63,137 @@ namespace SparkClient.Model.Services new JProperty("image_file_base_path", image_file_base_path), new JProperty("image_files", JToken.Parse(image_files)), new JProperty("half_circle", half_circle), - new JProperty("algorithm_log_path", algorithm_log_path), + new JProperty("algorithm_log_path", algorithm_log_path), new JProperty("algo_config", JObject.Parse(algo_config)) ); - string jsonDataString = jsonData.ToString(); - // 调用 C++ DLL 函数解析 JSON - IntPtr resultPtr = DetectDiamond(jsonDataString); - string resultJson = Marshal.PtrToStringAnsi(resultPtr); - // 释放 DLL 分配的内存 - FreeString(resultPtr); - // 检查返回的 JSON 字符串是否为空或无效 - if (string.IsNullOrEmpty(resultJson)) + string jsonDataString = jsonData.ToString().Replace("\"", "\\\""); + //string jsonDataString = jsonData.ToString(); + // // 调用 C++ DLL 函数解析 JSON + // IntPtr resultPtr = DetectDiamond(jsonDataString); + // string resultJson = Marshal.PtrToStringAnsi(resultPtr); + // // 释放 DLL 分配的内存 + // FreeString(resultPtr); + // // 检查返回的 JSON 字符串是否为空或无效 + // if (string.IsNullOrEmpty(resultJson)) + // { + // Logger.Error("Algorithm failed or no result returned."); + // // 返回一个默认的 AlgorithmResultEntity 对象表示解析失败 + // return new AlgorithmResultEntity + // { + // facets = new List(), + // measurements = new Measurements() + // }; + // } + + // 启动隔离的控制台应用程序 + var startInfo = new ProcessStartInfo { - // 返回一个默认的 AlgorithmResultEntity 对象表示解析失败 - return new AlgorithmResultEntity + FileName = "AlgorithmDllIsolationConsoleApp.exe", // 控制台应用程序路径 + Arguments = $"\"{jsonDataString}\"", // 将 JSON 数据作为参数传递给控制台应用程序 + RedirectStandardOutput = true, // 获取控制台应用程序的输出 + RedirectStandardError = true, + UseShellExecute = false, + CreateNoWindow = true + }; + + using (var process = Process.Start(startInfo)) + { + // 使用 StringBuilder 来捕获标准输出和标准错误 + var outputBuilder = new StringBuilder(); + var errorBuilder = new StringBuilder(); + + // 订阅标准输出事件 + process.OutputDataReceived += (sender, e) => { - facets = new List(), - measurements = new Measurements() + if (e.Data == null) + { + // 当 e.Data 为 null 时,表示读取完成 + outputBuilder.AppendLine(e.Data); + Console.WriteLine($"Output: {e.Data}"); + return; + } + // 将输出数据追加到 outputBuilder + outputBuilder.AppendLine(e.Data); + // 打印输出数据到控制台 + Console.WriteLine($"Output: {e.Data}"); }; - } - // 反序列化 JSON 字符串为 AlgorithmResultEntity 对象 - var result = JsonConvert.DeserializeObject(resultJson); - // 检查反序列化结果是否为 null - if (result == null) - { - // 返回一个默认的 AlgorithmResultEntity 对象表示解析失败 - return new AlgorithmResultEntity + // 订阅标准错误事件 + process.ErrorDataReceived += (sender, e) => { - facets = new List(), - measurements = new Measurements() + if (e.Data == null) + { + // 当 e.Data 为 null 时,表示读取完成 + errorBuilder.AppendLine(e.Data); + Logger.Error($"Error Output: {e.Data}"); + return; + } + // 将错误数据追加到 errorBuilder + errorBuilder.AppendLine(e.Data); + // 记录错误数据到日志 + Logger.Error($"Error Output: {e.Data}"); }; + + // 开始异步读取 + process.BeginOutputReadLine(); + process.BeginErrorReadLine(); + + // 等待控制台应用程序结束 + process.WaitForExit(); + + // 等待异步读取完成 + //Task.WaitAll(outputTcs.Task, errorTcs.Task); + + // 获取结果 + string resultJson = outputBuilder.ToString(); + string errorOutput = errorBuilder.ToString(); + + + // 如果控制台应用程序有错误输出 + if (!string.IsNullOrEmpty(errorOutput)) + { + Logger.Error($"Console App Error: {errorOutput}"); + } + + // 如果没有结果或失败 + if (string.IsNullOrEmpty(resultJson)) + { + Logger.Error("Algorithm failed or no result returned."); + return new AlgorithmResultEntity + { + facets = new List(), + measurements = new Measurements() + }; + } + + // 反序列化 JSON 字符串为 AlgorithmResultEntity 对象 + var result = JsonConvert.DeserializeObject(resultJson); + + // 检查反序列化结果是否为 null + if (result == null) + { + // 返回一个默认的 AlgorithmResultEntity 对象表示解析失败 + return new AlgorithmResultEntity + { + facets = new List(), + measurements = new Measurements() + }; + } + + // 记录算法失败的状态 + Logger.Info($"Algorithm failed with status: {result.status}"); + // 记录算法失败的错误信息 + Logger.Info($"Algorithm failed with errorMsg: {result.error_msg}"); + // 处理 C++ DLL 日志文件 + //ProcessDllLog(); + // 算法调用失败时,保存图片到历史记录文件夹 + // if (result.status == StatusCodes.AlgorithmFailed) + // { + // HandleAlgorithmFailure(image_files); + // } + return result; } - // 记录算法失败的状态 - Logger.Error($"Algorithm failed with status: {result.status}"); - // 记录算法失败的错误信息 - Logger.Error($"Algorithm failed with errorMsg: {result.error_msg}"); - // 处理 C++ DLL 日志文件 - //ProcessDllLog(); - // 算法调用失败时,保存图片到历史记录文件夹 - // if (result.status == StatusCodes.AlgorithmFailed) - // { - // HandleAlgorithmFailure(image_files); - // } - return result; } catch (Exception ex) { @@ -112,6 +206,7 @@ namespace SparkClient.Model.Services Logger.Error($"Inner Exception: {ex.InnerException.Message}"); Logger.Error($"Inner Stack Trace: {ex.InnerException.StackTrace}"); } + // 返回一个默认的 AlgorithmResultEntity 对象表示解析失败 return new AlgorithmResultEntity { diff --git a/SparkClient.sln.DotSettings.user b/SparkClient.sln.DotSettings.user index ebbda40..a510370 100644 --- a/SparkClient.sln.DotSettings.user +++ b/SparkClient.sln.DotSettings.user @@ -1,4 +1,6 @@  + ForceIncluded + ForceIncluded ForceIncluded ForceIncluded ForceIncluded @@ -49,6 +51,7 @@ ForceIncluded ForceIncluded ForceIncluded + ForceIncluded ForceIncluded ForceIncluded ForceIncluded @@ -59,6 +62,7 @@ ForceIncluded ForceIncluded ForceIncluded + ForceIncluded ForceIncluded ForceIncluded ForceIncluded