终端一体化运控平台
No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.
 
 
 

216 líneas
8.7 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.DosingSystem.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. Thread.Sleep(100);
  99. var res = SiemensDevice.GetInstance.MySiemens.Read<bool>("M10.0");
  100. SystemControlText = res ? "停止" : "启动";
  101. });
  102. ModelSwitch = new BPARelayCommand(() =>
  103. {
  104. SiemensDevice.GetInstance.MySiemens.Write("M10.1", ModelSwitchText == "手动" ? true : false);
  105. Thread.Sleep(100);
  106. var res = SiemensDevice.GetInstance.MySiemens.Read<bool>("M10.1");
  107. ModelSwitchText = res ? "自动" : "手动";
  108. });
  109. ThreadManage.GetInstance().StartLong(new Action(() =>
  110. {
  111. for (int i = 0; i < cylinderModels.Count; i++)
  112. {
  113. //升降气缸状态
  114. cylinderModels.ElementAt(i).RightTog = (bool)GlobalDevice.PlcData.cylinderFlagBitStatus[i]?.HomeSignal;
  115. cylinderModels.ElementAt(i).LeftTog = (bool)GlobalDevice.PlcData.cylinderFlagBitStatus[i]?.InPlaceSignal;
  116. }
  117. //上桶工位气缸状态
  118. OtherHandCylinders.ElementAt(0).RightTog = GlobalDevice.PlcData.OnCylinderDetection.HomeSignal;
  119. OtherHandCylinders.ElementAt(0).LeftTog = GlobalDevice.PlcData.OnCylinderDetection.InPlaceSignal;
  120. //下桶工位气缸状态
  121. OtherHandCylinders.ElementAt(1).RightTog = GlobalDevice.PlcData.UnderCylinderDetection.HomeSignal;
  122. OtherHandCylinders.ElementAt(1).LeftTog = GlobalDevice.PlcData.UnderCylinderDetection.InPlaceSignal;
  123. SystemStatus = GlobalDevice.PlcData.SystemStartOrStop ? "启动" : "停止";
  124. SystemMode = GlobalDevice.PlcData.HandOrAuto ? "自动" : "手动";
  125. Thread.Sleep(100);
  126. }), "手动气缸状态监控",true);
  127. }
  128. private void Init()
  129. {
  130. var res = SiemensDevice.GetInstance.MySiemens.Read<bool>("M10.0");
  131. SystemControlText = res ? "停止" : "启动";
  132. SystemStatus = res ? "停止" : "启动";
  133. var res1 = SiemensDevice.GetInstance.MySiemens.Read<bool>("M10.1");
  134. ModelSwitchText = res1 ? "自动" : "手动";
  135. SystemStatus = res ? "自动" : "手动";
  136. }
  137. /// <summary>
  138. /// 升降气缸
  139. /// </summary>
  140. public ObservableCollection<CylinderModel> cylinderModels { get; set; } = new ObservableCollection<CylinderModel>();
  141. /// <summary>
  142. /// 阻挡气缸
  143. /// </summary>
  144. public ObservableCollection<CylinderModel> BlockCylinders { get; set; } = new ObservableCollection<CylinderModel>();
  145. /// <summary>
  146. /// 托盘气缸
  147. /// </summary>
  148. public ObservableCollection<CylinderModel> PalletCylinders { get; set; } = new ObservableCollection<CylinderModel>();
  149. /// <summary>
  150. /// 其它手动控制气缸
  151. /// </summary>
  152. public ObservableCollection<CylinderModel> OtherHandCylinders { get; set; } = new ObservableCollection<CylinderModel>();
  153. public BPARelayCommand<object> Open { get; set; }
  154. public BPARelayCommand<object> Close { get; set; }
  155. public BPARelayCommand SystemStart { get; set; }
  156. public BPARelayCommand ModelSwitch { get; set; }
  157. public string SystemControlText { get { return _mSystemControlText; } set { _mSystemControlText = value; OnPropertyChanged(); } }
  158. private string _mSystemControlText;
  159. public string ModelSwitchText { get { return _mModelSwitchText; } set { _mModelSwitchText = value; OnPropertyChanged(); } }
  160. private string _mModelSwitchText;
  161. public string SystemMode { get { return _mSystemMode; } set { _mSystemMode = value; OnPropertyChanged(); } }
  162. private string _mSystemMode;
  163. public string SystemStatus { get { return _mSystemStatus; } set { _mSystemStatus = value; OnPropertyChanged(); } }
  164. private string _mSystemStatus;
  165. }
  166. public class CylinderModel : NotifyBase
  167. {
  168. public bool LeftTog { get { return _mLeftTog; } set { _mLeftTog = value; OnPropertyChanged(); } }
  169. private bool _mLeftTog;
  170. public bool RightTog { get { return _mRightTog; } set { _mRightTog = value; OnPropertyChanged(); } }
  171. private bool _mRightTog;
  172. public string Name { get { return _mName; } set { _mName = value; OnPropertyChanged(); } }
  173. private string _mName;
  174. public bool Control { get { return _mControl; } set { _mControl = value; OnPropertyChanged(); } }
  175. private bool _mControl;
  176. /// <summary>
  177. /// 气缸默认编号
  178. /// </summary>
  179. public int Num { get; set; }
  180. }
  181. }