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

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