终端一体化运控平台
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.
 
 
 

214 line
8.6 KiB

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