From ebfe68ca80b35f03dcbc46bc702375bbf2ac4d7d Mon Sep 17 00:00:00 2001 From: handefeng <1030428966@qq.com> Date: Thu, 19 Dec 2024 09:05:07 +0800 Subject: [PATCH] =?UTF-8?q?add=EF=BC=9Acheck?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Model/Services/AlgorithmServer.cs | 1 - Model/Services/SOCClientService.cs | 45 +++++++------- Resource/Document/log.txt | 18 ++++++ SparkClient.csproj | 9 +++ ViewModel/Grading/DiamondSelectVM.cs | 89 +++++++++++++++------------- Views/Dialog/MsgDialog.xaml | 45 ++++++++++++++ Views/Dialog/MsgDialog.xaml.cs | 62 +++++++++++++++++++ 7 files changed, 203 insertions(+), 66 deletions(-) create mode 100644 Views/Dialog/MsgDialog.xaml create mode 100644 Views/Dialog/MsgDialog.xaml.cs diff --git a/Model/Services/AlgorithmServer.cs b/Model/Services/AlgorithmServer.cs index 6291805..bf91b48 100644 --- a/Model/Services/AlgorithmServer.cs +++ b/Model/Services/AlgorithmServer.cs @@ -44,7 +44,6 @@ namespace SparkClient.Model.Services string resultJson = Marshal.PtrToStringAnsi(resultPtr); // 释放 DLL 分配的内存 FreeString(resultPtr); - Console.WriteLine("log999999999999999999"+resultJson); // 检查返回的 JSON 字符串是否为空或无效 if (string.IsNullOrEmpty(resultJson)) diff --git a/Model/Services/SOCClientService.cs b/Model/Services/SOCClientService.cs index 614fe58..ebcc0a1 100644 --- a/Model/Services/SOCClientService.cs +++ b/Model/Services/SOCClientService.cs @@ -19,18 +19,6 @@ namespace SparkClient.Model.Services // Log地址 private const string LogFilePath = @"..\..\..\Resource\Document\log.txt"; - private static readonly Dictionary StatusDescriptions = new Dictionary - { - { "S000", "成功" }, - { "S001", "采图正在进行中" }, - { "S002", "缓存图片被清理(读取不够及时)" }, - { "S003", "无法向单片机发送指令" }, - { "S004", "单片机访问超时" }, - { "S005", "单片机返回错误码" }, - { "P001", "未找到切工仪" }, - { "P002", "算法调用失败" } - }; - /// /// 基础URL,用于构建完整的API请求地址。 /// @@ -107,7 +95,13 @@ namespace SparkClient.Model.Services if (status == 200) { byte[] imageBytes = await response.Content.ReadAsByteArrayAsync(); - string fileName = Path.Combine(savePath, $"image_{imageIndex}.bmp"); + + // 获取 Content-Type 头以确定图片格式 + string contentType = response.Content.Headers.ContentType.MediaType; + string fileExtension = GetFileExtension(contentType); + + //string fileName = Path.Combine(savePath, $"image_{imageIndex}.bmp"); + string fileName = Path.Combine(savePath, $"image_{imageIndex}{fileExtension}"); // 图片名称List imageNames.Add(Path.GetFileName(fileName)); // 保存图片 @@ -203,21 +197,26 @@ namespace SparkClient.Model.Services } /// - /// 根据状态码获取相应的描述信息。 - /// 如果状态码在预定义的状态字典中存在,则返回对应的描述信息; - /// 否则返回“未知状态: [状态码]”。 + /// 根据给定的 MIME 类型获取对应的文件扩展名。 /// - /// 状态码。 - /// 状态描述信息。 - private string GetDescription(string status) + /// HTTP 响应中的 Content-Type 头字段,表示内容的 MIME 类型。 + /// 与 MIME 类型对应的文件扩展名。 + /// 当传入的 MIME 类型不受支持时抛出此异常。 + private string GetFileExtension(string contentType) { - if (StatusDescriptions.TryGetValue(status, out string description)) + switch (contentType.ToLower()) { - return description; + case "image/bmp": + return ".bmp"; + case "image/jpg": + return ".jpg"; + case "image/png": + return ".png"; + default: + throw new InvalidOperationException($"Unsupported content type: {contentType}"); } - return "未知状态: " + status; } - + } diff --git a/Resource/Document/log.txt b/Resource/Document/log.txt index 0de3e52..d0a6a0b 100644 --- a/Resource/Document/log.txt +++ b/Resource/Document/log.txt @@ -6,3 +6,21 @@ [2024-12-05 16:51:04.013] 发生异常: 由于目标计算机积极拒绝,无法连接。 (localhost:5000) [2024-12-05 16:58:31.184] 发生异常: 由于目标计算机积极拒绝,无法连接。 (localhost:5000) [2024-12-12 13:27:06.264] 发生异常: The process cannot access the file 'image_39.bmp' because it is being used by another process. +[2024-12-18 08:56:09.327] 发生异常: The request was canceled due to the configured HttpClient.Timeout of 100 seconds elapsing. +[2024-12-18 15:58:22.476] 发生异常: 由于目标计算机积极拒绝,无法连接。 (localhost:5000) +[2024-12-18 16:12:57.999] 发生异常: 由于目标计算机积极拒绝,无法连接。 (localhost:5000) +[2024-12-18 16:13:12.680] 发生异常: 由于目标计算机积极拒绝,无法连接。 (localhost:5000) +[2024-12-18 16:13:20.632] 发生异常: 由于目标计算机积极拒绝,无法连接。 (localhost:5000) +[2024-12-18 16:13:29.227] 发生异常: 由于目标计算机积极拒绝,无法连接。 (localhost:5000) +[2024-12-18 16:13:44.433] 发生异常: 由于目标计算机积极拒绝,无法连接。 (localhost:5000) +[2024-12-18 16:13:59.933] 发生异常: 由于目标计算机积极拒绝,无法连接。 (localhost:5000) +[2024-12-18 16:14:09.063] 发生异常: 由于目标计算机积极拒绝,无法连接。 (localhost:5000) +[2024-12-18 17:03:24.758] 发生异常: 由于目标计算机积极拒绝,无法连接。 (localhost:5000) +[2024-12-18 17:03:50.352] 发生异常: 由于目标计算机积极拒绝,无法连接。 (localhost:5000) +[2024-12-18 17:06:16.074] 发生异常: 由于目标计算机积极拒绝,无法连接。 (localhost:5000) +[2024-12-18 17:06:53.906] 发生异常: 由于目标计算机积极拒绝,无法连接。 (localhost:5000) +[2024-12-18 17:11:04.161] 发生异常: 由于目标计算机积极拒绝,无法连接。 (localhost:5000) +[2024-12-18 17:13:26.174] 发生异常: 由于目标计算机积极拒绝,无法连接。 (localhost:5000) +[2024-12-18 17:25:38.182] 发生异常: 由于目标计算机积极拒绝,无法连接。 (localhost:5000) +[2024-12-19 08:50:34.643] 发生异常: 由于目标计算机积极拒绝,无法连接。 (localhost:5000) +[2024-12-19 08:54:45.576] 发生异常: 由于目标计算机积极拒绝,无法连接。 (localhost:5000) diff --git a/SparkClient.csproj b/SparkClient.csproj index 4ceff56..d8fe62e 100644 --- a/SparkClient.csproj +++ b/SparkClient.csproj @@ -135,10 +135,19 @@ Designer + + MSBuild:Compile + Wpf + Designer + + + Code + MsgDialog.xaml + diff --git a/ViewModel/Grading/DiamondSelectVM.cs b/ViewModel/Grading/DiamondSelectVM.cs index c718b58..b096eef 100644 --- a/ViewModel/Grading/DiamondSelectVM.cs +++ b/ViewModel/Grading/DiamondSelectVM.cs @@ -115,9 +115,9 @@ public class DiamondSelectVM : BaseViewModel /// public async void StartGrading(object param) { - #if DEBUG - DoStartGrading(param); - #else + // #if DEBUG + // DoStartGrading(param); + // #else LoadingDialog loading = new LoadingDialog(); try { @@ -155,11 +155,33 @@ public class DiamondSelectVM : BaseViewModel parameter.CrownType = value.Split(" ")[1]; parameter.PavType = value.Split(" ")[2]; } - + // 启动soc socResolt = await DoSoc(); + if (socResolt.Status == "P001") + { + // 使用 Dispatcher 将 UI 操作调度到主线程 + Application.Current.Dispatcher.Invoke(() => + { + MsgDialog msgDialog = new MsgDialog(); + msgDialog.ShowDialog(); + }); + loading.Dispatcher.Invoke(() => loading.Close()); + return; + } // 启动算法 parameter = await DoAlgorithm(socResolt, parameter.Shape, parameter.CrownType); + if (parameter.status == "P002" || parameter.status == "P003" || parameter.status == "P004") + { + // 使用 Dispatcher 将 UI 操作调度到主线程 + Application.Current.Dispatcher.Invoke(() => + { + MsgDialog msgDialog = new MsgDialog(); + msgDialog.ShowDialog(); + }); + loading.Dispatcher.Invoke(() => loading.Close()); + return; + } parameter.Standard = "IGI 2024"; parameter.Shape = value.Split(" ")[0]; @@ -194,8 +216,8 @@ public class DiamondSelectVM : BaseViewModel } finally { - } - #endif + } + // #endif } /// @@ -287,35 +309,28 @@ public class DiamondSelectVM : BaseViewModel /// /// 启动切工仪接口。 /// - /// 图片索引 - /// 图片索引 - /// 图片索引 /// 图片的字节数组 private async Task DoSoc() { - // // 光照度 - // string sql = new CutterConfigEntity - // { - // ItemName = null, - // Key = null, - // Value = null - // }.GenerateSelectSQL(new List { "Value" }, new Dictionary { { "Key", "light_level" } }); - // SqliteParameter[] sqliteParameters = [new("@Key", "light_level")]; - // DataTable table = DataBaseHelper.ExecuteQuery(sql,sqliteParameters); - // object lightLevelValue = table.Rows[0][0]; - // if (!int.TryParse(lightLevelValue.ToString(), out int lightLevel)) - // { - // throw new InvalidOperationException("Light level value is not a valid integer."); - // } - // // 初始化SOC客户端服务,传入SOC端的地址和认证Token - // _socClientService = new SOCClientService(); - // // SOC接口 - // string savePath = @"d:\\diamond_images"; - // SocResultEntity resultEntity = await _socClientService.ProcessImageCollectionAsync(lightLevel, savePath); - // // 转换成json - // string jsonResult = JsonConvert.SerializeObject(resultEntity, Formatting.Indented); - - SocResultEntity resultEntity = new SocResultEntity(); + // 光照度 + string sql = new CutterConfigEntity + { + ItemName = null, + Key = null, + Value = null + }.GenerateSelectSQL(new List { "Value" }, new Dictionary { { "Key", "light_level" } }); + SqliteParameter[] sqliteParameters = [new("@Key", "light_level")]; + DataTable table = DataBaseHelper.ExecuteQuery(sql,sqliteParameters); + object lightLevelValue = table.Rows[0][0]; + if (!int.TryParse(lightLevelValue.ToString(), out int lightLevel)) + { + throw new InvalidOperationException("Light level value is not a valid integer."); + } + // 初始化SOC客户端服务,传入SOC端的地址和认证Token + _socClientService = new SOCClientService(); + // SOC接口 + string savePath = @"d:\\diamond_images"; + SocResultEntity resultEntity = await _socClientService.ProcessImageCollectionAsync(lightLevel, savePath); return resultEntity; } @@ -346,19 +361,9 @@ public class DiamondSelectVM : BaseViewModel AlgorithmResultEntity algoResult = _algorithmServer.CallParseJsonAndReturnActions(shape, shape_mode, image_file_base_path, image_files, algo_config); - - // 将 algoResult 序列化为格式化的 JSON 字符串 - string algoJsonResult = JsonConvert.SerializeObject(algoResult, Formatting.Indented); - ShowMessage(algoJsonResult); return Task.FromResult(algoResult); } - - private void ShowMessage(string message) - { - MessageBox.Show(message); - } - } public class ButtonInfo { diff --git a/Views/Dialog/MsgDialog.xaml b/Views/Dialog/MsgDialog.xaml new file mode 100644 index 0000000..b49866a --- /dev/null +++ b/Views/Dialog/MsgDialog.xaml @@ -0,0 +1,45 @@ + + + + + + + + + + + + + + + + + + + + +