终端一体化运控平台
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

RecipeControlViewModel.cs 10 KiB

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