From e69e99c65e8ebf39370a9d708e336f512cf7e660 Mon Sep 17 00:00:00 2001 From: handefeng <1030428966@qq.com> Date: Tue, 17 Dec 2024 09:58:48 +0800 Subject: [PATCH] =?UTF-8?q?fix=EF=BC=9A=E7=AE=97=E6=B3=95=E6=8E=A5?= =?UTF-8?q?=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Model/Entity/AlgorithmConfigEntity.cs | 29 ++++++ Model/Entity/CutterConfigEntity.cs | 25 ++++- Model/Services/AlgorithmServer.cs | 4 +- Model/Services/SOCClientService.cs | 6 +- ViewModel/BaseWindow/HomeWindowVM.cs | 3 - ViewModel/Grading/DiamondSelectVM.cs | 139 +++++++++++++++++--------- 6 files changed, 147 insertions(+), 59 deletions(-) diff --git a/Model/Entity/AlgorithmConfigEntity.cs b/Model/Entity/AlgorithmConfigEntity.cs index 1b77cdd..d8f74cc 100644 --- a/Model/Entity/AlgorithmConfigEntity.cs +++ b/Model/Entity/AlgorithmConfigEntity.cs @@ -77,7 +77,36 @@ public class AlgorithmConfigEntity return $"DELETE FROM {TableName} WHERE {whereClause};"; } } + // 生成 Select 语句 + public string GenerateSelectSQL(List fields = null, Dictionary conditions = null) + { + var mappings = GetFieldMappings(); + + // 如果没有指定字段,则选择所有字段 + if (fields == null || fields.Count == 0) + { + fields = mappings.Values.ToList(); + } + else + { + // 确保指定的字段存在于映射中 + fields = fields.Where(f => mappings.ContainsValue(f)).ToList(); + } + var columns = string.Join(", ", fields); + + var sql = $"SELECT {columns} FROM {TableName}"; + + if (conditions != null && conditions.Count > 0) + { + var whereClause = string.Join(" AND ", conditions.Keys.Select(f => $"{mappings[f]} = @{f}")); + sql += $" WHERE {whereClause}"; + } + + sql += ";"; + + return sql; + } } diff --git a/Model/Entity/CutterConfigEntity.cs b/Model/Entity/CutterConfigEntity.cs index c43ddf1..ffd577f 100644 --- a/Model/Entity/CutterConfigEntity.cs +++ b/Model/Entity/CutterConfigEntity.cs @@ -17,17 +17,17 @@ public class CutterConfigEntity /// 项目名称 /// [DbField("ITEM_NAME")] - public required string ItemName { get; set; } + public required string? ItemName { get; set; } /// /// key /// [DbField("KEY")] - public required string Key { get; set; } + public required string? Key { get; set; } /// /// value /// [DbField("VALUE")] - public required string Value { get; set; } + public required string? Value { get; set; } private static Dictionary GetFieldMappings() { var properties = typeof(CutterConfigEntity).GetProperties(BindingFlags.Public | BindingFlags.Instance); @@ -81,7 +81,26 @@ public class CutterConfigEntity return $"DELETE FROM {TableName} WHERE {whereClause};"; } } + // 生成 Select 语句 + public string GenerateSelectSQL(List? columns = null, Dictionary? conditions = null) + { + var mappings = GetFieldMappings(); + + // 如果没有指定列,则查询所有列 + string selectColumns = columns != null && columns.Any() + ? string.Join(", ", columns.Select(c => mappings[c])) + : string.Join(", ", mappings.Values); + + string sql = $"SELECT {selectColumns} FROM {TableName}"; + if (conditions != null && conditions.Any()) + { + var whereClause = string.Join(" AND ", conditions.Keys.Select(c => $"{mappings[c]} = @{c}")); + sql += $" WHERE {whereClause}"; + } + + return sql; + } } diff --git a/Model/Services/AlgorithmServer.cs b/Model/Services/AlgorithmServer.cs index a697780..310dbcc 100644 --- a/Model/Services/AlgorithmServer.cs +++ b/Model/Services/AlgorithmServer.cs @@ -36,9 +36,7 @@ namespace SparkClient.Model.Services new JProperty("algo_config", JObject.Parse(algo_config)) ); - //string jsonDataString = jsonData.ToString(); - string jsonDataString = "123"; - //byte[] jsonDataBytes = System.Text.Encoding.UTF8.GetBytes(jsonDataString); + string jsonDataString = jsonData.ToString(); // 调用 C++ DLL 函数解析 JSON IntPtr resultPtr = DetectDiamond(jsonDataString); diff --git a/Model/Services/SOCClientService.cs b/Model/Services/SOCClientService.cs index 6c0635e..614fe58 100644 --- a/Model/Services/SOCClientService.cs +++ b/Model/Services/SOCClientService.cs @@ -46,10 +46,10 @@ namespace SparkClient.Model.Services /// /// 基础URL /// 认证令牌 - public SOCClientService(string baseUrl, string authToken) + public SOCClientService() { - _baseUrl = baseUrl; - _authToken = authToken; + _baseUrl = "http://localhost:5000/api/SoC"; + _authToken = "your_basic_auth_token"; } /// diff --git a/ViewModel/BaseWindow/HomeWindowVM.cs b/ViewModel/BaseWindow/HomeWindowVM.cs index 1030b2c..e9d9114 100644 --- a/ViewModel/BaseWindow/HomeWindowVM.cs +++ b/ViewModel/BaseWindow/HomeWindowVM.cs @@ -28,9 +28,6 @@ public class HomeWindowVM : BaseViewModel ShowHelperPageCommand = new RelayCommand(ShowHelperPage); ShowConfigPageCommand = new RelayCommand(ShowConfigPage); ShowDiamondSelPageCommand = new RelayCommand(ShowDiamlondSelPage); - - // 初始化SOC客户端服务,传入SOC端的地址和认证Token - _socClientService = new SOCClientService("http://localhost:5000/api/SoC", "your_basic_auth_token"); } public void ShowHelperPage(object parameter) diff --git a/ViewModel/Grading/DiamondSelectVM.cs b/ViewModel/Grading/DiamondSelectVM.cs index 2d25edb..9b6ec3b 100644 --- a/ViewModel/Grading/DiamondSelectVM.cs +++ b/ViewModel/Grading/DiamondSelectVM.cs @@ -4,20 +4,27 @@ using SparkClient.Model.Helper; using SparkClient.ViewModel.BaseWindow; using SparkClient.Views.Dialog; using System; +using System.Data; using System.Diagnostics.Metrics; using System.DirectoryServices.ActiveDirectory; -using System.IO; using System.Reflection.Metadata; -using System.Text; using System.Windows; using System.Windows.Input; using System.Windows.Media.Imaging; using System.Windows.Threading; +using Microsoft.Data.Sqlite; +using SparkClient.Model.Entity; +using SparkClient.Model.Services; +using MessageBox = System.Windows.MessageBox; namespace SparkClient.ViewModel.Grading; public class DiamondSelectVM : BaseViewModel { + + private SOCClientService _socClientService; + private AlgorithmServer _algorithmServer; + private AlgorithmConfigEntity _algorithmConfigEntity; private string DiamondCode { get; set; } private List _buttons; private List _buttons2; @@ -98,45 +105,17 @@ public class DiamondSelectVM : BaseViewModel Buttons2 = tempButtons2; } } - public void StartGrading(object param) - { - DoStartGrading(param); - } /// /// 开始检测(对soc和算法开始通讯) /// /// - public async void DoStartGrading(object param) + public async void StartGrading(object param) { if (param != null) { - AlgorithmResultEntity parameter = new AlgorithmResultEntity(); - JsonImport jsonImport = new JsonImport(); - bool? a = jsonImport.ShowDialog(); - if (a ?? false) - { - string fileName = jsonImport.FilePath.Text; - string[] lines = File.ReadAllLines(fileName); - StringBuilder stringBuilder = new StringBuilder(); - - foreach (var line in lines) - { - stringBuilder.Append(line); - } - parameter = JsonConvert.DeserializeObject(stringBuilder.ToString()); - } - else - { - return; - } LoadingDialog loading = new LoadingDialog(); - var tcs = new TaskCompletionSource(); - var progressTask = Task.Run(() => Application.Current.Dispatcher.Invoke(() => { - loading.Closed += (s, e) => tcs.SetResult(true); - loading.ShowDialog(); - } - )); + var progressTask = Task.Run(() => Application.Current.Dispatcher.Invoke(() => loading.ShowDialog())); await Task.Run(async () => { for (int i = 0; i <= 100; i++) @@ -151,14 +130,8 @@ public class DiamondSelectVM : BaseViewModel }); } - // SocResultEntity socResolt = new SocResultEntity(); - // AlgorithmResultEntity parameter = new AlgorithmResultEntity(); - // // 启动soc - // socResolt = DoSoc(); - // // 启动算法 - // parameter = DoAlgorithm(); - - //parameter = DoSoc(); + SocResultEntity socResolt = new SocResultEntity(); + AlgorithmResultEntity parameter = new AlgorithmResultEntity(); parameter.Standard = "IGI 2024"; string value = param.ToString()??""; if (value!= null && value.Split(" ").Length==3) @@ -167,6 +140,13 @@ public class DiamondSelectVM : BaseViewModel parameter.CrownType = value.Split(" ")[1]; parameter.PavType = value.Split(" ")[2]; } + + // 启动soc + socResolt = await DoSoc(); + // 启动算法 + parameter = await DoAlgorithm(socResolt, parameter.Shape, parameter.CrownType); + + parameter.DiamondCode = DiamondCode; GradingResult(parameter); }); @@ -188,18 +168,83 @@ public class DiamondSelectVM : BaseViewModel WindowManager.mainViewModel.Content = vm; WindowManager.openContent.Add(vm); } + + + /// + /// 启动切工仪接口。 + /// + /// 图片索引 + /// 图片索引 + /// 图片索引 + /// 图片的字节数组 + private async Task DoSoc() + { + // // 光照度 + // string sql = new CutterConfigEntity + // { + // ItemName = null, + // Key = null, + // Value = null + // }.GenerateSelectSQL(new List { "Value" }, new Dictionary { { "Key", "light_level" } }); + // SqliteParameter[] sqliteParameters = [new("@Key", "light_level")]; + // DataTable table = DataBaseHelper.ExecuteQuery(sql,sqliteParameters); + // object lightLevelValue = table.Rows[0][0]; + // if (!int.TryParse(lightLevelValue.ToString(), out int lightLevel)) + // { + // throw new InvalidOperationException("Light level value is not a valid integer."); + // } + // // 初始化SOC客户端服务,传入SOC端的地址和认证Token + // _socClientService = new SOCClientService(); + // // SOC接口 + // string savePath = @"d:\\diamond_images"; + // SocResultEntity resultEntity = await _socClientService.ProcessImageCollectionAsync(lightLevel, savePath); + // // 转换成json + // string jsonResult = JsonConvert.SerializeObject(resultEntity, Formatting.Indented); + + SocResultEntity resultEntity = new SocResultEntity(); + return resultEntity; + } - private AlgorithmResultEntity DoAlgorithm() + /// + /// 启动算法接口。 + /// + /// 切工仪接口返回值 + /// + /// + /// 定级参数,3D模型参数 + private Task DoAlgorithm(SocResultEntity socResolt, String shape, String crownType) { - AlgorithmResultEntity param = new AlgorithmResultEntity(); - string json = $"{{\"status\": \"ok\",\r\n \"facets\": [\r\n {{\r\n \"coords\": [\r\n {{\r\n \"x\": 0.03402838855981827,\r\n \"y\": -0.11212713271379471,\r\n \"z\": 5.701290607452393\r\n }},\r\n {{\r\n \"x\": 0.46919262409210205,\r\n \"y\": 0.058160409331321716,\r\n \"z\": 5.289202690124512\r\n }},\r\n {{\r\n \"x\": 0.1256149709224701,\r\n \"y\": 0.3005124032497406,\r\n \"z\": 5.330680847167969\r\n }}\r\n ],\r\n \"facet_id\": \"21_0\",\r\n \"facet_type\": 21\r\n }},\r\n {{\r\n \"coords\": [\r\n {{\r\n \"x\": 2.9093382358551025,\r\n \"y\": 3.4028751850128174,\r\n \"z\": 1.724348783493042\r\n }},\r\n {{\r\n \"x\": 0.46919262409210205,\r\n \"y\": 0.058160409331321716,\r\n \"z\": 5.289202690124512\r\n }},\r\n {{\r\n \"x\": 0.1256149709224701,\r\n \"y\": 0.3005124032497406,\r\n \"z\": 5.330680847167969\r\n }}\r\n ],\r\n \"facet_id\": \"21_1\",\r\n \"facet_type\": 21\r\n }}\r\n ],\r\n \"measurements\": {{\r\n \"DIAMETER\": 6.43,\r\n \"DIAMETER_DEV\": 1.2,\r\n \"M1\": 6.409999847412109,\r\n \"M2\": 6.46999979019165,\r\n \"M3\": 3.9700000286102295,\r\n \"TABLE\": 58.0,\r\n \"TABLE_MIN\": 57.5,\r\n \"TABLE_MAX\": 58.6,\r\n \"CROWN_HEIGHT\": 11.5,\r\n \"CROWN_H_DEV\": 1.4,\r\n \"CROWN_H_MIN\": 10.7,\r\n \"CROWN_H_MAX\": 12.1,\r\n \"CROWN_ANGLE\": 30.5,\r\n \"CROWN_ANGLE_DEV\": 0.9,\r\n \"CROWN_ANGLE_MIN\": 30.2,\r\n \"CROWN_ANGLE_MAX\": 31.1,\r\n \"PAV_DEPTH\": 35.0,\r\n \"PAV_DEPTH_DEV\": 0.8,\r\n \"PAV_DEPTH_MIN\": 34.5,\r\n \"PAV_DEPTH_MAX\": 35.3,\r\n \"PAV_ANGLE\": 35.1,\r\n \"PAV_ANGLE_DEV\": 0.8,\r\n \"PAV_ANGLE_MIN\": 34.7,\r\n \"PAV_ANGLE_MAX\": 35.5,\r\n \"GIRDLE_BEZEL\": 7.1,\r\n \"GIRDLE_BEZEL_DEV\": 1.8,\r\n \"GIRDLE_BEZEL_MIN\": 6.8,\r\n \"GIRDLE_BEZEL_MAX\": 7.5,\r\n \"GIRDLE_BONE\": 7.8,\r\n \"GIRDLE_BONE_MIN\": 7.3,\r\n \"GIRDLE_BONE_MAX\": 8.3,\r\n \"GIRDLE\": 6.3,\r\n \"GIRDLE_DEV\": 3.3,\r\n \"GIRDLE_MIN\": 4.2,\r\n \"GIRDLE_MAX\": 7.5,\r\n \"TOTAL_DEPTH\": 53.5,\r\n \"CULET\": 1.8,\r\n \"LW_RATIO\": 1.0,\r\n \"TOC\": 1.4,\r\n \"COC\": 0.5,\r\n \"TA\": 3.81,\r\n \"LGF\": 75,\r\n \"STAR\": 65,\r\n \"STAR_MIN\": 61.1,\r\n \"STAR_MAX\": 65.4,\r\n \"LOWER_HALVES_RATIO\": 75,\r\n \"LOWER_HALVES_RATIO_MIN\": 73.3,\r\n \"LOWER_HALVES_RATIO_MAX\": 78.1,\r\n \"TWIST\": 1.3,\r\n \"TWIST_DEV\": 2.4,\r\n \"TWIST_MIN\": -0.2,\r\n \"TWIST_MAX\": -2.4,\r\n \"CULET_TO_TABLE\": 1.4\r\n }}\r\n}}\r\n"; - param = JsonConvert.DeserializeObject(json); - return param; + _algorithmServer = new AlgorithmServer(); + //钻石形状:shape + //钻石子形状 + string shape_mode = crownType; + //图片根目录 + 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\" ]" ; + + //算法配置参数 + 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(); + + AlgorithmResultEntity algoResult = _algorithmServer.CallParseJsonAndReturnActions(shape, shape_mode, image_file_base_path, image_files, algo_config); + + + // 将 algoResult 序列化为格式化的 JSON 字符串 + string algoJsonResult = JsonConvert.SerializeObject(algoResult, Formatting.Indented); + ShowMessage(algoJsonResult); + return Task.FromResult(algoResult); } - private SocResultEntity DoSoc() + + + private void ShowMessage(string message) { - return new SocResultEntity(); + MessageBox.Show(message); } + } public class ButtonInfo {