You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

110 lines
4.7 KiB

using System.Data;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using SparkClient.Model.Entity.ApiEntity;
using SparkClient.Model.Helper;
namespace SparkClient.Model.Services
{
public class AlgorithmServer
{
// 导入 C++ DLL 中的 DetectDiamond 函数
[DllImport("diamond_cut_inspector.dll", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
private static extern IntPtr DetectDiamond(string jsonData);
// 导入 C++ DLL 中的 FreeMemory 函数
[DllImport("diamond_cut_inspector.dll", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
private static extern void FreeString(IntPtr ptr);
// 添加公共方法
public AlgorithmResultEntity CallParseJsonAndReturnActions(string shape, string shape_mode, string image_files)
{
try
{
//半圆
string circleSql = $"SELECT VALUE FROM CUTTER_CONFIG WHERE KEY = 'half_circle'";
DataTable circleTable = DataBaseHelper.ExecuteQuery(circleSql);
object halfCircleValue = circleTable.Rows[0][0];
bool.TryParse(halfCircleValue.ToString(), out bool boolResult);
bool half_circle = boolResult;
//算法配置参数
string sql = $"SELECT JSON FROM ALGORITHM_CONFIG ORDER BY JSON_ORDER ASC";
DataTable table = DataBaseHelper.ExecuteQuery(sql);
object lightLevelValue = table.Rows[0][0];
string algo_config = lightLevelValue.ToString() ?? throw new InvalidOperationException();
//图片根目录
string image_file_base_path = "D:\\diamond_images";
// 将所有变量拼接成一个 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("half_circle", half_circle),
new JProperty("algo_config", JObject.Parse(algo_config))
);
string jsonDataString = jsonData.ToString();
// 调用 C++ DLL 函数解析 JSON
IntPtr resultPtr = DetectDiamond(jsonDataString);
string resultJson = Marshal.PtrToStringAnsi(resultPtr);
// 释放 DLL 分配的内存
FreeString(resultPtr);
// 检查返回的 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}");
// 如果有 InnerException,打印出来
if (ex.InnerException != null)
{
Console.WriteLine($"Inner Exception: {ex.InnerException.Message}");
Console.WriteLine($"Inner Stack Trace: {ex.InnerException.StackTrace}");
}
// 返回一个默认的 AlgorithmResultEntity 对象表示解析失败
MessageBox.Show(ex.Message);
return new AlgorithmResultEntity
{
facets = new List<Facet>(),
measurements = new Measurements()
};
}
}
}
}