using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using Microsoft.Toolkit.Mvvm.ComponentModel; using System.Collections.Concurrent; using System.Collections.ObjectModel; using System.Windows; using BPASmartClient.Helper; using Microsoft.Toolkit.Mvvm.Input; using BPASmartClient.JXJFoodSmallStation.Model; using System.Threading; using BPASmartClient.CustomResource.Pages.Model; using BPASmartClient.CustomResource.UserControls.MessageShow; using BPASmartClient.CustomResource.UserControls; namespace BPASmartClient.JXJFoodSmallStation.ViewModel { public class RecipeControlViewModel : ObservableObject { ConcurrentQueue devices = new ConcurrentQueue(); public RecipeControlViewModel() { Recipes = Json.Data.Recipes; StartCommand = new RelayCommand((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; } MessageLog.GetInstance.ShowUserLog($"下发工单 { Recipes.ElementAt(index).RecipeName}"); devices.Enqueue(deviceName); } NoticeDemoViewModel.OpenMsg(EnumPromptType.Success, App.MainWindow, "提示", $"配方下发成功!"); MessageLog.GetInstance.ShowUserLog($"下发工单 {Guid.NewGuid().ToString()}"); }); ThreadManage.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; foreach (var item in Recipes.ElementAt(index).RawMaterials) { DeviceInquire.GetInstance.GetDevice(item.DeviceIp)?.Start(item.RawMaterialWeight);//启动并写入每个原料重量 } Recipes.ElementAt(index).Are.WaitOne();//阻塞,直到当前配方完成 devices.TryDequeue(out string deviceName); } } Thread.Sleep(100); }), "启动配方下发"); ThreadManage.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; 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 } Recipes.ElementAt(i).IsEnable = true; Recipes.ElementAt(i).Are.Set(); } } } Thread.Sleep(100); }), "RecipeControlViewModelStatusInquire"); } public RelayCommand StartCommand { get; set; } public ObservableCollection Recipes { get; set; } } }