终端一体化运控平台
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.
 
 
 

624 řádky
42 KiB

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using BPA.Helper;
  7. using System.Collections.Concurrent;
  8. using System.Collections.ObjectModel;
  9. using System.Windows;
  10. using System.Threading;
  11. using BPASmartClient.CustomResource.Pages.Model;
  12. using BPASmartClient.CustomResource.UserControls.MessageShow;
  13. using BPASmartClient.CustomResource.UserControls;
  14. using BPASmartClient.Model.柔性味魔方;
  15. using BPASmartClient.Model;
  16. using System.Diagnostics;
  17. using Microsoft.EntityFrameworkCore.Metadata.Internal;
  18. using System.Data.SqlClient;
  19. namespace BPASmartClient.DosingSystem.ViewModel
  20. {
  21. public class RecipeControlViewModel : NotifyBase
  22. {
  23. static ConcurrentQueue<string> RecipeNames = new ConcurrentQueue<string>();
  24. static ObservableCollection<StockStatusModel> StockStatus = new ObservableCollection<StockStatusModel>();
  25. public RecipeControlViewModel()
  26. {
  27. StartCommand = new BPARelayCommand<object>(RecipeIssued);
  28. ChangeRecipeStateCommand = new BPARelayCommand<object>(ChangeRecipeState);
  29. CancelRecipeCommand = new BPARelayCommand<object>(CancelRecipe);
  30. RecipeRun();
  31. RecipeStatusInquire();
  32. }
  33. private async void CancelRecipe(object o)
  34. {
  35. if (o != null && o is RecipeModel recipe)
  36. {
  37. var res = MessageNotify.GetInstance.ShowDialog($"是否取消配方 【{recipe.RecipeName}】制作", DialogType.Warning);
  38. if (res)
  39. {
  40. int index = Recipes.ToList().FindIndex(p => p.RecipeName == recipe.RecipeName);
  41. if (index >= 0 && index < Recipes.Count)
  42. {
  43. await Task.Factory.StartNew(new Action(() =>
  44. {
  45. Recipes.ElementAt(index).IsEnable = true;
  46. Json<LocaPar>.Data.Recipes.ElementAt(index).IsEnable = true;
  47. Recipes.ElementAt(index).Are.Set();
  48. App.Current.Dispatcher.Invoke(new Action(() => { recipeProcesses.Clear(); }));
  49. SiemensDevice.GetInstance.MySiemens.Write("M10.5", true);
  50. Thread.Sleep(1000);
  51. SiemensDevice.GetInstance.MySiemens.Write("M10.5", false);
  52. NoticeDemoViewModel.OpenMsg(EnumPromptType.Success, App.MainWindow, "提示", $"配方 [{recipe.RecipeName}] 取消成功");
  53. }));
  54. }
  55. }
  56. }
  57. }
  58. /// <summary>
  59. /// 配方下发
  60. /// </summary>
  61. private void RecipeIssued(object o)
  62. {
  63. if (o != null && o is string deviceName)
  64. {
  65. int index = Array.FindIndex(Recipes.ToArray(), p => p.RecipeName == deviceName);
  66. if (index >= 0 && index < Recipes.Count)
  67. {
  68. for (int i = 0; i < Recipes.ElementAt(index).RawMaterials.Count; i++)
  69. {
  70. if (Recipes.ElementAt(index).RawMaterials.ElementAt(i).RawMaterialSource == 1)
  71. {
  72. string ip = Recipes.ElementAt(index).RawMaterials.ElementAt(i).DeviceIp;
  73. var device = DeviceInquire.GetInstance.GetDevice(ip);
  74. if (!device.IsConnected)
  75. {
  76. MessageNotify.GetInstance.ShowDialog($"设备 【{device.DeviceName}】 未连接,不允许下发此配方", DialogType.Error);
  77. return;
  78. }
  79. }
  80. }
  81. Recipes.ElementAt(index).IsEnable = false;
  82. Json<LocaPar>.Data.Recipes.ElementAt(index).IsEnable = false;
  83. }
  84. MessageNotify.GetInstance.ShowUserLog($"下发工单 {Recipes.ElementAt(index).RecipeName}");
  85. RecipeNames.Enqueue(deviceName);
  86. var t = RecipeNames.GetHashCode();
  87. var res = Recipes.FirstOrDefault(p => p.RecipeName == deviceName);
  88. UserTreeWait.Add(new RecipeModel { RecipStatus = "等待制作", SerialNum = UserTreeWait.Count + 1, RecipeName = deviceName, RawMaterials = res.RawMaterials });
  89. }
  90. NoticeDemoViewModel.OpenMsg(EnumPromptType.Success, App.MainWindow, "提示", $"配方下发成功!");
  91. MessageNotify.GetInstance.ShowUserLog($"下发工单 {Guid.NewGuid().ToString()}");
  92. }
  93. /// <summary>
  94. /// 配方业务执行
  95. /// </summary>
  96. private void RecipeRun()
  97. {
  98. ThreadManage.GetInstance().StartLong(new Action(() =>
  99. {
  100. if (RecipeNames.Count > 0)
  101. {
  102. int index = Array.FindIndex(Recipes.ToArray(), p => p.RecipeName == RecipeNames.ElementAt(0));
  103. if (index >= 0 && index < Recipes.Count)
  104. {
  105. Recipes.ElementAt(index).Are.Reset();
  106. Recipes.ElementAt(index).IsEnable = false;
  107. StockStatus.Clear();
  108. App.Current.Dispatcher.Invoke(new Action(() =>
  109. {
  110. recipeProcesses.Clear();
  111. if (UserTreeWait.Count > 0) UserTreeWait.RemoveAt(0);
  112. }));
  113. CurrentRecipeName = Recipes.ElementAt(index).RecipeName;
  114. #region 配方下发到PLC操作相关
  115. //配方数据写入到输送机
  116. var res = Json<LocaPar>.Data.Recipes.FirstOrDefault(p => p.RecipeName == CurrentRecipeName);
  117. /*List<int> recipeData = new List<int>();*/
  118. List<int> BarrelNum = new List<int>();
  119. if (res != null)
  120. {
  121. var tInfo = res.RawMaterials.GroupBy(p => p.Loc);//获取桶号信息
  122. if (tInfo != null)
  123. {
  124. for (int i = 0; i < tInfo.Count(); i++)
  125. {
  126. int data = 0;
  127. for (int m = 0; m < tInfo.ElementAt(i).Count(); m++)
  128. {
  129. var TempName = tInfo.ElementAt(i).ElementAt(m).RawMaterialName;
  130. var tempDevice = DeviceInquire.GetInstance.devices.FirstOrDefault(p => p.DeviceName == TempName);
  131. if (tempDevice != null)
  132. {
  133. /*data = data.SetBitValue((byte)tempDevice.DeviceNum, true);*/
  134. if (tempDevice.DeviceNum > 0)
  135. {
  136. int a = data.SetBitValue((byte)tInfo.ElementAt(i).ElementAt(m).Loc, true);
  137. byte[] test1 = a.ToBytes(BPA.Helper.DataFormat.ABCD);
  138. int item = test1.ToInt();
  139. SiemensDevice.GetInstance.MySiemens.Write($"DB4.DBD{2 + (tempDevice.DeviceNum - 1) * 4}", item, 5);
  140. MessageNotify.GetInstance.ShowRunLog($"下发配方DB4.DBD{2 + (tempDevice.DeviceNum - 1) * 4} :{item.ToBinString()}");
  141. }
  142. }
  143. if (tInfo.ElementAt(i).ElementAt(m).Loc > 0)
  144. {
  145. if (!BarrelNum.Contains(tInfo.ElementAt(i).ElementAt(m).Loc))
  146. {
  147. BarrelNum.Add(tInfo.ElementAt(i).ElementAt(m).Loc);
  148. }
  149. }
  150. }
  151. /*recipeData.Add(data);*/
  152. }
  153. }
  154. }
  155. SiemensDevice.GetInstance.MySiemens.Write("DB4.DBD198", BarrelNum.Count, 5);//配方使用桶数写入
  156. /*if (recipeData.Count <= 0)
  157. {
  158. MessageNotify.GetInstance.ShowRunLog("配方数据解析失败");
  159. return;
  160. }*/
  161. /*int offset = 2;
  162. //写入配方数据
  163. MessageNotify.GetInstance.ShowRunLog("开始写入配方数据");
  164. recipeData.ForEach(item =>
  165. {
  166. byte[] test1 = item.ToBytes(BPA.Helper.DataFormat.ABCD);
  167. item = test1.ToInt();
  168. SiemensDevice.GetInstance.MySiemens.Write($"DB4.DBD{offset}", item);
  169. MessageNotify.GetInstance.ShowRunLog($"下发配方DB4.DBD{offset} :{item.ToBinString()}");
  170. offset = offset += 4;
  171. });*/
  172. /*SiemensDevice.GetInstance.MySiemens.Write("DB4.DBD198", recipeData.Count);//配方使用桶数写入*/
  173. SiemensDevice.GetInstance.MySiemens.Write("DB4.DBX0.1", true);//配方执行启动
  174. Stopwatch sw = new Stopwatch();
  175. var recipe = Recipes.ElementAt(index);
  176. ObservableCollection<RawMaterialModel> RawMater = new ObservableCollection<RawMaterialModel>();
  177. foreach (var item in recipe.RawMaterials)
  178. {
  179. RawMater.Add(new RawMaterialModel()
  180. {
  181. RawMaterialName = item.RawMaterialName,
  182. DeviceIp = item.DeviceIp,
  183. DownLimtFeedback = item.DownLimtFeedback,
  184. Loc = item.Loc,
  185. RawMaterialId = item.RawMaterialId,
  186. RawMaterialSource = item.RawMaterialSource,
  187. RawMaterialType = item.RawMaterialType,
  188. RawMaterialWeight = item.RawMaterialWeight,
  189. RecipeStatus = item.RecipeStatus,
  190. SelectIndex = item.SelectIndex,
  191. Status = item.Status,
  192. UpLimtFeedback = item.UpLimtFeedback,
  193. UpLimtWeightFeedback = item.UpLimtWeightFeedback,
  194. WeightFeedback = item.WeightFeedback,
  195. });
  196. }
  197. App.Current.Dispatcher.Invoke(() =>
  198. {
  199. recipeProcesses.Add(new RecipeModel()
  200. {
  201. RawMaterials = RawMater,
  202. IsEnable = recipe.IsEnable,
  203. RecipeName = recipe.RecipeName,
  204. SerialNum = recipe.SerialNum,
  205. RecipCode = recipe.RecipCode,
  206. });
  207. });
  208. sw.Restart();
  209. while (true)
  210. {
  211. if (sw.ElapsedMilliseconds >= 3000 && !GlobalDevice.PlcData.ResComplete)
  212. {
  213. MessageNotify.GetInstance.ShowRunLog("获取配方下发反馈超时");
  214. break;
  215. }
  216. if (GlobalDevice.PlcData.ResComplete)
  217. {
  218. SiemensDevice.GetInstance.MySiemens.Write("DB3.DBX0.1", false);
  219. break;
  220. }
  221. Thread.Sleep(100);
  222. }
  223. #endregion
  224. Recipes.ElementAt(index).Are.WaitOne();//阻塞,直到当前配方完成
  225. RecipeNames.TryDequeue(out string deviceName);
  226. var recipeComple = Recipes.ElementAt(index);
  227. ObservableCollection<RawMaterialModel> RawMaterComple = new ObservableCollection<RawMaterialModel>();
  228. foreach (var item in recipeComple.RawMaterials)
  229. {
  230. RawMaterComple.Add(new RawMaterialModel()
  231. {
  232. RawMaterialName = item.RawMaterialName,
  233. DeviceIp = item.DeviceIp,
  234. DownLimtFeedback = item.DownLimtFeedback,
  235. Loc = item.Loc,
  236. RawMaterialId = item.RawMaterialId,
  237. RawMaterialSource = item.RawMaterialSource,
  238. RawMaterialType = item.RawMaterialType,
  239. RawMaterialWeight = item.RawMaterialWeight,
  240. RecipeStatus = item.RecipeStatus,
  241. SelectIndex = item.SelectIndex,
  242. Status = item.Status,
  243. UpLimtFeedback = item.UpLimtFeedback,
  244. UpLimtWeightFeedback = item.UpLimtWeightFeedback,
  245. WeightFeedback = item.WeightFeedback,
  246. });
  247. }
  248. App.Current.Dispatcher.Invoke(() =>
  249. {
  250. UserTreeCompelete.Add(new RecipeModel()
  251. {
  252. RawMaterials = RawMaterComple,
  253. IsEnable = recipeComple.IsEnable,
  254. RecipeName = recipeComple.RecipeName,
  255. SerialNum = UserTreeCompelete.Count + 1,
  256. RecipCode = recipeComple.RecipCode,
  257. RecipStatus = "制作完成"
  258. });
  259. });
  260. App.Current.Dispatcher.Invoke(new Action(() =>
  261. {
  262. recipeProcesses.Clear();
  263. CurrentRecipeName = string.Empty;
  264. }));//完成后清空当前配方
  265. }
  266. }
  267. Thread.Sleep(100);
  268. }), "启动配方下发");
  269. }
  270. /// <summary>
  271. /// 配方执行状态监听
  272. /// </summary>
  273. private void RecipeStatusInquire()
  274. {
  275. ThreadManage.GetInstance().StartLong(new Action(() =>
  276. {
  277. for (int i = 0; i < Recipes.Count; i++)
  278. {
  279. for (int m = 0; m < Recipes.ElementAt(i).RawMaterials.Count; m++)
  280. {
  281. var RunStatus = DeviceInquire.GetInstance.GetDevice(Recipes.ElementAt(i).RawMaterials.ElementAt(m).DeviceIp).deviceStatus.RunStatus;
  282. //设备状态显示
  283. if (Recipes.ElementAt(i).RecipeName == CurrentRecipeName)
  284. {
  285. string tempRawMaterialName = DeviceInquire.GetInstance.GetDevice(Recipes.ElementAt(i).RawMaterials.ElementAt(m).DeviceIp).DeviceName;
  286. int recIndex = recipeProcesses.ToList().FindIndex(p => p.RecipeName == CurrentRecipeName);
  287. if (recIndex >= 0 && recIndex < recipeProcesses.Count)
  288. {
  289. int index = recipeProcesses.ElementAt(recIndex).RawMaterials.ToList().FindIndex(p => p.RawMaterialName == tempRawMaterialName);
  290. if (index >= 0 && index < recipeProcesses.ElementAt(recIndex).RawMaterials.Count)
  291. {
  292. //测试使用
  293. recipeProcesses.ElementAt(recIndex).RawMaterials.ElementAt(index).RecipeStatus = RunStatus;
  294. }
  295. for (int j = 0; j < GlobalDevice.PlcData.IsAllowIngredients.Length; j++)
  296. {
  297. if (GlobalDevice.PlcData.IsAllowIngredients[j])
  298. {
  299. int barrel = j + 1;//桶号
  300. int cnt = GlobalDevice.PlcData.LocationFeedback[j];//允许配料的位置 0~6
  301. int Location1 = 0;
  302. int Location2 = 0;
  303. if (cnt == -1)
  304. {
  305. MessageNotify.GetInstance.ShowRunLog("错误");
  306. return;
  307. }
  308. else
  309. {
  310. switch (cnt)
  311. {
  312. case 1:
  313. int a1 = Array.FindIndex(DeviceInquire.GetInstance.devices.ToArray(), p => p.DeviceNum == 1);
  314. int b1 = Array.FindIndex(DeviceInquire.GetInstance.devices.ToArray(), p => p.DeviceNum == 2);
  315. if (a1 >= 0)
  316. {
  317. Location1 = Array.FindIndex(Recipes.ElementAt(i).RawMaterials.ToArray(), p => p.RawMaterialName == DeviceInquire.GetInstance.devices.ElementAt(a1).DeviceName && p.Loc == barrel);
  318. }
  319. if (b1 >= 0)
  320. {
  321. Location2 = Array.FindIndex(Recipes.ElementAt(i).RawMaterials.ToArray(), p => p.RawMaterialName == DeviceInquire.GetInstance.devices.ElementAt(b1).DeviceName && p.Loc == barrel);
  322. }
  323. break;
  324. case 2:
  325. int a2 = Array.FindIndex(DeviceInquire.GetInstance.devices.ToArray(), p => p.DeviceNum == 3);
  326. int b2 = Array.FindIndex(DeviceInquire.GetInstance.devices.ToArray(), p => p.DeviceNum == 4);
  327. if (a2 >= 0)
  328. {
  329. Location1 = Array.FindIndex(Recipes.ElementAt(i).RawMaterials.ToArray(), p => p.RawMaterialName == DeviceInquire.GetInstance.devices.ElementAt(a2).DeviceName && p.Loc == barrel);
  330. }
  331. if (b2 >= 0)
  332. {
  333. Location2 = Array.FindIndex(Recipes.ElementAt(i).RawMaterials.ToArray(), p => p.RawMaterialName == DeviceInquire.GetInstance.devices.ElementAt(b2).DeviceName && p.Loc == barrel);
  334. }
  335. break;
  336. case 3:
  337. int a3 = Array.FindIndex(DeviceInquire.GetInstance.devices.ToArray(), p => p.DeviceNum == 5);
  338. int b3 = Array.FindIndex(DeviceInquire.GetInstance.devices.ToArray(), p => p.DeviceNum == 6);
  339. if (a3 >= 0)
  340. {
  341. Location1 = Array.FindIndex(Recipes.ElementAt(i).RawMaterials.ToArray(), p => p.RawMaterialName == DeviceInquire.GetInstance.devices.ElementAt(a3).DeviceName && p.Loc == barrel);
  342. }
  343. if (b3 >= 0)
  344. {
  345. Location2 = Array.FindIndex(Recipes.ElementAt(i).RawMaterials.ToArray(), p => p.RawMaterialName == DeviceInquire.GetInstance.devices.ElementAt(b3).DeviceName && p.Loc == barrel);
  346. }
  347. break;
  348. case 4:
  349. int a4 = Array.FindIndex(DeviceInquire.GetInstance.devices.ToArray(), p => p.DeviceNum == 7);
  350. int b4 = Array.FindIndex(DeviceInquire.GetInstance.devices.ToArray(), p => p.DeviceNum == 8);
  351. if (a4 >= 0)
  352. {
  353. Location1 = Array.FindIndex(Recipes.ElementAt(i).RawMaterials.ToArray(), p => p.RawMaterialName == DeviceInquire.GetInstance.devices.ElementAt(a4).DeviceName && p.Loc == barrel);
  354. }
  355. if (b4 >= 0)
  356. {
  357. Location2 = Array.FindIndex(Recipes.ElementAt(i).RawMaterials.ToArray(), p => p.RawMaterialName == DeviceInquire.GetInstance.devices.ElementAt(b4).DeviceName && p.Loc == barrel);
  358. }
  359. break;
  360. case 5:
  361. int a5 = Array.FindIndex(DeviceInquire.GetInstance.devices.ToArray(), p => p.DeviceNum == 9);
  362. int b5 = Array.FindIndex(DeviceInquire.GetInstance.devices.ToArray(), p => p.DeviceNum == 10);
  363. if (a5 >= 0)
  364. {
  365. Location1 = Array.FindIndex(Recipes.ElementAt(i).RawMaterials.ToArray(), p => p.RawMaterialName == DeviceInquire.GetInstance.devices.ElementAt(a5).DeviceName && p.Loc == barrel);
  366. }
  367. if (b5 >= 0)
  368. {
  369. Location2 = Array.FindIndex(Recipes.ElementAt(i).RawMaterials.ToArray(), p => p.RawMaterialName == DeviceInquire.GetInstance.devices.ElementAt(b5).DeviceName && p.Loc == barrel);
  370. }
  371. break;
  372. case 6:
  373. int a6 = Array.FindIndex(DeviceInquire.GetInstance.devices.ToArray(), p => p.DeviceNum == 11);
  374. int b6 = Array.FindIndex(DeviceInquire.GetInstance.devices.ToArray(), p => p.DeviceNum == 12);
  375. if (a6 >= 0)
  376. {
  377. Location1 = Array.FindIndex(Recipes.ElementAt(i).RawMaterials.ToArray(), p => p.RawMaterialName == DeviceInquire.GetInstance.devices.ElementAt(a6).DeviceName && p.Loc == barrel);
  378. }
  379. if (b6 >= 0)
  380. {
  381. Location2 = Array.FindIndex(Recipes.ElementAt(i).RawMaterials.ToArray(), p => p.RawMaterialName == DeviceInquire.GetInstance.devices.ElementAt(b6).DeviceName && p.Loc == barrel);
  382. }
  383. break;
  384. default:
  385. break;
  386. }
  387. }
  388. if (Location1 >= 0)
  389. {
  390. string RawName = Recipes.ElementAt(i).RawMaterials.ElementAt(Location1).RawMaterialName;
  391. int abc = Array.FindIndex(DeviceInquire.GetInstance.devices.ToArray(), p => p.DeviceName == RawName);
  392. int DeviceNum = DeviceInquire.GetInstance.devices.ElementAt(abc).DeviceNum;
  393. if (DeviceNum > 0)
  394. {
  395. float weight = Recipes.ElementAt(i).RawMaterials.ElementAt(Location1).RawMaterialWeight;
  396. int loc = Array.FindIndex(Recipes.ElementAt(i).RawMaterials.ToArray(), p => p.RawMaterialName == RawName);
  397. if (loc >= 0 && RawName != null && weight >= 0)
  398. {
  399. int St_index = Array.FindIndex(StockStatus.ToArray(), p => p.MaterialName == RawName);
  400. if (St_index < 0)
  401. {
  402. StockStatus.Add(new StockStatusModel()
  403. {
  404. MaterialName = RawName,
  405. IssueWeight = weight,
  406. IssueStatus = 0,
  407. });
  408. }
  409. St_index = Array.FindIndex(StockStatus.ToArray(), p => p.MaterialName == RawName);
  410. if (St_index >= 0)
  411. {
  412. if (recipeProcesses.ElementAt(recIndex).RawMaterials.ElementAt(loc).RecipeStatus == 1 && StockStatus.ElementAt(St_index).IssueStatus == 0)
  413. {
  414. StockStatus.ElementAt(St_index).IssueStatus = 1;
  415. DeviceInquire.GetInstance.GetDevice(Recipes.ElementAt(i).RawMaterials.ElementAt(loc).DeviceIp).Start(weight);
  416. MessageNotify.GetInstance.ShowRunLog($"柔性味魔方{Recipes.ElementAt(i).RawMaterials.ElementAt(loc).DeviceIp},开始出料");
  417. }
  418. if (recipeProcesses.ElementAt(recIndex).RawMaterials.ElementAt(loc).RecipeStatus == 3 && StockStatus.ElementAt(St_index).IssueStatus == 1)
  419. {
  420. //GlobalDevice.PlcData.IsAllowIngredients[j] = false;//测试使用
  421. StockStatus.ElementAt(St_index).IssueStatus = 2;
  422. DeviceInquire.GetInstance.GetDevice(Recipes.ElementAt(i).RawMaterials.ElementAt(loc).DeviceIp).StatusReset();
  423. int a = 0.SetBitValue((byte)(DeviceNum), true);
  424. byte[] test1 = a.ToBytes(BPA.Helper.DataFormat.ABCD);
  425. int item = test1.ToInt();
  426. SiemensDevice.GetInstance.MySiemens.Write("DB4.DBD130", item);
  427. MessageNotify.GetInstance.ShowRunLog($"DB4.DBD130:{item.ToBinString()}");
  428. MessageNotify.GetInstance.ShowRunLog($"柔性味魔方{Recipes.ElementAt(i).RawMaterials.ElementAt(loc).DeviceIp},出料完成,状态复位");
  429. }
  430. }
  431. }
  432. }
  433. }
  434. if (Location1 >= 0)
  435. {
  436. string RawName111 = Recipes.ElementAt(i).RawMaterials.ElementAt(Location1).RawMaterialName;
  437. int St_index111 = Array.FindIndex(StockStatus.ToArray(), p => p.MaterialName == RawName111);
  438. if (Location2 >= 0 && ((Location1 >= 0 && StockStatus.ElementAt(St_index111).IssueStatus == 2)))
  439. {
  440. string RawName = Recipes.ElementAt(i).RawMaterials.ElementAt(Location2).RawMaterialName;
  441. int abc = Array.FindIndex(DeviceInquire.GetInstance.devices.ToArray(), p => p.DeviceName == RawName);
  442. int DeviceNum = DeviceInquire.GetInstance.devices.ElementAt(abc).DeviceNum;
  443. if (DeviceNum > 0)
  444. {
  445. float weight = Recipes.ElementAt(i).RawMaterials.ElementAt(Location2).RawMaterialWeight;
  446. int loc = Array.FindIndex(Recipes.ElementAt(i).RawMaterials.ToArray(), p => p.RawMaterialName == RawName);
  447. if (loc >= 0 && RawName != null && weight >= 0)
  448. {
  449. int St_index = Array.FindIndex(StockStatus.ToArray(), p => p.MaterialName == RawName);
  450. if (St_index < 0)
  451. {
  452. StockStatus.Add(new StockStatusModel()
  453. {
  454. MaterialName = RawName,
  455. IssueWeight = weight,
  456. IssueStatus = 0,
  457. });
  458. }
  459. St_index = Array.FindIndex(StockStatus.ToArray(), p => p.MaterialName == RawName);
  460. if (St_index >= 0)
  461. {
  462. if (recipeProcesses.ElementAt(recIndex).RawMaterials.ElementAt(loc).RecipeStatus == 1 && StockStatus.ElementAt(St_index).IssueStatus == 0)
  463. {
  464. StockStatus.ElementAt(St_index).IssueStatus = 1;
  465. DeviceInquire.GetInstance.GetDevice(Recipes.ElementAt(i).RawMaterials.ElementAt(loc).DeviceIp).Start(weight);
  466. MessageNotify.GetInstance.ShowRunLog($"柔性味魔方{Recipes.ElementAt(i).RawMaterials.ElementAt(loc).DeviceIp},开始出料");
  467. }
  468. if (recipeProcesses.ElementAt(recIndex).RawMaterials.ElementAt(loc).RecipeStatus == 3 && StockStatus.ElementAt(St_index).IssueStatus == 1)
  469. {
  470. //GlobalDevice.PlcData.IsAllowIngredients[j] = false;//测试使用
  471. StockStatus.ElementAt(St_index).IssueStatus = 2;
  472. DeviceInquire.GetInstance.GetDevice(Recipes.ElementAt(i).RawMaterials.ElementAt(loc).DeviceIp).StatusReset();
  473. int a = 0.SetBitValue((byte)(DeviceNum), true);
  474. byte[] test1 = a.ToBytes(BPA.Helper.DataFormat.ABCD);
  475. int item = test1.ToInt();
  476. SiemensDevice.GetInstance.MySiemens.Write("DB4.DBD130", item, 5);
  477. //MessageNotify.GetInstance.ShowRunLog($"DB4.DBD130:{item.ToBinString()}");
  478. MessageNotify.GetInstance.ShowRunLog($"DB4.DBD130:{item}");
  479. MessageNotify.GetInstance.ShowRunLog($"柔性味魔方{Recipes.ElementAt(i).RawMaterials.ElementAt(loc).DeviceIp},出料完成,状态复位");
  480. }
  481. }
  482. }
  483. }
  484. }
  485. }
  486. else
  487. {
  488. if (Location2 >= 0)
  489. {
  490. string RawName = Recipes.ElementAt(i).RawMaterials.ElementAt(Location2).RawMaterialName;
  491. int abc = Array.FindIndex(DeviceInquire.GetInstance.devices.ToArray(), p => p.DeviceName == RawName);
  492. int DeviceNum = DeviceInquire.GetInstance.devices.ElementAt(abc).DeviceNum;
  493. if (DeviceNum >= 0)
  494. {
  495. float weight = Recipes.ElementAt(i).RawMaterials.ElementAt(Location2).RawMaterialWeight;
  496. int loc = Array.FindIndex(Recipes.ElementAt(i).RawMaterials.ToArray(), p => p.RawMaterialName == RawName);
  497. if (cnt > 0 && loc >= 0 && RawName != null && weight >= 0)
  498. {
  499. int St_index = Array.FindIndex(StockStatus.ToArray(), p => p.MaterialName == RawName);
  500. if (St_index < 0)
  501. {
  502. StockStatus.Add(new StockStatusModel()
  503. {
  504. MaterialName = RawName,
  505. IssueWeight = weight,
  506. IssueStatus = 0,
  507. });
  508. }
  509. St_index = Array.FindIndex(StockStatus.ToArray(), p => p.MaterialName == RawName);
  510. if (St_index >= 0)
  511. {
  512. if (recipeProcesses.ElementAt(recIndex).RawMaterials.ElementAt(loc).RecipeStatus == 1 && StockStatus.ElementAt(St_index).IssueStatus == 0)
  513. {
  514. StockStatus.ElementAt(St_index).IssueStatus = 1;
  515. DeviceInquire.GetInstance.GetDevice(Recipes.ElementAt(i).RawMaterials.ElementAt(loc).DeviceIp).Start(weight);
  516. MessageNotify.GetInstance.ShowRunLog($"柔性味魔方{Recipes.ElementAt(i).RawMaterials.ElementAt(loc).DeviceIp},开始出料");
  517. }
  518. if (recipeProcesses.ElementAt(recIndex).RawMaterials.ElementAt(loc).RecipeStatus == 3 && StockStatus.ElementAt(St_index).IssueStatus == 1)
  519. {
  520. //GlobalDevice.PlcData.IsAllowIngredients[j] = false;//测试使用
  521. StockStatus.ElementAt(St_index).IssueStatus = 2;
  522. DeviceInquire.GetInstance.GetDevice(Recipes.ElementAt(i).RawMaterials.ElementAt(loc).DeviceIp).StatusReset();
  523. int a = 0.SetBitValue((byte)(DeviceNum), true);
  524. byte[] test1 = a.ToBytes(BPA.Helper.DataFormat.ABCD);
  525. int item = test1.ToInt();
  526. SiemensDevice.GetInstance.MySiemens.Write("DB4.DBD130", item);
  527. MessageNotify.GetInstance.ShowRunLog($"DB4.DBD130:{item.ToBinString()}");
  528. MessageNotify.GetInstance.ShowRunLog($"柔性味魔方{Recipes.ElementAt(i).RawMaterials.ElementAt(loc).DeviceIp},出料完成,状态复位");
  529. }
  530. }
  531. }
  532. }
  533. }
  534. }
  535. }
  536. }
  537. int Finish_Count = StockStatus.Where(s => s.IssueStatus == 2).Count();
  538. if (StockStatus.Count >= Recipes.ElementAt(i).RawMaterials.Count && Finish_Count >= Recipes.ElementAt(i).RawMaterials.Count) //配方配料完成
  539. {
  540. StockStatus.Clear();
  541. SiemensDevice.GetInstance.MySiemens.Write("DB4.DBX202.1", true);//配料完成
  542. MessageNotify.GetInstance.ShowRunLog($"配料完成");
  543. int recipIndex = Array.FindIndex(Recipes.ToArray(), p => p.RecipeName == CurrentRecipeName);
  544. App.Current.Dispatcher.Invoke(new Action(() => { recipeProcesses.Clear(); }));
  545. Recipes.ElementAt(recipIndex).IsEnable = true;
  546. Json<LocaPar>.Data.Recipes.ElementAt(recipIndex).IsEnable = true;
  547. Recipes.ElementAt(recipIndex).Are.Set();
  548. Thread.Sleep(100);
  549. }
  550. }
  551. }
  552. }
  553. }
  554. Thread.Sleep(100);
  555. }), "RecipeControlViewModelStatusInquire");
  556. }
  557. public BPARelayCommand<object> StartCommand { get; set; }
  558. public BPARelayCommand<object> ChangeRecipeStateCommand { get; set; }
  559. public BPARelayCommand<object> CancelRecipeCommand { get; set; }
  560. public static ObservableCollection<RecipeModel> Recipes { get; set; } = Json<LocaPar>.Data.Recipes;
  561. public static string CurrentRecipeName { get { return _RecipeName; } set { _RecipeName = value; OnStaticPropertyChanged(); } }
  562. private static string _RecipeName;
  563. /// <summary>
  564. /// 当前正在制作的配方
  565. /// </summary>
  566. public static ObservableCollection<RecipeModel> recipeProcesses { get; set; } = new ObservableCollection<RecipeModel>();
  567. /// <summary>
  568. /// 等待制作的配方
  569. /// </summary>
  570. public static ObservableCollection<RecipeModel> UserTreeWait { get; set; } = new ObservableCollection<RecipeModel>();
  571. /// <summary>
  572. /// 已完成的配方
  573. /// </summary>
  574. public static ObservableCollection<RecipeModel> UserTreeCompelete { get; set; } = new ObservableCollection<RecipeModel>();
  575. private void ChangeRecipeState(object o)
  576. {
  577. if (o == null) return;
  578. if (o is string id)
  579. {
  580. var Recipe = recipeProcesses.FirstOrDefault(p => p.RecipeName == CurrentRecipeName);
  581. if (Recipe != null)
  582. {
  583. var res = Recipe.RawMaterials.FirstOrDefault(p => p.RawMaterialId == id);
  584. if (res != null)
  585. {
  586. if (res.RecipeStatus == 3)
  587. {
  588. res.RecipeStatus = 1;
  589. }
  590. else
  591. {
  592. res.RecipeStatus = 3;
  593. }
  594. }
  595. }
  596. }
  597. }
  598. }
  599. }