终端一体化运控平台
25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

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