using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using BPA.Helper; using System.Collections.Concurrent; using System.Collections.ObjectModel; using System.Windows; using BPA.Helper; using BPASmartClient.JXJFoodSmallStation.Model; using System.Threading; using BPASmartClient.CustomResource.Pages.Model; using BPASmartClient.CustomResource.UserControls.MessageShow; using BPASmartClient.CustomResource.UserControls; using BPASmartClient.Model.柔性味魔方; using BPASmartClient.Model; namespace BPASmartClient.JXJFoodSmallStation.ViewModel { public class RecipeControlViewModel : NotifyBase { ConcurrentQueue devices = new ConcurrentQueue(); public RecipeControlViewModel() { StartCommand = new BPARelayCommand((o) => { if (o != null && o is string deviceName) { int index = Array.FindIndex(Recipes.ToArray(), p => p.RecipeName == deviceName); if (index >= 0 && index < Recipes.Count) { Recipes.ElementAt(index).IsEnable = false; } MessageNotify.GetInstance.ShowUserLog($"下发工单 { Recipes.ElementAt(index).RecipeName}"); var res = Recipes.FirstOrDefault(p => p.RecipeName == deviceName); App.Current.Dispatcher.Invoke(() => { UserTreeWait.Add(new RecipeModel { RecipeName = deviceName, RawMaterials = res.RawMaterials }); }); NoticeDemoViewModel.OpenMsg(EnumPromptType.Success, App.MainWindow, "提示", $"配方下发成功!"); MessageNotify.GetInstance.ShowUserLog($"下发工单 {Guid.NewGuid().ToString()}"); devices.Enqueue(deviceName); } }); ChangeRecipeStateCommand = new BPARelayCommand(ChangeRecipeState); TaskManage.GetInstance.StartLong(new Action(() => { if (devices.Count > 0) { int index = Array.FindIndex(Recipes.ToArray(), p => p.RecipeName == devices.ElementAt(0)); if (index >= 0 && index < Recipes.Count) { Recipes.ElementAt(index).Are.Reset(); Recipes.ElementAt(index).IsEnable = false; App.Current.Dispatcher.Invoke(new Action(() => { recipeProcesses.Clear(); UserTreeWait.RemoveAt(0); })); CurrentRecipeName = Recipes.ElementAt(index).RecipeName; foreach (var item in Recipes.ElementAt(index).RawMaterials) { DeviceInquire.GetInstance.GetDevice(item.DeviceIp)?.Start(item.RawMaterialWeight);//启动并写入每个原料重量 App.Current.Dispatcher.Invoke(new Action(() => { recipeProcesses.Add(new RawMaterialModel() { RawMaterialName = item.RawMaterialName, RecipeStatus = item.RecipeStatus, RawMaterialSource = item.RawMaterialSource, RawMaterialId = item.RawMaterialId, }); })); } Recipes.ElementAt(index).Are.WaitOne();//阻塞,直到当前配方完成 devices.TryDequeue(out string deviceName); App.Current.Dispatcher.Invoke(new Action(() => { UserTreeCompelete.Add(Recipes.ElementAt(index));//当前配方完成后添加到已完成的配方列表 })); App.Current.Dispatcher.Invoke(new Action(() => { recipeProcesses.Clear(); CurrentRecipeName = string.Empty; }));//完成后清空当前配方 } } Thread.Sleep(100); }), "启动配方下发",true); TaskManage.GetInstance.StartLong(new Action(() => { for (int i = 0; i < Recipes.Count; i++) { for (int m = 0; m < Recipes.ElementAt(i).RawMaterials.Count; m++) { var RunStatus = DeviceInquire.GetInstance.GetDevice(Recipes.ElementAt(i).RawMaterials.ElementAt(m).DeviceIp).deviceStatus.RunStatus; //设备状态显示 if (Recipes.ElementAt(i).RecipeName == CurrentRecipeName) { string deviceName = DeviceInquire.GetInstance.GetDevice(Recipes.ElementAt(i).RawMaterials.ElementAt(m).DeviceIp).DeviceName; int index = Array.FindIndex(Recipes.ElementAt(i).RawMaterials.ToArray(), p => p.RawMaterialName == deviceName); if (index >= 0 && index < recipeProcesses.Count) { App.Current.Dispatcher.Invoke(new Action(() => { recipeProcesses.ElementAt(index).RecipeStatus = RunStatus; })); } } var proc = recipeProcesses.Where(p=>p.RecipeStatus==3).ToList(); if (proc != null && proc.Count > 0 && proc.Count== recipeProcesses.Count) { int recipIndex = Array.FindIndex(Recipes.ToArray(), p => p.RecipeName == CurrentRecipeName); if (recipIndex >= 0 && recipIndex < Recipes.Count) { for (int n = 0; n < recipeProcesses.Count; n++) { DeviceInquire.GetInstance.GetDevice(Recipes.ElementAt(recipIndex).RawMaterials.ElementAt(n).DeviceIp).StatusReset();//完成配料的设备运行状态地址写0 MessageNotify.GetInstance.ShowRunLog(Recipes.ElementAt(recipIndex).RawMaterials.ElementAt(n).DeviceIp); } App.Current.Dispatcher.Invoke(new Action(() => { recipeProcesses.Clear(); })); Recipes.ElementAt(recipIndex).IsEnable = true; Recipes.ElementAt(recipIndex).Are.Set(); } } //Recipes.ElementAt(i).RawMaterials.ElementAt(m).RecipeStatus = RunStatus; //var res = Recipes.ElementAt(i).RawMaterials.Where(p => p.RecipeStatus == 3).ToList(); //if (res != null && res.Count == Recipes.ElementAt(i).RawMaterials.Count)//配方所有配料完成下料 //{ // for (int r = 0; r < Recipes.ElementAt(i).RawMaterials.Count; r++) // { // DeviceInquire.GetInstance.GetDevice(Recipes.ElementAt(i).RawMaterials.ElementAt(r).DeviceIp).StatusReset();//完成配料的设备运行状态地址写0 // App.Current.Dispatcher.Invoke(new Action(() => { recipeProcesses.Clear(); })); // } // Recipes.ElementAt(i).IsEnable = true; // Recipes.ElementAt(i).Are.Set(); //} } } Thread.Sleep(100); }), "RecipeControlViewModelStatusInquire",true); //测试数据 /* RawMaterialModel rawMaterial_1 = new RawMaterialModel { RawMaterialName = "香料_1" }; RawMaterialModel rawMaterial_2 = new RawMaterialModel { RawMaterialName = "香料_2" }; RawMaterialModel rawMaterial_3 = new RawMaterialModel { RawMaterialName = "香料_3" }; RawMaterialModel rawMaterial_4 = new RawMaterialModel { RawMaterialName = "香料_4" }; ObservableCollection rawMaterials = new ObservableCollection { rawMaterial_1, rawMaterial_2, rawMaterial_3, rawMaterial_4 }; UserTreeCompelete.Add(new RecipeModel { RecipeName = "完成的香料1", RawMaterials = rawMaterials }); UserTreeCompelete.Add(new RecipeModel { RecipeName = "完成的香料2", RawMaterials = rawMaterials });*/ } public BPARelayCommand StartCommand { get; set; } public BPARelayCommand ChangeRecipeStateCommand { get; set; } public static ObservableCollection Recipes { get; set; } = Json.Data.Recipes; public string CurrentRecipeName { get { return _RecipeName; }set { _RecipeName = value; OnPropertyChanged(); } } private static string _RecipeName; /// /// 当前正在制作的配方 /// public static ObservableCollection recipeProcesses { get; set; } = new ObservableCollection(); /// /// 等待制作的配方 /// public static ObservableCollection UserTreeWait { get; set; } = new ObservableCollection(); /// /// 已完成的配方 /// public static ObservableCollection UserTreeCompelete { get; set; } = new ObservableCollection(); private void ChangeRecipeState(object o) { if (o == null) return; if(o is string id) { var res = recipeProcesses.FirstOrDefault(p => p.RawMaterialId == id); if (res != null) { if(res.RecipeStatus == 3) { res.RecipeStatus = 1; }else { res.RecipeStatus = 3; } } } } } }