终端一体化运控平台
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.
 
 
 

93 lignes
4.2 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. namespace BPASmartClient.JXJFoodSmallStation.ViewModel
  18. {
  19. public class RecipeControlViewModel : ObservableObject
  20. {
  21. ConcurrentQueue<string> devices = new ConcurrentQueue<string>();
  22. public RecipeControlViewModel()
  23. {
  24. Recipes = Json<LocaPar>.Data.Recipes;
  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. }
  37. NoticeDemoViewModel.OpenMsg(EnumPromptType.Success, App.MainWindow, "提示", $"配方下发成功!");
  38. MessageLog.GetInstance.ShowUserLog($"下发工单 {Guid.NewGuid().ToString()}");
  39. });
  40. ThreadManage.GetInstance().StartLong(new Action(() =>
  41. {
  42. if (devices.Count > 0)
  43. {
  44. int index = Array.FindIndex(Recipes.ToArray(), p => p.RecipeName == devices.ElementAt(0));///???????
  45. if (index >= 0 && index < Recipes.Count)
  46. {
  47. Recipes.ElementAt(index).Are.Reset();
  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();//完成配料的设备运行状态地址写0
  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. }