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

215 рядки
10 KiB

  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(Recipes.ToArray(), p => p.RecipeName == deviceName);
  30. if (index >= 0 && index < Recipes.Count)
  31. {
  32. Recipes.ElementAt(index).IsEnable = false;
  33. }
  34. MessageNotify.GetInstance.ShowUserLog($"下发工单 { Recipes.ElementAt(index).RecipeName}");
  35. var res = Recipes.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(Recipes.ToArray(), p => p.RecipeName == devices.ElementAt(0));
  51. if (index >= 0 && index < Recipes.Count)
  52. {
  53. Recipes.ElementAt(index).Are.Reset();
  54. Recipes.ElementAt(index).IsEnable = false;
  55. App.Current.Dispatcher.Invoke(new Action(() =>
  56. {
  57. recipeProcesses.Clear();
  58. UserTreeWait.RemoveAt(0);
  59. }));
  60. CurrentRecipeName = Recipes.ElementAt(index).RecipeName;
  61. foreach (var item in Recipes.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. Recipes.ElementAt(index).Are.WaitOne();//阻塞,直到当前配方完成
  76. devices.TryDequeue(out string deviceName);
  77. App.Current.Dispatcher.Invoke(new Action(() =>
  78. {
  79. UserTreeCompelete.Add(Recipes.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 < Recipes.Count; i++)
  92. {
  93. for (int m = 0; m < Recipes.ElementAt(i).RawMaterials.Count; m++)
  94. {
  95. var RunStatus = DeviceInquire.GetInstance.GetDevice(Recipes.ElementAt(i).RawMaterials.ElementAt(m).DeviceIp).deviceStatus.RunStatus;
  96. //设备状态显示
  97. if (Recipes.ElementAt(i).RecipeName == CurrentRecipeName)
  98. {
  99. string deviceName = DeviceInquire.GetInstance.GetDevice(Recipes.ElementAt(i).RawMaterials.ElementAt(m).DeviceIp).DeviceName;
  100. int index = Array.FindIndex(Recipes.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(Recipes.ToArray(), p => p.RecipeName == CurrentRecipeName);
  110. if (recipIndex >= 0 && recipIndex < Recipes.Count)
  111. {
  112. for (int n = 0; n < recipeProcesses.Count; n++)
  113. {
  114. DeviceInquire.GetInstance.GetDevice(Recipes.ElementAt(recipIndex).RawMaterials.ElementAt(n).DeviceIp).StatusReset();//完成配料的设备运行状态地址写0
  115. MessageNotify.GetInstance.ShowRunLog(Recipes.ElementAt(recipIndex).RawMaterials.ElementAt(n).DeviceIp);
  116. }
  117. App.Current.Dispatcher.Invoke(new Action(() => { recipeProcesses.Clear(); }));
  118. Recipes.ElementAt(recipIndex).IsEnable = true;
  119. Recipes.ElementAt(recipIndex).Are.Set();
  120. }
  121. }
  122. //Recipes.ElementAt(i).RawMaterials.ElementAt(m).RecipeStatus = RunStatus;
  123. //var res = Recipes.ElementAt(i).RawMaterials.Where(p => p.RecipeStatus == 3).ToList();
  124. //if (res != null && res.Count == Recipes.ElementAt(i).RawMaterials.Count)//配方所有配料完成下料
  125. //{
  126. // for (int r = 0; r < Recipes.ElementAt(i).RawMaterials.Count; r++)
  127. // {
  128. // DeviceInquire.GetInstance.GetDevice(Recipes.ElementAt(i).RawMaterials.ElementAt(r).DeviceIp).StatusReset();//完成配料的设备运行状态地址写0
  129. // App.Current.Dispatcher.Invoke(new Action(() => { recipeProcesses.Clear(); }));
  130. // }
  131. // Recipes.ElementAt(i).IsEnable = true;
  132. // Recipes.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> Recipes { get; set; } = Json<LocaPar>.Data.Recipes;
  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. }