终端一体化运控平台
Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.
 
 
 

680 Zeilen
48 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. int a1 = Array.FindIndex(DeviceInquire.GetInstance.devices.ToArray(), p => p.DeviceNum == cnt );
  337. //int b1 = Array.FindIndex(DeviceInquire.GetInstance.devices.ToArray(), p => p.DeviceNum == cnt * 2);
  338. if (a1 >= 0)
  339. {
  340. Location1 = Array.FindIndex(Recipes.ElementAt(i).RawMaterials.ToArray(), p => p.RawMaterialName == DeviceInquire.GetInstance.devices.ElementAt(a1).DeviceName && p.Loc == barrel);
  341. }
  342. //if (b1 >= 0)
  343. //{
  344. // Location2 = Array.FindIndex(Recipes.ElementAt(i).RawMaterials.ToArray(), p => p.RawMaterialName == DeviceInquire.GetInstance.devices.ElementAt(b1).DeviceName && p.Loc == barrel);
  345. //}
  346. Location2 = Location1;
  347. }
  348. }
  349. if (Location1 >= 0)
  350. {
  351. string RawName = Recipes.ElementAt(i).RawMaterials.ElementAt(Location1).RawMaterialName;
  352. int abc = Array.FindIndex(DeviceInquire.GetInstance.devices.ToArray(), p => p.DeviceName == RawName);
  353. int DeviceNum = DeviceInquire.GetInstance.devices.ElementAt(abc).DeviceNum;
  354. if (DeviceNum > 0)
  355. {
  356. float weight = Recipes.ElementAt(i).RawMaterials.ElementAt(Location1).RawMaterialWeight;
  357. int loc = Array.FindIndex(Recipes.ElementAt(i).RawMaterials.ToArray(), p => p.RawMaterialName == RawName);
  358. if (loc >= 0 && RawName != null && weight >= 0)
  359. {
  360. int St_index = Array.FindIndex(StockStatus.ToArray(), p => p.MaterialName == RawName);
  361. if (St_index < 0)
  362. {
  363. StockStatus.Add(new StockStatusModel()
  364. {
  365. MaterialName = RawName,
  366. IssueWeight = weight,
  367. IssueStatus = 0,
  368. });
  369. }
  370. St_index = Array.FindIndex(StockStatus.ToArray(), p => p.MaterialName == RawName);
  371. if (St_index >= 0)
  372. {
  373. if (recipeProcesses.ElementAt(recIndex).RawMaterials.ElementAt(loc).RecipeStatus == 1 && StockStatus.ElementAt(St_index).IssueStatus == 0)
  374. {
  375. StockStatus.ElementAt(St_index).IssueStatus = 1;
  376. DeviceInquire.GetInstance.GetDevice(Recipes.ElementAt(i).RawMaterials.ElementAt(loc).DeviceIp).Start(weight);
  377. MessageNotify.GetInstance.ShowRunLog($"柔性味魔方{Recipes.ElementAt(i).RawMaterials.ElementAt(loc).DeviceIp},开始出料");
  378. }
  379. if (recipeProcesses.ElementAt(recIndex).RawMaterials.ElementAt(loc).RecipeStatus == 3 && StockStatus.ElementAt(St_index).IssueStatus == 1)
  380. {
  381. //GlobalDevice.PlcData.IsAllowIngredients[j] = false;//测试使用
  382. StockStatus.ElementAt(St_index).IssueStatus = 2;
  383. DeviceInquire.GetInstance.GetDevice(Recipes.ElementAt(i).RawMaterials.ElementAt(loc).DeviceIp).StatusReset();
  384. int a = 0.SetBitValue((byte)(DeviceNum), true);
  385. byte[] test1 = a.ToBytes(BPA.Helper.DataFormat.BADC);
  386. int item = test1.ToInt();
  387. SiemensDevice.GetInstance.MySiemens.Write("DB4.DBD130", item);
  388. //MessageNotify.GetInstance.ShowRunLog($"{res}");
  389. MessageNotify.GetInstance.ShowRunLog($"DB4.DBD130:{item.ToBinString()}");
  390. MessageNotify.GetInstance.ShowRunLog($"柔性味魔方{Recipes.ElementAt(i).RawMaterials.ElementAt(loc).DeviceIp},出料完成,状态复位");
  391. }
  392. }
  393. }
  394. }
  395. }
  396. if (Location1 >= 0)
  397. {
  398. string RawName111 = Recipes.ElementAt(i).RawMaterials.ElementAt(Location1).RawMaterialName;
  399. int St_index111 = Array.FindIndex(StockStatus.ToArray(), p => p.MaterialName == RawName111);
  400. if (Location2 >= 0 && ((Location1 >= 0 && StockStatus.ElementAt(St_index111).IssueStatus == 2)))
  401. {
  402. string RawName = Recipes.ElementAt(i).RawMaterials.ElementAt(Location2).RawMaterialName;
  403. int abc = Array.FindIndex(DeviceInquire.GetInstance.devices.ToArray(), p => p.DeviceName == RawName);
  404. int DeviceNum = DeviceInquire.GetInstance.devices.ElementAt(abc).DeviceNum;
  405. if (DeviceNum > 0)
  406. {
  407. float weight = Recipes.ElementAt(i).RawMaterials.ElementAt(Location2).RawMaterialWeight;
  408. int loc = Array.FindIndex(Recipes.ElementAt(i).RawMaterials.ToArray(), p => p.RawMaterialName == RawName);
  409. if (loc >= 0 && RawName != null && weight >= 0)
  410. {
  411. int St_index = Array.FindIndex(StockStatus.ToArray(), p => p.MaterialName == RawName);
  412. if (St_index < 0)
  413. {
  414. StockStatus.Add(new StockStatusModel()
  415. {
  416. MaterialName = RawName,
  417. IssueWeight = weight,
  418. IssueStatus = 0,
  419. });
  420. }
  421. St_index = Array.FindIndex(StockStatus.ToArray(), p => p.MaterialName == RawName);
  422. if (St_index >= 0)
  423. {
  424. if (recipeProcesses.ElementAt(recIndex).RawMaterials.ElementAt(loc).RecipeStatus == 1 && StockStatus.ElementAt(St_index).IssueStatus == 0)
  425. {
  426. StockStatus.ElementAt(St_index).IssueStatus = 1;
  427. DeviceInquire.GetInstance.GetDevice(Recipes.ElementAt(i).RawMaterials.ElementAt(loc).DeviceIp).Start(weight);
  428. MessageNotify.GetInstance.ShowRunLog($"柔性味魔方{Recipes.ElementAt(i).RawMaterials.ElementAt(loc).DeviceIp},开始出料");
  429. }
  430. if (recipeProcesses.ElementAt(recIndex).RawMaterials.ElementAt(loc).RecipeStatus == 3 && StockStatus.ElementAt(St_index).IssueStatus == 1)
  431. {
  432. //GlobalDevice.PlcData.IsAllowIngredients[j] = false;//测试使用
  433. StockStatus.ElementAt(St_index).IssueStatus = 2;
  434. DeviceInquire.GetInstance.GetDevice(Recipes.ElementAt(i).RawMaterials.ElementAt(loc).DeviceIp).StatusReset();
  435. int a = 0.SetBitValue((byte)(DeviceNum), true);
  436. byte[] test1 = a.ToBytes(BPA.Helper.DataFormat.BADC);
  437. int item = test1.ToInt();
  438. SiemensDevice.GetInstance.MySiemens.Write("DB4.DBD130", item);
  439. //MessageNotify.GetInstance.ShowRunLog($"{res}");
  440. //MessageNotify.GetInstance.ShowRunLog($"DB4.DBD130:{item.ToBinString()}");
  441. MessageNotify.GetInstance.ShowRunLog($"DB4.DBD130:{item}");
  442. MessageNotify.GetInstance.ShowRunLog($"柔性味魔方{Recipes.ElementAt(i).RawMaterials.ElementAt(loc).DeviceIp},出料完成,状态复位");
  443. }
  444. }
  445. }
  446. }
  447. }
  448. }
  449. else
  450. {
  451. if (Location2 >= 0)
  452. {
  453. string RawName = Recipes.ElementAt(i).RawMaterials.ElementAt(Location2).RawMaterialName;
  454. int abc = Array.FindIndex(DeviceInquire.GetInstance.devices.ToArray(), p => p.DeviceName == RawName);
  455. int DeviceNum = DeviceInquire.GetInstance.devices.ElementAt(abc).DeviceNum;
  456. if (DeviceNum >= 0)
  457. {
  458. float weight = Recipes.ElementAt(i).RawMaterials.ElementAt(Location2).RawMaterialWeight;
  459. int loc = Array.FindIndex(Recipes.ElementAt(i).RawMaterials.ToArray(), p => p.RawMaterialName == RawName);
  460. if (cnt > 0 && loc >= 0 && RawName != null && weight >= 0)
  461. {
  462. int St_index = Array.FindIndex(StockStatus.ToArray(), p => p.MaterialName == RawName);
  463. if (St_index < 0)
  464. {
  465. StockStatus.Add(new StockStatusModel()
  466. {
  467. MaterialName = RawName,
  468. IssueWeight = weight,
  469. IssueStatus = 0,
  470. });
  471. }
  472. St_index = Array.FindIndex(StockStatus.ToArray(), p => p.MaterialName == RawName);
  473. if (St_index >= 0)
  474. {
  475. if (recipeProcesses.ElementAt(recIndex).RawMaterials.ElementAt(loc).RecipeStatus == 1 && StockStatus.ElementAt(St_index).IssueStatus == 0)
  476. {
  477. StockStatus.ElementAt(St_index).IssueStatus = 1;
  478. DeviceInquire.GetInstance.GetDevice(Recipes.ElementAt(i).RawMaterials.ElementAt(loc).DeviceIp).Start(weight);
  479. MessageNotify.GetInstance.ShowRunLog($"柔性味魔方{Recipes.ElementAt(i).RawMaterials.ElementAt(loc).DeviceIp},开始出料");
  480. }
  481. if (recipeProcesses.ElementAt(recIndex).RawMaterials.ElementAt(loc).RecipeStatus == 3 && StockStatus.ElementAt(St_index).IssueStatus == 1)
  482. {
  483. //GlobalDevice.PlcData.IsAllowIngredients[j] = false;//测试使用
  484. StockStatus.ElementAt(St_index).IssueStatus = 2;
  485. DeviceInquire.GetInstance.GetDevice(Recipes.ElementAt(i).RawMaterials.ElementAt(loc).DeviceIp).StatusReset();
  486. int a = 0.SetBitValue((byte)(DeviceNum), true);
  487. byte[] test1 = a.ToBytes(BPA.Helper.DataFormat.BADC);
  488. int item = test1.ToInt();
  489. SiemensDevice.GetInstance.MySiemens.Write("DB4.DBD130", item);
  490. recipeProcesses.ElementAt(recIndex).RawMaterials.ElementAt(loc).UpLimtWeightFeedback = DeviceInquire.GetInstance.GetDevice(Recipes.ElementAt(i).RawMaterials.ElementAt(loc).DeviceIp).deviceStatus.NowWeightFeedback;
  491. //MessageNotify.GetInstance.ShowRunLog($"{res}");
  492. MessageNotify.GetInstance.ShowRunLog($"DB4.DBD130:{item.ToBinString()}");
  493. MessageNotify.GetInstance.ShowRunLog($"柔性味魔方{Recipes.ElementAt(i).RawMaterials.ElementAt(loc).DeviceIp},出料完成,状态复位");
  494. }
  495. }
  496. }
  497. }
  498. }
  499. }
  500. }
  501. //508配料项目代码
  502. //if (GlobalDevice.PlcData.IsAllowIngredients[j])
  503. //{
  504. // int barrel = j + 1;//桶号
  505. // int cnt = GlobalDevice.PlcData.LocationFeedback[j];//允许配料的位置 1~10
  506. // if (RTrig.GetInstance("a").Start(GlobalDevice.PlcData.IsAllowIngredients[j]))
  507. // {
  508. // MessageNotify.GetInstance.ShowRunLog($"桶号:{barrel},位置:{cnt},允许配料");
  509. // }
  510. // int Location1 = 0;
  511. // if (cnt == -1)
  512. // {
  513. // MessageNotify.GetInstance.ShowRunLog("没有读取到配料的位置");
  514. // return;
  515. // }
  516. // else
  517. // {
  518. // int a1 = Array.FindIndex(DeviceInquire.GetInstance.devices.ToArray(), p => p.DeviceNum == cnt);
  519. // Location1 = Array.FindIndex(Recipes.ElementAt(i).RawMaterials.ToArray(), p => p.RawMaterialName == DeviceInquire.GetInstance.devices.ElementAt(a1).DeviceName && p.Loc == barrel);
  520. // }
  521. // if (Location1 >= 0)
  522. // {
  523. // string RawName = Recipes.ElementAt(i).RawMaterials.ElementAt(Location1).RawMaterialName;
  524. // int abc = Array.FindIndex(DeviceInquire.GetInstance.devices.ToArray(), p => p.DeviceName == RawName);
  525. // int DeviceNum = DeviceInquire.GetInstance.devices.ElementAt(abc).DeviceNum;
  526. // if (DeviceNum > 0)
  527. // {
  528. // float weight = Recipes.ElementAt(i).RawMaterials.ElementAt(Location1).RawMaterialWeight;
  529. // int loc = Array.FindIndex(Recipes.ElementAt(i).RawMaterials.ToArray(), p => p.RawMaterialName == RawName);
  530. // if (loc >= 0 && RawName != null && weight >= 0)
  531. // {
  532. // int St_index = Array.FindIndex(StockStatus.ToArray(), p => p.MaterialName == RawName);
  533. // if (St_index < 0)
  534. // {
  535. // StockStatus.Add(new StockStatusModel()
  536. // {
  537. // MaterialName = RawName,
  538. // IssueWeight = weight,
  539. // IssueStatus = 0,
  540. // });
  541. // }
  542. // St_index = Array.FindIndex(StockStatus.ToArray(), p => p.MaterialName == RawName);
  543. // if (St_index >= 0)
  544. // {
  545. // if (recipeProcesses.ElementAt(recIndex).RawMaterials.ElementAt(loc).RecipeStatus == 1 && StockStatus.ElementAt(St_index).IssueStatus == 0)
  546. // {
  547. // StockStatus.ElementAt(St_index).IssueStatus = 1;
  548. // DeviceInquire.GetInstance.GetDevice(Recipes.ElementAt(i).RawMaterials.ElementAt(loc).DeviceIp).Start(weight);
  549. // MessageNotify.GetInstance.ShowRunLog($"柔性味魔方{Recipes.ElementAt(i).RawMaterials.ElementAt(loc).DeviceIp},开始出料");
  550. // }
  551. // if (recipeProcesses.ElementAt(recIndex).RawMaterials.ElementAt(loc).RecipeStatus == 3 && StockStatus.ElementAt(St_index).IssueStatus == 1)
  552. // {
  553. // //GlobalDevice.PlcData.IsAllowIngredients[j] = false;//测试使用
  554. // StockStatus.ElementAt(St_index).IssueStatus = 2;
  555. // DeviceInquire.GetInstance.GetDevice(Recipes.ElementAt(i).RawMaterials.ElementAt(loc).DeviceIp).StatusReset();
  556. // /*int a = 0.SetBitValue((byte)(DeviceNum), true);
  557. // byte[] test1 = a.ToBytes(BPA.Helper.DataFormat.ABCD);
  558. // int item = test1.ToInt();*/
  559. // if (DeviceNum >= 1 && DeviceNum <= 8)
  560. // {
  561. // SiemensDevice.GetInstance.MySiemens.Write("DB4.DBX130." + (DeviceNum - 1), true);
  562. // }else if (DeviceNum >= 9 && DeviceNum <= 16)
  563. // {
  564. // SiemensDevice.GetInstance.MySiemens.Write("DB4.DBX131." + (DeviceNum - 9), true);
  565. // }
  566. // else if (DeviceNum >=17 && DeviceNum <= 24)
  567. // {
  568. // SiemensDevice.GetInstance.MySiemens.Write("DB4.DBX132." + (DeviceNum - 17), true);
  569. // }else if (DeviceNum >= 25 && DeviceNum <= 32)
  570. // {
  571. // SiemensDevice.GetInstance.MySiemens.Write("DB4.DBX133." + (DeviceNum - 25), true);
  572. // }
  573. // MessageNotify.GetInstance.ShowRunLog($"料仓:{DeviceNum},配料完成");
  574. // /*string res = SiemensDevice.GetInstance.MySiemens.Write("DB4.DBD130", item, 2);
  575. // MessageNotify.GetInstance.ShowRunLog($"{res}");
  576. // MessageNotify.GetInstance.ShowRunLog($"DB4.DBD130:{item.ToBinString()}");*/
  577. // MessageNotify.GetInstance.ShowRunLog($"柔性味魔方{Recipes.ElementAt(i).RawMaterials.ElementAt(loc).DeviceIp},出料完成,状态复位");
  578. // }
  579. // }
  580. // }
  581. // }
  582. // }
  583. //}
  584. }
  585. int Finish_Count = StockStatus.Where(s => s.IssueStatus == 2).Count();
  586. if (StockStatus.Count >= Recipes.ElementAt(i).RawMaterials.Count && Finish_Count >= Recipes.ElementAt(i).RawMaterials.Count) //配方配料完成
  587. {
  588. StockStatus.Clear();
  589. MessageNotify.GetInstance.ShowUserLog($"配方:{recipeProcesses.ElementAt(recIndex).RecipeName},配料完成");
  590. foreach (var item in recipeProcesses.ElementAt(recIndex).RawMaterials)
  591. {
  592. MessageNotify.GetInstance.ShowUserLog($"原料:{item.RawMaterialName},桶号:{item.Loc},设定配料重量{item.RawMaterialWeight}g,实际配料重量{item.UpLimtWeightFeedback}g");
  593. }
  594. SiemensDevice.GetInstance.MySiemens.Write("DB4.DBX202.0", true);//配料完成
  595. MessageNotify.GetInstance.ShowRunLog($"配方:{CurrentRecipeName},配料完成");
  596. int recipIndex = Array.FindIndex(Recipes.ToArray(), p => p.RecipeName == CurrentRecipeName);
  597. App.Current.Dispatcher.Invoke(new Action(() => { recipeProcesses.Clear(); }));
  598. Recipes.ElementAt(recipIndex).IsEnable = true;
  599. Json<LocalRecipe>.Data.Recipes.ElementAt(recipIndex).IsEnable = true;
  600. Recipes.ElementAt(recipIndex).Are.Set();
  601. Thread.Sleep(100);
  602. }
  603. }
  604. }
  605. }
  606. }
  607. Thread.Sleep(100);
  608. }), "RecipeControlViewModelStatusInquire", true);
  609. }
  610. public BPARelayCommand<object> StartCommand { get; set; }
  611. public BPARelayCommand<object> ChangeRecipeStateCommand { get; set; }
  612. public BPARelayCommand<object> CancelRecipeCommand { get; set; }
  613. public static ObservableCollection<RecipeModel> Recipes { get; set; } = Json<LocalRecipe>.Data.Recipes;
  614. public static string CurrentRecipeName { get { return _RecipeName; } set { _RecipeName = value; OnStaticPropertyChanged(); } }
  615. private static string _RecipeName;
  616. /// <summary>
  617. /// 当前正在制作的配方
  618. /// </summary>
  619. public static ObservableCollection<RecipeModel> recipeProcesses { get; set; } = new ObservableCollection<RecipeModel>();
  620. /// <summary>
  621. /// 等待制作的配方
  622. /// </summary>
  623. public static ObservableCollection<RecipeModel> UserTreeWait { get; set; } = new ObservableCollection<RecipeModel>();
  624. /// <summary>
  625. /// 已完成的配方
  626. /// </summary>
  627. public static ObservableCollection<RecipeModel> UserTreeCompelete { get; set; } = new ObservableCollection<RecipeModel>();
  628. private void ChangeRecipeState(object o)
  629. {
  630. if (o == null) return;
  631. if (o is string id)
  632. {
  633. var Recipe = recipeProcesses.FirstOrDefault(p => p.RecipeName == CurrentRecipeName);
  634. if (Recipe != null)
  635. {
  636. var res = Recipe.RawMaterials.FirstOrDefault(p => p.RawMaterialId == id);
  637. if (res != null)
  638. {
  639. if (res.RecipeStatus == 3)
  640. {
  641. res.RecipeStatus = 1;
  642. }
  643. else
  644. {
  645. res.RecipeStatus = 3;
  646. }
  647. }
  648. }
  649. }
  650. }
  651. }
  652. }