|
- using BPASmartClient.Helper;
- using BPASmartClient.JXJFoodBigStation.Model.HK_PLC;
- using BPASmartClient.JXJFoodBigStation.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.Tasks;
-
- namespace BPASmartClient.JXJFoodBigStation.Model
- {
- public class ProcessControl
- {
- private volatile static ProcessControl _Instance;
- public static ProcessControl GetInstance => _Instance ?? (_Instance = new ProcessControl());
- private ProcessControl() { }
-
- public ObservableCollection<RemoteRecipeData> RemoteRecipes;
- ConcurrentQueue<long> RecipeQueue = new ConcurrentQueue<long>();
- ConcurrentQueue<string> DeviceQueue = new ConcurrentQueue<string>();
- public ObservableCollection<RemoteRecipeRawMaterial> RawMaterials { get; set; } = new ObservableCollection<RemoteRecipeRawMaterial>();
- public SiemensDeviceStatus SiemensDevice = new SiemensDeviceStatus();
- public HKDeviceStatus HKDevice = new HKDeviceStatus();
- public void Init()
- {
- string HK_PLC_IP = ConfigurationManager.AppSettings["HKPlc_IP"];
- string HK_PLC_Port = ConfigurationManager.AppSettings["HKPlc_Port"];
- string Siemens_PLC_IP = ConfigurationManager.AppSettings["Siemens_IP"];
- string Siemens_PLC_Port = ConfigurationManager.AppSettings["Siemens_Port"];
- Task.Run(new Action(() =>
- {
- HKDevice.ModBusHK.ModbusTcpConnect(HK_PLC_IP, int.Parse(HK_PLC_Port));
- SiemensDevice.SiemensTcp.ModbusTcpConnect(Siemens_PLC_IP, int.Parse(Siemens_PLC_Port));
- if (HKDevice.IsConnected)
- {
- HKDevice.Init();
- }
- if (SiemensDevice.IsConnected)
- {
- SiemensDevice.Init();
- }
- }));
- ActionManage.GetInstance.Register(new Action(()=>
- {
-
- }), "给流水线下发配方数据", true);
- ActionManage.GetInstance.Register(new Action(() =>
- {
- if (SiemensDevice.IsConnected)
- {
- var res = SiemensDevice.SiemensTcp.GetString(SiemensCommAddress.RecipeName, (ushort)20);
- var res1 = SiemensDevice.SiemensTcp.Read(SiemensCommAddress.RecipeID);
- var res2 = SiemensDevice.SiemensTcp.Read(SiemensCommAddress.RawMaterial);
- if ((res != null && res is string recipeName) &&
- (res1 != null && res1 is uint recipeID) &&
- (res2 != null && res2 is ushort[] rawMaterial))
- {
- RawMaterials.Clear();
- RawMaterials.Add(new RemoteRecipeRawMaterial()
- {
- RawMaterialBarrelNum = rawMaterial[0],
- RawMaterialLocation= rawMaterial[1],
- RawMaterialWeight= rawMaterial[2]
- });
- Json<RemoteRecipeDataColl>.Data.Recipes.Add(new RemoteRecipeData()
- {
- RecipeName = recipeName,
- RecipeCode = recipeID,
- RawMaterial = RawMaterials
- });
- }
- }
- }), "西门子下发配方", true);
- ActionManage.GetInstance.Register(new Action(() =>
- {
- if (SiemensDevice.IsConnected)
- {
- var res = SiemensDevice.SiemensTcp.Read(SiemensCommAddress.TrayLocationNum);
- var res1 = SiemensDevice.SiemensTcp.Read(SiemensCommAddress.RecipeID);
- if (res != null && res is int TrayLocation && res1 != null && res1 is int recipeId)
- {
- int index = Array.FindIndex(Json<RemoteRecipeDataColl>.Data.Recipes.ToArray(), p => p.RecipeCode == recipeId);
- if (index >= 0 && index < Json<RemoteRecipeDataColl>.Data.Recipes.Count)
- {
- Json<RemoteRecipeDataColl>.Data.Recipes.ElementAt(index).TrayCode = TrayLocation;
- }
- }
- }
- }), "AGV到位信号", true);//根据下发的配方ID将 托盘的位置信息添加到配方中
-
- RecipeQueue.Clear();
- ReceviceData();
- ThreadManage.GetInstance().StartLong(new Action(() =>
- {
- RecipeInfoToHKPLC();
- }), "流程处理", true);
- }
-
- private void ReceviceData()
- {
- RemoteRecipes = Json<RemoteRecipeDataColl>.Data.Recipes;
- RawMaterials = TestData.GetInstance.RawMaterials;//添加测试数据
- if (Json<RemoteRecipeDataColl>.Data.Recipes.Count > 0)
- {
- foreach (var data in Json<RemoteRecipeDataColl>.Data.Recipes)
- {
- if(!(RecipeQueue.Contains(data.RecipeCode)))
- RecipeQueue.Enqueue(data.RecipeCode);
- }
- }
- }
- /// <summary>
- /// 当前料仓的位置
- /// </summary>
- private int StockInIsWork;
- /// <summary>
- /// 单个配方执行完成标志
- /// </summary>
- private bool RecipeFinfish;
- 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)
- {
- long code = RemoteRecipes.ElementAt(index).RecipeCode;
- if (RTrig.GetInstance("IsAllowDosing").Start(HKDevice.ModBusHK.Read(HKPlcCommAddress.IsAllowDosing) is bool a))
- {
- foreach (var item in RemoteRecipes.ElementAt(index).RawMaterial)
- {
- HKDevice.StockBinPar((uint)item.RawMaterialBarrelNum, (ushort)item.RawMaterialLocation);
- }
- HKDevice.AllowDosingSignReset();
-
- }
-
- if (RTrig.GetInstance("StockState").Start(HKDevice.ModBusHK.Read(HKPlcCommAddress.RecipeDosingFinish) is bool))
- {
- HKDevice.RecipeDosingFinishReset();
- RecipeQueue.TryDequeue(out code);
- }
- }
- }
- }
- private void HKPLCIsWork()
- {
- }
- }
- }
|