using Newtonsoft.Json; using SparkClient.Model.Entity.ApiEntity; 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.Reflection.Metadata; 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; using System.Runtime.CompilerServices; using System.Text; using System.IO; 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; private ICommand ChangeDiamondTypeCommand; private ICommand StartGradingCommand; public List Buttons { get { return _buttons; } set { _buttons = value; OnPropertyChanged(nameof(Buttons)); } } public List Buttons2 { get { return _buttons2; } set { _buttons2 = value; OnPropertyChanged(nameof(Buttons2)); } } public DiamondSelectVM(string DiamondCode) { ChangeDiamondTypeCommand = new RelayCommand(ChangeDiamondType); StartGradingCommand = new RelayCommand(StartGrading); this.DiamondCode = DiamondCode; List tempButtons = new List(); List buttonInfos = GetButtonInfos(); foreach (var buttonInfo in buttonInfos) { var bitmap = new BitmapImage(new Uri("pack://application:,,,/Resource/Images/" + buttonInfo.ImageName, UriKind.RelativeOrAbsolute)); ButtonViewModel button = new ButtonViewModel() { Text = buttonInfo.ButtonName, ImageSource = bitmap, Type = buttonInfo.Type, Command = ChangeDiamondTypeCommand ,IsEnabled= buttonInfo.IsEnabled, IsHighlighted = false}; tempButtons.Add(button); } Buttons = tempButtons; } private List GetButtonInfos() { List buttonInfos = new List(); buttonInfos.Add(new ButtonInfo() { Type = "round", IsEnabled = true, ImageName = "round_3x.png", ButtonName = MultilingualHelper.getString("圆形") }); buttonInfos.Add(new ButtonInfo() { Type = "Heart", IsEnabled = false, ImageName = "Heart-shaped.png", ButtonName = MultilingualHelper.getString("心形") }); buttonInfos.Add(new ButtonInfo() { Type = "cushion", IsEnabled = false, ImageName = "Cushion-shaped.png", ButtonName = MultilingualHelper.getString("枕形") }); buttonInfos.Add(new ButtonInfo() { Type = "Pear", IsEnabled = false, ImageName = "Pear-shaped.png", ButtonName = MultilingualHelper.getString("梨形") }); buttonInfos.Add(new ButtonInfo() { Type = "Princess", IsEnabled = false, ImageName = "Princess-shaped.png", ButtonName = MultilingualHelper.getString("公主方形") }); buttonInfos.Add(new ButtonInfo() { Type = "oval", IsEnabled = false, ImageName = "oval.png", ButtonName = MultilingualHelper.getString("椭圆形") }); buttonInfos.Add(new ButtonInfo() { Type = "Emerald", IsEnabled = false, ImageName = "Emerald-shape.png", ButtonName = MultilingualHelper.getString("祖母绿形") }); buttonInfos.Add(new ButtonInfo() { Type = "Odd", IsEnabled = false, ImageName = "Odd-shaped.png", ButtonName = MultilingualHelper.getString("异形钻") }); return buttonInfos; } /// /// 获取钻石列表 /// /// public void InitDiamondList(object param) { } /// /// 切换钻石类型(右侧列表) /// /// 钻石类型 public void ChangeDiamondType(object type) { if (type!= null) { foreach (var item in Buttons.Where(x => x.Type == type)) { item.IsHighlighted = true; }; List tempButtons2 = new List(); var bitmap = new BitmapImage(new Uri("pack://application:,,,/Resource/Images/round_P8-P8.png", UriKind.RelativeOrAbsolute)); ButtonViewModel button = new ButtonViewModel() { Text = "P8-P8", ImageSource = bitmap, Type = "round P8 P8", Command = StartGradingCommand }; tempButtons2.Add(button); Buttons2 = tempButtons2; } } /// /// 开始检测(对soc和算法开始通讯) /// /// public async void StartGrading(object param) { DoStartGrading(param); //LoadingDialog loading = new LoadingDialog(); //try //{ // if (param != null) // { // var progressTask = Task.Run(() => Application.Current.Dispatcher.Invoke(() => loading.ShowDialog())); // await Task.Run(async () => // { // bool isEnd = false; // int progress = 0; // // 更新进度条的值(需要在UI线程上执行) // loading.Dispatcher.Invoke(async () => // { // for (int i = 0; i <= 97; i++) // { // if (isEnd) // { // break; // } // // 模拟耗时操作 // //System.Threading.Thread.Sleep(50); // 休眠50毫秒 // await Task.Delay(1000); // loading.setValue(i); // progress = i; // } // }); // SocResultEntity socResolt = new SocResultEntity(); // AlgorithmResultEntity parameter = new AlgorithmResultEntity(); // parameter.Standard = "IGI 2024"; // string value = param.ToString() ?? ""; // if (value != null && value.Split(" ").Length == 3) // { // parameter.Shape = value.Split(" ")[0]; // parameter.CrownType = value.Split(" ")[1]; // parameter.PavType = value.Split(" ")[2]; // } // // 启动soc // socResolt = await DoSoc(); // // 启动算法 // parameter = await DoAlgorithm(socResolt, parameter.Shape, parameter.CrownType); // parameter.Standard = "IGI 2024"; // parameter.Shape = value.Split(" ")[0]; // parameter.CrownType = value.Split(" ")[1]; // parameter.PavType = value.Split(" ")[2]; // parameter.DiamondCode = DiamondCode; // isEnd = true; // //GradingResult(parameter); // await loading.Dispatcher.Invoke(async () => // { // for (int i = progress; progress <= 100; i++) // { // Random random = new Random(); int minValue = 50; int maxValue = 150; // 生成50到150之间的随机整数 // int randomNumber = random.Next(minValue, maxValue + 1); // await Task.Delay(randomNumber); // loading.setValue(i); // if (loading.ProgressBar.Value == 98) // { // GradingResult(parameter); // } // if(loading.ProgressBar.Value >= 100) // { // loading.Close(); // break; // } // } // }); // }); // } //} //finally { //} } /// /// 开始检测(对soc和算法开始通讯) /// /// public async void DoStartGrading(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(); } )); await Task.Run(async () => { for (int i = 0; i <= 100; i++) { // 模拟耗时操作 //System.Threading.Thread.Sleep(50); // 休眠50毫秒 await Task.Delay(5); // 更新进度条的值(需要在UI线程上执行) loading.Dispatcher.Invoke(() => { loading.setValue(i); }); } // SocResultEntity socResolt = new SocResultEntity(); // AlgorithmResultEntity parameter = new AlgorithmResultEntity(); // // 启动soc // socResolt = DoSoc(); // // 启动算法 // parameter = DoAlgorithm(); //parameter = DoSoc(); parameter.Standard = "IGI 2024"; string value = param.ToString() ?? ""; if (value != null && value.Split(" ").Length == 3) { parameter.Shape = value.Split(" ")[0]; parameter.CrownType = value.Split(" ")[1]; parameter.PavType = value.Split(" ")[2]; } parameter.DiamondCode = DiamondCode; GradingResult(parameter); }); await Task.Delay(5); loading.Close(); } } /// /// 跳转至检测结果 /// /// public void GradingResult(AlgorithmResultEntity param) { WindowManager.mainViewModel.Content = WindowManager.PreviousVM(); BaseControlVM vm = new BaseControlVM(new GradingResultVM(param), MultilingualHelper.getString("DetectionResult")); vm.ShowFunctionButton = System.Windows.Visibility.Hidden; 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; } /// /// 启动算法接口。 /// /// 切工仪接口返回值 /// /// /// 定级参数,3D模型参数 private Task DoAlgorithm(SocResultEntity socResolt, String shape, String crownType) { _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 void ShowMessage(string message) { MessageBox.Show(message); } } public class ButtonInfo { public string Type { get; set; } public string ImageName { get; set; } public string ButtonName { get; set; } public bool IsEnabled { get; set; } } public class ButtonViewModel : BaseViewModel { private string _text; private BitmapImage _imageSource; private ICommand _command; private string _type; private int _col; private bool _isEnabled; private bool _isHighlighted; public string Text { get { return _text; } set { _text = value; OnPropertyChanged(nameof(Text)); } } public BitmapImage ImageSource { get { return _imageSource; } set { _imageSource = value; OnPropertyChanged(nameof(ImageSource)); } } public ICommand Command { get { return _command; } set { _command = value; OnPropertyChanged(nameof(Command)); } } public string Type { get { return _type; } set { _type = value; OnPropertyChanged(nameof(Type)); } } public int Column { get { return _col; } set { _col = value; OnPropertyChanged(nameof(Column)); } } public bool IsEnabled { get { return _isEnabled; } set { _isEnabled = value; OnPropertyChanged(nameof(IsEnabled)); } } public bool IsHighlighted { get { return _isHighlighted; } set { _isHighlighted = value; OnPropertyChanged(nameof(IsHighlighted)); } } }