终端一体化运控平台
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.
 
 
 

138 строки
6.3 KiB

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using BPA.Helper;
  7. using System.Collections.Concurrent;
  8. using System.Collections.ObjectModel;
  9. using System.Windows;
  10. using System.Threading;
  11. namespace BPASmartClient.DosingSystemSingle.ViewModel
  12. {
  13. public class HardwareStatusViewModel : NotifyBase
  14. {
  15. public HardwareStatusViewModel()
  16. {
  17. TopDeviceCurrentStatuses = DeviceInquire.GetInstance.TopDeviceCurrentStatuses;
  18. BottomDeviceCurrentStatuses = DeviceInquire.GetInstance.BottomDeviceCurrentStatuses;
  19. //for (int i = 0; i < Json<DevicePar>.Data.BaseParModel.ConveyerBeltCount; i++)
  20. //{
  21. // ConveyerBeltModels.Add(new ConveyerBeltModel() { Name = $"输送带{i}", Num = i++ });
  22. //}
  23. //ConveyerBeltControlCommand = new BPARelayCommand<object>(o =>
  24. //{
  25. // if (o != null && o is int tempCount)
  26. // {
  27. // string add = SiemensDevice.GetInstance.GetSiemensBitSingleAdd("DB5.DBX", tempCount, 6);
  28. // int index = ConveyerBeltModels.ToList().FindIndex(p => p.Num == tempCount);
  29. // if (index >= 0 && index < ConveyerBeltModels.Count)
  30. // {
  31. // SiemensDevice.GetInstance.MySiemens.Write(add, !ConveyerBeltModels.ElementAt(index).Control);
  32. // }
  33. // }
  34. //});
  35. TaskManage.GetInstance.StartLong(new Action(() =>
  36. {
  37. for (int i = 0; i < Json<DevicePar>.Data.OutletInfoModels.Count; i++)
  38. {
  39. int count = Json<DevicePar>.Data.OutletInfoModels.ElementAt(i).SiloInfos.Count;
  40. if (count >= 1)
  41. {
  42. for (int m = 0; m < count; m++)
  43. {
  44. var deviceName = Json<DevicePar>.Data.OutletInfoModels.ElementAt(i).SiloInfos.ElementAt(m);
  45. int topIndex = TopDeviceCurrentStatuses.ToList().FindIndex(p => p.DeviceName == deviceName);
  46. int bottomIndex = BottomDeviceCurrentStatuses.ToList().FindIndex(p => p.DeviceName == deviceName);
  47. if (topIndex >= 0 && topIndex < TopDeviceCurrentStatuses.Count)
  48. TopDeviceCurrentStatuses.ElementAt(i).BucketFlagbit = GlobalDevice.PlcData.StationDetection[i];
  49. if (bottomIndex >= 0 && bottomIndex < BottomDeviceCurrentStatuses.Count)
  50. BottomDeviceCurrentStatuses.ElementAt(i).BucketFlagbit = GlobalDevice.PlcData.StationDetection[i];
  51. }
  52. }
  53. }
  54. StatusUpdate(TopDeviceCurrentStatuses);
  55. StatusUpdate(BottomDeviceCurrentStatuses);
  56. OnDetection = GlobalDevice.PlcData.OnDetection;
  57. UnderDetection = GlobalDevice.PlcData.UnderDetection;
  58. ConveyerBeltStatus = GlobalDevice.MotorControlFeedback > 0;
  59. Thread.Sleep(100);
  60. }), "输送带料仓状态监控");
  61. RecipeControlCommand = new BPARelayCommand<object>((o) =>
  62. {
  63. if (o != null && o is DeviceCurrentStatus currentStatus)
  64. {
  65. var res = DeviceInquire.GetInstance.devices.FirstOrDefault(p => p.DeviceName == currentStatus.DeviceName);
  66. if (res != null)
  67. {
  68. DeviceInquire.GetInstance.GetDevice(res.IpAddress).Start(currentStatus.SetWeight);
  69. }
  70. }
  71. });
  72. }
  73. private void StatusUpdate(ObservableCollection<DeviceCurrentStatus> devices)
  74. {
  75. if (devices == null) return;
  76. for (int i = 0; i < devices.Count; i++)
  77. {
  78. if (devices.ElementAt(i).BaitingControl)
  79. {
  80. var res = DeviceInquire.GetInstance.devices.FirstOrDefault(p => p.DeviceName == devices.ElementAt(i).DeviceName);
  81. if (res != null)
  82. {
  83. var runStatus = DeviceInquire.GetInstance.GetDevice(res.IpAddress).deviceStatus.RunStatus;
  84. if (runStatus == 3)
  85. {
  86. if (TopDeviceCurrentStatuses.FirstOrDefault(p => p.DeviceName == devices.ElementAt(i).DeviceName) != null)
  87. {
  88. TopDeviceCurrentStatuses.ElementAt(i).BaitingControl = false;
  89. }
  90. else if (BottomDeviceCurrentStatuses.FirstOrDefault(p => p.DeviceName == devices.ElementAt(i).DeviceName) != null)
  91. {
  92. BottomDeviceCurrentStatuses.ElementAt(i).BaitingControl = false;
  93. }
  94. DeviceInquire.GetInstance.GetDevice(res.IpAddress).StatusReset();
  95. }
  96. }
  97. }
  98. }
  99. }
  100. public ObservableCollection<DeviceCurrentStatus> TopDeviceCurrentStatuses { get; set; }
  101. public ObservableCollection<DeviceCurrentStatus> BottomDeviceCurrentStatuses { get; set; }
  102. public ObservableCollection<ConveyerBeltModel> ConveyerBeltModels { get; set; } = new ObservableCollection<ConveyerBeltModel>();
  103. public BPARelayCommand<object> ConveyerBeltControlCommand { get; set; }
  104. public BPARelayCommand<object> RecipeControlCommand { get; set; }
  105. /// <summary>
  106. /// 输送带状态
  107. /// </summary>
  108. public bool ConveyerBeltStatus { get { return _mConveyerBeltStatus; } set { _mConveyerBeltStatus = value; OnPropertyChanged(); } }
  109. private bool _mConveyerBeltStatus;
  110. /// <summary>
  111. /// 上桶工位检测
  112. /// </summary>
  113. public bool OnDetection { get { return _mOnDetection; } set { _mOnDetection = value; OnPropertyChanged(); } }
  114. private bool _mOnDetection;
  115. /// <summary>
  116. /// 下桶工位检测
  117. /// </summary>
  118. public bool UnderDetection { get { return _mUnderDetection; } set { _mUnderDetection = value; OnPropertyChanged(); } }
  119. private bool _mUnderDetection;
  120. }
  121. }