|
- //#define Debug
- extern alias aliyun;
- using BPA.Helper;
- using BPASmartClient.CustomResource.Pages.Model;
- using BPASmartClient.CustomResource.UserControls;
- using BPASmartClient.CustomResource.UserControls.MessageShow;
- using BPASmartClient.DosingSystem.ViewModel;
- using BPASmartClient.Model;
- using System;
- using System.Collections.Concurrent;
- using System.Collections.ObjectModel;
- using System.Collections.Specialized;
- using System.Diagnostics;
- using System.Linq;
- using System.Threading;
- using System.Threading.Tasks;
- using aliyun.uPLibrary.Networking.M2Mqtt.Messages;
- using System.Collections.Generic;
- using aliyun::BPA.PahoMQ;
- using System.Configuration;
- using BPA.Communication;
- using Newtonsoft.Json;
- using System.Reflection;
- using System.Text;
- using BPASmartClient.DosingSystem.Model;
- using Microsoft.EntityFrameworkCore.Metadata.Internal;
- using System.Collections;
- using static Microsoft.EntityFrameworkCore.DbLoggerCategory;
- using System.Windows.Navigation;
- using System.Text.RegularExpressions;
- using static System.Resources.ResXFileRef;
- using BPASmartClient.DosingSystem.Model.Dto;
- using BPASmartClient.DosingSystem.Model.MQTT;
- using Microsoft.VisualBasic.Logging;
- using Org.BouncyCastle.Asn1.Utilities;
- using Google.Protobuf.WellKnownTypes;
- using System.Windows;
- using System.Windows.Media.Converters;
- using SqlSugar;
- using Microsoft.VisualBasic;
-
- namespace BPASmartClient.DosingSystem
- {
- /// <summary>
- /// 业务逻辑执行类
- /// </summary>
- public class ExcuteControl
- {
- private static ExcuteControl Instance;
- public static ExcuteControl GetInstance => Instance ??= new ExcuteControl();
-
- public ConcurrentDictionary<string, aliyun.BPA.PahoMQ.MessageHandler> mqttCollection = new ConcurrentDictionary<string, aliyun.BPA.PahoMQ.MessageHandler>();
-
- public ConcurrentDictionary<string, string> aliyun = new ConcurrentDictionary<string, string>();
- public ObservableCollection<RecipeModel> Recipes { get; set; } = Json<LocalRecipe>.Data.Recipes;
- public ObservableCollection<StockStatusModel> StockStatus = new ObservableCollection<StockStatusModel>();
-
- public Action Raction { get; set; }
-
-
-
- public string CurrentRecipeName { get; set; }
- /// <summary>
- /// 当前正在制作的配方
- /// </summary>
- public ObservableCollection<RecipeModel> recipeProcesses { get; set; } = new ObservableCollection<RecipeModel>();
-
- /// <summary>
- /// 等待制作的配方
- /// </summary>
- public ObservableCollection<RecipeModel> UserTreeWait { get; set; } = new ObservableCollection<RecipeModel>();
-
- /// <summary>
- /// 已完成的配方
- /// </summary>
- public ObservableCollection<RecipeModel> UserTreeCompelete { get; set; } = new ObservableCollection<RecipeModel>();
- public ConcurrentQueue<string> RecipeNames = new ConcurrentQueue<string>();
-
-
- /// <summary>
- /// 配料完成计数
- /// </summary>
- int Comcount = 0;
- public static bool PropertySet = false;//云端属性设置回调主题订阅标志
- public static bool PropertyReport = false; //属性上报主题订阅标志
-
- public static bool EventReport = false; //事件上报主题订阅标志
-
- public static bool ServiceCall = false;//服务调用回调主题订阅标志
-
- public NameValueCollection produtInfo;
- public NameValueCollection mqttInfo;
- public NameValueCollection ConveyerInfo;
- public NameValueCollection ServicesInfo;
-
- public bool AliyunIsConnect = false;
- public ExcuteControl()
- {
- ActionManage.GetInstance.Register<object>(new Action<object>((o) =>
- {
- if (o != null && o is string recipeName)
- {
- RecipeSend(recipeName);
- }
-
- }), "配方下发");
- ActionManage.GetInstance.Register<object>(new Action<object>((ob) =>
- {
- if (ob != null && ob is string recipeName)
- {
- CancelRecipe(recipeName);
- }
-
- }), "取消配方");
-
- //AliYunInit();
- //RecipeRun();
- //BusinessExcute();
- MatchRun();
- //ConveyerPropertyReport();
- //ThreadTaskInit();
- #if Debug
- Task.Run(() => {
- while (true)
- {
- if (AliyunIsConnect)
- {
- for (int i = 0; i < 6; i++)
- {
- AliyunEventReport($"192.168.0.{i+1}0", new AliyunStockEventReportModel { Result = "1" }, "StockJerk1StatusEvent", AliyunMaterialModelType.StockModel);
- AliyunEventReport($"192.168.0.{i + 1}0", new AliyunStockEventReportModel { Result = "1" }, "StockServoFalutEvent", AliyunMaterialModelType.StockModel);
- AliyunEventReport($"192.168.0.{i + 1}0", new AliyunStockEventReportModel { Result = "1" }, "StockJerk2StatusEvent", AliyunMaterialModelType.StockModel);
- AliyunEventReport($"192.168.0.{i + 1}0", new AliyunStockEventReportModel { Result = "1" }, "StockUpLimitEvent", AliyunMaterialModelType.StockModel);
- AliyunEventReport($"192.168.0.{i + 1}0", new AliyunStockEventReportModel { Result = "1" }, "StockDownLimitEvent", AliyunMaterialModelType.StockModel);
-
- AliyunStockPropertyReportModel model = new AliyunStockPropertyReportModel();
- model.StockRealWeight = 1;
- model.BucketRealWeight = 2;
- model.StockDeviceNum = 2;
- model.StockDeviceName = "测试";
- model.StockWorkModel = 0;
- model.StockSlowlyAddWeight = 6;
- model.StockPreCloseValueWeight = 7;
- model.StockRapidAcceleration = 46;
- model.StockSlowAcceleration = 24;
- model.StockServoManualSpeed = 35;
- model.StockUpperLimitWeight = 57;
- model.StockLowerLimitWeight = 34;
- model.StockStirringSpeed = 54;
- model.StockMaterialType = "粉体";
- model.StockBatchStatus = "待机";
- StokcPropertyReport($"192.168.0.{i + 1}0", model);
- }
- AliyunConveyerPropertyReportModel model1 = new AliyunConveyerPropertyReportModel();
- model1.TopBucketControlStatus = 1;
- model1.DownBucketControlStatus =1;
- model1.RunStatus = 1;
- model1.WorkModel = 1;
- model1.TopBucketSigleCheck = 1;
- model1.ButtonBucketSigleCheck = 1;
- model1.ConveyerOnlineStatus =1;
- var mqdeviceTestProperty = new PostPropertyParams<AliyunConveyerPropertyReportModel>();
- mqdeviceTestProperty.sys = new Sys { ack = 1 };
- mqdeviceTestProperty.method = "thing.event.property.post";
- mqdeviceTestProperty.version = "1.0";
- mqdeviceTestProperty.ModelName = $"{produtInfo.Get("Conveyer")}";
- mqdeviceTestProperty.Model = model1;
- mqdeviceTestProperty.Init();
- var message = JsonConvert.SerializeObject(mqdeviceTestProperty);
- var topic = $"/sys/{produtInfo.Get("ProductKey")}/Conveyer/thing/event/property/post";//发布主题
-
- var topicReply = $"/sys/{produtInfo.Get("ProductKey")}/Conveyer/thing/event/property/post_reply";//订阅主题
- if (mqttCollection.ContainsKey("Conveyer"))
- {
- try
- {
- mqttCollection["Conveyer"].Publish((op) =>
- {
- op.Message = message;
- op.Topic = topic;
- op.TopicReply = topicReply;
- //if (!PropertyReport)
- //{
- // op.ThresholdCallback += MqttPostProperty_MqttMsgPublishReceived; // MqttPostProperty_MqttMsgPublishReceived;
- // PropertyReport = true;
- //}
- });
- }
- catch (Exception)
- {
-
- //throw;
- }
- }
- SystemEventReport("ProcessRecipe", new { RecipeName = "测试5" });//正在执行配方上报
- if (UserTreeWait.Count > 0)
- {
- string[] recipeNames = new string[UserTreeWait.Count];
- for (int i = 0; i < recipeNames.Length; i++)
- {
- recipeNames[i] = UserTreeWait.ElementAt(i).RecipeName;
- }
- SystemEventReport("WaitProcessRecipe", new { RecipeNames = recipeNames });//等待执行配方上报
- }
- else
- {
- SystemEventReport("WaitProcessRecipe", new { RecipeNames = new string[] { } });//等待执行配方上报
- }
- }
- Thread.Sleep(5000);
- }
- });
- #endif
-
- }
-
-
- /// <summary>
- /// 料仓属性上报
- /// </summary>
- /// <param name="ipaddres"></param>
- /// <param name="model"></param>
- public void StokcPropertyReport<T>(string ipaddres, T model)
- {
- var mqdeviceTestProperty = new PostPropertyParams<T>();
- mqdeviceTestProperty.sys = new Sys { ack = 1 };
- mqdeviceTestProperty.method = "thing.event.property.post";
- mqdeviceTestProperty.version = "1.0";
- mqdeviceTestProperty.ModelName = $"{produtInfo.Get("Stock")}";
- mqdeviceTestProperty.Model = model;
- mqdeviceTestProperty.Init();
- var message = JsonConvert.SerializeObject(mqdeviceTestProperty);
- var topic = $"/sys/{produtInfo.Get("ProductKey")}/{aliyun[ipaddres]}/thing/event/property/post";//发布主题
- var topicReply = $"/sys/{produtInfo.Get("ProductKey")}/{aliyun[ipaddres]}/thing/event/property/post_reply";//订阅主题
- if (mqttCollection.ContainsKey(aliyun[ipaddres]))
- {
- try
- {
- mqttCollection[aliyun[ipaddres]].Publish((op) =>
- {
- op.Message = message;
- op.Topic = topic;
- op.TopicReply = topicReply;
- //if (!PropertyReport)
- //{
- // op.ThresholdCallback += MqttPostProperty_MqttMsgPublishReceived; // MqttPostProperty_MqttMsgPublishReceived;
- // PropertyReport = true;
- //}
- });
- }
- catch (Exception)
- {
-
- //throw;
- }
- }
-
- }
-
-
-
- /// <summary>
- /// 业务系统属性上报
- /// </summary>
- public void SystemPropertyReport<T>(T model)
- {
- var mqdeviceTestProperty = new PostPropertyParams<T>();
- mqdeviceTestProperty.sys = new Sys { ack = 1 };
- mqdeviceTestProperty.method = "thing.event.property.post";
- mqdeviceTestProperty.version = "1.0";
- mqdeviceTestProperty.Model = model;
- mqdeviceTestProperty.Init();
- var message = JsonConvert.SerializeObject(mqdeviceTestProperty);
- var topic = $"/sys/{produtInfo.Get("ProductKey")}/Conveyer/thing/event/property/post";//发布主题
-
- var topicReply = $"/sys/{produtInfo.Get("ProductKey")}/Conveyer/thing/event/property/post_reply";//订阅主题
- if (mqttCollection.ContainsKey("Conveyer"))
- {
- try
- {
- mqttCollection["Conveyer"].Publish((op) =>
- {
- op.Message = message;
- op.Topic = topic;
- op.TopicReply = topicReply;
- //if (!PropertyReport)
- //{
- // op.ThresholdCallback += MqttPostProperty_MqttMsgPublishReceived; // MqttPostProperty_MqttMsgPublishReceived;
- // PropertyReport = true;
- //}
- });
- }
- catch (Exception)
- {
-
- //throw;
- }
- }
-
- }
-
- /// <summary>
- /// 取消配方
- /// </summary>
- /// <param name="RecipeName"></param>
- public async void CancelRecipe(string RecipeName)
- {
- if (RecipeName != null)
- {
- //var res = MessageNotify.GetInstance.ShowDialog($"是否取消配方 【{RecipeName}】制作", DialogType.Warning);
- //if (res)
- //{
- int index = Recipes.ToList().FindIndex(p => p.RecipeName == RecipeName);
- if (index >= 0 && index < Recipes.Count)
- {
- await Task.Factory.StartNew(new Action(() =>
- {
- Recipes.ElementAt(index).IsEnable = true;
- Json<LocalRecipe>.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");
- App.Current.Dispatcher.Invoke(() =>
- {
- NoticeDemoViewModel.OpenMsg(EnumPromptType.Success, App.MainWindow, "提示", $"配方 [{RecipeName}] 取消成功");
- });
- MessageNotify.GetInstance.ShowUserLog($"取消配方制作 {RecipeName}");
- //取消成功
- }));
- }
- else
- {
- //取消失败
- }
- //}
- }
- else
- {
- MessageNotify.GetInstance.ShowUserLog($"配方 {RecipeName}取消失败");
- }
-
- }
- /// <summary>
- /// 配方下发
- /// </summary>
- public void RecipeSend(string RecipeName)
- {
- if (RecipeName != null)
- {
- int index = Array.FindIndex(Recipes.ToArray(), p => p.RecipeName == RecipeName);
- if (index >= 0 && index < Recipes.Count)
- {
- for (int i = 0; i < Recipes.ElementAt(index).RawMaterials.Count; i++)
- {
- 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;
- }
- if (Recipes.ElementAt(index).RawMaterials.ElementAt(i).RawMaterialSource == 1)
- {
-
- 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;
- }
-
- }
-
-
- }
- Recipes.ElementAt(index).IsEnable = false;
- Json<LocalRecipe>.Data.Recipes.ElementAt(index).IsEnable = false;
- }
- MessageNotify.GetInstance.ShowUserLog($"下发工单 {Recipes.ElementAt(index).RecipeName}");
- RecipeNames.Enqueue(RecipeName);
- var res = Recipes.FirstOrDefault(p => p.RecipeName == RecipeName);
-
- App.Current.Dispatcher.Invoke(() =>
- {
- var raw = new ObservableCollection<RawMaterialModel>();
- res.RawMaterials.ToList().ForEach(X =>
- {
- raw.Add(new RawMaterialModel()
- {
- DeviceIp = X.DeviceIp,
- DownLimtFeedback = X.DownLimtFeedback,
- Loc= X.Loc,
- RawMaterialId = X.RawMaterialId,
- RawMaterialName = X.RawMaterialName,
- RawMaterialWeight = X.RawMaterialWeight,
- RecipeStatus = 1,
- TotalWeight = X.TotalWeight,
- Status = 0
-
- });
-
- });
- int count = RecipeNames.Count(p=>p == RecipeName);
- UserTreeWait.Add(new RecipeModel { RecipStatus = "等待制作", SerialNum = count, RecipeName = RecipeName, RawMaterials = raw });
- send = true;
- });
- }
-
-
- }
- public ConcurrentDictionary<string, int> doDeviceCount = new ConcurrentDictionary<string, int>();
-
- int dd = 0;
-
- public void ShutDownHandler()
- {
- TaskManage.GetInstance.StopTask("设备下发配方");
- TaskManage.GetInstance.StopTask("下发设备参数");
- TaskManage.GetInstance.StopTask("控制传送带");
- List<Task> tasks = new();
- tasks.Add(SimensSend.GetInstance.SendSimens.WriteAsync("M0.0", false));
- tasks.Add(SimensSend.GetInstance.SendSimens.WriteAsync("M0.1", false));
- /* foreach (var item in DeviceInquire.GetInstance.DeviceLists.Values)
- {
- tasks.Add( item.modbusTcp.WriteAsync<ushort>(DeviceAddress.TranspportSwitch.ToAdd(), 0));
- }
- Task.WaitAll(tasks.ToArray(),TimeSpan.FromSeconds(5));*/
- }
-
- public void ShutDown()
- {
- TaskManage.GetInstance.StopTask("设备下发配方");
- TaskManage.GetInstance.StopTask("配料机设备上线监听");
- TaskManage.GetInstance.StopTask("起始传送带控制");
- List<Task> tasks = new();
- tasks.Add(SimensSend.GetInstance.SendSimens.WriteAsync("M0.0", false));
- tasks.Add(SimensSend.GetInstance.SendSimens.WriteAsync("M0.1", false));
- tasks.Add(new Task(() =>
- {
- if (DeviceInquire.GetInstance.devices.Count==18)
- {
- Parallel.ForEach(DeviceInquire.GetInstance.devices, item =>
- {
- DeviceInquire.GetInstance.DeviceLists[item.IpAddress].ResetAll().Wait();
- });
- }
-
- }));
- Task.WaitAll(tasks.ToArray(), TimeSpan.FromSeconds(5));
- }
- bool send = false;
- public ConcurrentDictionary<string, bool> recipeSend = new ConcurrentDictionary<string, bool>();
-
- bool stop = true;
-
- bool start_Send = false;
-
- bool allowStart=false;
- public ConcurrentDictionary<string, bool> start_Stop = new ConcurrentDictionary<string, bool>();
- int pas = 1;
-
-
- private void MatchRun()
- {
-
- recipeProcesses.Clear();
- TaskManage.GetInstance.StartLong(() =>
- {
-
- if (RecipeNames.Count > 0 && SimensSend.GetInstance.IsConnect)
- {
-
- //下发设备参数
-
- //遍历每一个味魔方。
- for (int i = 0; i < DeviceInquire.GetInstance.devices.Count; i++)
- {
- //判断IP是否在列表中
- if (DeviceInquire.GetInstance.DeviceLists.ContainsKey(DeviceInquire.GetInstance.devices[i].IpAddress))
- {
- string ip = DeviceInquire.GetInstance.devices[i].IpAddress;
- if (!recipeSend.ContainsKey(ip))
- {
- recipeSend.TryAdd(ip, false);
- }
- if (doDeviceCount.ContainsKey(ip)&& recipeSend.ContainsKey(ip))
- {
- if (send)
- {
- recipeSend.ToList().ForEach(o =>
- {
- recipeSend.TryUpdate(o.Key, true, false);
- });
- send = false;
- }
- int index = 0;//配方索引
- int maxPail = 0;//最大桶数
- List<int> device_Loc = new List<int>();//下发桶号
- int recipeNameNum = doDeviceCount[ip];//配方数量
- int recipNameNum_1 = 0;//配方数量
- var dnum = DeviceInquire.GetInstance.DeviceLists[ip].deviceStatus.DeviceNum;//设备编号
- int passPail = DeviceInquire.GetInstance.DeviceLists[ip].deviceStatus.PassPail;//当前通过桶数
- int passPail_1 = -1;//下一料仓桶数
- string nextIp = "";
- if (dnum < 18)
- {
- nextIp = $"192.168.2.{(dnum + 1) * 10}";
- if (DeviceInquire.GetInstance.DeviceLists.ContainsKey(nextIp))
- {
- passPail_1 = DeviceInquire.GetInstance.DeviceLists[nextIp].deviceStatus.PassPail;
- recipNameNum_1 = doDeviceCount[nextIp];
- }
- }
- else
- {
- passPail_1 = passPail - 1;
- recipNameNum_1 = doDeviceCount["192.168.2.170"]-1;
- }
- if (recipeNameNum == 0)
- {
- recipeSend.TryUpdate(ip, false, true);
- index = Array.FindIndex(Recipes.ToArray(), p => p.RecipeName == RecipeNames.ElementAt(recipeNameNum));
- if (index >= 0 && dnum > 0)
- {
- foreach (var rawMaterial in Recipes.ElementAt(index).RawMaterials)
- {
- if (rawMaterial.Loc > maxPail)
- {
- maxPail = rawMaterial.Loc;
- }
- if (rawMaterial.DeviceIp == DeviceInquire.GetInstance.devices[i].IpAddress)
- {
- device_Loc.Add(rawMaterial.Loc);
- }
- }
- if (passPail == 0)
- {
- doDeviceCount.TryUpdate(ip, recipeNameNum + 1, doDeviceCount[ip]);
- foreach (var down_Loc in device_Loc)
- {
- SimensSend.GetInstance.SendSimens.Write<bool>($"DB1.DBX{710 + 4 * (dnum - 1) + (int)((down_Loc - 1) / 8)}.{(down_Loc - 1) % 8}", true);
-
- }
- SimensSend.GetInstance.SendSimens.Write<ushort>($"DB1.DBW{106 + 2 * (dnum - 1)}", 500);//电机速度
- MessageNotify.GetInstance.ShowRunLog($"设备{dnum}下发配方{RecipeNames.ElementAt(recipeNameNum)}");
- if (dnum == 1)
- {
- App.Current.Dispatcher.Invoke(() =>
- {
- if (recipeNameNum >= 0 && recipeNameNum < RecipeNames.Count)
- {
- RecipeModel recipe = UserTreeWait.ToList().Find(a => a.RecipeName == RecipeNames.ElementAt(recipeNameNum));
- if (recipe != null)
- {
- recipe.RecipStatus = "正在配料";
- recipeProcesses.Insert(0, recipe);
- ActionManage.GetInstance.Send("正在配料", recipeProcesses);
- /*Json<OnRecipe>.Data.Recipes.Insert(0, recipe);
- Json<OnRecipe>.Save();*/
- int removeObj = UserTreeWait.ToList().FindIndex(a => a.RecipeName == RecipeNames.ElementAt(recipeNameNum));
- if (removeObj >= 0 && removeObj < UserTreeWait.Count())
- {
- UserTreeWait.RemoveAt(removeObj);
- }
- }
- }
-
- });
- }
- }
- }
- }
- bool pailAv = DeviceInquire.GetInstance.DeviceLists[ip].deviceStatus.PailArrive;
- if (recipeNameNum > 0 && recipeNameNum <= RecipeNames.Count&&!pailAv)
- {
- index = Array.FindIndex(Recipes.ToArray(), p => p.RecipeName == RecipeNames.ElementAt(recipeNameNum - 1));
-
- if (index >= 0 && dnum > 0)
- {
- foreach (var rawMaterial in Recipes.ElementAt(index).RawMaterials)
- {
- if (rawMaterial.Loc > maxPail)
- {
- maxPail = rawMaterial.Loc;
- }
- }
- int recipecount = recipeProcesses.Count - (recipeNameNum - UserTreeCompelete.Count);
- int re = 0;
- /*if (recipeNameNum - (UserTreeCompelete.Count + recipeProcesses.Count) == 0)
- {
- re = -1;
- recipNameNum_1 = recipeNameNum;
- }*/
-
- if (recipecount < recipeProcesses.Count && recipecount >= 0)
- {
- re = recipeProcesses.ElementAt(recipecount).RawMaterials.ToList().FindIndex(a => a.RawMaterialName == DeviceInquire.GetInstance.DeviceLists[ip].DeviceName && a.Loc == DeviceInquire.GetInstance.DeviceLists[ip].deviceStatus.PassPail && a.RecipeStatus != 3);
- }
- else
- {
- re = -1;
- recipNameNum_1 = recipeNameNum;
- }
- if (recipeSend[ip])
- {
- if (re<0)
- {
- re = -1;
- recipNameNum_1 = recipeNameNum;
- }
- }
- if (dnum == 18 && maxPail <= passPail)
- {
- App.Current.Dispatcher.Invoke(() =>
- {
- if (recipecount < recipeProcesses.Count && recipecount >= 0)
- {
- RecipeModel recipe = recipeProcesses.ElementAt(recipecount);
- if (recipe != null)
- {
- int res = recipe.RawMaterials.ToList().FindIndex(a => a.RecipeStatus != 3);
- if (res < 0)
- {
- recipe.RecipStatus = "制作完成";
- UserTreeCompelete.Insert(0, recipe);
- recipeProcesses.RemoveAt(recipecount);
- Json<OnRecipe>.Data.Recipes = recipeProcesses;
- Json<OnRecipe>.Save();
- Json<OldRecipe>.Data.Recipes.Insert(0, recipe);
- Json<OldRecipe>.Save();
- ActionManage.GetInstance.Send("配料完成", UserTreeCompelete);
- ActionManage.GetInstance.Send("历史记录", Json<OldRecipe>.Data.Recipes);
- }
- }
- }
-
-
- });
- }
-
- if (maxPail <= passPail && recipeNameNum < RecipeNames.Count &&re<0&& recipNameNum_1==recipeNameNum)
- {
- SimensSend.GetInstance.SendSimens.Write<ushort>($"DB1.DBW{810 + 2 * (dnum - 1)}", 0);
- MessageNotify.GetInstance.ShowRunLog($"设备{dnum}桶数已清零");
- recipeSend.TryUpdate(ip, false, true);
- doDeviceCount.TryUpdate(ip, recipeNameNum + 1, doDeviceCount[ip]);
- recipeNameNum = doDeviceCount[ip];
- index = Array.FindIndex(Recipes.ToArray(), p => p.RecipeName == RecipeNames.ElementAt(recipeNameNum - 1));
- if (index >= 0)
- {
- foreach (var rawMaterial in Recipes.ElementAt(index).RawMaterials)
- {
- if (rawMaterial.DeviceIp == DeviceInquire.GetInstance.devices[i].IpAddress)
- {
- device_Loc.Add(rawMaterial.Loc);
- }
- }
- }
- foreach (var down_Loc in device_Loc)
- {
- SimensSend.GetInstance.SendSimens.Write<bool>($"DB1.DBX{710 + 4 * (dnum - 1) + (int)((down_Loc - 1) / 8)}.{(down_Loc - 1) % 8}", true);
- }
- MessageNotify.GetInstance.ShowRunLog($"设备{dnum}下发配方桶下发成功");
- SimensSend.GetInstance.SendSimens.Write<ushort>($"DB1.DBW{106 + 2 * (dnum - 1)}", 500);
- if (dnum == 1)
- {
- App.Current.Dispatcher.Invoke(() =>
- {
- RecipeModel recipe = UserTreeWait.ElementAt(0);
- if (recipe != null)
- {
- recipe.RecipStatus = "正在配料";
- recipeProcesses.Insert(0, recipe);
- ActionManage.GetInstance.Send("正在配料", recipeProcesses);
- /* Json<OnRecipe>.Data.Recipes.Insert(0, recipe);
- Json<OnRecipe>.Save();*/
- UserTreeWait.RemoveAt(0);
-
- }
-
- });
- }
- }
- }
- }
- if (DeviceInquire.GetInstance.DeviceLists[ip].deviceStatus.PailArrive&& recipeNameNum - 1< RecipeNames.Count)
- {
- if (recipeNameNum > 0)
- {
- int recipecount = recipeProcesses.Count - (recipeNameNum - UserTreeCompelete.Count);
- #region 下料
- int doCount = Array.FindIndex(Recipes.ToArray(), p => p.RecipeName == RecipeNames.ElementAt(recipeNameNum - 1));
- if (doCount >= 0)
- {
- int re = 0;
- if (recipecount < recipeProcesses.Count && recipecount >= 0)
- {
- re = recipeProcesses.ElementAt(recipecount).RawMaterials.ToList().FindIndex(a => a.RawMaterialName == DeviceInquire.GetInstance.DeviceLists[ip].DeviceName && a.Loc == DeviceInquire.GetInstance.DeviceLists[ip].deviceStatus.PassPail && a.RecipeStatus == 1);
- if (re>=0)
- {
- float weight= recipeProcesses.ElementAt(recipecount).RawMaterials.ElementAt(re).RawMaterialWeight;
- DeviceInquire.GetInstance.DeviceLists[ip].UpdateState(weight);
- recipeProcesses.ElementAt(recipecount).RawMaterials.ElementAt(re).RecipeStatus = 2;
- recipeProcesses.ElementAt(recipecount).RawMaterials.ElementAt(re).Status = (Status)1;
- MessageNotify.GetInstance.ShowRunLog($"设备{dnum}配料状态:正在配料");
- }
-
- }
-
- }
-
- #endregion
- #region 正在配料记重
- if (recipecount < recipeProcesses.Count && recipecount >= 0&& DeviceInquire.GetInstance.DeviceLists[ip].deviceStatus.RunStatus==2)
- {
- int re_1 = recipeProcesses.ElementAt(recipecount).RawMaterials.ToList().FindIndex(a => a.RawMaterialName == DeviceInquire.GetInstance.DeviceLists[ip].DeviceName && a.Loc == DeviceInquire.GetInstance.DeviceLists[ip].deviceStatus.PassPail && a.RecipeStatus == 2);
- if (re_1>=0)
- {
- recipeProcesses.ElementAt(recipecount).RawMaterials.ElementAt(re_1).TotalWeight = DeviceInquire.GetInstance.DeviceLists[ip].deviceStatus.RealWeightFeedback;
- }
- }
- #endregion
-
- #region 完成计重
- if (DeviceInquire.GetInstance.DeviceLists[ip].deviceStatus.Finish_mt && passPail_1 == passPail - 1)
- {
- if (recipecount < recipeProcesses.Count && recipecount >= 0)
- {
- int re_1 = recipeProcesses.ElementAt(recipecount).RawMaterials.ToList().FindIndex(a => a.RawMaterialName == DeviceInquire.GetInstance.DeviceLists[ip].DeviceName && a.Loc == DeviceInquire.GetInstance.DeviceLists[ip].deviceStatus.PassPail && a.RecipeStatus == 2);
- if (re_1 >= 0)
- {
- Thread.Sleep(50);
- float xx = DeviceInquire.GetInstance.DeviceLists[ip].deviceStatus.CutWeightFeedback;
- recipeProcesses.ElementAt(recipecount).RawMaterials.ElementAt(re_1).TotalWeight = xx;
- float t = Math.Abs(recipeProcesses.ElementAt(recipecount).RawMaterials.ElementAt(re_1).TotalWeight - recipeProcesses.ElementAt(recipecount).RawMaterials.ElementAt(re_1).RawMaterialWeight);
- if (t > Json<DevicePar>.Data.BaseParModel.Limit)
- {
- SimensSend.GetInstance.SendSimens.Write<bool>("M11.2", true);
- while (!MessageNotify.GetInstance.ShowDialog($"设备{dnum}物料下料超出范围报警,请处理后点击确认!", DialogType.Warning))
- {
- App.Current.Dispatcher.Invoke(() => { NoticeDemoViewModel.OpenMsg(EnumPromptType.Success, App.MainWindow, "提示", $"需确认处理后才能继续运行!"); });
- }
- SimensSend.GetInstance.SendSimens.Write<bool>("M11.2", false);
- }
- recipeProcesses.ElementAt(recipecount).RawMaterials.ElementAt(re_1).RecipeStatus = 3;
- recipeProcesses.ElementAt(recipecount).RawMaterials.ElementAt(re_1).Status = (Status)2;
- MessageNotify.GetInstance.ShowRunLog($"设备{dnum}配料状态:完成配料,物料状态:{recipeProcesses.ElementAt(recipecount).RawMaterials.ElementAt(re_1).Status}");
- Json<OnRecipe>.Data.Recipes = recipeProcesses;
- Json<OnRecipe>.Save();
-
- DeviceInquire.GetInstance.DeviceLists[ip].Reset();
-
-
- ActionManage.GetInstance.Send("正在配料", recipeProcesses);
- MessageNotify.GetInstance.ShowRunLog($"设备{dnum}已下料{recipeProcesses.ElementAt(recipecount).RawMaterials.ElementAt(re_1).TotalWeight}g");
-
-
- }
- }
- }
-
-
- #endregion
- }
-
-
-
- }
- }
- else
- {
- doDeviceCount.TryAdd(DeviceInquire.GetInstance.devices[i].IpAddress, 0);
- }
-
-
- }
-
-
- }
-
- int rep = doDeviceCount.Count(a => a.Value == 1);
-
- if ( rep== DeviceInquire.GetInstance.devices.Count&&!allowStart)
- {
- App.Current.Dispatcher.Invoke(() => { NoticeDemoViewModel.OpenMsg(EnumPromptType.Success, App.MainWindow, "提示", $"可以开始放入配料桶!"); });
- allowStart = true;
- }
- }
-
- Thread.Sleep(10);
- }, "设备下发配方");
- MessageNotify.GetInstance.ShowRunLog($"设备下发配方运行");
-
- TaskManage.GetInstance.StartLong(() =>
- {
- string ip = "192.168.2.10";
- if (DeviceInquire.GetInstance.DeviceLists.ContainsKey(ip) && doDeviceCount.ContainsKey(ip)&& RecipeNames.Count>0&& doDeviceCount[ip] >0)
- {
-
- int maxPail = 0;
- int index = Array.FindIndex(Recipes.ToArray(), p => p.RecipeName == RecipeNames.ElementAt(doDeviceCount[ip] - 1));
- if (index >= 0)
- {
- foreach (var rawMaterial in Recipes.ElementAt(index).RawMaterials)
- {
- if (rawMaterial.Loc > maxPail)
- {
- maxPail = rawMaterial.Loc;
- }
- }
- int passing = DeviceInquire.GetInstance.DeviceLists[ip].deviceStatus.PassPail;
- if (pas <= maxPail && passing == pas - 1)
- {
- if (!start_Send)
- {
- SimensSend.GetInstance.SendSimens.Write<bool>("M11.5", true);
- MessageNotify.GetInstance.ShowRunLog($"状态:配方续桶");
- start_Send = true;
- }
-
- }
-
- if (pas <= maxPail && passing == pas)
- {
- if (start_Send)
- {
- SimensSend.GetInstance.SendSimens.Write<bool>("M11.5", false);
- MessageNotify.GetInstance.ShowRunLog($"状态:配方停桶");
- start_Send = false;
- pas++;
- }
-
- }
-
- if (pas > maxPail)
- {
- pas = 1;
- }
- }
-
-
-
- }
- Thread.Sleep(100);
- }, "起始传送带控制");
-
- }
-
- }
- }
|