终端一体化运控平台
Não pode escolher mais do que 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 
 

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