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

213 lines
10 KiB

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