终端一体化运控平台
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ProcessControl.cs 17 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. using BPASmartClient.CustomResource.Pages.Model;
  2. using BPASmartClient.Helper;
  3. using BPASmartClient.DosingHKProject.Model.HK_PLC;
  4. using BPASmartClient.DosingHKProject.Model.Siemens;
  5. using BPASmartClient.Modbus;
  6. using System;
  7. using System.Collections.Concurrent;
  8. using System.Collections.Generic;
  9. using System.Collections.ObjectModel;
  10. using System.Configuration;
  11. using System.Linq;
  12. using System.Text;
  13. using System.Threading;
  14. using System.Threading.Tasks;
  15. using BPASmartClient.DosingProject;
  16. namespace BPASmartClient.DosingHKProject.Model
  17. {
  18. public class ProcessControl
  19. {
  20. private volatile static ProcessControl _Instance;
  21. public static ProcessControl GetInstance => _Instance ?? (_Instance = new ProcessControl());
  22. private ProcessControl() { }
  23. /// <summary>
  24. /// 配方数据
  25. /// </summary>
  26. public ObservableCollection<RemoteRecipeData> RemoteRecipes = new ObservableCollection<RemoteRecipeData>();
  27. /// <summary>
  28. /// 原料的名称和料仓的位置对应
  29. /// </summary>
  30. public Dictionary<string, short> RawMaterialsNamePos = new Dictionary<string, short>();
  31. /// <summary>
  32. /// 配方队列
  33. /// </summary>
  34. public ConcurrentQueue<string> RecipeQueue = new ConcurrentQueue<string>();
  35. public ConcurrentQueue<string> RecipeQueueTray2 = new ConcurrentQueue<string>();
  36. public HKDeviceStatus HKDevice = new HKDeviceStatus();
  37. public void Init()
  38. {
  39. for (int i = 0; i < 16; i++)
  40. {
  41. if (DeviceInquire.GetInstance.GetDevice(i).DeviceName != null)
  42. {
  43. if (!RawMaterialsNamePos.ContainsKey(DeviceInquire.GetInstance.GetDevice(i).DeviceName))
  44. {
  45. RawMaterialsNamePos.Add(DeviceInquire.GetInstance.GetDevice(i).DeviceName, (short)DeviceInquire.GetInstance.GetDevice(i).deviceStatus.DeviceNum);
  46. }
  47. }
  48. }
  49. string HK_PLC_IP = ConfigurationManager.AppSettings["HKPlc_IP"];
  50. try
  51. {
  52. //HKDevice.HK_PLC_S7.Connect(S7.Net.CpuType.S71200, HK_PLC_IP);
  53. if (HKDevice.IsConnected)
  54. {
  55. HKDevice.Init();
  56. MessageLog.GetInstance.ShowUserLog("海科plc连接成功,并初始化完成");
  57. }
  58. }
  59. catch(Exception ex)
  60. {
  61. }
  62. RecipeQueue.Clear();
  63. ThreadManage.GetInstance().StartLong(new Action(() =>
  64. {
  65. ReceviceData();
  66. RecipeInfoToHKPLC();
  67. Thread.Sleep(10);
  68. }), "流程处理", true);
  69. ThreadManage.GetInstance().StartLong(new Action(() =>
  70. {
  71. if (HKDevice.IsConnected)
  72. {
  73. GetStatus();
  74. ManualOpen();
  75. ManualClose();
  76. }
  77. Thread.Sleep(10);
  78. }), "手动操作", true);
  79. }
  80. private void GetStatus()
  81. {
  82. for (int i = 0; i < 8; i++)
  83. {
  84. HKDevice.HK_PLC_S7.Read<bool>("DB5.DBX0." + i);
  85. }
  86. for (int i = 0; i < 8; i++)
  87. {
  88. HKDevice.HK_PLC_S7.Read<bool>("DB5.DBX1." + i);
  89. }
  90. }
  91. private void ManualOpen()
  92. {
  93. ActionManage.GetInstance.Register(new Action<object>((o) =>
  94. {
  95. if (o != null)
  96. {
  97. if (o.ToString().Contains("升降气缸"))
  98. {
  99. int index = Convert.ToInt16(o.ToString().Substring(o.ToString().Length - 1));
  100. switch (index)
  101. {
  102. case 1:
  103. HKDevice.HK_PLC_S7.Write<bool>("DB5.DBX0.0", true);
  104. break;
  105. case 2:
  106. HKDevice.HK_PLC_S7.Write<bool>("DB5.DBX0.1", true);
  107. break;
  108. case 3:
  109. HKDevice.HK_PLC_S7.Write<bool>("DB5.DBX0.2", true);
  110. break;
  111. case 4:
  112. HKDevice.HK_PLC_S7.Write<bool>("DB5.DBX0.3", true);
  113. break;
  114. case 5:
  115. HKDevice.HK_PLC_S7.Write<bool>("DB5.DBX0.4", true);
  116. break;
  117. case 6:
  118. HKDevice.HK_PLC_S7.Write<bool>("DB5.DBX0.5", true);
  119. break;
  120. default:
  121. break;
  122. }
  123. }
  124. }
  125. }), "ManualOpen", true);//根据下发的配方ID将 托盘的位置信息添加到配方中
  126. }
  127. private void ManualClose()
  128. {
  129. ActionManage.GetInstance.Register(new Action<object>((o) =>
  130. {
  131. if (o != null)
  132. {
  133. if (o.ToString().Contains("升降气缸"))
  134. {
  135. int index = Convert.ToInt16(o.ToString().Substring(o.ToString().Length - 1));
  136. switch (index)
  137. {
  138. case 1:
  139. HKDevice.HK_PLC_S7.Write("DB5.DBX0.0", false);
  140. break;
  141. case 2:
  142. HKDevice.HK_PLC_S7.Write("DB5.DBX0.1", false);
  143. break;
  144. case 3:
  145. HKDevice.HK_PLC_S7.Write("DB5.DBX0.2", false);
  146. break;
  147. case 4:
  148. HKDevice.HK_PLC_S7.Write("DB5.DBX0.3", false);
  149. break;
  150. case 5:
  151. HKDevice.HK_PLC_S7.Write("DB5.DBX0.4", false);
  152. break;
  153. case 6:
  154. HKDevice.HK_PLC_S7.Write("DB5.DBX0.5", false);
  155. break;
  156. default:
  157. break;
  158. }
  159. }
  160. }
  161. }), "ManualClose", true);//根据下发的配方ID将 托盘的位置信息添加到配方中
  162. }
  163. /// <summary>
  164. /// 将配方添加到配方队列中
  165. /// </summary>
  166. private void ReceviceData()
  167. {
  168. RemoteRecipes = Json<RemoteRecipeDataColl>.Data.Recipes;
  169. if (RemoteRecipes.Count > 0)
  170. {
  171. foreach (var data in RemoteRecipes)
  172. {
  173. if (!(RecipeQueue.Contains(data.RecipeCode)))
  174. {
  175. RecipeQueue.Enqueue(data.RecipeCode);
  176. }
  177. }
  178. }
  179. else
  180. {
  181. RecipeQueue.Clear();
  182. GVL_SmallStation.GetInstance.RecipeStatusID = 0;
  183. }
  184. }
  185. /// <summary>
  186. /// 执行配方队列中的第一个配方
  187. /// </summary>
  188. private void RecipeInfoToHKPLC()
  189. {
  190. if (RecipeQueue.Count > 0)
  191. {
  192. int index = Array.FindIndex(RemoteRecipes.ToArray(), p => p.RecipeCode == RecipeQueue.ElementAt(0));
  193. if (index >= 0 && index < RemoteRecipes.Count)
  194. {
  195. string code = RemoteRecipes.ElementAt(index).RecipeCode;
  196. int trayCode = RemoteRecipes.ElementAt(index).TrayCode;
  197. string recipeName = RemoteRecipes.ElementAt(index).RecipeName;
  198. string windSend = RemoteRecipes.ElementAt(index).ToString();
  199. if (GVL_SmallStation.GetInstance.RecipeStatusID == 0)
  200. {
  201. HKDevice.HK_PLC_S7.Write("DB4.DBX1.3", true);
  202. GVL_SmallStation.GetInstance.RecipeStatusID = 1;
  203. MessageLog.GetInstance.ShowRunLog($"托盘1,配方:{recipeName},下发完成");
  204. HKDevice.HK_PLC_S7.Write("DB3.DBX1.3", true);
  205. }
  206. if (HKDevice.HK_PLC_S7.Read<bool>("DB3.DBX1.3") && GVL_SmallStation.GetInstance.RecipeStatusID == 1)
  207. {
  208. HKDevice.HK_PLC_S7.Write("DB3.DBX1.3", false);
  209. GVL_SmallStation.GetInstance.RecipeStatusID = 2;
  210. MessageLog.GetInstance.ShowRunLog($"托盘1,{ recipeName}plc端 配方接收完成");
  211. }
  212. if (GVL_SmallStation.GetInstance.RecipeStatusID == 2)
  213. {
  214. if (RTrig.GetInstance("DB3.DBX50.0").Start(HKDevice.HK_PLC_S7.Read<bool>("DB3.DBX50.0")))
  215. {
  216. var res = HKDevice.HK_PLC_S7.Read<float>("DB3.DBD10");
  217. MessageLog.GetInstance.ShowRunLog($"托盘1,配方:{recipeName},1号桶,{res}料仓,允许配料");
  218. if (res > 0 && res is float loc)
  219. {
  220. //int decimalNum = Convert.ToInt32(loc.ToString().Substring(loc.ToString().IndexOf(".") + 1));
  221. int loc_index = Array.FindIndex(RemoteRecipes.ElementAt(index).RawMaterial.ToArray(), p => p.RawMaterialLocation == loc);
  222. float weight = RemoteRecipes.ElementAt(index).RawMaterial.ElementAt(loc_index).RawMaterialWeight;
  223. if (loc_index >= 0)
  224. {
  225. DeviceInquire.GetInstance.GetDevice((int)loc)?.Start(weight);//根据料仓编号 启动并写入每个原料重量
  226. GVL_SmallStation.GetInstance.StockInIsWork = (int)loc;
  227. }
  228. GVL_SmallStation.GetInstance.DosingTray1 = true;
  229. MessageLog.GetInstance.ShowRunLog($"托盘1,配方:{recipeName},1号桶,{(int)loc}号仓,配料完成");
  230. }
  231. }
  232. else if(RTrig.GetInstance("DB3.DBX50.1").Start(HKDevice.HK_PLC_S7.Read<bool>("DB3.DBX50.1")))
  233. {
  234. var res = HKDevice.HK_PLC_S7.Read<float>("DB3.DBD14");
  235. MessageLog.GetInstance.ShowRunLog($"托盘1,配方:{recipeName},2号桶,{res}料仓,允许配料");
  236. if (res > 0 && res is float loc)
  237. {
  238. int loc_index = Array.FindIndex(RemoteRecipes.ElementAt(index).RawMaterial.ToArray(), p => p.RawMaterialLocation == loc);
  239. float weight = RemoteRecipes.ElementAt(index).RawMaterial.ElementAt(loc_index).RawMaterialWeight;
  240. if (loc_index >= 0)
  241. {
  242. DeviceInquire.GetInstance.GetDevice((int)loc)?.Start(weight);//启动并写入每个原料重量
  243. GVL_SmallStation.GetInstance.StockInIsWork = (int)loc;
  244. }
  245. GVL_SmallStation.GetInstance.DosingTray1 = true;
  246. MessageLog.GetInstance.ShowRunLog($"托盘1,配方:{recipeName},2号桶,{(int)loc}号仓,配料完成");
  247. }
  248. }
  249. else if (RTrig.GetInstance("DB3.DBX50.2").Start(HKDevice.HK_PLC_S7.Read<bool>("DB3.DBX50.2")))
  250. {
  251. var res = HKDevice.HK_PLC_S7.Read<float>("DB3.DBD18");
  252. MessageLog.GetInstance.ShowRunLog($"托盘1,配方:{recipeName},3号桶,{res}料仓,允许配料");
  253. if (res > 0 && res is float loc)
  254. {
  255. int loc_index = Array.FindIndex(RemoteRecipes.ElementAt(index).RawMaterial.ToArray(), p => p.RawMaterialLocation == loc);
  256. float weight = RemoteRecipes.ElementAt(index).RawMaterial.ElementAt(loc_index).RawMaterialWeight;
  257. if (loc_index >= 0)
  258. {
  259. DeviceInquire.GetInstance.GetDevice((int)loc)?.Start(weight);//启动并写入每个原料重量
  260. GVL_SmallStation.GetInstance.StockInIsWork = (int)loc;
  261. }
  262. GVL_SmallStation.GetInstance.DosingTray1 = true;
  263. MessageLog.GetInstance.ShowRunLog($"托盘1,配方:{recipeName},3号桶,{(int)loc}号仓,配料完成");
  264. }
  265. }
  266. else if (RTrig.GetInstance("DB3.DBX50.3").Start(HKDevice.HK_PLC_S7.Read<bool>("DB3.DBX50.3")))
  267. {
  268. var res = HKDevice.HK_PLC_S7.Read<float>("DB3.DBD22");
  269. MessageLog.GetInstance.ShowRunLog($"托盘1,配方:{recipeName},4号桶,{res}料仓,允许配料");
  270. if (res > 0 && res is float loc)
  271. {
  272. int loc_index = Array.FindIndex(RemoteRecipes.ElementAt(index).RawMaterial.ToArray(), p => p.RawMaterialLocation == loc);
  273. float weight = RemoteRecipes.ElementAt(index).RawMaterial.ElementAt(loc_index).RawMaterialWeight;
  274. if (loc_index >= 0)
  275. {
  276. DeviceInquire.GetInstance.GetDevice((int)loc)?.Start(weight);//启动并写入每个原料重量
  277. GVL_SmallStation.GetInstance.StockInIsWork = (int)loc;
  278. }
  279. GVL_SmallStation.GetInstance.DosingTray1 = true;
  280. MessageLog.GetInstance.ShowRunLog($"托盘1,配方:{recipeName},4号桶,{(int)loc}号仓,配料完成");
  281. }
  282. }
  283. if (GVL_SmallStation.GetInstance.DosingTray1)
  284. {
  285. for (int i = 1; i < 16; i++)
  286. {
  287. if (RTrig.GetInstance("GetDeviceRunStatus").Start(DeviceInquire.GetInstance.GetDevice(i).deviceStatus.RunStatus == 3))
  288. {
  289. MessageLog.GetInstance.ShowRunLog($"柔性味魔方,托盘1,配方:{recipeName},{i}号仓,配料完成");
  290. int res = Array.FindIndex(RemoteRecipes.ElementAt(index).RawMaterial.ToArray(), p => p.RawMaterialLocation == i);
  291. RemoteRecipes.ElementAt(index).RawMaterial.ElementAt(res).Laying_Off_Weight = DeviceInquire.GetInstance.GetDevice(i).deviceStatus.CutWeightFeedback;
  292. DeviceInquire.GetInstance.GetDevice(i).StatusReset();
  293. if (i >= 1 && i <= 8)
  294. {
  295. HKDevice.HK_PLC_S7.Write("DB4.DBX30." + (i - 1), true);
  296. }
  297. else if (i >= 9 && i <= 15)
  298. {
  299. HKDevice.HK_PLC_S7.Write("DB4.DBX31." + (i - 9), true);
  300. }
  301. GVL_SmallStation.GetInstance.DosingTray1 = false;
  302. }
  303. }
  304. }
  305. if (RTrig.GetInstance("配方配料完成").Start(HKDevice.HK_PLC_S7.Read<bool>("DB3.DBX1.1")) && (GVL_SmallStation.GetInstance.WindSendDosingFinish || !GVL_SmallStation.GetInstance.IsUseWindSend))
  306. {
  307. var res = Json<RemoteRecipeDataColl>.Data.Recipes.FirstOrDefault(p => p.RecipeCode == code);
  308. MessageLog.GetInstance.ShowRunLog($"托盘1,配方{res.RecipeName},配料完成");
  309. App.Current.Dispatcher.Invoke(() =>
  310. {
  311. Json<RemoteRecipeDataColl>.Data.Recipes.Remove(res);
  312. });
  313. RecipeQueue.TryDequeue(out code);
  314. HKDevice.HK_PLC_S7.Write("DB3.DBX1.1", false);
  315. GVL_SmallStation.GetInstance.RecipeStatusID = 0;
  316. }
  317. }
  318. }
  319. }
  320. }
  321. }
  322. }