diff --git a/ViewModel/Grading/DiamondSelectVM.cs b/ViewModel/Grading/DiamondSelectVM.cs index 130ba2c..9f184fd 100644 --- a/ViewModel/Grading/DiamondSelectVM.cs +++ b/ViewModel/Grading/DiamondSelectVM.cs @@ -17,6 +17,8 @@ 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; @@ -113,85 +115,156 @@ public class DiamondSelectVM : BaseViewModel /// public async void StartGrading(object param) { - LoadingDialog loading = new LoadingDialog(); - try + 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) { - 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 () => { - var progressTask = Task.Run(() => Application.Current.Dispatcher.Invoke(() => loading.ShowDialog())); - await Task.Run(async () => + for (int i = 0; i <= 100; i++) { - bool isEnd = false; - int progress = 0; + // 模拟耗时操作 + //System.Threading.Thread.Sleep(50); // 休眠50毫秒 + await Task.Delay(5); // 更新进度条的值(需要在UI线程上执行) - loading.Dispatcher.Invoke(async () => + loading.Dispatcher.Invoke(() => { - 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; - } + loading.setValue(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); + } + // SocResultEntity socResolt = new SocResultEntity(); + // AlgorithmResultEntity parameter = new AlgorithmResultEntity(); + // // 启动soc + // socResolt = DoSoc(); + // // 启动算法 + // parameter = DoAlgorithm(); - parameter.Standard = "IGI 2024"; + //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; - 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 { - + } + parameter.DiamondCode = DiamondCode; + GradingResult(parameter); + }); + await Task.Delay(5); + loading.Close(); } } - /// /// 跳转至检测结果 ///