终端一体化运控平台
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.
 
 
 

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