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

1046 lines
81 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. using S7.Net.Types;
  20. using BPA.Communication;
  21. using BPASmartClient.DosingSystem.Model;
  22. using System.Security.Cryptography;
  23. namespace BPASmartClient.DosingSystem.ViewModel
  24. {
  25. public class RecipeControlViewModel : NotifyBase
  26. {
  27. int ComCount = 0;//配料完成计数
  28. static ConcurrentQueue<string> RecipeNames = new ConcurrentQueue<string>();
  29. static ObservableCollection<StockStatusModel> StockStatus = new ObservableCollection<StockStatusModel>();
  30. public RecipeControlViewModel()
  31. {
  32. StartCommand = new BPARelayCommand<object>(RecipeIssued);
  33. ChangeRecipeStateCommand = new BPARelayCommand<object>(ChangeRecipeState);
  34. CancelRecipeCommand = new BPARelayCommand<object>(CancelRecipe);
  35. RecipeRun();
  36. RecipeStatusInquire();
  37. }
  38. private async void CancelRecipe(object o)
  39. {
  40. if (o != null && o is RecipeModel recipe)
  41. {
  42. var res = MessageNotify.GetInstance.ShowDialog($"是否取消配方 【{recipe.RecipeName}】制作", DialogType.Warning);
  43. if (res)
  44. {
  45. int index = Recipes.ToList().FindIndex(p => p.RecipeName == recipe.RecipeName);
  46. if (index >= 0 && index < Recipes.Count)
  47. {
  48. await Task.Factory.StartNew(new Action(() =>
  49. {
  50. Recipes.ElementAt(index).IsEnable = true;
  51. Json<LocalRecipe>.Data.Recipes.ElementAt(index).IsEnable = true;
  52. Recipes.ElementAt(index).Are.Set();
  53. App.Current.Dispatcher.Invoke(new Action(() => { recipeProcesses.Clear(); }));
  54. SiemensDevice.GetInstance.MySiemens.Write("M10.5", true);
  55. MessageNotify.GetInstance.ShowRunLog($"M10.5:true");
  56. Thread.Sleep(1000);
  57. SiemensDevice.GetInstance.MySiemens.Write("M10.5", false);
  58. MessageNotify.GetInstance.ShowRunLog($"M10.5:false");
  59. NoticeDemoViewModel.OpenMsg(EnumPromptType.Success, App.MainWindow, "提示", $"配方 [{recipe.RecipeName}] 取消成功");
  60. MessageNotify.GetInstance.ShowUserLog($"取消配方制作 {recipe.RecipeName}");
  61. }));
  62. }
  63. }
  64. }
  65. }
  66. /// <summary>
  67. /// 配方下发
  68. /// </summary>
  69. private void RecipeIssued(object o)
  70. {
  71. if (o != null && o is string deviceName)
  72. {
  73. int index = Array.FindIndex(Recipes.ToArray(), p => p.RecipeName == deviceName);
  74. if (index >= 0 && index < Recipes.Count)
  75. {
  76. for (int i = 0; i < Recipes.ElementAt(index).RawMaterials.Count; i++)
  77. {
  78. if (Recipes.ElementAt(index).RawMaterials.ElementAt(i).RawMaterialSource == 1)
  79. {
  80. string ip = Recipes.ElementAt(index).RawMaterials.ElementAt(i).DeviceIp;
  81. var device = DeviceInquire.GetInstance.GetDevice(ip);
  82. if (ip == null && ip == "")
  83. {
  84. 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);
  85. return;
  86. }
  87. if (!device.IsConnected)
  88. {
  89. MessageNotify.GetInstance.ShowDialog($"设备 【{device.DeviceName}】 未连接,不允许下发此配方", DialogType.Error);
  90. return;
  91. }
  92. }
  93. }
  94. Recipes.ElementAt(index).IsEnable = false;
  95. Json<LocalRecipe>.Data.Recipes.ElementAt(index).IsEnable = false;
  96. }
  97. MessageNotify.GetInstance.ShowUserLog($"下发工单 {Recipes.ElementAt(index).RecipeName}");
  98. RecipeNames.Enqueue(deviceName);
  99. //var t = RecipeNames.GetHashCode();
  100. var res = Recipes.FirstOrDefault(p => p.RecipeName == deviceName);
  101. UserTreeWait.Add(new RecipeModel { RecipStatus = "等待制作", SerialNum = UserTreeWait.Count + 1, RecipeName = deviceName, RawMaterials = res.RawMaterials });
  102. }
  103. NoticeDemoViewModel.OpenMsg(EnumPromptType.Success, App.MainWindow, "提示", $"配方下发成功!");
  104. }
  105. /// <summary>
  106. /// 配方业务执行
  107. /// </summary>
  108. private void RecipeRun()
  109. {
  110. TaskManage.GetInstance.StartLong(new Action(() =>
  111. {
  112. if (RecipeNames.Count > 0)
  113. {
  114. int index = Array.FindIndex(Recipes.ToArray(), p => p.RecipeName == RecipeNames.ElementAt(0));
  115. if (index >= 0 && index < Recipes.Count)
  116. {
  117. Recipes.ElementAt(index).Are.Reset();
  118. Recipes.ElementAt(index).IsEnable = false;
  119. StockStatus.Clear();
  120. App.Current.Dispatcher.Invoke(new Action(() =>
  121. {
  122. recipeProcesses.Clear();
  123. if (UserTreeWait.Count > 0) UserTreeWait.RemoveAt(0);
  124. }));
  125. CurrentRecipeName = Recipes.ElementAt(index).RecipeName;
  126. #region 配方下发到PLC操作相关
  127. //配方数据写入到输送机
  128. var res = Json<LocalRecipe>.Data.Recipes.FirstOrDefault(p => p.RecipeName == CurrentRecipeName);
  129. List<int> BarrelNum = new List<int>();
  130. ConcurrentDictionary<string, int[]> values = new ();
  131. if (res != null)
  132. {
  133. var tInfo = res.RawMaterials.GroupBy(p => p.Loc);//获取桶号信息
  134. if (tInfo != null)
  135. {
  136. for (int i = 0; i < tInfo.Count(); i++)
  137. {
  138. int data = 0;
  139. for (int m = 0; m < tInfo.ElementAt(i).Count(); m++)
  140. {
  141. var TempName = tInfo.ElementAt(i).ElementAt(m).RawMaterialName;
  142. var tempDevice = DeviceInquire.GetInstance.devices.FirstOrDefault(p => p.DeviceName == TempName);
  143. if (tempDevice != null)
  144. {
  145. /*data = data.SetBitValue((byte)tempDevice.DeviceNum, true);*/
  146. if (tempDevice.DeviceNum > 0)
  147. {
  148. // //int a = data.SetBitValue((byte)tInfo.ElementAt(i).ElementAt(m).Loc, true);
  149. // //byte[] test1 = a.ToBytes(BPA.Helper.DataFormat.BADC);
  150. // //int item = test1.ToInt();
  151. // int item = data.SetBitValue((byte)tInfo.ElementAt(i).ElementAt(m).Loc, true).ToBytes(BPA.Helper.DataFormat.BADC).ToInt();
  152. // int item1 = SiemensDevice.GetInstance.MySiemens.Read<int>($"DB4.DBD{2 + (tempDevice.DeviceNum - 1) * 4}").Content;
  153. ////prop1:
  154. // var RE = SiemensDevice.GetInstance.MySiemens.Write($"DB4.DBD{2 + (tempDevice.DeviceNum - 1) * 4}", item + item1);
  155. // //var values = SiemensDevice.GetInstance.MySiemens.Read<int>($"DB4.DBD{2 + (tempDevice.DeviceNum - 1) * 4}");
  156. // //if ((values != null && values.ToString() != (item + item1).ToString()) || values == null)
  157. // // goto prop1;
  158. // if (RE.IsSuccess)
  159. // MessageNotify.GetInstance.ShowRunLog($"下发配方DB4.DBD{2 + (tempDevice.DeviceNum - 1) * 4} :{(byte)tInfo.ElementAt(i).ElementAt(m).Loc}-{item + item1}-{(item + item1).ToBinString()}");
  160. // else
  161. // MessageNotify.GetInstance.ShowRunLog($"下发配方DB4.DBD{2 + (tempDevice.DeviceNum - 1) * 4} 失败");
  162. int item = data.SetBitValue((byte)tInfo.ElementAt(i).ElementAt(m).Loc, true).ToBytes(BPA.Helper.DataFormat.BADC).ToInt();
  163. string tempAdd = $"DB4.DBD{2 + (tempDevice.DeviceNum - 1) * 4}";
  164. if (!values.ContainsKey(tempAdd)) values.TryAdd(tempAdd, new int[2] { 0,0});
  165. values[tempAdd][0] = values[tempAdd][0] + item;
  166. values[tempAdd][1] = tInfo.ElementAt(i).ElementAt(m).Loc;
  167. }
  168. }
  169. if (tInfo.ElementAt(i).ElementAt(m).Loc > 0)
  170. {
  171. if (!BarrelNum.Contains(tInfo.ElementAt(i).ElementAt(m).Loc))
  172. {
  173. BarrelNum.Add(tInfo.ElementAt(i).ElementAt(m).Loc);
  174. }
  175. }
  176. }
  177. }
  178. }
  179. }
  180. values?.ToList().ForEach(x =>
  181. {
  182. SiemensDevice.GetInstance.MySiemens.Write(x.Key, x.Value[0]).OnSuccess(() =>
  183. {
  184. MessageNotify.GetInstance.ShowRunLog($"下发配方-{x.Key} :桶数:[{x.Value[1]}]-{x.Value[0].ToBinString()}");
  185. }).OnFail(s =>
  186. {
  187. MessageNotify.GetInstance.ShowRunLog($"下发配方-{x.Key} 失败");
  188. });
  189. });
  190. SiemensDevice.GetInstance.MySiemens.Write("DB4.DBD198", BarrelNum.Count);//配方使用桶数写入
  191. SiemensDevice.GetInstance.MySiemens.Write("DB4.DBX0.1", true);//配方执行启动
  192. Stopwatch sw = new Stopwatch();
  193. var recipe = Recipes.ElementAt(index);
  194. ObservableCollection<RawMaterialModel> RawMater = new ObservableCollection<RawMaterialModel>();
  195. foreach (var item in recipe.RawMaterials)
  196. {
  197. RawMater.Add(new RawMaterialModel()
  198. {
  199. RawMaterialName = item.RawMaterialName,
  200. DeviceIp = item.DeviceIp,
  201. DownLimtFeedback = item.DownLimtFeedback,
  202. Loc = item.Loc,
  203. RawMaterialId = item.RawMaterialId,
  204. RawMaterialSource = item.RawMaterialSource,
  205. RawMaterialType = item.RawMaterialType,
  206. RawMaterialWeight = item.RawMaterialWeight,
  207. RecipeStatus = item.RecipeStatus,
  208. SelectIndex = item.SelectIndex,
  209. Status = item.Status,
  210. UpLimtFeedback = item.UpLimtFeedback,
  211. UpLimtWeightFeedback = item.UpLimtWeightFeedback,
  212. WeightFeedback = item.WeightFeedback,
  213. });
  214. }
  215. App.Current.Dispatcher.Invoke(() =>
  216. {
  217. recipeProcesses.Add(new RecipeModel()
  218. {
  219. RawMaterials = RawMater,
  220. IsEnable = recipe.IsEnable,
  221. RecipeName = recipe.RecipeName,
  222. SerialNum = recipe.SerialNum,
  223. RecipCode = recipe.RecipCode,
  224. });
  225. });
  226. sw.Restart();
  227. while (true)
  228. {
  229. if (sw.ElapsedMilliseconds >= 3000 && !GlobalDevice.PlcData.ResComplete)
  230. {
  231. MessageNotify.GetInstance.ShowRunLog("获取配方下发反馈超时");
  232. break;
  233. }
  234. if (GlobalDevice.PlcData.ResComplete)
  235. {
  236. SiemensDevice.GetInstance.MySiemens.Write("DB3.DBX0.1", false);
  237. break;
  238. }
  239. Thread.Sleep(100);
  240. }
  241. #endregion
  242. Recipes.ElementAt(index).Are.WaitOne();//阻塞,直到当前配方完成
  243. RecipeNames.TryDequeue(out string deviceName);
  244. var recipeComple = Recipes.ElementAt(index);
  245. ObservableCollection<RawMaterialModel> RawMaterComple = new ObservableCollection<RawMaterialModel>();
  246. foreach (var item in recipeComple.RawMaterials)
  247. {
  248. RawMaterComple.Add(new RawMaterialModel()
  249. {
  250. RawMaterialName = item.RawMaterialName,
  251. DeviceIp = item.DeviceIp,
  252. DownLimtFeedback = item.DownLimtFeedback,
  253. Loc = item.Loc,
  254. RawMaterialId = item.RawMaterialId,
  255. RawMaterialSource = item.RawMaterialSource,
  256. RawMaterialType = item.RawMaterialType,
  257. RawMaterialWeight = item.RawMaterialWeight,
  258. RecipeStatus = item.RecipeStatus,
  259. SelectIndex = item.SelectIndex,
  260. Status = item.Status,
  261. UpLimtFeedback = item.UpLimtFeedback,
  262. UpLimtWeightFeedback = item.UpLimtWeightFeedback,
  263. WeightFeedback = item.WeightFeedback,
  264. });
  265. }
  266. App.Current.Dispatcher.Invoke(() =>
  267. {
  268. UserTreeCompelete.Add(new RecipeModel()
  269. {
  270. RawMaterials = RawMaterComple,
  271. IsEnable = recipeComple.IsEnable,
  272. RecipeName = recipeComple.RecipeName,
  273. SerialNum = UserTreeCompelete.Count + 1,
  274. RecipCode = recipeComple.RecipCode,
  275. RecipStatus = "制作完成"
  276. });
  277. });
  278. App.Current.Dispatcher.Invoke(new Action(() =>
  279. {
  280. recipeProcesses.Clear();
  281. CurrentRecipeName = string.Empty;
  282. }));//完成后清空当前配方
  283. }
  284. }
  285. Thread.Sleep(100);
  286. }), "启动配方下发", true);
  287. }
  288. /// <summary>
  289. /// 配方执行状态监听
  290. /// </summary>
  291. private void RecipeStatusInquire()
  292. {
  293. TaskManage.GetInstance.StartLong(new Action(() =>
  294. {
  295. #region v1.0
  296. //for (int i = 0; i < Recipes.Count; i++)
  297. //{
  298. // for (int m = 0; m < Recipes.ElementAt(i).RawMaterials.Count; m++)
  299. // {
  300. // var RunStatus = DeviceInquire.GetInstance.GetDevice(Recipes.ElementAt(i).RawMaterials.ElementAt(m).DeviceIp).deviceStatus.RunStatus;
  301. // //设备状态显示
  302. // if (Recipes.ElementAt(i).RecipeName == CurrentRecipeName)
  303. // {
  304. // string tempRawMaterialName = DeviceInquire.GetInstance.GetDevice(Recipes.ElementAt(i).RawMaterials.ElementAt(m).DeviceIp).DeviceName;
  305. // int recIndex = recipeProcesses.ToList().FindIndex(p => p.RecipeName == CurrentRecipeName);
  306. // if (recIndex >= 0 && recIndex < recipeProcesses.Count)
  307. // {
  308. // int index = recipeProcesses.ElementAt(recIndex).RawMaterials.ToList().FindIndex(p => p.RawMaterialName == tempRawMaterialName);
  309. // if (index >= 0 && index < recipeProcesses.ElementAt(recIndex).RawMaterials.Count)
  310. // {
  311. // //测试使用
  312. // recipeProcesses.ElementAt(recIndex).RawMaterials.ElementAt(index).RecipeStatus = RunStatus;
  313. // }
  314. // for (int j = 0; j < GlobalDevice.PlcData.IsAllowIngredients.Length; j++)
  315. // {
  316. // if (GlobalDevice.PlcData.IsAllowIngredients[j])
  317. // {
  318. // int barrel = j + 1;//桶号
  319. // int cnt = GlobalDevice.PlcData.LocationFeedback[j];//允许配料的位置 0~6
  320. // if (RTrig.GetInstance("a").Start(GlobalDevice.PlcData.IsAllowIngredients[j]))
  321. // {
  322. // MessageNotify.GetInstance.ShowRunLog($"桶号:{barrel},位置:{cnt},允许配料");
  323. // }
  324. // int Location1 = 0;
  325. // int Location2 = 0;
  326. // if (cnt == -1)
  327. // {
  328. // MessageNotify.GetInstance.ShowRunLog("错误");
  329. // return;
  330. // }
  331. // else
  332. // {
  333. // if (cnt >= 1 && cnt <= 6)
  334. // {
  335. // //int a1 = Array.FindIndex(DeviceInquire.GetInstance.devices.ToArray(), p => p.DeviceNum == cnt * 2 - 1);
  336. // //int b1 = Array.FindIndex(DeviceInquire.GetInstance.devices.ToArray(), p => p.DeviceNum == cnt * 2);
  337. // //if (a1 >= 0)
  338. // //{
  339. // // Location1 = Array.FindIndex(Recipes.ElementAt(i).RawMaterials.ToArray(), p => p.RawMaterialName == DeviceInquire.GetInstance.devices.ElementAt(a1).DeviceName && p.Loc == barrel);
  340. // //}
  341. // //if (b1 >= 0)
  342. // //{
  343. // // Location2 = Array.FindIndex(Recipes.ElementAt(i).RawMaterials.ToArray(), p => p.RawMaterialName == DeviceInquire.GetInstance.devices.ElementAt(b1).DeviceName && p.Loc == barrel);
  344. // //}
  345. // int a1 = Array.FindIndex(DeviceInquire.GetInstance.devices.ToArray(), p => p.DeviceNum == cnt);
  346. // //int b1 = Array.FindIndex(DeviceInquire.GetInstance.devices.ToArray(), p => p.DeviceNum == cnt * 2);
  347. // if (a1 >= 0)
  348. // {
  349. // Location1 = Array.FindIndex(Recipes.ElementAt(i).RawMaterials.ToArray(), p => p.RawMaterialName == DeviceInquire.GetInstance.devices.ElementAt(a1).DeviceName && p.Loc == barrel);
  350. // }
  351. // //if (b1 >= 0)
  352. // //{
  353. // // Location2 = Array.FindIndex(Recipes.ElementAt(i).RawMaterials.ToArray(), p => p.RawMaterialName == DeviceInquire.GetInstance.devices.ElementAt(b1).DeviceName && p.Loc == barrel);
  354. // //}
  355. // Location2 = Location1;
  356. // }
  357. // }
  358. // if (Location1 >= 0)
  359. // {
  360. // string RawName = Recipes.ElementAt(i).RawMaterials.ElementAt(Location1).RawMaterialName;
  361. // int abc = Array.FindIndex(DeviceInquire.GetInstance.devices.ToArray(), p => p.DeviceName == RawName);
  362. // int DeviceNum = DeviceInquire.GetInstance.devices.ElementAt(abc).DeviceNum;
  363. // if (DeviceNum > 0)
  364. // {
  365. // float weight = Recipes.ElementAt(i).RawMaterials.ElementAt(Location1).RawMaterialWeight;
  366. // //测试
  367. // int loc = Array.FindIndex(Recipes.ElementAt(i).RawMaterials.ToArray(), p => (p.RawMaterialName == RawName));
  368. // if (loc >= 0 && RawName != null && weight >= 0)
  369. // {
  370. // int St_index = Array.FindIndex(StockStatus.ToArray(), p => p.MaterialName == RawName);
  371. // int materialCount = recipeProcesses.ElementAt(recIndex).RawMaterials.Where(p => p.RawMaterialName == RawName).Count();
  372. // if (St_index < 0 || (StockStatus.Count < materialCount && St_index > 0))
  373. // {
  374. // StockStatus.Add(new StockStatusModel()
  375. // {
  376. // MaterialName = RawName,
  377. // IssueWeight = weight,
  378. // IssueStatus = 0,
  379. // });
  380. // }
  381. // St_index = Array.FindIndex(StockStatus.ToArray(), p => (p.MaterialName == RawName && p.IssueStatus == 0));
  382. // int St_index1 = Array.FindIndex(StockStatus.ToArray(), p => (p.MaterialName == RawName && p.IssueStatus == 1));
  383. // if (St_index >= 0)
  384. // {
  385. // if ((recipeProcesses.ElementAt(recIndex).RawMaterials.ElementAt(loc).RecipeStatus == 1 && StockStatus.ElementAt(St_index).IssueStatus == 0))//
  386. // {
  387. // StockStatus.ElementAt(St_index).IssueStatus = 1;
  388. // DeviceInquire.GetInstance.GetDevice(Recipes.ElementAt(i).RawMaterials.ElementAt(loc).DeviceIp).Start(weight);
  389. // MessageNotify.GetInstance.ShowRunLog($"柔性味魔方{Recipes.ElementAt(i).RawMaterials.ElementAt(loc).DeviceIp},开始出料");
  390. // }
  391. // //if (recipeProcesses.ElementAt(recIndex).RawMaterials.ElementAt(loc).RecipeStatus == 3 && StockStatus.ElementAt(St_index).IssueStatus == 1)
  392. // //{
  393. // // //GlobalDevice.PlcData.IsAllowIngredients[j] = false;//测试使用
  394. // // StockStatus.ElementAt(St_index).IssueStatus = 2;
  395. // // DeviceInquire.GetInstance.GetDevice(Recipes.ElementAt(i).RawMaterials.ElementAt(loc).DeviceIp).StatusReset();
  396. // // int a = 0.SetBitValue((byte)(DeviceNum), true);
  397. // // byte[] test1 = a.ToBytes(BPA.Helper.DataFormat.BADC);
  398. // // int item = test1.ToInt();
  399. // // SiemensDevice.GetInstance.MySiemens.Write("DB4.DBD130", item);
  400. // // //MessageNotify.GetInstance.ShowRunLog($"{res}");
  401. // // MessageNotify.GetInstance.ShowRunLog($"DB4.DBD130:{item.ToBinString()}");
  402. // // MessageNotify.GetInstance.ShowRunLog($"柔性味魔方{Recipes.ElementAt(i).RawMaterials.ElementAt(loc).DeviceIp},出料完成,状态复位");
  403. // //}
  404. // //测试
  405. // if (recipeProcesses.ElementAt(recIndex).RawMaterials.ElementAt(loc).RecipeStatus == 3 && StockStatus.ElementAt(St_index1).IssueStatus == 1)
  406. // {
  407. // //GlobalDevice.PlcData.IsAllowIngredients[j] = false;//测试使用
  408. // StockStatus.ElementAt(St_index1).IssueStatus = 2;
  409. // DeviceInquire.GetInstance.GetDevice(Recipes.ElementAt(i).RawMaterials.ElementAt(loc).DeviceIp).StatusReset();
  410. // int a = 0.SetBitValue((byte)(DeviceNum), true);
  411. // byte[] test1 = a.ToBytes(BPA.Helper.DataFormat.BADC);
  412. // int item = test1.ToInt();
  413. // SiemensDevice.GetInstance.MySiemens.Write("DB4.DBD130", item);
  414. // //MessageNotify.GetInstance.ShowRunLog($"{res}");
  415. // MessageNotify.GetInstance.ShowRunLog($"DB4.DBD130:{item.ToBinString()}");
  416. // MessageNotify.GetInstance.ShowRunLog($"柔性味魔方{Recipes.ElementAt(i).RawMaterials.ElementAt(loc).DeviceIp},出料完成,状态复位");
  417. // }
  418. // }
  419. // }
  420. // }
  421. // }
  422. // //调试
  423. // //if (Location1 >= 0)
  424. // //{
  425. // // string RawName = Recipes.ElementAt(i).RawMaterials.ElementAt(Location1).RawMaterialName;
  426. // // int abc = Array.FindIndex(DeviceInquire.GetInstance.devices.ToArray(), p => p.DeviceName == RawName);
  427. // // int DeviceNum = DeviceInquire.GetInstance.devices.ElementAt(abc).DeviceNum;
  428. // // if (DeviceNum > 0)
  429. // // {
  430. // // float weight = Recipes.ElementAt(i).RawMaterials.ElementAt(Location1).RawMaterialWeight;
  431. // // //测试
  432. // // // int loc = Array.FindIndex(Recipes.ElementAt(i).RawMaterials.ToArray(), p => (p.RawMaterialName == RawName));
  433. // // if (Location1 >= 0 && RawName != null && weight >= 0)
  434. // // {
  435. // // int St_index = Array.FindIndex(StockStatus.ToArray(), p => p.MaterialName == RawName);
  436. // // int materialCount = recipeProcesses.ElementAt(recIndex).RawMaterials.Where(p => p.RawMaterialName == RawName).Count();
  437. // // if (St_index < 0 || (StockStatus.Count < materialCount && St_index > 0))
  438. // // {
  439. // // StockStatus.Add(new StockStatusModel()
  440. // // {
  441. // // MaterialName = RawName,
  442. // // IssueWeight = weight,
  443. // // IssueStatus = 0,
  444. // // });
  445. // // }
  446. // // St_index = Array.FindIndex(StockStatus.ToArray(), p => (p.MaterialName == RawName && p.IssueStatus == 0));
  447. // // int St_index1 = Array.FindIndex(StockStatus.ToArray(), p => (p.MaterialName == RawName && p.IssueStatus == 1));
  448. // // if (St_index >= 0)
  449. // // {
  450. // // if ((recipeProcesses.ElementAt(recIndex).RawMaterials.ElementAt(Location1).RecipeStatus == 1 && StockStatus.ElementAt(St_index).IssueStatus == 0))//
  451. // // {
  452. // // StockStatus.ElementAt(St_index).IssueStatus = 1;
  453. // // DeviceInquire.GetInstance.GetDevice(Recipes.ElementAt(i).RawMaterials.ElementAt(Location1).DeviceIp).Start(weight);
  454. // // MessageNotify.GetInstance.ShowRunLog($"柔性味魔方{Recipes.ElementAt(i).RawMaterials.ElementAt(Location1).DeviceIp},开始出料");
  455. // // }
  456. // // //if (recipeProcesses.ElementAt(recIndex).RawMaterials.ElementAt(loc).RecipeStatus == 3 && StockStatus.ElementAt(St_index).IssueStatus == 1)
  457. // // //{
  458. // // // //GlobalDevice.PlcData.IsAllowIngredients[j] = false;//测试使用
  459. // // // StockStatus.ElementAt(St_index).IssueStatus = 2;
  460. // // // DeviceInquire.GetInstance.GetDevice(Recipes.ElementAt(i).RawMaterials.ElementAt(loc).DeviceIp).StatusReset();
  461. // // // int a = 0.SetBitValue((byte)(DeviceNum), true);
  462. // // // byte[] test1 = a.ToBytes(BPA.Helper.DataFormat.BADC);
  463. // // // int item = test1.ToInt();
  464. // // // SiemensDevice.GetInstance.MySiemens.Write("DB4.DBD130", item);
  465. // // // //MessageNotify.GetInstance.ShowRunLog($"{res}");
  466. // // // MessageNotify.GetInstance.ShowRunLog($"DB4.DBD130:{item.ToBinString()}");
  467. // // // MessageNotify.GetInstance.ShowRunLog($"柔性味魔方{Recipes.ElementAt(i).RawMaterials.ElementAt(loc).DeviceIp},出料完成,状态复位");
  468. // // //}
  469. // // //测试
  470. // // if (recipeProcesses.ElementAt(recIndex).RawMaterials.ElementAt(Location1).RecipeStatus == 3 && StockStatus.ElementAt(St_index1).IssueStatus == 1)
  471. // // {
  472. // // //GlobalDevice.PlcData.IsAllowIngredients[j] = false;//测试使用
  473. // // StockStatus.ElementAt(St_index1).IssueStatus = 2;
  474. // // DeviceInquire.GetInstance.GetDevice(Recipes.ElementAt(i).RawMaterials.ElementAt(Location1).DeviceIp).StatusReset();
  475. // // int a = 0.SetBitValue((byte)(DeviceNum), true);
  476. // // byte[] test1 = a.ToBytes(BPA.Helper.DataFormat.BADC);
  477. // // int item = test1.ToInt();
  478. // // SiemensDevice.GetInstance.MySiemens.Write("DB4.DBD130", item);
  479. // // //MessageNotify.GetInstance.ShowRunLog($"{res}");
  480. // // MessageNotify.GetInstance.ShowRunLog($"DB4.DBD130:{item.ToBinString()}");
  481. // // MessageNotify.GetInstance.ShowRunLog($"柔性味魔方{Recipes.ElementAt(i).RawMaterials.ElementAt(Location1).DeviceIp},出料完成,状态复位");
  482. // // }
  483. // // }
  484. // // }
  485. // // }
  486. // //}
  487. // if (Location1 >= 0)
  488. // {
  489. // string RawName111 = Recipes.ElementAt(i).RawMaterials.ElementAt(Location1).RawMaterialName;
  490. // int St_index111 = Array.FindIndex(StockStatus.ToArray(), p => p.MaterialName == RawName111);
  491. // if (Location2 >= 0 && ((Location1 >= 0 && StockStatus.ElementAt(St_index111).IssueStatus == 2)))
  492. // {
  493. // string RawName = Recipes.ElementAt(i).RawMaterials.ElementAt(Location2).RawMaterialName;
  494. // int abc = Array.FindIndex(DeviceInquire.GetInstance.devices.ToArray(), p => p.DeviceName == RawName);
  495. // int DeviceNum = DeviceInquire.GetInstance.devices.ElementAt(abc).DeviceNum;
  496. // if (DeviceNum > 0)
  497. // {
  498. // float weight = Recipes.ElementAt(i).RawMaterials.ElementAt(Location2).RawMaterialWeight;
  499. // int loc = Array.FindIndex(Recipes.ElementAt(i).RawMaterials.ToArray(), p => p.RawMaterialName == RawName);
  500. // if (loc >= 0 && RawName != null && weight >= 0)
  501. // {
  502. // int St_index = Array.FindIndex(StockStatus.ToArray(), p => p.MaterialName == RawName);
  503. // if (St_index < 0)
  504. // {
  505. // StockStatus.Add(new StockStatusModel()
  506. // {
  507. // MaterialName = RawName,
  508. // IssueWeight = weight,
  509. // IssueStatus = 0,
  510. // });
  511. // }
  512. // St_index = Array.FindIndex(StockStatus.ToArray(), p => p.MaterialName == RawName);
  513. // if (St_index >= 0)
  514. // {
  515. // if (recipeProcesses.ElementAt(recIndex).RawMaterials.ElementAt(loc).RecipeStatus == 1 && StockStatus.ElementAt(St_index).IssueStatus == 0)
  516. // {
  517. // StockStatus.ElementAt(St_index).IssueStatus = 1;
  518. // DeviceInquire.GetInstance.GetDevice(Recipes.ElementAt(i).RawMaterials.ElementAt(loc).DeviceIp).Start(weight);
  519. // MessageNotify.GetInstance.ShowRunLog($"柔性味魔方{Recipes.ElementAt(i).RawMaterials.ElementAt(loc).DeviceIp},开始出料");
  520. // }
  521. // if (recipeProcesses.ElementAt(recIndex).RawMaterials.ElementAt(loc).RecipeStatus == 3 && StockStatus.ElementAt(St_index).IssueStatus == 1)
  522. // {
  523. // //GlobalDevice.PlcData.IsAllowIngredients[j] = false;//测试使用
  524. // StockStatus.ElementAt(St_index).IssueStatus = 2;
  525. // DeviceInquire.GetInstance.GetDevice(Recipes.ElementAt(i).RawMaterials.ElementAt(loc).DeviceIp).StatusReset();
  526. // int a = 0.SetBitValue((byte)(DeviceNum), true);
  527. // byte[] test1 = a.ToBytes(BPA.Helper.DataFormat.BADC);
  528. // int item = test1.ToInt();
  529. // SiemensDevice.GetInstance.MySiemens.Write("DB4.DBD130", item);
  530. // //MessageNotify.GetInstance.ShowRunLog($"{res}");
  531. // //MessageNotify.GetInstance.ShowRunLog($"DB4.DBD130:{item.ToBinString()}");
  532. // MessageNotify.GetInstance.ShowRunLog($"DB4.DBD130:{item}");
  533. // MessageNotify.GetInstance.ShowRunLog($"柔性味魔方{Recipes.ElementAt(i).RawMaterials.ElementAt(loc).DeviceIp},出料完成,状态复位");
  534. // }
  535. // }
  536. // }
  537. // }
  538. // }
  539. // }
  540. // else
  541. // {
  542. // if (Location2 >= 0)
  543. // {
  544. // string RawName = Recipes.ElementAt(i).RawMaterials.ElementAt(Location2).RawMaterialName;
  545. // int abc = Array.FindIndex(DeviceInquire.GetInstance.devices.ToArray(), p => p.DeviceName == RawName);
  546. // int DeviceNum = DeviceInquire.GetInstance.devices.ElementAt(abc).DeviceNum;
  547. // if (DeviceNum >= 0)
  548. // {
  549. // float weight = Recipes.ElementAt(i).RawMaterials.ElementAt(Location2).RawMaterialWeight;
  550. // int loc = Array.FindIndex(Recipes.ElementAt(i).RawMaterials.ToArray(), p => p.RawMaterialName == RawName);
  551. // if (cnt > 0 && loc >= 0 && RawName != null && weight >= 0)
  552. // {
  553. // int St_index = Array.FindIndex(StockStatus.ToArray(), p => p.MaterialName == RawName);
  554. // if (St_index < 0)
  555. // {
  556. // StockStatus.Add(new StockStatusModel()
  557. // {
  558. // MaterialName = RawName,
  559. // IssueWeight = weight,
  560. // IssueStatus = 0,
  561. // });
  562. // }
  563. // St_index = Array.FindIndex(StockStatus.ToArray(), p => p.MaterialName == RawName);
  564. // if (St_index >= 0)
  565. // {
  566. // if (recipeProcesses.ElementAt(recIndex).RawMaterials.ElementAt(loc).RecipeStatus == 1 && StockStatus.ElementAt(St_index).IssueStatus == 0)
  567. // {
  568. // StockStatus.ElementAt(St_index).IssueStatus = 1;
  569. // DeviceInquire.GetInstance.GetDevice(Recipes.ElementAt(i).RawMaterials.ElementAt(loc).DeviceIp).Start(weight);
  570. // MessageNotify.GetInstance.ShowRunLog($"柔性味魔方{Recipes.ElementAt(i).RawMaterials.ElementAt(loc).DeviceIp},开始出料");
  571. // }
  572. // if (recipeProcesses.ElementAt(recIndex).RawMaterials.ElementAt(loc).RecipeStatus == 3 && StockStatus.ElementAt(St_index).IssueStatus == 1)
  573. // {
  574. // //GlobalDevice.PlcData.IsAllowIngredients[j] = false;//测试使用
  575. // StockStatus.ElementAt(St_index).IssueStatus = 2;
  576. // DeviceInquire.GetInstance.GetDevice(Recipes.ElementAt(i).RawMaterials.ElementAt(loc).DeviceIp).StatusReset();
  577. // int a = 0.SetBitValue((byte)(DeviceNum), true);
  578. // byte[] test1 = a.ToBytes(BPA.Helper.DataFormat.BADC);
  579. // int item = test1.ToInt();
  580. // SiemensDevice.GetInstance.MySiemens.Write("DB4.DBD130", item);
  581. // recipeProcesses.ElementAt(recIndex).RawMaterials.ElementAt(loc).UpLimtWeightFeedback = DeviceInquire.GetInstance.GetDevice(Recipes.ElementAt(i).RawMaterials.ElementAt(loc).DeviceIp).deviceStatus.NowWeightFeedback;
  582. // //MessageNotify.GetInstance.ShowRunLog($"{res}");
  583. // MessageNotify.GetInstance.ShowRunLog($"DB4.DBD130:{item.ToBinString()}");
  584. // MessageNotify.GetInstance.ShowRunLog($"柔性味魔方{Recipes.ElementAt(i).RawMaterials.ElementAt(loc).DeviceIp},出料完成,状态复位");
  585. // }
  586. // }
  587. // }
  588. // }
  589. // }
  590. // }
  591. // }
  592. // //508配料项目代码
  593. // //if (GlobalDevice.PlcData.IsAllowIngredients[j])
  594. // //{
  595. // // int barrel = j + 1;//桶号
  596. // // int cnt = GlobalDevice.PlcData.LocationFeedback[j];//允许配料的位置 1~10
  597. // // if (RTrig.GetInstance("a").Start(GlobalDevice.PlcData.IsAllowIngredients[j]))
  598. // // {
  599. // // MessageNotify.GetInstance.ShowRunLog($"桶号:{barrel},位置:{cnt},允许配料");
  600. // // }
  601. // // int Location1 = 0;
  602. // // if (cnt == -1)
  603. // // {
  604. // // MessageNotify.GetInstance.ShowRunLog("没有读取到配料的位置");
  605. // // return;
  606. // // }
  607. // // else
  608. // // {
  609. // // int a1 = Array.FindIndex(DeviceInquire.GetInstance.devices.ToArray(), p => p.DeviceNum == cnt);
  610. // // Location1 = Array.FindIndex(Recipes.ElementAt(i).RawMaterials.ToArray(), p => p.RawMaterialName == DeviceInquire.GetInstance.devices.ElementAt(a1).DeviceName && p.Loc == barrel);
  611. // // }
  612. // // if (Location1 >= 0)
  613. // // {
  614. // // string RawName = Recipes.ElementAt(i).RawMaterials.ElementAt(Location1).RawMaterialName;
  615. // // int abc = Array.FindIndex(DeviceInquire.GetInstance.devices.ToArray(), p => p.DeviceName == RawName);
  616. // // int DeviceNum = DeviceInquire.GetInstance.devices.ElementAt(abc).DeviceNum;
  617. // // if (DeviceNum > 0)
  618. // // {
  619. // // float weight = Recipes.ElementAt(i).RawMaterials.ElementAt(Location1).RawMaterialWeight;
  620. // // int loc = Array.FindIndex(Recipes.ElementAt(i).RawMaterials.ToArray(), p => p.RawMaterialName == RawName);
  621. // // if (loc >= 0 && RawName != null && weight >= 0)
  622. // // {
  623. // // int St_index = Array.FindIndex(StockStatus.ToArray(), p => p.MaterialName == RawName);
  624. // // if (St_index < 0)
  625. // // {
  626. // // StockStatus.Add(new StockStatusModel()
  627. // // {
  628. // // MaterialName = RawName,
  629. // // IssueWeight = weight,
  630. // // IssueStatus = 0,
  631. // // });
  632. // // }
  633. // // St_index = Array.FindIndex(StockStatus.ToArray(), p => p.MaterialName == RawName);
  634. // // if (St_index >= 0)
  635. // // {
  636. // // if (recipeProcesses.ElementAt(recIndex).RawMaterials.ElementAt(loc).RecipeStatus == 1 && StockStatus.ElementAt(St_index).IssueStatus == 0)
  637. // // {
  638. // // StockStatus.ElementAt(St_index).IssueStatus = 1;
  639. // // DeviceInquire.GetInstance.GetDevice(Recipes.ElementAt(i).RawMaterials.ElementAt(loc).DeviceIp).Start(weight);
  640. // // MessageNotify.GetInstance.ShowRunLog($"柔性味魔方{Recipes.ElementAt(i).RawMaterials.ElementAt(loc).DeviceIp},开始出料");
  641. // // }
  642. // // if (recipeProcesses.ElementAt(recIndex).RawMaterials.ElementAt(loc).RecipeStatus == 3 && StockStatus.ElementAt(St_index).IssueStatus == 1)
  643. // // {
  644. // // //GlobalDevice.PlcData.IsAllowIngredients[j] = false;//测试使用
  645. // // StockStatus.ElementAt(St_index).IssueStatus = 2;
  646. // // DeviceInquire.GetInstance.GetDevice(Recipes.ElementAt(i).RawMaterials.ElementAt(loc).DeviceIp).StatusReset();
  647. // // /*int a = 0.SetBitValue((byte)(DeviceNum), true);
  648. // // byte[] test1 = a.ToBytes(BPA.Helper.DataFormat.ABCD);
  649. // // int item = test1.ToInt();*/
  650. // // if (DeviceNum >= 1 && DeviceNum <= 8)
  651. // // {
  652. // // SiemensDevice.GetInstance.MySiemens.Write("DB4.DBX130." + (DeviceNum - 1), true);
  653. // // }else if (DeviceNum >= 9 && DeviceNum <= 16)
  654. // // {
  655. // // SiemensDevice.GetInstance.MySiemens.Write("DB4.DBX131." + (DeviceNum - 9), true);
  656. // // }
  657. // // else if (DeviceNum >=17 && DeviceNum <= 24)
  658. // // {
  659. // // SiemensDevice.GetInstance.MySiemens.Write("DB4.DBX132." + (DeviceNum - 17), true);
  660. // // }else if (DeviceNum >= 25 && DeviceNum <= 32)
  661. // // {
  662. // // SiemensDevice.GetInstance.MySiemens.Write("DB4.DBX133." + (DeviceNum - 25), true);
  663. // // }
  664. // // MessageNotify.GetInstance.ShowRunLog($"料仓:{DeviceNum},配料完成");
  665. // // /*string res = SiemensDevice.GetInstance.MySiemens.Write("DB4.DBD130", item, 2);
  666. // // MessageNotify.GetInstance.ShowRunLog($"{res}");
  667. // // MessageNotify.GetInstance.ShowRunLog($"DB4.DBD130:{item.ToBinString()}");*/
  668. // // MessageNotify.GetInstance.ShowRunLog($"柔性味魔方{Recipes.ElementAt(i).RawMaterials.ElementAt(loc).DeviceIp},出料完成,状态复位");
  669. // // }
  670. // // }
  671. // // }
  672. // // }
  673. // // }
  674. // //}
  675. // }
  676. // int Finish_Count = StockStatus.Where(s => s.IssueStatus == 2).Count();
  677. // int Finish = Recipes.ElementAt(i).RawMaterials.Where(p => p.RecipeStatus == 3).Count();
  678. // if (StockStatus.Count >= Recipes.ElementAt(i).RawMaterials.Count && Finish == Recipes.ElementAt(i).RawMaterials.Count) //配方配料完成 Finish_Count >= Recipes.ElementAt(i).RawMaterials.Count
  679. // {
  680. // StockStatus.Clear();
  681. // MessageNotify.GetInstance.ShowUserLog($"配方:{recipeProcesses.ElementAt(recIndex).RecipeName},配料完成");
  682. // foreach (var item in recipeProcesses.ElementAt(recIndex).RawMaterials)
  683. // {
  684. // MessageNotify.GetInstance.ShowUserLog($"原料:{item.RawMaterialName},桶号:{item.Loc},设定配料重量{item.RawMaterialWeight}g,实际配料重量{item.UpLimtWeightFeedback}g");
  685. // }
  686. // SiemensDevice.GetInstance.MySiemens.Write("DB4.DBX202.0", true);//配料完成
  687. // MessageNotify.GetInstance.ShowRunLog($"配方:{CurrentRecipeName},配料完成");
  688. // int recipIndex = Array.FindIndex(Recipes.ToArray(), p => p.RecipeName == CurrentRecipeName);
  689. // App.Current.Dispatcher.Invoke(new Action(() => { recipeProcesses.Clear(); }));
  690. // Recipes.ElementAt(recipIndex).IsEnable = true;
  691. // Json<LocalRecipe>.Data.Recipes.ElementAt(recipIndex).IsEnable = true;
  692. // Recipes.ElementAt(recipIndex).Are.Set();
  693. // Thread.Sleep(100);
  694. // }
  695. // }
  696. // }
  697. // }
  698. //}
  699. #endregion
  700. #region 正式版
  701. for (int i = 0; i < Recipes.Count; i++)
  702. {
  703. for (int m = 0; m < Recipes.ElementAt(i).RawMaterials.Count; m++)
  704. {
  705. var RunStatus = DeviceInquire.GetInstance.GetDevice(Recipes.ElementAt(i).RawMaterials.ElementAt(m).DeviceIp).deviceStatus.RunStatus;
  706. //设备状态显示
  707. if (Recipes.ElementAt(i).RecipeName == CurrentRecipeName)
  708. {
  709. string tempRawMaterialName = DeviceInquire.GetInstance.GetDevice(Recipes.ElementAt(i).RawMaterials.ElementAt(m).DeviceIp).DeviceName;
  710. int recIndex = recipeProcesses.ToList().FindIndex(p => p.RecipeName == CurrentRecipeName);
  711. if (recIndex >= 0 && recIndex < recipeProcesses.Count)
  712. {
  713. int index = recipeProcesses.ElementAt(recIndex).RawMaterials.ToList().FindIndex(p => p.RawMaterialName == tempRawMaterialName);
  714. if (index >= 0 && index < recipeProcesses.ElementAt(recIndex).RawMaterials.Count)
  715. {
  716. //测试使用
  717. recipeProcesses.ElementAt(recIndex).RawMaterials.ElementAt(index).RecipeStatus = RunStatus;
  718. }
  719. for (int j = 0; j < GlobalDevice.PlcData.IsAllowIngredients.Length; j++)
  720. {
  721. if (GlobalDevice.PlcData.IsAllowIngredients[j])
  722. {
  723. int barrel = j + 1;//桶号
  724. int cnt = GlobalDevice.PlcData.LocationFeedback[j];//允许配料的位置 1~6
  725. if (RTrig.GetInstance($"a{j}").Start(GlobalDevice.PlcData.IsAllowIngredients[j]))
  726. {
  727. MessageNotify.GetInstance.ShowRunLog($"桶号:{barrel},位置:{cnt},允许配料");
  728. }
  729. int Location1 = 0;
  730. int Location2 = 0;
  731. if (cnt == -1)
  732. {
  733. MessageNotify.GetInstance.ShowRunLog("错误");
  734. return;
  735. }
  736. else
  737. {
  738. if (cnt >= 1 && cnt <= 6)
  739. {
  740. int a1 = Array.FindIndex(DeviceInquire.GetInstance.devices.ToArray(), p => p.DeviceNum == cnt);
  741. if (a1 >= 0)
  742. {
  743. Location1 = Array.FindIndex(Recipes.ElementAt(i).RawMaterials.ToArray(), p => p.RawMaterialName == DeviceInquire.GetInstance.devices.ElementAt(a1).DeviceName && p.Loc == barrel);
  744. }
  745. }
  746. }
  747. if (Location1 >= 0)
  748. {
  749. string RawName = Recipes.ElementAt(i).RawMaterials.ElementAt(Location1).RawMaterialName;
  750. int abc = Array.FindIndex(DeviceInquire.GetInstance.devices.ToArray(), p => p.DeviceName == RawName);
  751. int DeviceNum = DeviceInquire.GetInstance.devices.ElementAt(abc).DeviceNum;
  752. if (DeviceNum > 0)
  753. {
  754. float weight = Recipes.ElementAt(i).RawMaterials.ElementAt(Location1).RawMaterialWeight;
  755. int loc = Array.FindIndex(Recipes.ElementAt(i).RawMaterials.ToArray(), p => (p.RawMaterialName == RawName));
  756. if (loc >= 0 && RawName != null && weight >= 0)
  757. {
  758. int St_index = Array.FindIndex(StockStatus.ToArray(), p => p.MaterialName == RawName);
  759. if (St_index < 0) //|| (StockStatus.Count < materialCount && St_index > 0)
  760. {
  761. StockStatus.Add(new StockStatusModel()
  762. {
  763. MaterialName = RawName,
  764. IssueWeight = weight,
  765. IssueStatus = 0,
  766. });
  767. }
  768. St_index = Array.FindIndex(StockStatus.ToArray(), p => p.MaterialName == RawName);//&& p.IssueStatus == 0)
  769. if (St_index >= 0)
  770. {
  771. if (RTrig.GetInstance($"ac{j}-{St_index}").Start(GlobalDevice.PlcData.IsAllowIngredients[j]))
  772. {
  773. MessageNotify.GetInstance.ShowRunLog($"{St_index}-{j}--》允许配料");
  774. if (StockStatus.ElementAt(St_index).IssueStatus == 2)
  775. {
  776. StockStatus.ElementAt(St_index).IssueStatus = 0;
  777. MessageNotify.GetInstance.ShowRunLog($"{St_index}-复位");
  778. }
  779. }
  780. var rrr = DeviceInquire.GetInstance.GetDevice(Recipes.ElementAt(i).RawMaterials.ElementAt(loc).DeviceIp).deviceStatus.RunStatus;
  781. if ((recipeProcesses.ElementAt(recIndex).RawMaterials.ElementAt(loc).RecipeStatus == 1 && StockStatus.ElementAt(St_index).IssueStatus == 0))//
  782. {
  783. StockStatus.ElementAt(St_index).IssueStatus = 1;
  784. DeviceInquire.GetInstance.GetDevice(Recipes.ElementAt(i).RawMaterials.ElementAt(loc).DeviceIp).Start(weight);
  785. var res = recipeProcesses.ElementAt(recIndex).RawMaterials.FirstOrDefault(p => p.Status !=Status.正在配料 &&p.Status!=Status.配料完成&& p.RawMaterialName == tempRawMaterialName && p.Loc == j + 1);
  786. if (res != null)
  787. res.Status = Status.正在配料;
  788. MessageNotify.GetInstance.ShowRunLog($"设备执行状态:{rrr}");
  789. MessageNotify.GetInstance.ShowRunLog($"柔性味魔方{Recipes.ElementAt(i).RawMaterials.ElementAt(loc).DeviceIp},开始出料");
  790. }
  791. if (recipeProcesses.ElementAt(recIndex).RawMaterials.ElementAt(loc).RecipeStatus == 3 && StockStatus.ElementAt(St_index).IssueStatus == 1)
  792. {
  793. //GlobalDevice.PlcData.IsAllowIngredients[j] = false;//测试使用
  794. ComCount++;
  795. StockStatus.ElementAt(St_index).IssueStatus = 2;
  796. DeviceInquire.GetInstance.GetDevice(Recipes.ElementAt(i).RawMaterials.ElementAt(loc).DeviceIp).StatusReset();
  797. //int a = 0.SetBitValue((byte)(DeviceNum), true);
  798. //byte[] test1 = a.ToBytes(BPA.Helper.DataFormat.BADC);
  799. //int item = test1.ToInt();
  800. //prop1:
  801. // SiemensDevice.GetInstance.MySiemens.Write("DB4.DBD130", item);
  802. SiemensDevice.GetInstance.MySiemens.Write($"DB4.DBX130.{DeviceNum - 1}", true);
  803. Task.Run(() =>
  804. {
  805. int cout = DeviceNum - 1;
  806. while (!SiemensDevice.GetInstance.MySiemens.Read<bool>($"DB3.DBX148.{cout}").Content) //DB3.DBX148.0-31 148.0-148.5代表6个料仓位置
  807. {
  808. SiemensDevice.GetInstance.MySiemens.Write($"DB4.DBX130.{cout}", true);
  809. Thread.Sleep(200);
  810. }
  811. while (SiemensDevice.GetInstance.MySiemens.Read<bool>($"DB3.DBX148.{cout}").Content)
  812. {
  813. SiemensDevice.GetInstance.MySiemens.Write($"DB4.DBX130.{cout}", false);
  814. }
  815. MessageNotify.GetInstance.ShowRunLog($"{cout}号气缸复位信号写入成功!");
  816. });
  817. var res = recipeProcesses.ElementAt(recIndex).RawMaterials.FirstOrDefault(p => p.Status != Status.配料完成 && p.RawMaterialName == tempRawMaterialName && p.Loc == j + 1);
  818. if (res != null)
  819. res.Status = Status.配料完成;
  820. MessageNotify.GetInstance.ShowRunLog($"设备完成执行状态:{rrr}");
  821. MessageNotify.GetInstance.ShowRunLog($"柔性味魔方{Recipes.ElementAt(i).RawMaterials.ElementAt(loc).DeviceIp},出料完成,状态复位");
  822. }
  823. }
  824. }
  825. }
  826. }
  827. }
  828. //508配料项目代码
  829. //if (GlobalDevice.PlcData.IsAllowIngredients[j])
  830. //{
  831. // int barrel = j + 1;//桶号
  832. // int cnt = GlobalDevice.PlcData.LocationFeedback[j];//允许配料的位置 1~10
  833. // if (RTrig.GetInstance("a").Start(GlobalDevice.PlcData.IsAllowIngredients[j]))
  834. // {
  835. // MessageNotify.GetInstance.ShowRunLog($"桶号:{barrel},位置:{cnt},允许配料");
  836. // }
  837. // int Location1 = 0;
  838. // if (cnt == -1)
  839. // {
  840. // MessageNotify.GetInstance.ShowRunLog("没有读取到配料的位置");
  841. // return;
  842. // }
  843. // else
  844. // {
  845. // int a1 = Array.FindIndex(DeviceInquire.GetInstance.devices.ToArray(), p => p.DeviceNum == cnt);
  846. // Location1 = Array.FindIndex(Recipes.ElementAt(i).RawMaterials.ToArray(), p => p.RawMaterialName == DeviceInquire.GetInstance.devices.ElementAt(a1).DeviceName && p.Loc == barrel);
  847. // }
  848. // if (Location1 >= 0)
  849. // {
  850. // string RawName = Recipes.ElementAt(i).RawMaterials.ElementAt(Location1).RawMaterialName;
  851. // int abc = Array.FindIndex(DeviceInquire.GetInstance.devices.ToArray(), p => p.DeviceName == RawName);
  852. // int DeviceNum = DeviceInquire.GetInstance.devices.ElementAt(abc).DeviceNum;
  853. // if (DeviceNum > 0)
  854. // {
  855. // float weight = Recipes.ElementAt(i).RawMaterials.ElementAt(Location1).RawMaterialWeight;
  856. // int loc = Array.FindIndex(Recipes.ElementAt(i).RawMaterials.ToArray(), p => p.RawMaterialName == RawName);
  857. // if (loc >= 0 && RawName != null && weight >= 0)
  858. // {
  859. // int St_index = Array.FindIndex(StockStatus.ToArray(), p => p.MaterialName == RawName);
  860. // if (St_index < 0)
  861. // {
  862. // StockStatus.Add(new StockStatusModel()
  863. // {
  864. // MaterialName = RawName,
  865. // IssueWeight = weight,
  866. // IssueStatus = 0,
  867. // });
  868. // }
  869. // St_index = Array.FindIndex(StockStatus.ToArray(), p => p.MaterialName == RawName);
  870. // if (St_index >= 0)
  871. // {
  872. // if (recipeProcesses.ElementAt(recIndex).RawMaterials.ElementAt(loc).RecipeStatus == 1 && StockStatus.ElementAt(St_index).IssueStatus == 0)
  873. // {
  874. // StockStatus.ElementAt(St_index).IssueStatus = 1;
  875. // DeviceInquire.GetInstance.GetDevice(Recipes.ElementAt(i).RawMaterials.ElementAt(loc).DeviceIp).Start(weight);
  876. // MessageNotify.GetInstance.ShowRunLog($"柔性味魔方{Recipes.ElementAt(i).RawMaterials.ElementAt(loc).DeviceIp},开始出料");
  877. // }
  878. // if (recipeProcesses.ElementAt(recIndex).RawMaterials.ElementAt(loc).RecipeStatus == 3 && StockStatus.ElementAt(St_index).IssueStatus == 1)
  879. // {
  880. // //GlobalDevice.PlcData.IsAllowIngredients[j] = false;//测试使用
  881. // StockStatus.ElementAt(St_index).IssueStatus = 2;
  882. // DeviceInquire.GetInstance.GetDevice(Recipes.ElementAt(i).RawMaterials.ElementAt(loc).DeviceIp).StatusReset();
  883. // /*int a = 0.SetBitValue((byte)(DeviceNum), true);
  884. // byte[] test1 = a.ToBytes(BPA.Helper.DataFormat.ABCD);
  885. // int item = test1.ToInt();*/
  886. // if (DeviceNum >= 1 && DeviceNum <= 8)
  887. // {
  888. // SiemensDevice.GetInstance.MySiemens.Write("DB4.DBX130." + (DeviceNum - 1), true);
  889. // }else if (DeviceNum >= 9 && DeviceNum <= 16)
  890. // {
  891. // SiemensDevice.GetInstance.MySiemens.Write("DB4.DBX131." + (DeviceNum - 9), true);
  892. // }
  893. // else if (DeviceNum >=17 && DeviceNum <= 24)
  894. // {
  895. // SiemensDevice.GetInstance.MySiemens.Write("DB4.DBX132." + (DeviceNum - 17), true);
  896. // }else if (DeviceNum >= 25 && DeviceNum <= 32)
  897. // {
  898. // SiemensDevice.GetInstance.MySiemens.Write("DB4.DBX133." + (DeviceNum - 25), true);
  899. // }
  900. // MessageNotify.GetInstance.ShowRunLog($"料仓:{DeviceNum},配料完成");
  901. // /*string res = SiemensDevice.GetInstance.MySiemens.Write("DB4.DBD130", item, 2);
  902. // MessageNotify.GetInstance.ShowRunLog($"{res}");
  903. // MessageNotify.GetInstance.ShowRunLog($"DB4.DBD130:{item.ToBinString()}");*/
  904. // MessageNotify.GetInstance.ShowRunLog($"柔性味魔方{Recipes.ElementAt(i).RawMaterials.ElementAt(loc).DeviceIp},出料完成,状态复位");
  905. // }
  906. // }
  907. // }
  908. // }
  909. // }
  910. //}
  911. }
  912. if (ComCount >= Recipes.ElementAt(i).RawMaterials.Count) //配方配料完成 StockStatus.Count >= Recipes.ElementAt(i).RawMaterials.Count &&
  913. {
  914. int cc = recipeProcesses.ElementAt(recIndex).RawMaterials.OrderBy(p => p.RawMaterialName).Count();
  915. for (int k = 0; k < 32; k++)
  916. {
  917. for (int l = 0; l < cc; l++)
  918. {
  919. RTrig.GetInstance($"ac{k}-{l}").Start(false);
  920. }
  921. }
  922. ComCount = 0;
  923. StockStatus.Clear();
  924. MessageNotify.GetInstance.ShowUserLog($"配方:{recipeProcesses.ElementAt(recIndex).RecipeName},配料完成");
  925. foreach (var item in recipeProcesses.ElementAt(recIndex).RawMaterials)
  926. {
  927. MessageNotify.GetInstance.ShowUserLog($"原料:{item.RawMaterialName},桶号:{item.Loc},设定配料重量{item.RawMaterialWeight}g,实际配料重量{item.UpLimtWeightFeedback}g");
  928. }
  929. SiemensDevice.GetInstance.MySiemens.Write("DB4.DBX202.0", true);//配料完成
  930. MessageNotify.GetInstance.ShowRunLog($"配方:{CurrentRecipeName},配料完成");
  931. int recipIndex = Array.FindIndex(Recipes.ToArray(), p => p.RecipeName == CurrentRecipeName);
  932. App.Current.Dispatcher.Invoke(new Action(() => { recipeProcesses.Clear(); }));
  933. Recipes.ElementAt(recipIndex).IsEnable = true;
  934. Json<LocalRecipe>.Data.Recipes.ElementAt(recipIndex).IsEnable = true;
  935. Recipes.ElementAt(recipIndex).Are.Set();
  936. Thread.Sleep(100);
  937. }
  938. }
  939. }
  940. }
  941. }
  942. #endregion
  943. Thread.Sleep(100);
  944. }), "RecipeControlViewModelStatusInquire", true);
  945. }
  946. public BPARelayCommand<object> StartCommand { get; set; }
  947. public BPARelayCommand<object> ChangeRecipeStateCommand { get; set; }
  948. public BPARelayCommand<object> CancelRecipeCommand { get; set; }
  949. public static ObservableCollection<RecipeModel> Recipes { get; set; } = Json<LocalRecipe>.Data.Recipes;
  950. public static string CurrentRecipeName { get { return _RecipeName; } set { _RecipeName = value; OnStaticPropertyChanged(); } }
  951. private static string _RecipeName;
  952. /// <summary>
  953. /// 当前正在制作的配方
  954. /// </summary>
  955. public static ObservableCollection<RecipeModel> recipeProcesses { get; set; } = new ObservableCollection<RecipeModel>();
  956. /// <summary>
  957. /// 等待制作的配方
  958. /// </summary>
  959. public static ObservableCollection<RecipeModel> UserTreeWait { get; set; } = new ObservableCollection<RecipeModel>();
  960. /// <summary>
  961. /// 已完成的配方
  962. /// </summary>
  963. public static ObservableCollection<RecipeModel> UserTreeCompelete { get; set; } = new ObservableCollection<RecipeModel>();
  964. private void ChangeRecipeState(object o)
  965. {
  966. if (o == null) return;
  967. if (o is string id)
  968. {
  969. var Recipe = recipeProcesses.FirstOrDefault(p => p.RecipeName == CurrentRecipeName);
  970. if (Recipe != null)
  971. {
  972. var res = Recipe.RawMaterials.FirstOrDefault(p => p.RawMaterialId == id);
  973. //var res1=Recipe.RawMaterials.Where(p => p.RawMaterialId == id);
  974. if (res != null)
  975. {
  976. if (res.RecipeStatus == 3)
  977. {
  978. res.RecipeStatus = 1;
  979. }
  980. else
  981. {
  982. res.RecipeStatus = 3;
  983. }
  984. }
  985. }
  986. }
  987. }
  988. }
  989. }