From de2778b793fc3415399c4cd26e34803d40baa64a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A6=82=E6=84=8F=20=E5=BD=AD?= <2417589739@qq.com> Date: Wed, 26 Oct 2022 13:29:53 +0800 Subject: [PATCH] =?UTF-8?q?=E9=85=8D=E6=96=B9=E4=B8=9A=E5=8A=A1=E4=BF=AE?= =?UTF-8?q?=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ViewModel/RecipeControlViewModel.cs | 115 +++++++----------- 1 file changed, 44 insertions(+), 71 deletions(-) diff --git a/DosingSystem/ViewModel/RecipeControlViewModel.cs b/DosingSystem/ViewModel/RecipeControlViewModel.cs index dd05036e..374d5d0b 100644 --- a/DosingSystem/ViewModel/RecipeControlViewModel.cs +++ b/DosingSystem/ViewModel/RecipeControlViewModel.cs @@ -23,39 +23,51 @@ namespace BPASmartClient.DosingSystem.ViewModel ObservableCollection StockStatus = new ObservableCollection(); public RecipeControlViewModel() { - StartCommand = new RelayCommand((o) => + StartCommand = new RelayCommand(RecipeIssued); + ChangeRecipeStateCommand = new RelayCommand(ChangeRecipeState); + RecipeRun(); + RecipeStatusInquire(); + } + + /// + /// 配方下发 + /// + private void RecipeIssued(object o) + { + if (o != null && o is string deviceName) { - if (o != null && o is string deviceName) + int index = Array.FindIndex(Recipes.ToArray(), p => p.RecipeName == deviceName); + if (index >= 0 && index < Recipes.Count) { - int index = Array.FindIndex(Recipes.ToArray(), p => p.RecipeName == deviceName); - if (index >= 0 && index < Recipes.Count) + for (int i = 0; i < Recipes.ElementAt(index).RawMaterials.Count; i++) { - for (int i = 0; i < Recipes.ElementAt(index).RawMaterials.Count; i++) + if (Recipes.ElementAt(index).RawMaterials.ElementAt(i).RawMaterialSource == 1) { - if (Recipes.ElementAt(index).RawMaterials.ElementAt(i).RawMaterialSource == 1) - { - string ip = Recipes.ElementAt(index).RawMaterials.ElementAt(i).DeviceIp; - var device = DeviceInquire.GetInstance.GetDevice(ip); - //if (!device.IsConnected) - //{ - // MessageNotify.GetInstance.ShowDialog($"设备 【{device.DeviceName}】 未连接,不允许下发此配方", DialogType.Error); - // return; - //} - } + string ip = Recipes.ElementAt(index).RawMaterials.ElementAt(i).DeviceIp; + var device = DeviceInquire.GetInstance.GetDevice(ip); + //if (!device.IsConnected) + //{ + // MessageNotify.GetInstance.ShowDialog($"设备 【{device.DeviceName}】 未连接,不允许下发此配方", DialogType.Error); + // return; + //} } - Recipes.ElementAt(index).IsEnable = false; } - MessageNotify.GetInstance.ShowUserLog($"下发工单 {Recipes.ElementAt(index).RecipeName}"); - RecipeNames.Enqueue(deviceName); - var res = Recipes.FirstOrDefault(p => p.RecipeName == deviceName); - UserTreeWait.Add(new RecipeModel {SerialNum= UserTreeWait.Count + 1, RecipeName = deviceName, RawMaterials = res.RawMaterials }); + Recipes.ElementAt(index).IsEnable = false; } - NoticeDemoViewModel.OpenMsg(EnumPromptType.Success, App.MainWindow, "提示", $"配方下发成功!"); - MessageNotify.GetInstance.ShowUserLog($"下发工单 {Guid.NewGuid().ToString()}"); - }); - - ChangeRecipeStateCommand = new RelayCommand(ChangeRecipeState); + MessageNotify.GetInstance.ShowUserLog($"下发工单 {Recipes.ElementAt(index).RecipeName}"); + RecipeNames.Enqueue(deviceName); + var res = Recipes.FirstOrDefault(p => p.RecipeName == deviceName); + UserTreeWait.Add(new RecipeModel { SerialNum = UserTreeWait.Count + 1, RecipeName = deviceName, RawMaterials = res.RawMaterials }); + } + NoticeDemoViewModel.OpenMsg(EnumPromptType.Success, App.MainWindow, "提示", $"配方下发成功!"); + MessageNotify.GetInstance.ShowUserLog($"下发工单 {Guid.NewGuid().ToString()}"); + } + /// + /// 配方业务执行 + /// + private void RecipeRun() + { ThreadManage.GetInstance().StartLong(new Action(() => { if (RecipeNames.Count > 0) @@ -132,35 +144,6 @@ namespace BPASmartClient.DosingSystem.ViewModel } #endregion - //foreach (var item in Recipes.ElementAt(index).RawMaterials) - //{ - // //DeviceInquire.GetInstance.GetDevice(item.DeviceIp)?.Start(item.RawMaterialWeight);//启动并写入每个原料重量 - // //var res = DeviceInquire.GetInstance.GetDevice(item.DeviceIp).deviceStatus.DeviceNum; - // App.Current.Dispatcher.Invoke(new Action(() => - // { - - // //ObservableCollection rawMaterialModels = new ObservableCollection(); - // //rawMaterialModels.Add(new RawMaterialModel() - // //{ - // // RawMaterialName = item.RawMaterialName, - // // RecipeStatus = item.RecipeStatus, - // // RawMaterialSource = item.RawMaterialSource, - // // RawMaterialId = item.RawMaterialId, - // //}); - - // //if (recipeProcesses.Count > 0) - // //{ - // // recipeProcesses.ElementAt(0).RawMaterials.Add(new RawMaterialModel() - // // { - // // RawMaterialName = item.RawMaterialName, - // // RecipeStatus = item.RecipeStatus, - // // RawMaterialSource = item.RawMaterialSource, - // // RawMaterialId = item.RawMaterialId, - // // }); - // //} - // })); - //} - Recipes.ElementAt(index).Are.WaitOne();//阻塞,直到当前配方完成 RecipeNames.TryDequeue(out string deviceName); App.Current.Dispatcher.Invoke(new Action(() => @@ -177,8 +160,13 @@ namespace BPASmartClient.DosingSystem.ViewModel } Thread.Sleep(100); }), "启动配方下发"); + } - + /// + /// 配方执行状态监听 + /// + private void RecipeStatusInquire() + { ThreadManage.GetInstance().StartLong(new Action(() => { for (int i = 0; i < Recipes.Count; i++) @@ -190,7 +178,6 @@ namespace BPASmartClient.DosingSystem.ViewModel if (Recipes.ElementAt(i).RecipeName == CurrentRecipeName) { string tempRawMaterialName = DeviceInquire.GetInstance.GetDevice(Recipes.ElementAt(i).RawMaterials.ElementAt(m).DeviceIp).DeviceName; - //int index = Array.FindIndex(Recipes.ElementAt(i).RawMaterials.ToArray(), p => p.RawMaterialName == tempRawMaterialName); int recIndex = recipeProcesses.ToList().FindIndex(p => p.RecipeName == CurrentRecipeName); if (recIndex >= 0 && recIndex < recipeProcesses.Count) { @@ -198,9 +185,7 @@ namespace BPASmartClient.DosingSystem.ViewModel if (index >= 0 && index < recipeProcesses.ElementAt(recIndex).RawMaterials.Count) { recipeProcesses.ElementAt(recIndex).RawMaterials.ElementAt(index).RecipeStatus = RunStatus; - //App.Current?.Dispatcher?.Invoke(new Action(() => { recipeProcesses.ElementAt(recIndex).RawMaterials.ElementAt(index).RecipeStatus = RunStatus; })); } - //App.Current.Dispatcher.Invoke(new Action(() => { recipeProcesses.ElementAt(index).RecipeStatus = RunStatus; })); for (int j = 0; j < GlobalDevice.PlcData.IsAllowIngredients.Length; j++) { if (GlobalDevice.PlcData.IsAllowIngredients[j]) @@ -270,12 +255,12 @@ namespace BPASmartClient.DosingSystem.ViewModel /// 当前正在制作的配方 /// public static ObservableCollection recipeProcesses { get; set; } = new ObservableCollection(); - //public static ObservableCollection recipeProcesses { get; set; } = new ObservableCollection(); /// /// 等待制作的配方 /// public static ObservableCollection UserTreeWait { get; set; } = new ObservableCollection(); + /// /// 已完成的配方 /// @@ -303,18 +288,6 @@ namespace BPASmartClient.DosingSystem.ViewModel } } } - //var res = recipeProcesses.FirstOrDefault(p => p.RawMaterialId == id); - //if (res != null) - //{ - // if (res.RecipeStatus == 3) - // { - // res.RecipeStatus = 1; - // } - // else - // { - // res.RecipeStatus = 3; - // } - //} } } }