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

RecipeControlViewModel.cs 10 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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.DosingHKProject.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. using BPASmartClient.DosingProject;
  20. namespace BPASmartClient.DosingHKProject.ViewModel
  21. {
  22. public class RecipeControlViewModel : ObservableObject
  23. {
  24. ConcurrentQueue<string> devices = new ConcurrentQueue<string>();
  25. public RecipeControlViewModel()
  26. {
  27. StartCommand = new RelayCommand<object>((o) =>
  28. {
  29. if (o != null && o is string deviceName)
  30. {
  31. int index = Array.FindIndex(Recipes.ToArray(), p => p.RecipeName == deviceName);
  32. if (index >= 0 && index < Recipes.Count)
  33. {
  34. Recipes.ElementAt(index).IsEnable = false;
  35. }
  36. MessageNotify.GetInstance.ShowUserLog($"下发工单 { Recipes.ElementAt(index).RecipeName}");
  37. devices.Enqueue(deviceName);
  38. var res = Recipes.FirstOrDefault(p => p.RecipeName == deviceName);
  39. UserTreeWait.Add(new RecipeModel { RecipeName = deviceName, RawMaterials = res.RawMaterials });
  40. }
  41. NoticeDemoViewModel.OpenMsg(EnumPromptType.Success, App.MainWindow, "提示", $"配方下发成功!");
  42. MessageNotify.GetInstance.ShowUserLog($"下发工单 {Guid.NewGuid().ToString()}");
  43. });
  44. ChangeRecipeStateCommand = new RelayCommand<object>(ChangeRecipeState);
  45. ThreadManage.GetInstance().StartLong(new Action(() =>
  46. {
  47. if (devices.Count > 0)
  48. {
  49. int index = Array.FindIndex(Recipes.ToArray(), p => p.RecipeName == devices.ElementAt(0));
  50. if (index >= 0 && index < Recipes.Count)
  51. {
  52. Recipes.ElementAt(index).Are.Reset();
  53. Recipes.ElementAt(index).IsEnable = false;
  54. App.Current.Dispatcher.Invoke(new Action(() =>
  55. {
  56. recipeProcesses.Clear();
  57. UserTreeWait.RemoveAt(0);
  58. }));
  59. CurrentRecipeName = Recipes.ElementAt(index).RecipeName;
  60. foreach (var item in Recipes.ElementAt(index).RawMaterials)
  61. {
  62. DeviceInquire.GetInstance.GetDevice(item.DeviceIp)?.Start(item.RawMaterialWeight);//启动并写入每个原料重量
  63. App.Current.Dispatcher.Invoke(new Action(() =>
  64. {
  65. recipeProcesses.Add(new RawMaterialModel()
  66. {
  67. RawMaterialName = item.RawMaterialName,
  68. RecipeStatus = item.RecipeStatus,
  69. RawMaterialSource = item.RawMaterialSource,
  70. RawMaterialId = item.RawMaterialId,
  71. });
  72. }));
  73. }
  74. Recipes.ElementAt(index).Are.WaitOne();//阻塞,直到当前配方完成
  75. devices.TryDequeue(out string deviceName);
  76. App.Current.Dispatcher.Invoke(new Action(() =>
  77. {
  78. UserTreeCompelete.Add(Recipes.ElementAt(index));//当前配方完成后添加到已完成的配方列表
  79. }));
  80. App.Current.Dispatcher.Invoke(new Action(() => {
  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. MessageNotify.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. }else
  175. {
  176. res.RecipeStatus = 3;
  177. }
  178. }
  179. }
  180. }
  181. }
  182. }