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

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