diff --git a/Model/Services/AlgorithmServer.cs b/Model/Services/AlgorithmServer.cs index bf91b48..db14aa3 100644 --- a/Model/Services/AlgorithmServer.cs +++ b/Model/Services/AlgorithmServer.cs @@ -15,15 +15,9 @@ namespace SparkClient.Model.Services // 导入 C++ DLL 中的 FreeMemory 函数 [DllImport("diamond_cut_inspector.dll", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)] private static extern void FreeString(IntPtr ptr); - // 导入 C++ DLL 中的 Echo 函数 - [DllImport("diamond_cut_inspector.dll", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)] - private static extern string Echo(string jsonData); - // 导入 C++ DLL 中的 ParseJsonAndReturnActions 函数 - [DllImport("AlgorithmServer.dll", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)] - private static extern IntPtr ParseJsonAndReturnActions(string jsonData); // 添加公共方法 - public AlgorithmResultEntity CallParseJsonAndReturnActions(string shape, string shape_mode, string image_file_base_path, string image_files, string algo_config) + public AlgorithmResultEntity CallParseJsonAndReturnActions(string shape, string shape_mode, string image_file_base_path, string image_files, string algo_config, Boolean half_circle) { try { @@ -33,11 +27,11 @@ namespace SparkClient.Model.Services new JProperty("shape_mode", shape_mode), 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("algo_config", JObject.Parse(algo_config)) ); string jsonDataString = jsonData.ToString(); - // 调用 C++ DLL 函数解析 JSON IntPtr resultPtr = DetectDiamond(jsonDataString); @@ -85,6 +79,7 @@ namespace SparkClient.Model.Services Console.WriteLine($"Inner Stack Trace: {ex.InnerException.StackTrace}"); } // 返回一个默认的 AlgorithmResultEntity 对象表示解析失败 + MessageBox.Show(ex.Message); return new AlgorithmResultEntity { facets = new List(), diff --git a/Model/Services/SOCClientService.cs b/Model/Services/SOCClientService.cs index ebcc0a1..99eb449 100644 --- a/Model/Services/SOCClientService.cs +++ b/Model/Services/SOCClientService.cs @@ -36,6 +36,7 @@ namespace SparkClient.Model.Services /// 认证令牌 public SOCClientService() { + //_baseUrl = "http://192.168.3.100:8080"; _baseUrl = "http://localhost:5000/api/SoC"; _authToken = "your_basic_auth_token"; } @@ -62,17 +63,31 @@ namespace SparkClient.Model.Services public async Task CollectImagesAsync(int lightLevel) { string url = $"{_baseUrl}/collect_images?light_level={lightLevel}"; - var response = await SendGetRequestAsync(url); - if (response.IsSuccessStatusCode) + try { + var response = await SendGetRequestAsync(url); + + if (!response.IsSuccessStatusCode) + { + return "P001"; + } + var jsonResponse = await response.Content.ReadAsStringAsync(); var result = JsonConvert.DeserializeObject(jsonResponse); + + if (result == null) + { + return "P001"; + } + return result.Status; } - else + catch (Exception ex) { - return "collect_images_Error: " + (int)response.StatusCode; + // 记录异常信息 + Console.WriteLine($"An error occurred while collecting images: {ex.Message}"); + return "P001"; } } @@ -89,35 +104,58 @@ namespace SparkClient.Model.Services while (true) { string url = $"{_baseUrl}/retrieve_image/{imageIndex}"; - var response = await SendGetRequestAsync(url); - int status = (int)response.StatusCode; - - if (status == 200) - { - byte[] imageBytes = await response.Content.ReadAsByteArrayAsync(); - - // 获取 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)); - // 保存图片 - await File.WriteAllBytesAsync(fileName, imageBytes); - imageIndex++; - } - else if (status == 425) + try { - await Task.Delay(1000); + var response = await SendGetRequestAsync(url); + int status = (int)response.StatusCode; + + switch (status) + { + case 200: + byte[] imageBytes = await response.Content.ReadAsByteArrayAsync(); + + // 获取 Content-Type 头以确定图片格式 + string contentType = response.Content.Headers.ContentType.MediaType; + string fileExtension = GetFileExtension(contentType); + + string fileName = Path.Combine(savePath, $"image_{imageIndex}{fileExtension}"); + // 图片名称List + imageNames.Add(Path.GetFileName(fileName)); + // 保存图片 + await File.WriteAllBytesAsync(fileName, imageBytes); + imageIndex++; + break; + case 425: + // 请求被锁定,等待一段时间后重试 + await Task.Delay(1000); + break; + case 410: + // 资源已被永久删除,跳过当前索引 + imageIndex++; + break; + + case 404: + // 资源未找到,结束循环 + return imageNames; + default: + // 其他状态码,记录警告并继续 + Console.WriteLine($"Unexpected status code: {status} for URL: {url}"); + imageIndex++; + break; + } } - else if (status == 410) + catch (HttpRequestException ex) { - imageIndex++; + // 捕获HTTP请求异常并记录错误信息 + Console.WriteLine($"HTTP request failed for URL: {url}, Exception: {ex.Message}"); + imageNames.Clear(); + return imageNames; } - else if (status == 404) + catch (Exception ex) { + // 捕获其他异常并记录错误信息,结束循环 + Console.WriteLine($"An unexpected error occurred for URL: {url}, Exception: {ex.Message}"); + imageNames.Clear(); return imageNames; } } @@ -130,17 +168,33 @@ namespace SparkClient.Model.Services public async Task CollectStatusAsync() { string url = $"{_baseUrl}/collect_status"; - var response = await SendGetRequestAsync(url); - if (response.IsSuccessStatusCode) + try { - var jsonResponse = await response.Content.ReadAsStringAsync(); - var result = JsonConvert.DeserializeObject(jsonResponse); - return result.Status; + var response = await SendGetRequestAsync(url); + + if (response.IsSuccessStatusCode) + { + var jsonResponse = await response.Content.ReadAsStringAsync(); + var result = JsonConvert.DeserializeObject(jsonResponse); + return result.Status; + } + else + { + return "collect_status_Error: " + (int)response.StatusCode; + } + } + catch (HttpRequestException ex) + { + return "P001"; + } + catch (JsonException ex) + { + return "P001"; } - else + catch (Exception ex) { - return "collect_status_Error: " + (int)response.StatusCode; + return "P001"; } } diff --git a/Resource/Document/log.txt b/Resource/Document/log.txt index bacd20b..c7bebe0 100644 --- a/Resource/Document/log.txt +++ b/Resource/Document/log.txt @@ -24,6 +24,11 @@ [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) +[2024-12-19 10:41:33.529] 发生异常: 由于目标计算机积极拒绝,无法连接。 (localhost:5000) +[2024-12-19 10:45:23.466] 发生异常: 由于目标计算机积极拒绝,无法连接。 (localhost:5000) +[2024-12-19 10:45:35.270] 发生异常: 由于目标计算机积极拒绝,无法连接。 (localhost:5000) +[2024-12-19 16:22:02.662] 发生异常: 由于目标计算机积极拒绝,无法连接。 (localhost:5000) +[2024-12-19 16:22:15.784] 发生异常: 由于目标计算机积极拒绝,无法连接。 (localhost:5000) [2024-12-19 22:00:52.183] 发生异常: 由于目标计算机积极拒绝,无法连接。 (localhost:5000) [2024-12-19 22:01:00.827] 发生异常: 由于目标计算机积极拒绝,无法连接。 (localhost:5000) [2024-12-19 22:01:33.476] 发生异常: 由于目标计算机积极拒绝,无法连接。 (localhost:5000) diff --git a/SparkDB.db b/SparkDB.db index c0d48f8..035e56a 100644 Binary files a/SparkDB.db and b/SparkDB.db differ diff --git a/ViewModel/Grading/DiamondSelectVM.cs b/ViewModel/Grading/DiamondSelectVM.cs index 2eea17f..db2e259 100644 --- a/ViewModel/Grading/DiamondSelectVM.cs +++ b/ViewModel/Grading/DiamondSelectVM.cs @@ -155,32 +155,39 @@ public class DiamondSelectVM : BaseViewModel } // 启动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; - } + // socResolt = await DoSoc(); + // if (socResolt.Status == "P001" || socResolt.Images.Count == 0) + // { + // // /*// 使用 Dispatcher 将 UI 操作调度到主线程 + // // Application.Current.Dispatcher.Invoke(() => + // // { + // // MsgDialog msgDialog = new MsgDialog(); + // // msgDialog.ShowDialog(); + // // }); + // // loading.Dispatcher.Invoke(() => loading.Close()); + // // return;*/ + // MessageBox.Show("未找到切工仪", "错误", MessageBoxButton.OK, MessageBoxImage.Error); + // loading.Dispatcher.Invoke(() => loading.Close()); + // return; + // } // 启动算法 parameter = await DoAlgorithm(socResolt, parameter.Shape, parameter.CrownType); - if (parameter.status == "P002" || parameter.status == "P003" || parameter.status == "P004") + MessageBox.Show(parameter.error_msg); + switch (parameter.status) { - // 使用 Dispatcher 将 UI 操作调度到主线程 - Application.Current.Dispatcher.Invoke(() => - { - MsgDialog msgDialog = new MsgDialog(); - msgDialog.ShowDialog(); - }); - loading.Dispatcher.Invoke(() => loading.Close()); - return; + case "P002": + MessageBox.Show("P002:调用算法失败", "错误", MessageBoxButton.OK, MessageBoxImage.Error); + loading.Dispatcher.Invoke(() => loading.Close()); + return; + case "P003": + MessageBox.Show("P003:图片文件读取失败", "错误", MessageBoxButton.OK, MessageBoxImage.Error); + loading.Dispatcher.Invoke(() => loading.Close()); + return; + case "P004": + MessageBox.Show("P004:JSON解析失败", "错误", MessageBoxButton.OK, MessageBoxImage.Error); + loading.Dispatcher.Invoke(() => loading.Close()); + return; } - parameter.Standard = "IGI 2024"; parameter.Shape = value.Split(" ")[0]; parameter.CrownType = value.Split(" ")[1]; @@ -346,10 +353,12 @@ public class DiamondSelectVM : BaseViewModel //钻石子形状 string shape_mode = crownType; //图片根目录 - string image_file_base_path = "d:\\diamond_images"; + string image_file_base_path = "D:\\diamond_images"; //图片集合 //string image_files = JsonConvert.SerializeObject(socResolt.Images, Formatting.Indented); string image_files =$"[ \"image_0.bmp\", \"image_1.bmp\", \"image_2.bmp\", \"image_3.bmp\", \"image_4.bmp\", \"image_5.bmp\", \"image_6.bmp\", \"image_7.bmp\", \"image_8.bmp\", \"image_9.bmp\", \"image_10.bmp\", \"image_11.bmp\", \"image_12.bmp\", \"image_13.bmp\", \"image_14.bmp\", \"image_15.bmp\", \"image_16.bmp\", \"image_17.bmp\", \"image_18.bmp\", \"image_19.bmp\", \"image_20.bmp\", \"image_21.bmp\", \"image_22.bmp\", \"image_23.bmp\", \"image_24.bmp\", \"image_25.bmp\", \"image_26.bmp\", \"image_27.bmp\", \"image_28.bmp\", \"image_29.bmp\", \"image_30.bmp\", \"image_31.bmp\", \"image_32.bmp\", \"image_33.bmp\", \"image_34.bmp\", \"image_35.bmp\", \"image_36.bmp\", \"image_37.bmp\", \"image_38.bmp\", \"image_39.bmp\", \"image_40.bmp\", \"image_41.bmp\", \"image_42.bmp\", \"image_43.bmp\", \"image_44.bmp\", \"image_45.bmp\", \"image_46.bmp\", \"image_47.bmp\", \"image_48.bmp\", \"image_49.bmp\" ]" ; + //半圆 + Boolean half_circle = false; //算法配置参数 string sql = $"SELECT JSON FROM ALGORITHM_CONFIG ORDER BY JSON_ORDER ASC"; @@ -357,7 +366,7 @@ public class DiamondSelectVM : BaseViewModel object lightLevelValue = table.Rows[0][0]; string algo_config = lightLevelValue.ToString() ?? throw new InvalidOperationException(); - AlgorithmResultEntity algoResult = _algorithmServer.CallParseJsonAndReturnActions(shape, shape_mode, image_file_base_path, image_files, algo_config); + AlgorithmResultEntity algoResult = _algorithmServer.CallParseJsonAndReturnActions(shape, shape_mode, image_file_base_path, image_files, algo_config,half_circle); return Task.FromResult(algoResult); }