|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328 |
- using BPASmartClient.CustomResource.Pages.Model;
- using BPASmartClient.Helper;
- using BPASmartClient.DosingHKProject.Model.HK_PLC;
- using BPASmartClient.DosingHKProject.Model.Siemens;
- using BPASmartClient.Modbus;
- using System;
- using System.Collections.Concurrent;
- using System.Collections.Generic;
- using System.Collections.ObjectModel;
- using System.Configuration;
- using System.Linq;
- using System.Text;
- using System.Threading;
- using System.Threading.Tasks;
- using BPASmartClient.DosingProject;
-
- namespace BPASmartClient.DosingHKProject.Model
- {
- public class ProcessControl
- {
- private volatile static ProcessControl _Instance;
- public static ProcessControl GetInstance => _Instance ?? (_Instance = new ProcessControl());
- private ProcessControl() { }
-
- /// <summary>
- /// 配方数据
- /// </summary>
- public ObservableCollection<RemoteRecipeData> RemoteRecipes = new ObservableCollection<RemoteRecipeData>();
- /// <summary>
- /// 原料的名称和料仓的位置对应
- /// </summary>
- public Dictionary<string, short> RawMaterialsNamePos = new Dictionary<string, short>();
- /// <summary>
- /// 配方队列
- /// </summary>
- public ConcurrentQueue<string> RecipeQueue = new ConcurrentQueue<string>();
- public ConcurrentQueue<string> RecipeQueueTray2 = new ConcurrentQueue<string>();
-
- public HKDeviceStatus HKDevice = new HKDeviceStatus();
- public void Init()
- {
- for (int i = 0; i < 16; i++)
- {
- if (DeviceInquire.GetInstance.GetDevice(i).DeviceName != null)
- {
- if (!RawMaterialsNamePos.ContainsKey(DeviceInquire.GetInstance.GetDevice(i).DeviceName))
- {
- RawMaterialsNamePos.Add(DeviceInquire.GetInstance.GetDevice(i).DeviceName, (short)DeviceInquire.GetInstance.GetDevice(i).deviceStatus.DeviceNum);
- }
- }
- }
- string HK_PLC_IP = ConfigurationManager.AppSettings["HKPlc_IP"];
- try
- {
- //HKDevice.HK_PLC_S7.Connect(S7.Net.CpuType.S71200, HK_PLC_IP);
- if (HKDevice.IsConnected)
- {
- HKDevice.Init();
- MessageLog.GetInstance.ShowUserLog("海科plc连接成功,并初始化完成");
- }
- }
- catch(Exception ex)
- {
-
- }
- RecipeQueue.Clear();
- ThreadManage.GetInstance().StartLong(new Action(() =>
- {
- ReceviceData();
- RecipeInfoToHKPLC();
- Thread.Sleep(10);
- }), "流程处理", true);
- ThreadManage.GetInstance().StartLong(new Action(() =>
- {
- if (HKDevice.IsConnected)
- {
- GetStatus();
- ManualOpen();
- ManualClose();
- }
- Thread.Sleep(10);
- }), "手动操作", true);
- }
- private void GetStatus()
- {
- for (int i = 0; i < 8; i++)
- {
- HKDevice.HK_PLC_S7.Read<bool>("DB5.DBX0." + i);
- }
- for (int i = 0; i < 8; i++)
- {
- HKDevice.HK_PLC_S7.Read<bool>("DB5.DBX1." + i);
- }
- }
- private void ManualOpen()
- {
- ActionManage.GetInstance.Register(new Action<object>((o) =>
- {
- if (o != null)
- {
- if (o.ToString().Contains("升降气缸"))
- {
- int index = Convert.ToInt16(o.ToString().Substring(o.ToString().Length - 1));
- switch (index)
- {
- case 1:
- HKDevice.HK_PLC_S7.Write<bool>("DB5.DBX0.0", true);
- break;
- case 2:
- HKDevice.HK_PLC_S7.Write<bool>("DB5.DBX0.1", true);
- break;
- case 3:
- HKDevice.HK_PLC_S7.Write<bool>("DB5.DBX0.2", true);
- break;
- case 4:
- HKDevice.HK_PLC_S7.Write<bool>("DB5.DBX0.3", true);
- break;
- case 5:
- HKDevice.HK_PLC_S7.Write<bool>("DB5.DBX0.4", true);
- break;
- case 6:
- HKDevice.HK_PLC_S7.Write<bool>("DB5.DBX0.5", true);
- break;
- default:
- break;
-
- }
- }
- }
- }), "ManualOpen", true);//根据下发的配方ID将 托盘的位置信息添加到配方中
- }
- private void ManualClose()
- {
- ActionManage.GetInstance.Register(new Action<object>((o) =>
- {
- if (o != null)
- {
- if (o.ToString().Contains("升降气缸"))
- {
- int index = Convert.ToInt16(o.ToString().Substring(o.ToString().Length - 1));
- switch (index)
- {
- case 1:
- HKDevice.HK_PLC_S7.Write("DB5.DBX0.0", false);
- break;
- case 2:
- HKDevice.HK_PLC_S7.Write("DB5.DBX0.1", false);
- break;
- case 3:
- HKDevice.HK_PLC_S7.Write("DB5.DBX0.2", false);
- break;
- case 4:
- HKDevice.HK_PLC_S7.Write("DB5.DBX0.3", false);
- break;
- case 5:
- HKDevice.HK_PLC_S7.Write("DB5.DBX0.4", false);
- break;
- case 6:
- HKDevice.HK_PLC_S7.Write("DB5.DBX0.5", false);
- break;
- default:
- break;
-
- }
- }
- }
- }), "ManualClose", true);//根据下发的配方ID将 托盘的位置信息添加到配方中
- }
- /// <summary>
- /// 将配方添加到配方队列中
- /// </summary>
- private void ReceviceData()
- {
- RemoteRecipes = Json<RemoteRecipeDataColl>.Data.Recipes;
- if (RemoteRecipes.Count > 0)
- {
- foreach (var data in RemoteRecipes)
- {
- if (!(RecipeQueue.Contains(data.RecipeCode)))
- {
- RecipeQueue.Enqueue(data.RecipeCode);
- }
- }
- }
- else
- {
- RecipeQueue.Clear();
- GVL_SmallStation.GetInstance.RecipeStatusID = 0;
- }
- }
- /// <summary>
- /// 执行配方队列中的第一个配方
- /// </summary>
- private void RecipeInfoToHKPLC()
- {
- if (RecipeQueue.Count > 0)
- {
- int index = Array.FindIndex(RemoteRecipes.ToArray(), p => p.RecipeCode == RecipeQueue.ElementAt(0));
- if (index >= 0 && index < RemoteRecipes.Count)
- {
- string code = RemoteRecipes.ElementAt(index).RecipeCode;
- int trayCode = RemoteRecipes.ElementAt(index).TrayCode;
- string recipeName = RemoteRecipes.ElementAt(index).RecipeName;
- string windSend = RemoteRecipes.ElementAt(index).ToString();
- if (GVL_SmallStation.GetInstance.RecipeStatusID == 0)
- {
- HKDevice.HK_PLC_S7.Write("DB4.DBX1.3", true);
- GVL_SmallStation.GetInstance.RecipeStatusID = 1;
- MessageLog.GetInstance.ShowRunLog($"托盘1,配方:{recipeName},下发完成");
- HKDevice.HK_PLC_S7.Write("DB3.DBX1.3", true);
- }
- if (HKDevice.HK_PLC_S7.Read<bool>("DB3.DBX1.3") && GVL_SmallStation.GetInstance.RecipeStatusID == 1)
- {
- HKDevice.HK_PLC_S7.Write("DB3.DBX1.3", false);
- GVL_SmallStation.GetInstance.RecipeStatusID = 2;
- MessageLog.GetInstance.ShowRunLog($"托盘1,{ recipeName}plc端 配方接收完成");
- }
- if (GVL_SmallStation.GetInstance.RecipeStatusID == 2)
- {
- if (RTrig.GetInstance("DB3.DBX50.0").Start(HKDevice.HK_PLC_S7.Read<bool>("DB3.DBX50.0")))
- {
- var res = HKDevice.HK_PLC_S7.Read<float>("DB3.DBD10");
- MessageLog.GetInstance.ShowRunLog($"托盘1,配方:{recipeName},1号桶,{res}料仓,允许配料");
- if (res > 0 && res is float loc)
- {
- //int decimalNum = Convert.ToInt32(loc.ToString().Substring(loc.ToString().IndexOf(".") + 1));
- int loc_index = Array.FindIndex(RemoteRecipes.ElementAt(index).RawMaterial.ToArray(), p => p.RawMaterialLocation == loc);
- float weight = RemoteRecipes.ElementAt(index).RawMaterial.ElementAt(loc_index).RawMaterialWeight;
- if (loc_index >= 0)
- {
- DeviceInquire.GetInstance.GetDevice((int)loc)?.Start(weight);//根据料仓编号 启动并写入每个原料重量
- GVL_SmallStation.GetInstance.StockInIsWork = (int)loc;
- }
- GVL_SmallStation.GetInstance.DosingTray1 = true;
- MessageLog.GetInstance.ShowRunLog($"托盘1,配方:{recipeName},1号桶,{(int)loc}号仓,配料完成");
- }
- }
- else if(RTrig.GetInstance("DB3.DBX50.1").Start(HKDevice.HK_PLC_S7.Read<bool>("DB3.DBX50.1")))
- {
- var res = HKDevice.HK_PLC_S7.Read<float>("DB3.DBD14");
- MessageLog.GetInstance.ShowRunLog($"托盘1,配方:{recipeName},2号桶,{res}料仓,允许配料");
- if (res > 0 && res is float loc)
- {
- int loc_index = Array.FindIndex(RemoteRecipes.ElementAt(index).RawMaterial.ToArray(), p => p.RawMaterialLocation == loc);
- float weight = RemoteRecipes.ElementAt(index).RawMaterial.ElementAt(loc_index).RawMaterialWeight;
- if (loc_index >= 0)
- {
- DeviceInquire.GetInstance.GetDevice((int)loc)?.Start(weight);//启动并写入每个原料重量
- GVL_SmallStation.GetInstance.StockInIsWork = (int)loc;
- }
- GVL_SmallStation.GetInstance.DosingTray1 = true;
- MessageLog.GetInstance.ShowRunLog($"托盘1,配方:{recipeName},2号桶,{(int)loc}号仓,配料完成");
- }
- }
- else if (RTrig.GetInstance("DB3.DBX50.2").Start(HKDevice.HK_PLC_S7.Read<bool>("DB3.DBX50.2")))
- {
- var res = HKDevice.HK_PLC_S7.Read<float>("DB3.DBD18");
- MessageLog.GetInstance.ShowRunLog($"托盘1,配方:{recipeName},3号桶,{res}料仓,允许配料");
- if (res > 0 && res is float loc)
- {
- int loc_index = Array.FindIndex(RemoteRecipes.ElementAt(index).RawMaterial.ToArray(), p => p.RawMaterialLocation == loc);
- float weight = RemoteRecipes.ElementAt(index).RawMaterial.ElementAt(loc_index).RawMaterialWeight;
- if (loc_index >= 0)
- {
- DeviceInquire.GetInstance.GetDevice((int)loc)?.Start(weight);//启动并写入每个原料重量
- GVL_SmallStation.GetInstance.StockInIsWork = (int)loc;
- }
- GVL_SmallStation.GetInstance.DosingTray1 = true;
- MessageLog.GetInstance.ShowRunLog($"托盘1,配方:{recipeName},3号桶,{(int)loc}号仓,配料完成");
- }
- }
- else if (RTrig.GetInstance("DB3.DBX50.3").Start(HKDevice.HK_PLC_S7.Read<bool>("DB3.DBX50.3")))
- {
- var res = HKDevice.HK_PLC_S7.Read<float>("DB3.DBD22");
- MessageLog.GetInstance.ShowRunLog($"托盘1,配方:{recipeName},4号桶,{res}料仓,允许配料");
- if (res > 0 && res is float loc)
- {
- int loc_index = Array.FindIndex(RemoteRecipes.ElementAt(index).RawMaterial.ToArray(), p => p.RawMaterialLocation == loc);
- float weight = RemoteRecipes.ElementAt(index).RawMaterial.ElementAt(loc_index).RawMaterialWeight;
- if (loc_index >= 0)
- {
- DeviceInquire.GetInstance.GetDevice((int)loc)?.Start(weight);//启动并写入每个原料重量
- GVL_SmallStation.GetInstance.StockInIsWork = (int)loc;
- }
- GVL_SmallStation.GetInstance.DosingTray1 = true;
- MessageLog.GetInstance.ShowRunLog($"托盘1,配方:{recipeName},4号桶,{(int)loc}号仓,配料完成");
- }
- }
- if (GVL_SmallStation.GetInstance.DosingTray1)
- {
- for (int i = 1; i < 16; i++)
- {
- if (RTrig.GetInstance("GetDeviceRunStatus").Start(DeviceInquire.GetInstance.GetDevice(i).deviceStatus.RunStatus == 3))
- {
- MessageLog.GetInstance.ShowRunLog($"柔性味魔方,托盘1,配方:{recipeName},{i}号仓,配料完成");
- int res = Array.FindIndex(RemoteRecipes.ElementAt(index).RawMaterial.ToArray(), p => p.RawMaterialLocation == i);
- RemoteRecipes.ElementAt(index).RawMaterial.ElementAt(res).Laying_Off_Weight = DeviceInquire.GetInstance.GetDevice(i).deviceStatus.CutWeightFeedback;
- DeviceInquire.GetInstance.GetDevice(i).StatusReset();
- if (i >= 1 && i <= 8)
- {
- HKDevice.HK_PLC_S7.Write("DB4.DBX30." + (i - 1), true);
- }
- else if (i >= 9 && i <= 15)
- {
- HKDevice.HK_PLC_S7.Write("DB4.DBX31." + (i - 9), true);
- }
- GVL_SmallStation.GetInstance.DosingTray1 = false;
- }
- }
- }
- if (RTrig.GetInstance("配方配料完成").Start(HKDevice.HK_PLC_S7.Read<bool>("DB3.DBX1.1")) && (GVL_SmallStation.GetInstance.WindSendDosingFinish || !GVL_SmallStation.GetInstance.IsUseWindSend))
- {
- var res = Json<RemoteRecipeDataColl>.Data.Recipes.FirstOrDefault(p => p.RecipeCode == code);
- MessageLog.GetInstance.ShowRunLog($"托盘1,配方{res.RecipeName},配料完成");
- App.Current.Dispatcher.Invoke(() =>
- {
- Json<RemoteRecipeDataColl>.Data.Recipes.Remove(res);
- });
- RecipeQueue.TryDequeue(out code);
- HKDevice.HK_PLC_S7.Write("DB3.DBX1.1", false);
- GVL_SmallStation.GetInstance.RecipeStatusID = 0;
- }
- }
- }
- }
- }
- }
- }
|