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

RecipeControlViewModel.cs 10 KiB

vor 2 Jahren
vor 2 Jahren
vor 2 Jahren
vor 2 Jahren
vor 2 Jahren
vor 2 Jahren
vor 2 Jahren
vor 2 Jahren
vor 2 Jahren
vor 2 Jahren
vor 2 Jahren
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using Microsoft.Toolkit.Mvvm.ComponentModel;
  7. using System.Collections.Concurrent;
  8. using System.Collections.ObjectModel;
  9. using System.Windows;
  10. using BPASmartClient.Helper;
  11. using Microsoft.Toolkit.Mvvm.Input;
  12. using BPASmartClient.DosingSystem.Model;
  13. using System.Threading;
  14. using BPASmartClient.CustomResource.Pages.Model;
  15. using BPASmartClient.CustomResource.UserControls.MessageShow;
  16. using BPASmartClient.CustomResource.UserControls;
  17. using BPASmartClient.Model.柔性味魔方;
  18. using BPASmartClient.Model;
  19. namespace BPASmartClient.DosingSystem.ViewModel
  20. {
  21. public class RecipeControlViewModel : ObservableObject
  22. {
  23. ConcurrentQueue<string> devices = new ConcurrentQueue<string>();
  24. public RecipeControlViewModel()
  25. {
  26. StartCommand = new RelayCommand<object>((o) =>
  27. {
  28. if (o != null && o is string deviceName)
  29. {
  30. int index = Array.FindIndex(Recipes.ToArray(), p => p.RecipeName == deviceName);
  31. if (index >= 0 && index < Recipes.Count)
  32. {
  33. Recipes.ElementAt(index).IsEnable = false;
  34. }
  35. MessageLog.GetInstance.ShowUserLog($"下发工单 {Recipes.ElementAt(index).RecipeName}");
  36. devices.Enqueue(deviceName);
  37. var res = Recipes.FirstOrDefault(p => p.RecipeName == deviceName);
  38. UserTreeWait.Add(new RecipeModel { RecipeName = deviceName, RawMaterials = res.RawMaterials });
  39. }
  40. NoticeDemoViewModel.OpenMsg(EnumPromptType.Success, App.MainWindow, "提示", $"配方下发成功!");
  41. MessageLog.GetInstance.ShowUserLog($"下发工单 {Guid.NewGuid().ToString()}");
  42. });
  43. ChangeRecipeStateCommand = new RelayCommand<object>(ChangeRecipeState);
  44. ThreadManage.GetInstance().StartLong(new Action(() =>
  45. {
  46. if (devices.Count > 0)
  47. {
  48. int index = Array.FindIndex(Recipes.ToArray(), p => p.RecipeName == devices.ElementAt(0));
  49. if (index >= 0 && index < Recipes.Count)
  50. {
  51. Recipes.ElementAt(index).Are.Reset();
  52. Recipes.ElementAt(index).IsEnable = false;
  53. App.Current.Dispatcher.Invoke(new Action(() =>
  54. {
  55. recipeProcesses.Clear();
  56. UserTreeWait.RemoveAt(0);
  57. }));
  58. CurrentRecipeName = Recipes.ElementAt(index).RecipeName;
  59. foreach (var item in Recipes.ElementAt(index).RawMaterials)
  60. {
  61. DeviceInquire.GetInstance.GetDevice(item.DeviceIp)?.Start(item.RawMaterialWeight);//启动并写入每个原料重量
  62. App.Current.Dispatcher.Invoke(new Action(() =>
  63. {
  64. recipeProcesses.Add(new RawMaterialModel()
  65. {
  66. RawMaterialName = item.RawMaterialName,
  67. RecipeStatus = item.RecipeStatus,
  68. RawMaterialSource = item.RawMaterialSource,
  69. RawMaterialId = item.RawMaterialId,
  70. });
  71. }));
  72. }
  73. Recipes.ElementAt(index).Are.WaitOne();//阻塞,直到当前配方完成
  74. devices.TryDequeue(out string deviceName);
  75. App.Current.Dispatcher.Invoke(new Action(() =>
  76. {
  77. UserTreeCompelete.Add(Recipes.ElementAt(index));//当前配方完成后添加到已完成的配方列表
  78. }));
  79. App.Current.Dispatcher.Invoke(new Action(() =>
  80. {
  81. recipeProcesses.Clear();
  82. CurrentRecipeName = string.Empty;
  83. }));//完成后清空当前配方
  84. }
  85. }
  86. Thread.Sleep(100);
  87. }), "启动配方下发");
  88. ThreadManage.GetInstance().StartLong(new Action(() =>
  89. {
  90. for (int i = 0; i < Recipes.Count; i++)
  91. {
  92. for (int m = 0; m < Recipes.ElementAt(i).RawMaterials.Count; m++)
  93. {
  94. var RunStatus = DeviceInquire.GetInstance.GetDevice(Recipes.ElementAt(i).RawMaterials.ElementAt(m).DeviceIp).deviceStatus.RunStatus;
  95. //设备状态显示
  96. if (Recipes.ElementAt(i).RecipeName == CurrentRecipeName)
  97. {
  98. string deviceName = DeviceInquire.GetInstance.GetDevice(Recipes.ElementAt(i).RawMaterials.ElementAt(m).DeviceIp).DeviceName;
  99. int index = Array.FindIndex(Recipes.ElementAt(i).RawMaterials.ToArray(), p => p.RawMaterialName == deviceName);
  100. if (index >= 0 && index < recipeProcesses.Count)
  101. {
  102. App.Current.Dispatcher.Invoke(new Action(() => { recipeProcesses.ElementAt(index).RecipeStatus = RunStatus; }));
  103. }
  104. }
  105. var proc = recipeProcesses.Where(p => p.RecipeStatus == 3).ToList();
  106. if (proc != null && proc.Count > 0 && proc.Count == recipeProcesses.Count)
  107. {
  108. int recipIndex = Array.FindIndex(Recipes.ToArray(), p => p.RecipeName == CurrentRecipeName);
  109. if (recipIndex >= 0 && recipIndex < Recipes.Count)
  110. {
  111. for (int n = 0; n < recipeProcesses.Count; n++)
  112. {
  113. DeviceInquire.GetInstance.GetDevice(Recipes.ElementAt(recipIndex).RawMaterials.ElementAt(n).DeviceIp).StatusReset();//完成配料的设备运行状态地址写0
  114. MessageLog.GetInstance.ShowRunLog(Recipes.ElementAt(recipIndex).RawMaterials.ElementAt(n).DeviceIp);
  115. }
  116. App.Current.Dispatcher.Invoke(new Action(() => { recipeProcesses.Clear(); }));
  117. Recipes.ElementAt(recipIndex).IsEnable = true;
  118. Recipes.ElementAt(recipIndex).Are.Set();
  119. }
  120. }
  121. //Recipes.ElementAt(i).RawMaterials.ElementAt(m).RecipeStatus = RunStatus;
  122. //var res = Recipes.ElementAt(i).RawMaterials.Where(p => p.RecipeStatus == 3).ToList();
  123. //if (res != null && res.Count == Recipes.ElementAt(i).RawMaterials.Count)//配方所有配料完成下料
  124. //{
  125. // for (int r = 0; r < Recipes.ElementAt(i).RawMaterials.Count; r++)
  126. // {
  127. // DeviceInquire.GetInstance.GetDevice(Recipes.ElementAt(i).RawMaterials.ElementAt(r).DeviceIp).StatusReset();//完成配料的设备运行状态地址写0
  128. // App.Current.Dispatcher.Invoke(new Action(() => { recipeProcesses.Clear(); }));
  129. // }
  130. // Recipes.ElementAt(i).IsEnable = true;
  131. // Recipes.ElementAt(i).Are.Set();
  132. //}
  133. }
  134. }
  135. Thread.Sleep(100);
  136. }), "RecipeControlViewModelStatusInquire");
  137. //测试数据
  138. /* RawMaterialModel rawMaterial_1 = new RawMaterialModel { RawMaterialName = "香料_1" };
  139. RawMaterialModel rawMaterial_2 = new RawMaterialModel { RawMaterialName = "香料_2" };
  140. RawMaterialModel rawMaterial_3 = new RawMaterialModel { RawMaterialName = "香料_3" };
  141. RawMaterialModel rawMaterial_4 = new RawMaterialModel { RawMaterialName = "香料_4" };
  142. ObservableCollection<RawMaterialModel> rawMaterials = new ObservableCollection<RawMaterialModel> { rawMaterial_1, rawMaterial_2, rawMaterial_3, rawMaterial_4 };
  143. UserTreeCompelete.Add(new RecipeModel { RecipeName = "完成的香料1", RawMaterials = rawMaterials });
  144. UserTreeCompelete.Add(new RecipeModel { RecipeName = "完成的香料2", RawMaterials = rawMaterials });*/
  145. }
  146. public RelayCommand<object> StartCommand { get; set; }
  147. public RelayCommand<object> ChangeRecipeStateCommand { get; set; }
  148. public static ObservableCollection<RecipeModel> Recipes { get; set; } = Json<LocaPar>.Data.Recipes;
  149. public string CurrentRecipeName { get { return _RecipeName; } set { _RecipeName = value; OnPropertyChanged(); } }
  150. private static string _RecipeName;
  151. /// <summary>
  152. /// 当前正在制作的配方
  153. /// </summary>
  154. public static ObservableCollection<RawMaterialModel> recipeProcesses { get; set; } = new ObservableCollection<RawMaterialModel>();
  155. /// <summary>
  156. /// 等待制作的配方
  157. /// </summary>
  158. public static ObservableCollection<RecipeModel> UserTreeWait { get; set; } = new ObservableCollection<RecipeModel>();
  159. /// <summary>
  160. /// 已完成的配方
  161. /// </summary>
  162. public static ObservableCollection<RecipeModel> UserTreeCompelete { get; set; } = new ObservableCollection<RecipeModel>();
  163. private void ChangeRecipeState(object o)
  164. {
  165. if (o == null) return;
  166. if (o is string id)
  167. {
  168. var res = recipeProcesses.FirstOrDefault(p => p.RawMaterialId == id);
  169. if (res != null)
  170. {
  171. if (res.RecipeStatus == 3)
  172. {
  173. res.RecipeStatus = 1;
  174. }
  175. else
  176. {
  177. res.RecipeStatus = 3;
  178. }
  179. }
  180. }
  181. }
  182. }
  183. }