终端一体化运控平台
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

180 lines
8.3 KiB

  1. using BPA.Helper;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Collections.ObjectModel;
  5. using System.Linq;
  6. using System.Threading;
  7. namespace BPASmartClient.DosingSystem.ViewModel
  8. {
  9. public class HardwareStatusViewModel : NotifyBase
  10. {
  11. public HardwareStatusViewModel()
  12. {
  13. //TopDeviceCurrentStatuses = DeviceInquire.GetInstance.TopDeviceCurrentStatuses;
  14. //BottomDeviceCurrentStatuses = DeviceInquire.GetInstance.BottomDeviceCurrentStatuses;
  15. //2023/7/3调整
  16. TopDeviceCurrentStatuses = ConvertToObservableCollention(DeviceInquire.GetInstance.TopDeviceCurrentStatuses.OrderByDescending(p => p.DeviceNum).Reverse());
  17. BottomDeviceCurrentStatuses = ConvertToObservableCollention(DeviceInquire.GetInstance.BottomDeviceCurrentStatuses.OrderByDescending(p => p.DeviceNum).Reverse());
  18. UnifordColumNum();
  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. }), "输送带料仓状态监控", true);
  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. /// <summary>
  74. /// 数组集合转换
  75. /// </summary>
  76. /// <typeparam name="T"></typeparam>
  77. /// <param name="array"></param>
  78. /// <returns></returns>
  79. public ObservableCollection<T> ConvertToObservableCollention<T>(IEnumerable<T> array)
  80. {
  81. ObservableCollection<T> collec = new ObservableCollection<T>();
  82. foreach (var item in array)
  83. {
  84. collec.Add(item);
  85. }
  86. return collec;
  87. }
  88. private void StatusUpdate(ObservableCollection<DeviceCurrentStatus> devices)
  89. {
  90. if (devices == null) return;
  91. for (int i = 0; i < devices.Count; i++)
  92. {
  93. if (devices.ElementAt(i).BaitingControl)
  94. {
  95. var res = DeviceInquire.GetInstance.devices.FirstOrDefault(p => p.DeviceName == devices.ElementAt(i).DeviceName);
  96. if (res != null)
  97. {
  98. var runStatus = DeviceInquire.GetInstance.GetDevice(res.IpAddress).deviceStatus.RunStatus;
  99. if (runStatus == 3)
  100. {
  101. if (TopDeviceCurrentStatuses.FirstOrDefault(p => p.DeviceName == devices.ElementAt(i).DeviceName) != null)
  102. {
  103. TopDeviceCurrentStatuses.ElementAt(i).BaitingControl = false;
  104. }
  105. else if (BottomDeviceCurrentStatuses.FirstOrDefault(p => p.DeviceName == devices.ElementAt(i).DeviceName) != null)
  106. {
  107. BottomDeviceCurrentStatuses.ElementAt(i).BaitingControl = false;
  108. }
  109. DeviceInquire.GetInstance.GetDevice(res.IpAddress).StatusReset();
  110. }
  111. }
  112. }
  113. }
  114. }
  115. /// <summary>
  116. /// 根据料仓数来进行uniform列划分
  117. /// </summary>
  118. private void UnifordColumNum()
  119. {
  120. StockNum = TopDeviceCurrentStatuses.Count >= BottomDeviceCurrentStatuses.Count ? TopDeviceCurrentStatuses.Count : BottomDeviceCurrentStatuses.Count;
  121. //switch ((ConveryType)System.Enum.Parse(typeof(ConveryType), Json<DevicePar>.Data.BaseParModel.ConveryType))
  122. //{
  123. // case ConveryType.直线型: StockNum = Json<DevicePar>.Data.BaseParModel.StockCount; break;
  124. // //case ConveryType.直线型:
  125. // // if (Json<DevicePar>.Data.BaseParModel.StockCount > 0 && Json<DevicePar>.Data.BaseParModel.StockCount % 2 == 0)
  126. // // {
  127. // // StockNum = Json<DevicePar>.Data.BaseParModel.StockCount / 2;
  128. // // }
  129. // // else
  130. // // {
  131. // // StockNum = 8;//默认值
  132. // // }
  133. // // break;
  134. //}
  135. }
  136. public ObservableCollection<DeviceCurrentStatus> TopDeviceCurrentStatuses { get; set; }
  137. public ObservableCollection<DeviceCurrentStatus> BottomDeviceCurrentStatuses { get; set; }
  138. public ObservableCollection<ConveyerBeltModel> ConveyerBeltModels { get; set; } = new ObservableCollection<ConveyerBeltModel>();
  139. public BPARelayCommand<object> ConveyerBeltControlCommand { get; set; }
  140. public BPARelayCommand<object> RecipeControlCommand { get; set; }
  141. /// <summary>
  142. /// Uniford根据料仓数量来划分列数量
  143. /// </summary>
  144. private int _stockNum;
  145. public int StockNum { get { return _stockNum; } set { _stockNum = value; OnPropertyChanged(); } }
  146. /// <summary>
  147. /// 输送带状态
  148. /// </summary>
  149. public bool ConveyerBeltStatus { get { return _mConveyerBeltStatus; } set { _mConveyerBeltStatus = value; OnPropertyChanged(); } }
  150. private bool _mConveyerBeltStatus;
  151. /// <summary>
  152. /// 上桶工位检测
  153. /// </summary>
  154. public bool OnDetection { get { return _mOnDetection; } set { _mOnDetection = value; OnPropertyChanged(); } }
  155. private bool _mOnDetection;
  156. /// <summary>
  157. /// 下桶工位检测
  158. /// </summary>
  159. public bool UnderDetection { get { return _mUnderDetection; } set { _mUnderDetection = value; OnPropertyChanged(); } }
  160. private bool _mUnderDetection;
  161. }
  162. }