using Newtonsoft.Json; using SparkClient.Model.Entity.ApiEntity; using SparkClient.Model.Helper; using SparkClient.ViewModel.BaseWindow; using SparkClient.Views.Dialog; using System.Data; 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.Text; using System.IO; using System.Windows.Media; using SparkClient.Model.Common; using Color = System.Windows.Media.Color; 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/UIResource/" + 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.png", ButtonName = MultilingualHelper.getString("圆形") }); buttonInfos.Add(new ButtonInfo() { Type = "Heart", IsEnabled = false, ImageName = "Heart.png", ButtonName = MultilingualHelper.getString("心形") }); buttonInfos.Add(new ButtonInfo() { Type = "cushion", IsEnabled = false, ImageName = "Cushion.png", ButtonName = MultilingualHelper.getString("枕形") }); buttonInfos.Add(new ButtonInfo() { Type = "Pear", IsEnabled = false, ImageName = "Pear.png", ButtonName = MultilingualHelper.getString("梨形") }); buttonInfos.Add(new ButtonInfo() { Type = "Princess", IsEnabled = false, ImageName = "Princess.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.png", ButtonName = MultilingualHelper.getString("祖母绿形") }); buttonInfos.Add(new ButtonInfo() { Type = "Odd", IsEnabled = false, ImageName = "Odd.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/UIResource/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) { 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(710); 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客户端服务,传入SOC端的地址和认证Token _socClientService = new SOCClientService(); // 启动soc socResolt = await _socClientService.ProcessImageCollectionAsync(); switch (socResolt.Status) { case StatusCodes.DeviceNotFound: ShowErrorMessage(MultilingualHelper.getString("DeviceNotFound"), loading); return; case StatusCodes.InProgress: ShowErrorMessage(MultilingualHelper.getString("InProgress"), loading); return; case StatusCodes.CacheCleared: ShowErrorMessage(MultilingualHelper.getString("CacheCleared"), loading); return; case StatusCodes.CannotSendCommand: ShowErrorMessage(MultilingualHelper.getString("CannotSendCommand"), loading); return; case StatusCodes.MicrocontrollerTimeout: ShowErrorMessage(MultilingualHelper.getString("MicrocontrollerTimeout"), loading); return; case StatusCodes.MicrocontrollerError: ShowErrorMessage(MultilingualHelper.getString("MicrocontrollerError"), loading); return; case StatusCodes.CameraNotConnected: ShowErrorMessage(MultilingualHelper.getString("CameraNotConnected"), loading); return; } _algorithmServer = new AlgorithmServer(); //图片集合 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\" ]" ; // 启动算法 parameter = _algorithmServer.CallParseJsonAndReturnActions(parameter.Shape, parameter.CrownType, image_files); //机器号 parameter.DeviceId = socResolt.DeviceId; switch (parameter.status) { case StatusCodes.AlgorithmFailed: ShowErrorMessage(MultilingualHelper.getString("AlgorithmFailed"), loading); return; case StatusCodes.ImageFileReadFailure: ShowErrorMessage(MultilingualHelper.getString("ImageFileReadFailure"), loading); return; case StatusCodes.JsonParseFailure: ShowErrorMessage(MultilingualHelper.getString("JsonParseFailure"), loading); return; } 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 = 20; int maxValue = 100; // 生成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 { } } // 将 UI 操作调度到主线程并显示错误信息 void ShowErrorMessage(string errorMessage, LoadingDialog loading) { Application.Current.Dispatcher.Invoke(() => { MsgDialog msgDialog = new MsgDialog(); msgDialog.ErrorMessage = errorMessage; msgDialog.ShowDialog(); }); loading.Dispatcher.Invoke(() => loading.Close()); } /// /// 开始检测(对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); } } 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; private Brush _foreground; private Brush _background; 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)); UpdateStyles(); } } public Brush ForegroundColor { get { return _foreground; } set { _foreground = value; OnPropertyChanged(nameof(ForegroundColor)); } } public Brush BackgroundColor { get { return _background; } set { _background = value; OnPropertyChanged(nameof(BackgroundColor)); } } private void UpdateStyles() { if (IsHighlighted) { // 高亮状态的样式 LinearGradientBrush linearGradientBrush = new LinearGradientBrush( new GradientStopCollection() { new GradientStop(Color.FromRgb(64, 177, 255), 0), // #61bde2 new GradientStop(Color.FromRgb(60, 156, 230), 1) // #53abd9 }, new Point(0, 0), // StartPoint new Point(0, 1) // EndPoint ); BackgroundColor = linearGradientBrush; ForegroundColor = new SolidColorBrush(Color.FromRgb(255, 255, 255)); string path = _imageSource.UriSource.AbsolutePath; if (!path.Contains("_select.png")) { path = path.Replace(".png", "_select.png"); } ImageSource = new BitmapImage(new Uri("pack://application:,,," + path)); } else { // 默认状态的样式 BackgroundColor = new SolidColorBrush(Color.FromRgb(235, 238, 245)); ForegroundColor = Brushes.Black; string path = _imageSource.UriSource.AbsolutePath.Replace("_select.png", ".png"); ImageSource = new BitmapImage(new Uri("pack://application:,,," + path)); } } }