From e0f699bbb835035711bd9b779aa61293cd3ac413 Mon Sep 17 00:00:00 2001 From: handefeng <1030428966@qq.com> Date: Mon, 9 Dec 2024 10:43:46 +0800 Subject: [PATCH] =?UTF-8?q?fix=EF=BC=9Asoc?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Libs/AlgorithmServer.dll | 0 .../Entity/ApiEntity/ImageCollectionResult.cs | 15 ++++++ .../ApiEntity}/StatusCodes.cs | 16 +++--- Model/Services/AlgorithmServer.cs | 54 +++++-------------- Model/Services/SOCClientService.cs | 20 +++---- 5 files changed, 46 insertions(+), 59 deletions(-) create mode 100644 Libs/AlgorithmServer.dll create mode 100644 Model/Entity/ApiEntity/ImageCollectionResult.cs rename Model/{Services => Entity/ApiEntity}/StatusCodes.cs (52%) diff --git a/Libs/AlgorithmServer.dll b/Libs/AlgorithmServer.dll new file mode 100644 index 0000000..e69de29 diff --git a/Model/Entity/ApiEntity/ImageCollectionResult.cs b/Model/Entity/ApiEntity/ImageCollectionResult.cs new file mode 100644 index 0000000..0c41c23 --- /dev/null +++ b/Model/Entity/ApiEntity/ImageCollectionResult.cs @@ -0,0 +1,15 @@ +namespace SparkClient.Model.Common; + +public class ImageCollectionResult +{ + + /// + /// 状态码 + /// + public string Status { get; set; } + + /// + /// 图片名称列表 + /// + public List Images { get; set; } +} \ No newline at end of file diff --git a/Model/Services/StatusCodes.cs b/Model/Entity/ApiEntity/StatusCodes.cs similarity index 52% rename from Model/Services/StatusCodes.cs rename to Model/Entity/ApiEntity/StatusCodes.cs index f7209bd..81abb75 100644 --- a/Model/Services/StatusCodes.cs +++ b/Model/Entity/ApiEntity/StatusCodes.cs @@ -6,27 +6,27 @@ public static class StatusCodes { // 成功 - public const string S000 = "S000"; + public const string Success = "S000"; // 采图正在进行中 - public const string S001 = "S001"; + public const string InProgress = "S001"; // 缓存图片被清理(读取不够及时) - public const string S002 = "S002"; + public const string CacheCleared = "S002"; // 无法向单片机发送指令 - public const string S003 = "S003"; + public const string CannotSendCommand = "S003"; // 单片机访问超时 - public const string S004 = "S004"; + public const string MicrocontrollerTimeout = "S004"; // 单片机返回错误码 - public const string S005 = "S005"; + public const string MicrocontrollerError = "S005"; // 未找到切工仪 - public const string P001 = "P001"; + public const string DeviceNotFound = "P001"; // 算法调用失败 - public const string P002 = "P002"; + public const string AlgorithmFailed = "P002"; } } \ No newline at end of file diff --git a/Model/Services/AlgorithmServer.cs b/Model/Services/AlgorithmServer.cs index 3920f59..09194bb 100644 --- a/Model/Services/AlgorithmServer.cs +++ b/Model/Services/AlgorithmServer.cs @@ -1,35 +1,28 @@ -using Newtonsoft.Json; -using System; -using System.Net.Http; -using System.Runtime.InteropServices; -using System.Text; -using System.Threading.Tasks; -using System.Windows; -using System.Windows.Input; -using HandyControl.Controls; -using HandyControl.Tools; +using System.Runtime.InteropServices; +using System.Windows.Forms; using Newtonsoft.Json.Linq; -using MessageBox = HandyControl.Controls.MessageBox; namespace SparkClient.Model.Services { public class AlgorithmServer { // 使用 P/Invoke 声明 C++ 函数 - [DllImport("D:/workspace/dayuAI/SparkClient/Libs/AlgorithmServer.dll", CallingConvention = CallingConvention.Cdecl)] + [DllImport("../../../Libs/AlgorithmServer.dll", CallingConvention = CallingConvention.Cdecl)] private static extern double Add(double a, double b); - [DllImport("D:D:/workspace/dayuAI/SparkClient/Libs/AlgorithmServer.dll", CallingConvention = CallingConvention.Cdecl)] + [DllImport("../../../Libs/AlgorithmServer.dll", CallingConvention = CallingConvention.Cdecl)] private static extern double Multiply(double a, double b); // 导入 C++ DLL 中的 ParseJsonAndReturnActions 函数 - [DllImport("D:/workspace/dayuAI/SparkClient/Libs/AlgorithmServer.dll", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)] + [DllImport("../../../Libs/AlgorithmServer.dll", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)] private static extern IntPtr ParseJsonAndReturnActions(byte[] jsonData); - - public AlgorithmServer() + // 添加公共方法供外部调用 + 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}"); @@ -37,38 +30,17 @@ namespace SparkClient.Model.Services // 调用 C++ 函数进行乘法运算 double resultMultiply = Multiply(3.5, 4.5); MessageBox.Show($"Multiply(3.5, 4.5) = {resultMultiply}"); - - // 测试 JSON 数据 - // 定义并初始化 JObject - JObject jsonData = new JObject( - new JProperty("dataAttributes", new JArray( - new JObject(new JProperty("name", "你好")), - new JObject(new JProperty("name", "hello")), - new JObject(new JProperty("name", "666")), - new JObject(new JProperty("name", "是的")), - new JObject(new JProperty("name", "确认")) - )) - ); - - // 将 JObject 序列化为字符串 - string jsonDataString = jsonData.ToString(); - - // 将 JSON 字符串转换为字节数组 - byte[] jsonDataBytes = System.Text.Encoding.UTF8.GetBytes(jsonDataString); - + // 调用 C++ DLL 函数解析 JSON IntPtr resultPtr = ParseJsonAndReturnActions(jsonDataBytes); - // 检查返回的指针是否为空 if (resultPtr != IntPtr.Zero) { - string? result = Marshal.PtrToStringAnsi(resultPtr); - MessageBox.Show(result, "解析结果", MessageBoxButton.OK, MessageBoxImage.Information); - + return Marshal.PtrToStringAnsi(resultPtr) ?? "解析结果为空"; } else { - MessageBox.Show("解析结果为空", "解析结果", MessageBoxButton.OK, MessageBoxImage.Warning); + return "解析结果为空"; } } } diff --git a/Model/Services/SOCClientService.cs b/Model/Services/SOCClientService.cs index 2aaad50..7821a18 100644 --- a/Model/Services/SOCClientService.cs +++ b/Model/Services/SOCClientService.cs @@ -6,6 +6,7 @@ using System.Text; using System.Threading.Tasks; using System.Windows; using HandyControl.Tools.Extension; +using SparkClient.Model.Common; namespace SparkClient.Model.Services { @@ -154,7 +155,7 @@ namespace SparkClient.Model.Services /// 光照级别 /// 图片保存路径 /// 操作结果 - public async Task ProcessImageCollectionAsync(int lightLevel, string savePath) + public async Task ProcessImageCollectionAsync(int lightLevel, string savePath) { try { @@ -167,10 +168,11 @@ namespace SparkClient.Model.Services // 启动任务接口 string startStatus = await CollectImagesAsync(lightLevel); - if (startStatus != "S000") + // 成功 + if (startStatus != StatusCodes.Success) { // 启动任务失败 - return JsonConvert.SerializeObject(new { status = startStatus, images = new List() }); + return new ImageCollectionResult { Status = startStatus, Images = new List() }; } // 读取图片接口 @@ -179,16 +181,15 @@ namespace SparkClient.Model.Services { // 采集状态接口 string acquisitionStatus = await CollectStatusAsync(); - if (acquisitionStatus != "S000") + // 成功 + if (acquisitionStatus != StatusCodes.Success) { // 采集状态失败 - return JsonConvert.SerializeObject(new { status = acquisitionStatus, images = new List() }); - + return new ImageCollectionResult { Status = acquisitionStatus, Images = new List() }; } } // 按下载时间排序图片名称 - return JsonConvert.SerializeObject(new { status = "S000", images = imageNames }); - + return new ImageCollectionResult { Status = StatusCodes.Success, Images = imageNames }; } catch (Exception e) { @@ -196,8 +197,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 JsonConvert.SerializeObject(new { status = "P001", images = new List() }); - + return new ImageCollectionResult { Status = StatusCodes.DeviceNotFound, Images = new List() }; } }