终端一体化运控平台
Você não pode selecionar mais de 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.

RecipeControlViewModel.cs 3.8 KiB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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 DosingSystem.Model;
  13. using System.Threading;
  14. namespace DosingSystem.ViewModel
  15. {
  16. public class RecipeControlViewModel : ObservableObject
  17. {
  18. ConcurrentQueue<string> devices = new ConcurrentQueue<string>();
  19. public RecipeControlViewModel()
  20. {
  21. Recipes = Json<LocaPar>.Data.Recipes;
  22. StartCommand = new RelayCommand<object>((o) =>
  23. {
  24. if (o != null && o is string deviceName)
  25. {
  26. //Task.Run(new Action(() =>
  27. //{
  28. int index = Array.FindIndex(Recipes.ToArray(), p => p.RecipeName == deviceName);
  29. if (index >= 0 && index < Recipes.Count)
  30. {
  31. Recipes.ElementAt(index).IsEnable = false;
  32. //foreach (var item in Recipes.ElementAt(index).RawMaterials)
  33. //{
  34. // DeviceInquire.GetInstance.GetDevice(item.DeviceIp)?.Start(item.RawMaterialWeight);//启动写入
  35. //}
  36. }
  37. //}));
  38. devices.Enqueue(deviceName);
  39. }
  40. });
  41. ThreadManage.GetInstance().StartLong(new Action(() =>
  42. {
  43. if (devices.Count > 0)
  44. {
  45. int index = Array.FindIndex(Recipes.ToArray(), p => p.RecipeName == devices.ElementAt(0));
  46. if (index >= 0 && index < Recipes.Count)
  47. {
  48. Recipes.ElementAt(index).IsEnable = false;
  49. foreach (var item in Recipes.ElementAt(index).RawMaterials)
  50. {
  51. DeviceInquire.GetInstance.GetDevice(item.DeviceIp)?.Start(item.RawMaterialWeight);//启动写入
  52. }
  53. Recipes.ElementAt(index).Are.WaitOne();
  54. devices.TryDequeue(out string deviceName);
  55. }
  56. }
  57. Thread.Sleep(100);
  58. }), "启动配发下发");
  59. ThreadManage.GetInstance().StartLong(new Action(() =>
  60. {
  61. for (int i = 0; i < Recipes.Count; i++)
  62. {
  63. for (int m = 0; m < Recipes.ElementAt(i).RawMaterials.Count; m++)
  64. {
  65. var RunStatus = DeviceInquire.GetInstance.GetDevice(Recipes.ElementAt(i).RawMaterials.ElementAt(m).DeviceIp).deviceStatus.RunStatus;
  66. Recipes.ElementAt(i).RawMaterials.ElementAt(m).RecipeStatus = RunStatus;
  67. var res = Recipes.ElementAt(i).RawMaterials.Where(p => p.RecipeStatus == 3).ToList();
  68. if (res != null && res.Count == Recipes.ElementAt(i).RawMaterials.Count)
  69. {
  70. for (int r = 0; r < Recipes.ElementAt(i).RawMaterials.Count; r++)
  71. {
  72. DeviceInquire.GetInstance.GetDevice(Recipes.ElementAt(i).RawMaterials.ElementAt(r).DeviceIp).StatusReset();
  73. }
  74. Recipes.ElementAt(i).IsEnable = true;
  75. Recipes.ElementAt(i).Are.Set();
  76. }
  77. }
  78. }
  79. Thread.Sleep(100);
  80. }), "RecipeControlViewModelStatusInquire");
  81. }
  82. public RelayCommand<object> StartCommand { get; set; }
  83. public ObservableCollection<RecipeModel> Recipes { get; set; }
  84. }
  85. }