终端一体化运控平台
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。
 
 
 

188 行
8.7 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.DosingSystem.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.DosingSystem.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. UserTreeCompelete.Add(Recipes.ElementAt(index));//当前配方完成后添加到已完成的配方列表
  76. App.Current.Dispatcher.Invoke(new Action(() => { recipeProcesses.Clear(); }));//完成后清空当前配方
  77. }
  78. }
  79. Thread.Sleep(100);
  80. }), "启动配方下发");
  81. ThreadManage.GetInstance().StartLong(new Action(() =>
  82. {
  83. for (int i = 0; i < Recipes.Count; i++)
  84. {
  85. for (int m = 0; m < Recipes.ElementAt(i).RawMaterials.Count; m++)
  86. {
  87. var RunStatus = DeviceInquire.GetInstance.GetDevice(Recipes.ElementAt(i).RawMaterials.ElementAt(m).DeviceIp).deviceStatus.RunStatus;
  88. //设备状态显示
  89. if (Recipes.ElementAt(i).RecipeName == CurrentRecipeName)
  90. {
  91. string deviceName = DeviceInquire.GetInstance.GetDevice(Recipes.ElementAt(i).RawMaterials.ElementAt(m).DeviceIp).DeviceName;
  92. int index = Array.FindIndex(Recipes.ElementAt(i).RawMaterials.ToArray(), p => p.RawMaterialName == deviceName);
  93. if (index >= 0 && index < recipeProcesses.Count)
  94. {
  95. App.Current.Dispatcher.Invoke(new Action(() => { recipeProcesses.ElementAt(index).RecipeStatus = RunStatus; }));
  96. }
  97. }
  98. Recipes.ElementAt(i).RawMaterials.ElementAt(m).RecipeStatus = RunStatus;
  99. var res = Recipes.ElementAt(i).RawMaterials.Where(p => p.RecipeStatus == 3).ToList();
  100. if (res != null && res.Count == Recipes.ElementAt(i).RawMaterials.Count)//配方所有配料完成下料
  101. {
  102. for (int r = 0; r < Recipes.ElementAt(i).RawMaterials.Count; r++)
  103. {
  104. DeviceInquire.GetInstance.GetDevice(Recipes.ElementAt(i).RawMaterials.ElementAt(r).DeviceIp).StatusReset();//完成配料的设备运行状态地址写0
  105. App.Current.Dispatcher.Invoke(new Action(() => { recipeProcesses.Clear(); }));
  106. }
  107. Recipes.ElementAt(i).IsEnable = true;
  108. Recipes.ElementAt(i).Are.Set();
  109. }
  110. }
  111. }
  112. Thread.Sleep(100);
  113. }), "RecipeControlViewModelStatusInquire");
  114. //测试数据
  115. RawMaterialModel rawMaterial_1 = new RawMaterialModel { RawMaterialName = "香料_1" };
  116. RawMaterialModel rawMaterial_2 = new RawMaterialModel { RawMaterialName = "香料_2" };
  117. RawMaterialModel rawMaterial_3 = new RawMaterialModel { RawMaterialName = "香料_3" };
  118. RawMaterialModel rawMaterial_4 = new RawMaterialModel { RawMaterialName = "香料_4" };
  119. ObservableCollection<RawMaterialModel> rawMaterials = new ObservableCollection<RawMaterialModel> { rawMaterial_1, rawMaterial_2, rawMaterial_3, rawMaterial_4 };
  120. UserTreeCompelete.Add(new RecipeModel { RecipeName = "完成的香料1", RawMaterials = rawMaterials });
  121. UserTreeCompelete.Add(new RecipeModel { RecipeName = "完成的香料2", RawMaterials = rawMaterials });
  122. }
  123. public RelayCommand<object> StartCommand { get; set; }
  124. public RelayCommand<object> ChangeRecipeStateCommand { get; set; }
  125. public static ObservableCollection<RecipeModel> Recipes { get; set; } = Json<LocaPar>.Data.Recipes;
  126. public string CurrentRecipeName { get { return _RecipeName; }set { _RecipeName = value; OnPropertyChanged(); } }
  127. private static string _RecipeName;
  128. /// <summary>
  129. /// 当前正在制作的配方
  130. /// </summary>
  131. public static ObservableCollection<RawMaterialModel> recipeProcesses { get; set; } = new ObservableCollection<RawMaterialModel>();
  132. /// <summary>
  133. /// 等待制作的配方
  134. /// </summary>
  135. public static ObservableCollection<RecipeModel> UserTreeWait { get; set; } = new ObservableCollection<RecipeModel>();
  136. /// <summary>
  137. /// 已完成的配方
  138. /// </summary>
  139. public static ObservableCollection<RecipeModel> UserTreeCompelete { get; set; } = new ObservableCollection<RecipeModel>();
  140. private void ChangeRecipeState(object o)
  141. {
  142. if (o == null) return;
  143. if(o is string id)
  144. {
  145. var res = recipeProcesses.FirstOrDefault(p => p.RawMaterialId == id);
  146. if (res != null)
  147. {
  148. if(res.RecipeStatus == 3)
  149. {
  150. res.RecipeStatus = 1;
  151. }else
  152. {
  153. res.RecipeStatus = 3;
  154. }
  155. }
  156. }
  157. }
  158. }
  159. }