fix:算法接口

master
handefeng 7 months ago
parent fc90fc6568
commit 4c4d8cd913
  1. 0
      Libs/AlgorithmServer.dll
  2. 4
      Model/Entity/ApiEntity/SocResultEntity.cs
  3. 100
      Model/Services/AlgorithmServer.cs
  4. 11
      Model/Services/SOCClientService.cs
  5. 1
      Resource/Document/log.txt
  6. 5
      SparkClient.csproj
  7. 2
      SparkClient.sln.DotSettings.user

@ -1,6 +1,6 @@
namespace SparkClient.Model.Common;
namespace SparkClient.Model.Entity.ApiEntity;
public class ImageCollectionResult
public class SocResultEntity
{
/// <summary>

@ -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)
// 添加公共方法
public AlgorithmResultEntity CallParseJsonAndReturnActions(string shape, string shape_mode, string image_file_base_path, string image_files, string algo_config)
{
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}");
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))
);
// 调用 C++ 函数进行乘法运算
double resultMultiply = Multiply(3.5, 4.5);
MessageBox.Show($"Multiply(3.5, 4.5) = {resultMultiply}");
//string jsonDataString = jsonData.ToString();
string jsonDataString = "123";
//byte[] jsonDataBytes = System.Text.Encoding.UTF8.GetBytes(jsonDataString);
// 调用 C++ DLL 函数解析 JSON
IntPtr resultPtr = ParseJsonAndReturnActions(jsonDataBytes);
IntPtr resultPtr = DetectDiamond(jsonDataString);
string resultJson = Marshal.PtrToStringAnsi(resultPtr);
// 释放 DLL 分配的内存
FreeString(resultPtr);
Console.WriteLine("log999999999999999999"+resultJson);
// 检查返回的 JSON 字符串是否为空或无效
if (string.IsNullOrEmpty(resultJson))
{
// 返回一个默认的 AlgorithmResultEntity 对象表示解析失败
return new AlgorithmResultEntity
{
facets = new List<Facet>(),
measurements = new Measurements()
};
}
// 反序列化 JSON 字符串为 AlgorithmResultEntity 对象
var result = JsonConvert.DeserializeObject<AlgorithmResultEntity>(resultJson);
// 检查反序列化结果是否为 null
if (result == null)
{
// 返回一个默认的 AlgorithmResultEntity 对象表示解析失败
return new AlgorithmResultEntity
{
facets = new List<Facet>(),
measurements = new Measurements()
};
}
return result;
}
catch (Exception ex)
{
// 记录日志或处理异常
Console.WriteLine($"Error in CallParseJsonAndReturnActions: {ex.Message}");
Console.WriteLine($"Stack Trace: {ex.StackTrace}");
if (resultPtr != IntPtr.Zero)
// 如果有 InnerException,打印出来
if (ex.InnerException != null)
{
return Marshal.PtrToStringAnsi(resultPtr) ?? "解析结果为空";
Console.WriteLine($"Inner Exception: {ex.InnerException.Message}");
Console.WriteLine($"Inner Stack Trace: {ex.InnerException.StackTrace}");
}
else
// 返回一个默认的 AlgorithmResultEntity 对象表示解析失败
return new AlgorithmResultEntity
{
return "解析结果为空";
facets = new List<Facet>(),
measurements = new Measurements()
};
}
}
}

@ -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
/// <param name="lightLevel">光照级别</param>
/// <param name="savePath">图片保存路径</param>
/// <returns>操作结果</returns>
public async Task<ImageCollectionResult> ProcessImageCollectionAsync(int lightLevel, string savePath)
public async Task<SocResultEntity> 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<string>() };
return new SocResultEntity { Status = startStatus, Images = new List<string>() };
}
// 读取图片接口
@ -185,11 +186,11 @@ namespace SparkClient.Model.Services
if (acquisitionStatus != StatusCodes.Success)
{
// 采集状态失败
return new ImageCollectionResult { Status = acquisitionStatus, Images = new List<string>() };
return new SocResultEntity { Status = acquisitionStatus, Images = new List<string>() };
}
}
// 按下载时间排序图片名称
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<string>() };
return new SocResultEntity { Status = StatusCodes.DeviceNotFound, Images = new List<string>() };
}
}

@ -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.

@ -140,9 +140,4 @@
<Compile Remove="Resource\Images\Temp\**" />
</ItemGroup>
<ItemGroup>
<Folder Include="Libs\" />
<Folder Include="Resource\SOCImages\" />
</ItemGroup>
</Project>

@ -12,6 +12,8 @@
<s:String x:Key="/Default/CodeInspection/ExcludedFiles/FilesAndFoldersToSkip2/=7020124F_002D9FFC_002D4AC3_002D8F3D_002DAAB8E0240759_002Ff_003AFrameworkElement_002Ecs_002Fl_003AC_0021_003FUsers_003Ftongg_003FAppData_003FRoaming_003FJetBrains_003FRider2024_002E3_003Fresharper_002Dhost_003FDecompilerCache_003Fdecompiler_003F6412d4331611499aab4eb63809a2a83bf60910_003F07_003Fdab5922a_003FFrameworkElement_002Ecs/@EntryIndexedValue">ForceIncluded</s:String>
<s:String x:Key="/Default/CodeInspection/ExcludedFiles/FilesAndFoldersToSkip2/=7020124F_002D9FFC_002D4AC3_002D8F3D_002DAAB8E0240759_002Ff_003AGuid_002Ecs_002Fl_003AC_0021_003FUsers_003Ftongg_003FAppData_003FRoaming_003FJetBrains_003FRider2024_002E3_003Fresharper_002Dhost_003FDecompilerCache_003Fdecompiler_003F211e6f3b24fa438a92f1815153647ce2c8f908_003Faa_003Fa49e75b9_003FGuid_002Ecs/@EntryIndexedValue">ForceIncluded</s:String>
<s:String x:Key="/Default/CodeInspection/ExcludedFiles/FilesAndFoldersToSkip2/=7020124F_002D9FFC_002D4AC3_002D8F3D_002DAAB8E0240759_002Ff_003AILog_002Ecs_002Fl_003AC_0021_003FUsers_003Ftongg_003FAppData_003FRoaming_003FJetBrains_003FRider2024_002E3_003Fresharper_002Dhost_003FDecompilerCache_003Fdecompiler_003F6f4e00a876324444bc5ae4e52ed22ade46200_003F49_003Fe673ccfc_003FILog_002Ecs/@EntryIndexedValue">ForceIncluded</s:String>
<s:String x:Key="/Default/CodeInspection/ExcludedFiles/FilesAndFoldersToSkip2/=7020124F_002D9FFC_002D4AC3_002D8F3D_002DAAB8E0240759_002Ff_003AJsonSerializerSettings_002Ecs_002Fl_003AC_0021_003FUsers_003FAdministrator_003FAppData_003FRoaming_003FJetBrains_003FRider2024_002E3_003Fresharper_002Dhost_003FDecompilerCache_003Fdecompiler_003F7e62198beab24380bbac29171862d1d8adf10_003Fbe_003Fd5d07dac_003FJsonSerializerSettings_002Ecs/@EntryIndexedValue">ForceIncluded</s:String>
<s:String x:Key="/Default/CodeInspection/ExcludedFiles/FilesAndFoldersToSkip2/=7020124F_002D9FFC_002D4AC3_002D8F3D_002DAAB8E0240759_002Ff_003AJsonSerializer_002Ecs_002Fl_003AC_0021_003FUsers_003FAdministrator_003FAppData_003FRoaming_003FJetBrains_003FRider2024_002E3_003Fresharper_002Dhost_003FDecompilerCache_003Fdecompiler_003F7e62198beab24380bbac29171862d1d8adf10_003F9d_003Fb98e2c8a_003FJsonSerializer_002Ecs/@EntryIndexedValue">ForceIncluded</s:String>
<s:String x:Key="/Default/CodeInspection/ExcludedFiles/FilesAndFoldersToSkip2/=7020124F_002D9FFC_002D4AC3_002D8F3D_002DAAB8E0240759_002Ff_003AJsonTextReader_002Ecs_002Fl_003AC_0021_003FUsers_003Ftongg_003FAppData_003FRoaming_003FJetBrains_003FRider2024_002E3_003Fresharper_002Dhost_003FDecompilerCache_003Fdecompiler_003F7e62198beab24380bbac29171862d1d8adf10_003Fcf_003F3a31e3b0_003FJsonTextReader_002Ecs/@EntryIndexedValue">ForceIncluded</s:String>
<s:String x:Key="/Default/CodeInspection/ExcludedFiles/FilesAndFoldersToSkip2/=7020124F_002D9FFC_002D4AC3_002D8F3D_002DAAB8E0240759_002Ff_003AJToken_002Ecs_002Fl_003AC_0021_003FUsers_003Ftongg_003FAppData_003FRoaming_003FJetBrains_003FRider2024_002E3_003Fresharper_002Dhost_003FSourcesCache_003F58b0b7706a8be4f7160749424eb996a3f845a1682c3c7ac5e405a346a8c20f1_003FJToken_002Ecs/@EntryIndexedValue">ForceIncluded</s:String>
<s:String x:Key="/Default/CodeInspection/ExcludedFiles/FilesAndFoldersToSkip2/=7020124F_002D9FFC_002D4AC3_002D8F3D_002DAAB8E0240759_002Ff_003ARoutedEventArgs_002Ecs_002Fl_003AC_0021_003FUsers_003Ftongg_003FAppData_003FRoaming_003FJetBrains_003FRider2024_002E3_003Fresharper_002Dhost_003FSourcesCache_003F1a88b4a860176dd5f825206bbebf3ee3d44ff3f058ceed9eb693c1eaa018_003FRoutedEventArgs_002Ecs/@EntryIndexedValue">ForceIncluded</s:String>

Loading…
Cancel
Save