diff --git a/Libs/AlgorithmServer.dll b/Libs/AlgorithmServer.dll deleted file mode 100644 index e69de29..0000000 diff --git a/Model/Entity/ApiEntity/ImageCollectionResult.cs b/Model/Entity/ApiEntity/SocResultEntity.cs similarity index 72% rename from Model/Entity/ApiEntity/ImageCollectionResult.cs rename to Model/Entity/ApiEntity/SocResultEntity.cs index 0c41c23..6e27429 100644 --- a/Model/Entity/ApiEntity/ImageCollectionResult.cs +++ b/Model/Entity/ApiEntity/SocResultEntity.cs @@ -1,6 +1,6 @@ -namespace SparkClient.Model.Common; +namespace SparkClient.Model.Entity.ApiEntity; -public class ImageCollectionResult +public class SocResultEntity { /// diff --git a/Model/Services/AlgorithmServer.cs b/Model/Services/AlgorithmServer.cs index 09194bb..a697780 100644 --- a/Model/Services/AlgorithmServer.cs +++ b/Model/Services/AlgorithmServer.cs @@ -1,46 +1,98 @@ using System.Runtime.InteropServices; using System.Windows.Forms; +using Newtonsoft.Json; using Newtonsoft.Json.Linq; +using SparkClient.Model.Entity.ApiEntity; namespace SparkClient.Model.Services { public class AlgorithmServer { - // 使用 P/Invoke 声明 C++ 函数 - [DllImport("../../../Libs/AlgorithmServer.dll", CallingConvention = CallingConvention.Cdecl)] - private static extern double Add(double a, double b); - - [DllImport("../../../Libs/AlgorithmServer.dll", CallingConvention = CallingConvention.Cdecl)] - private static extern double Multiply(double a, double b); + // 导入 C++ DLL 中的 DetectDiamond 函数 + [DllImport("D:/workspace/dayuAI/SparkClient/bin/Debug/net8.0-windows/diamond_cut_inspector.dll", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)] + private static extern IntPtr DetectDiamond(string jsonData); + // 导入 C++ DLL 中的 FreeMemory 函数 + [DllImport("D:/workspace/dayuAI/SparkClient/bin/Debug/net8.0-windows/diamond_cut_inspector.dll", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)] + private static extern void FreeString(IntPtr ptr); + // 导入 C++ DLL 中的 Echo 函数 + [DllImport("D:/workspace/dayuAI/SparkClient/bin/Debug/net8.0-windows/diamond_cut_inspector.dll", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)] + private static extern string Echo(string jsonData); // 导入 C++ DLL 中的 ParseJsonAndReturnActions 函数 - [DllImport("../../../Libs/AlgorithmServer.dll", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)] - private static extern IntPtr ParseJsonAndReturnActions(byte[] jsonData); + [DllImport("D:/workspace/dayuAI/SparkClient/bin/Debug/net8.0-windows/AlgorithmServer.dll", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)] + private static extern IntPtr ParseJsonAndReturnActions(string jsonData); - // 添加公共方法供外部调用 - public string CallParseJsonAndReturnActions(JObject jsonData) - { - string jsonDataString = jsonData.ToString(); - byte[] jsonDataBytes = System.Text.Encoding.UTF8.GetBytes(jsonDataString); - - // 调用 C++ 函数进行加法运算 - double resultAdd = Add(3.5, 4.5); - MessageBox.Show($"Add(3.5, 4.5) = {resultAdd}"); + // 添加公共方法 + public AlgorithmResultEntity CallParseJsonAndReturnActions(string shape, string shape_mode, string image_file_base_path, string image_files, string algo_config) + { + try + { + // 将所有变量拼接成一个 JSON 对象 + JObject jsonData = new JObject( + new JProperty("shape", shape), + 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("algo_config", JObject.Parse(algo_config)) + ); + + //string jsonDataString = jsonData.ToString(); + string jsonDataString = "123"; + //byte[] jsonDataBytes = System.Text.Encoding.UTF8.GetBytes(jsonDataString); + + // 调用 C++ DLL 函数解析 JSON + IntPtr resultPtr = DetectDiamond(jsonDataString); + + string resultJson = Marshal.PtrToStringAnsi(resultPtr); + // 释放 DLL 分配的内存 + FreeString(resultPtr); + Console.WriteLine("log999999999999999999"+resultJson); - // 调用 C++ 函数进行乘法运算 - double resultMultiply = Multiply(3.5, 4.5); - MessageBox.Show($"Multiply(3.5, 4.5) = {resultMultiply}"); - - // 调用 C++ DLL 函数解析 JSON - IntPtr resultPtr = ParseJsonAndReturnActions(jsonDataBytes); + // 检查返回的 JSON 字符串是否为空或无效 + if (string.IsNullOrEmpty(resultJson)) + { + // 返回一个默认的 AlgorithmResultEntity 对象表示解析失败 + return new AlgorithmResultEntity + { + facets = new List(), + measurements = new Measurements() + }; + } - if (resultPtr != IntPtr.Zero) - { - return Marshal.PtrToStringAnsi(resultPtr) ?? "解析结果为空"; + // 反序列化 JSON 字符串为 AlgorithmResultEntity 对象 + var result = JsonConvert.DeserializeObject(resultJson); + + // 检查反序列化结果是否为 null + if (result == null) + { + // 返回一个默认的 AlgorithmResultEntity 对象表示解析失败 + return new AlgorithmResultEntity + { + facets = new List(), + measurements = new Measurements() + }; + } + + return result; } - else + catch (Exception ex) { - return "解析结果为空"; + // 记录日志或处理异常 + Console.WriteLine($"Error in CallParseJsonAndReturnActions: {ex.Message}"); + Console.WriteLine($"Stack Trace: {ex.StackTrace}"); + + // 如果有 InnerException,打印出来 + if (ex.InnerException != null) + { + Console.WriteLine($"Inner Exception: {ex.InnerException.Message}"); + Console.WriteLine($"Inner Stack Trace: {ex.InnerException.StackTrace}"); + } + // 返回一个默认的 AlgorithmResultEntity 对象表示解析失败 + return new AlgorithmResultEntity + { + facets = new List(), + measurements = new Measurements() + }; } } } diff --git a/Model/Services/SOCClientService.cs b/Model/Services/SOCClientService.cs index 7821a18..6c0635e 100644 --- a/Model/Services/SOCClientService.cs +++ b/Model/Services/SOCClientService.cs @@ -7,6 +7,7 @@ using System.Threading.Tasks; using System.Windows; using HandyControl.Tools.Extension; using SparkClient.Model.Common; +using SparkClient.Model.Entity.ApiEntity; namespace SparkClient.Model.Services { @@ -155,7 +156,7 @@ namespace SparkClient.Model.Services /// 光照级别 /// 图片保存路径 /// 操作结果 - public async Task ProcessImageCollectionAsync(int lightLevel, string savePath) + public async Task ProcessImageCollectionAsync(int lightLevel, string savePath) { try { @@ -172,7 +173,7 @@ namespace SparkClient.Model.Services if (startStatus != StatusCodes.Success) { // 启动任务失败 - return new ImageCollectionResult { Status = startStatus, Images = new List() }; + return new SocResultEntity { Status = startStatus, Images = new List() }; } // 读取图片接口 @@ -185,11 +186,11 @@ namespace SparkClient.Model.Services if (acquisitionStatus != StatusCodes.Success) { // 采集状态失败 - return new ImageCollectionResult { Status = acquisitionStatus, Images = new List() }; + return new SocResultEntity { Status = acquisitionStatus, Images = new List() }; } } // 按下载时间排序图片名称 - return new ImageCollectionResult { Status = StatusCodes.Success, Images = imageNames }; + return new SocResultEntity { Status = StatusCodes.Success, Images = imageNames }; } catch (Exception e) { @@ -197,7 +198,7 @@ namespace SparkClient.Model.Services // logger.Error(e, "发生异常"); string logMessage = $"[{DateTime.Now:yyyy-MM-dd HH:mm:ss.fff}] 发生异常: {e.Message}{Environment.NewLine}"; File.AppendAllText(LogFilePath, logMessage); - return new ImageCollectionResult { Status = StatusCodes.DeviceNotFound, Images = new List() }; + return new SocResultEntity { Status = StatusCodes.DeviceNotFound, Images = new List() }; } } diff --git a/Resource/Document/log.txt b/Resource/Document/log.txt index ea7f075..0de3e52 100644 --- a/Resource/Document/log.txt +++ b/Resource/Document/log.txt @@ -5,3 +5,4 @@ [2024-12-05 16:50:56.037] 发生异常: 由于目标计算机积极拒绝,无法连接。 (localhost:5000) [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. diff --git a/SparkClient.csproj b/SparkClient.csproj index abc6cc5..1334678 100644 --- a/SparkClient.csproj +++ b/SparkClient.csproj @@ -140,9 +140,4 @@ - - - - - diff --git a/SparkClient.sln.DotSettings.user b/SparkClient.sln.DotSettings.user index f3fef9c..52109ee 100644 --- a/SparkClient.sln.DotSettings.user +++ b/SparkClient.sln.DotSettings.user @@ -12,6 +12,8 @@ ForceIncluded ForceIncluded ForceIncluded + ForceIncluded + ForceIncluded ForceIncluded ForceIncluded ForceIncluded