终端一体化运控平台
25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

RecipeControlViewModel.cs 10 KiB

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