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 System.Threading; using BPASmartClient.CustomResource.Pages.Model; using BPASmartClient.CustomResource.UserControls.MessageShow; using BPASmartClient.CustomResource.UserControls; using BPASmartClient.Model.柔性味魔方; using BPASmartClient.Model; using System.Diagnostics; using Microsoft.EntityFrameworkCore.Metadata.Internal; using System.Data.SqlClient; namespace BPASmartClient.DosingSystem.ViewModel { public class RecipeControlViewModel : NotifyBase { static ConcurrentQueue RecipeNames = new ConcurrentQueue(); static ObservableCollection StockStatus = new ObservableCollection(); public RecipeControlViewModel() { StartCommand = new BPARelayCommand(RecipeIssued); ChangeRecipeStateCommand = new BPARelayCommand(ChangeRecipeState); CancelRecipeCommand = new BPARelayCommand(CancelRecipe); RecipeRun(); RecipeStatusInquire(); } private async void CancelRecipe(object o) { if (o != null && o is RecipeModel recipe) { var res = MessageNotify.GetInstance.ShowDialog($"是否取消配方 【{recipe.RecipeName}】制作", DialogType.Warning); if (res) { int index = Recipes.ToList().FindIndex(p => p.RecipeName == recipe.RecipeName); if (index >= 0 && index < Recipes.Count) { await Task.Factory.StartNew(new Action(() => { Recipes.ElementAt(index).IsEnable = true; Json.Data.Recipes.ElementAt(index).IsEnable = true; Recipes.ElementAt(index).Are.Set(); App.Current.Dispatcher.Invoke(new Action(() => { recipeProcesses.Clear(); })); SiemensDevice.GetInstance.MySiemens.Write("M10.5", true); MessageNotify.GetInstance.ShowRunLog($"M10.5:true"); Thread.Sleep(1000); SiemensDevice.GetInstance.MySiemens.Write("M10.5", false); MessageNotify.GetInstance.ShowRunLog($"M10.5:false"); NoticeDemoViewModel.OpenMsg(EnumPromptType.Success, App.MainWindow, "提示", $"配方 [{recipe.RecipeName}] 取消成功"); MessageNotify.GetInstance.ShowUserLog($"取消配方制作 {recipe.RecipeName}"); })); } } } } /// /// 配方下发 /// private void RecipeIssued(object o) { if (o != null && o is string deviceName) { 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++) { 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 (ip == null && ip == "") { MessageNotify.GetInstance.ShowDialog($"原料 【{Recipes.ElementAt(index).RawMaterials.ElementAt(i).RawMaterialName}】配料系统无法配料,请人工配置此原料:原料{Recipes.ElementAt(index).RawMaterials.ElementAt(i).RawMaterialName},重量{Recipes.ElementAt(index).RawMaterials.ElementAt(i).RawMaterialWeight}", DialogType.Information); return; } if (!device.IsConnected) { MessageNotify.GetInstance.ShowDialog($"设备 【{device.DeviceName}】 未连接,不允许下发此配方", DialogType.Error); return; } } } Recipes.ElementAt(index).IsEnable = false; Json.Data.Recipes.ElementAt(index).IsEnable = false; } MessageNotify.GetInstance.ShowUserLog($"下发工单 {Recipes.ElementAt(index).RecipeName}"); RecipeNames.Enqueue(deviceName); //var t = RecipeNames.GetHashCode(); var res = Recipes.FirstOrDefault(p => p.RecipeName == deviceName); UserTreeWait.Add(new RecipeModel { RecipStatus = "等待制作", SerialNum = UserTreeWait.Count + 1, RecipeName = deviceName, RawMaterials = res.RawMaterials }); } NoticeDemoViewModel.OpenMsg(EnumPromptType.Success, App.MainWindow, "提示", $"配方下发成功!"); } /// /// 配方业务执行 /// private void RecipeRun() { TaskManage.GetInstance.StartLong(new Action(() => { if (RecipeNames.Count > 0) { int index = Array.FindIndex(Recipes.ToArray(), p => p.RecipeName == RecipeNames.ElementAt(0)); if (index >= 0 && index < Recipes.Count) { Recipes.ElementAt(index).Are.Reset(); Recipes.ElementAt(index).IsEnable = false; StockStatus.Clear(); App.Current.Dispatcher.Invoke(new Action(() => { recipeProcesses.Clear(); if (UserTreeWait.Count > 0) UserTreeWait.RemoveAt(0); })); CurrentRecipeName = Recipes.ElementAt(index).RecipeName; #region 配方下发到PLC操作相关 //配方数据写入到输送机 var res = Json.Data.Recipes.FirstOrDefault(p => p.RecipeName == CurrentRecipeName); List BarrelNum = new List(); if (res != null) { var tInfo = res.RawMaterials.GroupBy(p => p.Loc);//获取桶号信息 if (tInfo != null) { for (int i = 0; i < tInfo.Count(); i++) { int data = 0; for (int m = 0; m < tInfo.ElementAt(i).Count(); m++) { var TempName = tInfo.ElementAt(i).ElementAt(m).RawMaterialName; var tempDevice = DeviceInquire.GetInstance.devices.FirstOrDefault(p => p.DeviceName == TempName); if (tempDevice != null) { /*data = data.SetBitValue((byte)tempDevice.DeviceNum, true);*/ if (tempDevice.DeviceNum > 0) { //int a = data.SetBitValue((byte)tInfo.ElementAt(i).ElementAt(m).Loc, true); //byte[] test1 = a.ToBytes(BPA.Helper.DataFormat.BADC); //int item = test1.ToInt(); int item = data.SetBitValue((byte)tInfo.ElementAt(i).ElementAt(m).Loc, true).ToBytes(BPA.Helper.DataFormat.BADC).ToInt(); var RE = SiemensDevice.GetInstance.MySiemens.Write($"DB4.DBD{2 + (tempDevice.DeviceNum - 1) * 4}", item); if (RE.IsSuccess) MessageNotify.GetInstance.ShowRunLog($"下发配方DB4.DBD{2 + (tempDevice.DeviceNum - 1) * 4} :{(byte)tInfo.ElementAt(i).ElementAt(m).Loc}-{item}-{item.ToBinString()}"); else MessageNotify.GetInstance.ShowRunLog($"下发配方DB4.DBD{2 + (tempDevice.DeviceNum - 1) * 4} 失败"); } } if (tInfo.ElementAt(i).ElementAt(m).Loc > 0) { if (!BarrelNum.Contains(tInfo.ElementAt(i).ElementAt(m).Loc)) { BarrelNum.Add(tInfo.ElementAt(i).ElementAt(m).Loc); } } } /*recipeData.Add(data);*/ } } } SiemensDevice.GetInstance.MySiemens.Write("DB4.DBD198", BarrelNum.Count);//配方使用桶数写入 /*if (recipeData.Count <= 0) { MessageNotify.GetInstance.ShowRunLog("配方数据解析失败"); return; }*/ /*int offset = 2; //写入配方数据 MessageNotify.GetInstance.ShowRunLog("开始写入配方数据"); recipeData.ForEach(item => { byte[] test1 = item.ToBytes(BPA.Helper.DataFormat.ABCD); item = test1.ToInt(); SiemensDevice.GetInstance.MySiemens.Write($"DB4.DBD{offset}", item); MessageNotify.GetInstance.ShowRunLog($"下发配方DB4.DBD{offset} :{item.ToBinString()}"); offset = offset += 4; });*/ /*SiemensDevice.GetInstance.MySiemens.Write("DB4.DBD198", recipeData.Count);//配方使用桶数写入*/ SiemensDevice.GetInstance.MySiemens.Write("DB4.DBX0.1", true);//配方执行启动 Stopwatch sw = new Stopwatch(); var recipe = Recipes.ElementAt(index); ObservableCollection RawMater = new ObservableCollection(); foreach (var item in recipe.RawMaterials) { RawMater.Add(new RawMaterialModel() { RawMaterialName = item.RawMaterialName, DeviceIp = item.DeviceIp, DownLimtFeedback = item.DownLimtFeedback, Loc = item.Loc, RawMaterialId = item.RawMaterialId, RawMaterialSource = item.RawMaterialSource, RawMaterialType = item.RawMaterialType, RawMaterialWeight = item.RawMaterialWeight, RecipeStatus = item.RecipeStatus, SelectIndex = item.SelectIndex, Status = item.Status, UpLimtFeedback = item.UpLimtFeedback, UpLimtWeightFeedback = item.UpLimtWeightFeedback, WeightFeedback = item.WeightFeedback, }); } App.Current.Dispatcher.Invoke(() => { recipeProcesses.Add(new RecipeModel() { RawMaterials = RawMater, IsEnable = recipe.IsEnable, RecipeName = recipe.RecipeName, SerialNum = recipe.SerialNum, RecipCode = recipe.RecipCode, }); }); sw.Restart(); while (true) { if (sw.ElapsedMilliseconds >= 3000 && !GlobalDevice.PlcData.ResComplete) { MessageNotify.GetInstance.ShowRunLog("获取配方下发反馈超时"); break; } if (GlobalDevice.PlcData.ResComplete) { SiemensDevice.GetInstance.MySiemens.Write("DB3.DBX0.1", false); break; } Thread.Sleep(100); } #endregion Recipes.ElementAt(index).Are.WaitOne();//阻塞,直到当前配方完成 RecipeNames.TryDequeue(out string deviceName); var recipeComple = Recipes.ElementAt(index); ObservableCollection RawMaterComple = new ObservableCollection(); foreach (var item in recipeComple.RawMaterials) { RawMaterComple.Add(new RawMaterialModel() { RawMaterialName = item.RawMaterialName, DeviceIp = item.DeviceIp, DownLimtFeedback = item.DownLimtFeedback, Loc = item.Loc, RawMaterialId = item.RawMaterialId, RawMaterialSource = item.RawMaterialSource, RawMaterialType = item.RawMaterialType, RawMaterialWeight = item.RawMaterialWeight, RecipeStatus = item.RecipeStatus, SelectIndex = item.SelectIndex, Status = item.Status, UpLimtFeedback = item.UpLimtFeedback, UpLimtWeightFeedback = item.UpLimtWeightFeedback, WeightFeedback = item.WeightFeedback, }); } App.Current.Dispatcher.Invoke(() => { UserTreeCompelete.Add(new RecipeModel() { RawMaterials = RawMaterComple, IsEnable = recipeComple.IsEnable, RecipeName = recipeComple.RecipeName, SerialNum = UserTreeCompelete.Count + 1, RecipCode = recipeComple.RecipCode, RecipStatus = "制作完成" }); }); App.Current.Dispatcher.Invoke(new Action(() => { recipeProcesses.Clear(); CurrentRecipeName = string.Empty; }));//完成后清空当前配方 } } Thread.Sleep(100); }), "启动配方下发", true); } /// /// 配方执行状态监听 /// private void RecipeStatusInquire() { 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 tempRawMaterialName = DeviceInquire.GetInstance.GetDevice(Recipes.ElementAt(i).RawMaterials.ElementAt(m).DeviceIp).DeviceName; int recIndex = recipeProcesses.ToList().FindIndex(p => p.RecipeName == CurrentRecipeName); if (recIndex >= 0 && recIndex < recipeProcesses.Count) { int index = recipeProcesses.ElementAt(recIndex).RawMaterials.ToList().FindIndex(p => p.RawMaterialName == tempRawMaterialName); if (index >= 0 && index < recipeProcesses.ElementAt(recIndex).RawMaterials.Count) { //测试使用 recipeProcesses.ElementAt(recIndex).RawMaterials.ElementAt(index).RecipeStatus = RunStatus; } for (int j = 0; j < GlobalDevice.PlcData.IsAllowIngredients.Length; j++) { if (GlobalDevice.PlcData.IsAllowIngredients[j]) { int barrel = j + 1;//桶号 int cnt = GlobalDevice.PlcData.LocationFeedback[j];//允许配料的位置 0~6 if (RTrig.GetInstance("a").Start(GlobalDevice.PlcData.IsAllowIngredients[j])) { MessageNotify.GetInstance.ShowRunLog($"桶号:{barrel},位置:{cnt},允许配料"); } int Location1 = 0; int Location2 = 0; if (cnt == -1) { MessageNotify.GetInstance.ShowRunLog("错误"); return; } else { if (cnt >= 1 && cnt <= 6) { //int a1 = Array.FindIndex(DeviceInquire.GetInstance.devices.ToArray(), p => p.DeviceNum == cnt * 2 - 1); //int b1 = Array.FindIndex(DeviceInquire.GetInstance.devices.ToArray(), p => p.DeviceNum == cnt * 2); //if (a1 >= 0) //{ // Location1 = Array.FindIndex(Recipes.ElementAt(i).RawMaterials.ToArray(), p => p.RawMaterialName == DeviceInquire.GetInstance.devices.ElementAt(a1).DeviceName && p.Loc == barrel); //} //if (b1 >= 0) //{ // Location2 = Array.FindIndex(Recipes.ElementAt(i).RawMaterials.ToArray(), p => p.RawMaterialName == DeviceInquire.GetInstance.devices.ElementAt(b1).DeviceName && p.Loc == barrel); //} int a1 = Array.FindIndex(DeviceInquire.GetInstance.devices.ToArray(), p => p.DeviceNum == cnt ); //int b1 = Array.FindIndex(DeviceInquire.GetInstance.devices.ToArray(), p => p.DeviceNum == cnt * 2); if (a1 >= 0) { Location1 = Array.FindIndex(Recipes.ElementAt(i).RawMaterials.ToArray(), p => p.RawMaterialName == DeviceInquire.GetInstance.devices.ElementAt(a1).DeviceName && p.Loc == barrel); } //if (b1 >= 0) //{ // Location2 = Array.FindIndex(Recipes.ElementAt(i).RawMaterials.ToArray(), p => p.RawMaterialName == DeviceInquire.GetInstance.devices.ElementAt(b1).DeviceName && p.Loc == barrel); //} Location2 = Location1; } } if (Location1 >= 0) { string RawName = Recipes.ElementAt(i).RawMaterials.ElementAt(Location1).RawMaterialName; int abc = Array.FindIndex(DeviceInquire.GetInstance.devices.ToArray(), p => p.DeviceName == RawName); int DeviceNum = DeviceInquire.GetInstance.devices.ElementAt(abc).DeviceNum; if (DeviceNum > 0) { float weight = Recipes.ElementAt(i).RawMaterials.ElementAt(Location1).RawMaterialWeight; int loc = Array.FindIndex(Recipes.ElementAt(i).RawMaterials.ToArray(), p => p.RawMaterialName == RawName); if (loc >= 0 && RawName != null && weight >= 0) { int St_index = Array.FindIndex(StockStatus.ToArray(), p => p.MaterialName == RawName); if (St_index < 0) { StockStatus.Add(new StockStatusModel() { MaterialName = RawName, IssueWeight = weight, IssueStatus = 0, }); } St_index = Array.FindIndex(StockStatus.ToArray(), p => p.MaterialName == RawName); if (St_index >= 0) { if (recipeProcesses.ElementAt(recIndex).RawMaterials.ElementAt(loc).RecipeStatus == 1 && StockStatus.ElementAt(St_index).IssueStatus == 0) { StockStatus.ElementAt(St_index).IssueStatus = 1; DeviceInquire.GetInstance.GetDevice(Recipes.ElementAt(i).RawMaterials.ElementAt(loc).DeviceIp).Start(weight); MessageNotify.GetInstance.ShowRunLog($"柔性味魔方{Recipes.ElementAt(i).RawMaterials.ElementAt(loc).DeviceIp},开始出料"); } if (recipeProcesses.ElementAt(recIndex).RawMaterials.ElementAt(loc).RecipeStatus == 3 && StockStatus.ElementAt(St_index).IssueStatus == 1) { //GlobalDevice.PlcData.IsAllowIngredients[j] = false;//测试使用 StockStatus.ElementAt(St_index).IssueStatus = 2; DeviceInquire.GetInstance.GetDevice(Recipes.ElementAt(i).RawMaterials.ElementAt(loc).DeviceIp).StatusReset(); int a = 0.SetBitValue((byte)(DeviceNum), true); byte[] test1 = a.ToBytes(BPA.Helper.DataFormat.BADC); int item = test1.ToInt(); SiemensDevice.GetInstance.MySiemens.Write("DB4.DBD130", item); //MessageNotify.GetInstance.ShowRunLog($"{res}"); MessageNotify.GetInstance.ShowRunLog($"DB4.DBD130:{item.ToBinString()}"); MessageNotify.GetInstance.ShowRunLog($"柔性味魔方{Recipes.ElementAt(i).RawMaterials.ElementAt(loc).DeviceIp},出料完成,状态复位"); } } } } } if (Location1 >= 0) { string RawName111 = Recipes.ElementAt(i).RawMaterials.ElementAt(Location1).RawMaterialName; int St_index111 = Array.FindIndex(StockStatus.ToArray(), p => p.MaterialName == RawName111); if (Location2 >= 0 && ((Location1 >= 0 && StockStatus.ElementAt(St_index111).IssueStatus == 2))) { string RawName = Recipes.ElementAt(i).RawMaterials.ElementAt(Location2).RawMaterialName; int abc = Array.FindIndex(DeviceInquire.GetInstance.devices.ToArray(), p => p.DeviceName == RawName); int DeviceNum = DeviceInquire.GetInstance.devices.ElementAt(abc).DeviceNum; if (DeviceNum > 0) { float weight = Recipes.ElementAt(i).RawMaterials.ElementAt(Location2).RawMaterialWeight; int loc = Array.FindIndex(Recipes.ElementAt(i).RawMaterials.ToArray(), p => p.RawMaterialName == RawName); if (loc >= 0 && RawName != null && weight >= 0) { int St_index = Array.FindIndex(StockStatus.ToArray(), p => p.MaterialName == RawName); if (St_index < 0) { StockStatus.Add(new StockStatusModel() { MaterialName = RawName, IssueWeight = weight, IssueStatus = 0, }); } St_index = Array.FindIndex(StockStatus.ToArray(), p => p.MaterialName == RawName); if (St_index >= 0) { if (recipeProcesses.ElementAt(recIndex).RawMaterials.ElementAt(loc).RecipeStatus == 1 && StockStatus.ElementAt(St_index).IssueStatus == 0) { StockStatus.ElementAt(St_index).IssueStatus = 1; DeviceInquire.GetInstance.GetDevice(Recipes.ElementAt(i).RawMaterials.ElementAt(loc).DeviceIp).Start(weight); MessageNotify.GetInstance.ShowRunLog($"柔性味魔方{Recipes.ElementAt(i).RawMaterials.ElementAt(loc).DeviceIp},开始出料"); } if (recipeProcesses.ElementAt(recIndex).RawMaterials.ElementAt(loc).RecipeStatus == 3 && StockStatus.ElementAt(St_index).IssueStatus == 1) { //GlobalDevice.PlcData.IsAllowIngredients[j] = false;//测试使用 StockStatus.ElementAt(St_index).IssueStatus = 2; DeviceInquire.GetInstance.GetDevice(Recipes.ElementAt(i).RawMaterials.ElementAt(loc).DeviceIp).StatusReset(); int a = 0.SetBitValue((byte)(DeviceNum), true); byte[] test1 = a.ToBytes(BPA.Helper.DataFormat.BADC); int item = test1.ToInt(); SiemensDevice.GetInstance.MySiemens.Write("DB4.DBD130", item); //MessageNotify.GetInstance.ShowRunLog($"{res}"); //MessageNotify.GetInstance.ShowRunLog($"DB4.DBD130:{item.ToBinString()}"); MessageNotify.GetInstance.ShowRunLog($"DB4.DBD130:{item}"); MessageNotify.GetInstance.ShowRunLog($"柔性味魔方{Recipes.ElementAt(i).RawMaterials.ElementAt(loc).DeviceIp},出料完成,状态复位"); } } } } } } else { if (Location2 >= 0) { string RawName = Recipes.ElementAt(i).RawMaterials.ElementAt(Location2).RawMaterialName; int abc = Array.FindIndex(DeviceInquire.GetInstance.devices.ToArray(), p => p.DeviceName == RawName); int DeviceNum = DeviceInquire.GetInstance.devices.ElementAt(abc).DeviceNum; if (DeviceNum >= 0) { float weight = Recipes.ElementAt(i).RawMaterials.ElementAt(Location2).RawMaterialWeight; int loc = Array.FindIndex(Recipes.ElementAt(i).RawMaterials.ToArray(), p => p.RawMaterialName == RawName); if (cnt > 0 && loc >= 0 && RawName != null && weight >= 0) { int St_index = Array.FindIndex(StockStatus.ToArray(), p => p.MaterialName == RawName); if (St_index < 0) { StockStatus.Add(new StockStatusModel() { MaterialName = RawName, IssueWeight = weight, IssueStatus = 0, }); } St_index = Array.FindIndex(StockStatus.ToArray(), p => p.MaterialName == RawName); if (St_index >= 0) { if (recipeProcesses.ElementAt(recIndex).RawMaterials.ElementAt(loc).RecipeStatus == 1 && StockStatus.ElementAt(St_index).IssueStatus == 0) { StockStatus.ElementAt(St_index).IssueStatus = 1; DeviceInquire.GetInstance.GetDevice(Recipes.ElementAt(i).RawMaterials.ElementAt(loc).DeviceIp).Start(weight); MessageNotify.GetInstance.ShowRunLog($"柔性味魔方{Recipes.ElementAt(i).RawMaterials.ElementAt(loc).DeviceIp},开始出料"); } if (recipeProcesses.ElementAt(recIndex).RawMaterials.ElementAt(loc).RecipeStatus == 3 && StockStatus.ElementAt(St_index).IssueStatus == 1) { //GlobalDevice.PlcData.IsAllowIngredients[j] = false;//测试使用 StockStatus.ElementAt(St_index).IssueStatus = 2; DeviceInquire.GetInstance.GetDevice(Recipes.ElementAt(i).RawMaterials.ElementAt(loc).DeviceIp).StatusReset(); int a = 0.SetBitValue((byte)(DeviceNum), true); byte[] test1 = a.ToBytes(BPA.Helper.DataFormat.BADC); int item = test1.ToInt(); SiemensDevice.GetInstance.MySiemens.Write("DB4.DBD130", item); recipeProcesses.ElementAt(recIndex).RawMaterials.ElementAt(loc).UpLimtWeightFeedback = DeviceInquire.GetInstance.GetDevice(Recipes.ElementAt(i).RawMaterials.ElementAt(loc).DeviceIp).deviceStatus.NowWeightFeedback; //MessageNotify.GetInstance.ShowRunLog($"{res}"); MessageNotify.GetInstance.ShowRunLog($"DB4.DBD130:{item.ToBinString()}"); MessageNotify.GetInstance.ShowRunLog($"柔性味魔方{Recipes.ElementAt(i).RawMaterials.ElementAt(loc).DeviceIp},出料完成,状态复位"); } } } } } } } //508配料项目代码 //if (GlobalDevice.PlcData.IsAllowIngredients[j]) //{ // int barrel = j + 1;//桶号 // int cnt = GlobalDevice.PlcData.LocationFeedback[j];//允许配料的位置 1~10 // if (RTrig.GetInstance("a").Start(GlobalDevice.PlcData.IsAllowIngredients[j])) // { // MessageNotify.GetInstance.ShowRunLog($"桶号:{barrel},位置:{cnt},允许配料"); // } // int Location1 = 0; // if (cnt == -1) // { // MessageNotify.GetInstance.ShowRunLog("没有读取到配料的位置"); // return; // } // else // { // int a1 = Array.FindIndex(DeviceInquire.GetInstance.devices.ToArray(), p => p.DeviceNum == cnt); // Location1 = Array.FindIndex(Recipes.ElementAt(i).RawMaterials.ToArray(), p => p.RawMaterialName == DeviceInquire.GetInstance.devices.ElementAt(a1).DeviceName && p.Loc == barrel); // } // if (Location1 >= 0) // { // string RawName = Recipes.ElementAt(i).RawMaterials.ElementAt(Location1).RawMaterialName; // int abc = Array.FindIndex(DeviceInquire.GetInstance.devices.ToArray(), p => p.DeviceName == RawName); // int DeviceNum = DeviceInquire.GetInstance.devices.ElementAt(abc).DeviceNum; // if (DeviceNum > 0) // { // float weight = Recipes.ElementAt(i).RawMaterials.ElementAt(Location1).RawMaterialWeight; // int loc = Array.FindIndex(Recipes.ElementAt(i).RawMaterials.ToArray(), p => p.RawMaterialName == RawName); // if (loc >= 0 && RawName != null && weight >= 0) // { // int St_index = Array.FindIndex(StockStatus.ToArray(), p => p.MaterialName == RawName); // if (St_index < 0) // { // StockStatus.Add(new StockStatusModel() // { // MaterialName = RawName, // IssueWeight = weight, // IssueStatus = 0, // }); // } // St_index = Array.FindIndex(StockStatus.ToArray(), p => p.MaterialName == RawName); // if (St_index >= 0) // { // if (recipeProcesses.ElementAt(recIndex).RawMaterials.ElementAt(loc).RecipeStatus == 1 && StockStatus.ElementAt(St_index).IssueStatus == 0) // { // StockStatus.ElementAt(St_index).IssueStatus = 1; // DeviceInquire.GetInstance.GetDevice(Recipes.ElementAt(i).RawMaterials.ElementAt(loc).DeviceIp).Start(weight); // MessageNotify.GetInstance.ShowRunLog($"柔性味魔方{Recipes.ElementAt(i).RawMaterials.ElementAt(loc).DeviceIp},开始出料"); // } // if (recipeProcesses.ElementAt(recIndex).RawMaterials.ElementAt(loc).RecipeStatus == 3 && StockStatus.ElementAt(St_index).IssueStatus == 1) // { // //GlobalDevice.PlcData.IsAllowIngredients[j] = false;//测试使用 // StockStatus.ElementAt(St_index).IssueStatus = 2; // DeviceInquire.GetInstance.GetDevice(Recipes.ElementAt(i).RawMaterials.ElementAt(loc).DeviceIp).StatusReset(); // /*int a = 0.SetBitValue((byte)(DeviceNum), true); // byte[] test1 = a.ToBytes(BPA.Helper.DataFormat.ABCD); // int item = test1.ToInt();*/ // if (DeviceNum >= 1 && DeviceNum <= 8) // { // SiemensDevice.GetInstance.MySiemens.Write("DB4.DBX130." + (DeviceNum - 1), true); // }else if (DeviceNum >= 9 && DeviceNum <= 16) // { // SiemensDevice.GetInstance.MySiemens.Write("DB4.DBX131." + (DeviceNum - 9), true); // } // else if (DeviceNum >=17 && DeviceNum <= 24) // { // SiemensDevice.GetInstance.MySiemens.Write("DB4.DBX132." + (DeviceNum - 17), true); // }else if (DeviceNum >= 25 && DeviceNum <= 32) // { // SiemensDevice.GetInstance.MySiemens.Write("DB4.DBX133." + (DeviceNum - 25), true); // } // MessageNotify.GetInstance.ShowRunLog($"料仓:{DeviceNum},配料完成"); // /*string res = SiemensDevice.GetInstance.MySiemens.Write("DB4.DBD130", item, 2); // MessageNotify.GetInstance.ShowRunLog($"{res}"); // MessageNotify.GetInstance.ShowRunLog($"DB4.DBD130:{item.ToBinString()}");*/ // MessageNotify.GetInstance.ShowRunLog($"柔性味魔方{Recipes.ElementAt(i).RawMaterials.ElementAt(loc).DeviceIp},出料完成,状态复位"); // } // } // } // } // } //} } int Finish_Count = StockStatus.Where(s => s.IssueStatus == 2).Count(); if (StockStatus.Count >= Recipes.ElementAt(i).RawMaterials.Count && Finish_Count >= Recipes.ElementAt(i).RawMaterials.Count) //配方配料完成 { StockStatus.Clear(); MessageNotify.GetInstance.ShowUserLog($"配方:{recipeProcesses.ElementAt(recIndex).RecipeName},配料完成"); foreach (var item in recipeProcesses.ElementAt(recIndex).RawMaterials) { MessageNotify.GetInstance.ShowUserLog($"原料:{item.RawMaterialName},桶号:{item.Loc},设定配料重量{item.RawMaterialWeight}g,实际配料重量{item.UpLimtWeightFeedback}g"); } SiemensDevice.GetInstance.MySiemens.Write("DB4.DBX202.0", true);//配料完成 MessageNotify.GetInstance.ShowRunLog($"配方:{CurrentRecipeName},配料完成"); int recipIndex = Array.FindIndex(Recipes.ToArray(), p => p.RecipeName == CurrentRecipeName); App.Current.Dispatcher.Invoke(new Action(() => { recipeProcesses.Clear(); })); Recipes.ElementAt(recipIndex).IsEnable = true; Json.Data.Recipes.ElementAt(recipIndex).IsEnable = true; Recipes.ElementAt(recipIndex).Are.Set(); Thread.Sleep(100); } } } } } Thread.Sleep(100); }), "RecipeControlViewModelStatusInquire", true); } public BPARelayCommand StartCommand { get; set; } public BPARelayCommand ChangeRecipeStateCommand { get; set; } public BPARelayCommand CancelRecipeCommand { get; set; } public static ObservableCollection Recipes { get; set; } = Json.Data.Recipes; public static string CurrentRecipeName { get { return _RecipeName; } set { _RecipeName = value; OnStaticPropertyChanged(); } } 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 Recipe = recipeProcesses.FirstOrDefault(p => p.RecipeName == CurrentRecipeName); if (Recipe != null) { var res = Recipe.RawMaterials.FirstOrDefault(p => p.RawMaterialId == id); if (res != null) { if (res.RecipeStatus == 3) { res.RecipeStatus = 1; } else { res.RecipeStatus = 3; } } } } } } }