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

209 строки
8.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 BPA.Helper;
  7. using System.Collections.ObjectModel;
  8. using System.Threading;
  9. using BPASmartClient.S7Net;
  10. namespace BPASmartClient.DosingSystemSingle.ViewModel
  11. {
  12. public class ManualControlViewModel : NotifyBase
  13. {
  14. public ManualControlViewModel()
  15. {
  16. Init();
  17. for (int i = 0; i < Json<DevicePar>.Data.BaseParModel.LiftCylinderCount; i++)
  18. {
  19. cylinderModels.Add(new CylinderModel()
  20. {
  21. Name = $"升降气缸 {i + 1}",
  22. LeftTog = false,
  23. RightTog = false,
  24. Num = i + 1,
  25. });
  26. }
  27. for (int i = 0; i < Json<DevicePar>.Data.BaseParModel.BlockCylinderCount; i++)
  28. {
  29. BlockCylinders.Add(new CylinderModel()
  30. {
  31. Name = $"阻挡气缸 {i + 1}",
  32. LeftTog = false,
  33. RightTog = false,
  34. Num = i + 1,
  35. });
  36. }
  37. for (int i = 0; i < Json<DevicePar>.Data.BaseParModel.PalletCylinderCount; i++)
  38. {
  39. PalletCylinders.Add(new CylinderModel()
  40. {
  41. Name = $"托盘气缸 {i + 1}",
  42. LeftTog = false,
  43. RightTog = false,
  44. Num = i + 1,
  45. });
  46. }
  47. OtherHandCylinders.Add(new CylinderModel() { Name = "上桶工位气缸" });
  48. OtherHandCylinders.Add(new CylinderModel() { Name = "下桶工位气缸" });
  49. Open = new BPARelayCommand<object>((o) =>
  50. {
  51. if (o != null)
  52. {
  53. if (o.ToString().Contains("升降气缸"))
  54. {
  55. int index = Array.FindIndex(cylinderModels.ToArray(), p => p.Name == o.ToString());
  56. if (index >= 0 && index < cylinderModels.Count)
  57. {
  58. var addRes = SiemensDevice.GetInstance.GetSiemensBitSingleAdd("DB5.DBX", cylinderModels.ElementAt(index).Num);
  59. SiemensDevice.GetInstance.MySiemens.Write(addRes, true);
  60. }
  61. }
  62. else if (o.ToString().Contains("上桶工位气缸"))
  63. {
  64. SiemensDevice.GetInstance.MySiemens.Write("DB5.DBX4.0", true);
  65. }
  66. else if (o.ToString().Contains("下桶工位气缸"))
  67. {
  68. SiemensDevice.GetInstance.MySiemens.Write("DB5.DBX4.1", true);
  69. }
  70. }
  71. });
  72. Close = new BPARelayCommand<object>((o) =>
  73. {
  74. if (o != null)
  75. {
  76. if (o.ToString().Contains("升降气缸"))
  77. {
  78. int index = Array.FindIndex(cylinderModels.ToArray(), p => p.Name == o.ToString());
  79. if (index >= 0 && index < cylinderModels.Count)
  80. {
  81. var addRes = SiemensDevice.GetInstance.GetSiemensBitSingleAdd("DB5.DBX", cylinderModels.ElementAt(index).Num);
  82. SiemensDevice.GetInstance.MySiemens.Write(addRes, false);
  83. }
  84. }
  85. else if (o.ToString().Contains("上桶工位气缸"))
  86. {
  87. SiemensDevice.GetInstance.MySiemens.Write("DB5.DBX4.0", false);
  88. }
  89. else if (o.ToString().Contains("下桶工位气缸"))
  90. {
  91. SiemensDevice.GetInstance.MySiemens.Write("DB5.DBX4.1", false);
  92. }
  93. }
  94. });
  95. SystemStart = new BPARelayCommand(() =>
  96. {
  97. SiemensDevice.GetInstance.MySiemens.Write("M10.0", SystemControlText == "停止" ? false : true);
  98. /*var res = SiemensDevice.GetInstance.MySiemens.Read<bool>("M10.0");
  99. SystemControlText = res ? "停止" : "启动";*/
  100. });
  101. ModelSwitch = new BPARelayCommand(() =>
  102. {
  103. SiemensDevice.GetInstance.MySiemens.Write("M10.1", ModelSwitchText == "手动" ? true : false);
  104. /*var res = SiemensDevice.GetInstance.MySiemens.Read<bool>("M10.1");
  105. ModelSwitchText = res ? "自动" : "手动";*/
  106. });
  107. TaskManage.GetInstance.StartLong(new Action(() =>
  108. {
  109. for (int i = 0; i < cylinderModels.Count; i++)
  110. {
  111. //升降气缸状态
  112. cylinderModels.ElementAt(i).RightTog = (bool)GlobalDevice.PlcData.cylinderFlagBitStatus[i]?.HomeSignal;
  113. cylinderModels.ElementAt(i).LeftTog = (bool)GlobalDevice.PlcData.cylinderFlagBitStatus[i]?.InPlaceSignal;
  114. }
  115. //上桶工位气缸状态
  116. OtherHandCylinders.ElementAt(0).RightTog = GlobalDevice.PlcData.OnCylinderDetection.HomeSignal;
  117. OtherHandCylinders.ElementAt(0).LeftTog = GlobalDevice.PlcData.OnCylinderDetection.InPlaceSignal;
  118. //下桶工位气缸状态
  119. OtherHandCylinders.ElementAt(1).RightTog = GlobalDevice.PlcData.UnderCylinderDetection.HomeSignal;
  120. OtherHandCylinders.ElementAt(1).LeftTog = GlobalDevice.PlcData.UnderCylinderDetection.InPlaceSignal;
  121. SystemControlText = GlobalDevice.PlcData.SystemStartOrStop ? "停止" : "启动";
  122. ModelSwitchText = GlobalDevice.PlcData.HandOrAuto ? "自动" : "手动";
  123. Thread.Sleep(100);
  124. }), "手动气缸状态监控");
  125. }
  126. private void Init()
  127. {
  128. var res = SiemensDevice.GetInstance.MySiemens.Read<bool>("M10.0");
  129. SystemControlText = res ? "停止" : "启动";
  130. var res1 = SiemensDevice.GetInstance.MySiemens.Read<bool>("M10.1");
  131. ModelSwitchText = res1 ? "自动" : "手动";
  132. }
  133. /// <summary>
  134. /// 升降气缸
  135. /// </summary>
  136. public ObservableCollection<CylinderModel> cylinderModels { get; set; } = new ObservableCollection<CylinderModel>();
  137. /// <summary>
  138. /// 阻挡气缸
  139. /// </summary>
  140. public ObservableCollection<CylinderModel> BlockCylinders { get; set; } = new ObservableCollection<CylinderModel>();
  141. /// <summary>
  142. /// 托盘气缸
  143. /// </summary>
  144. public ObservableCollection<CylinderModel> PalletCylinders { get; set; } = new ObservableCollection<CylinderModel>();
  145. /// <summary>
  146. /// 其它手动控制气缸
  147. /// </summary>
  148. public ObservableCollection<CylinderModel> OtherHandCylinders { get; set; } = new ObservableCollection<CylinderModel>();
  149. public BPARelayCommand<object> Open { get; set; }
  150. public BPARelayCommand<object> Close { get; set; }
  151. public BPARelayCommand SystemStart { get; set; }
  152. public BPARelayCommand ModelSwitch { get; set; }
  153. public string SystemControlText { get { return _mSystemControlText; } set { _mSystemControlText = value; OnPropertyChanged(); } }
  154. private string _mSystemControlText;
  155. public string ModelSwitchText { get { return _mModelSwitchText; } set { _mModelSwitchText = value; OnPropertyChanged(); } }
  156. private string _mModelSwitchText;
  157. }
  158. public class CylinderModel : NotifyBase
  159. {
  160. public bool LeftTog { get { return _mLeftTog; } set { _mLeftTog = value; OnPropertyChanged(); } }
  161. private bool _mLeftTog;
  162. public bool RightTog { get { return _mRightTog; } set { _mRightTog = value; OnPropertyChanged(); } }
  163. private bool _mRightTog;
  164. public string Name { get { return _mName; } set { _mName = value; OnPropertyChanged(); } }
  165. private string _mName;
  166. public bool Control { get { return _mControl; } set { _mControl = value; OnPropertyChanged(); } }
  167. private bool _mControl;
  168. /// <summary>
  169. /// 气缸默认编号
  170. /// </summary>
  171. public int Num { get; set; }
  172. }
  173. }