using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using BPA.Helper; using System.Collections.ObjectModel; using System.Threading; using BPASmartClient.S7Net; namespace BPASmartClient.DosingSystemSingle.ViewModel { public class ManualControlViewModel : NotifyBase { public ManualControlViewModel() { Init(); for (int i = 0; i < Json.Data.BaseParModel.LiftCylinderCount; i++) { cylinderModels.Add(new CylinderModel() { Name = $"升降气缸 {i + 1}", LeftTog = false, RightTog = false, Num = i + 1, }); } for (int i = 0; i < Json.Data.BaseParModel.BlockCylinderCount; i++) { BlockCylinders.Add(new CylinderModel() { Name = $"阻挡气缸 {i + 1}", LeftTog = false, RightTog = false, Num = i + 1, }); } for (int i = 0; i < Json.Data.BaseParModel.PalletCylinderCount; i++) { PalletCylinders.Add(new CylinderModel() { Name = $"托盘气缸 {i + 1}", LeftTog = false, RightTog = false, Num = i + 1, }); } OtherHandCylinders.Add(new CylinderModel() { Name = "上桶工位气缸" }); OtherHandCylinders.Add(new CylinderModel() { Name = "下桶工位气缸" }); Open = new BPARelayCommand((o) => { if (o != null) { if (o.ToString().Contains("升降气缸")) { int index = Array.FindIndex(cylinderModels.ToArray(), p => p.Name == o.ToString()); if (index >= 0 && index < cylinderModels.Count) { var addRes = SiemensDevice.GetInstance.GetSiemensBitSingleAdd("DB5.DBX", cylinderModels.ElementAt(index).Num); SiemensDevice.GetInstance.MySiemens.Write(addRes, true); } } else if (o.ToString().Contains("上桶工位气缸")) { SiemensDevice.GetInstance.MySiemens.Write("DB5.DBX4.0", true); } else if (o.ToString().Contains("下桶工位气缸")) { SiemensDevice.GetInstance.MySiemens.Write("DB5.DBX4.1", true); } } }); Close = new BPARelayCommand((o) => { if (o != null) { if (o.ToString().Contains("升降气缸")) { int index = Array.FindIndex(cylinderModels.ToArray(), p => p.Name == o.ToString()); if (index >= 0 && index < cylinderModels.Count) { var addRes = SiemensDevice.GetInstance.GetSiemensBitSingleAdd("DB5.DBX", cylinderModels.ElementAt(index).Num); SiemensDevice.GetInstance.MySiemens.Write(addRes, false); } } else if (o.ToString().Contains("上桶工位气缸")) { SiemensDevice.GetInstance.MySiemens.Write("DB5.DBX4.0", false); } else if (o.ToString().Contains("下桶工位气缸")) { SiemensDevice.GetInstance.MySiemens.Write("DB5.DBX4.1", false); } } }); SystemStart = new BPARelayCommand(() => { SiemensDevice.GetInstance.MySiemens.Write("M10.0", SystemControlText == "停止" ? false : true); /*var res = SiemensDevice.GetInstance.MySiemens.Read("M10.0"); SystemControlText = res ? "停止" : "启动";*/ }); ModelSwitch = new BPARelayCommand(() => { SiemensDevice.GetInstance.MySiemens.Write("M10.1", ModelSwitchText == "手动" ? true : false); /*var res = SiemensDevice.GetInstance.MySiemens.Read("M10.1"); ModelSwitchText = res ? "自动" : "手动";*/ }); ThreadManage.GetInstance().StartLong(new Action(() => { for (int i = 0; i < cylinderModels.Count; i++) { //升降气缸状态 cylinderModels.ElementAt(i).RightTog = (bool)GlobalDevice.PlcData.cylinderFlagBitStatus[i]?.HomeSignal; cylinderModels.ElementAt(i).LeftTog = (bool)GlobalDevice.PlcData.cylinderFlagBitStatus[i]?.InPlaceSignal; } //上桶工位气缸状态 OtherHandCylinders.ElementAt(0).RightTog = GlobalDevice.PlcData.OnCylinderDetection.HomeSignal; OtherHandCylinders.ElementAt(0).LeftTog = GlobalDevice.PlcData.OnCylinderDetection.InPlaceSignal; //下桶工位气缸状态 OtherHandCylinders.ElementAt(1).RightTog = GlobalDevice.PlcData.UnderCylinderDetection.HomeSignal; OtherHandCylinders.ElementAt(1).LeftTog = GlobalDevice.PlcData.UnderCylinderDetection.InPlaceSignal; SystemControlText = GlobalDevice.PlcData.SystemStartOrStop ? "停止" : "启动"; ModelSwitchText = GlobalDevice.PlcData.HandOrAuto ? "自动" : "手动"; Thread.Sleep(100); }), "手动气缸状态监控"); } private void Init() { var res = SiemensDevice.GetInstance.MySiemens.Read("M10.0"); SystemControlText = res ? "停止" : "启动"; var res1 = SiemensDevice.GetInstance.MySiemens.Read("M10.1"); ModelSwitchText = res1 ? "自动" : "手动"; } /// /// 升降气缸 /// public ObservableCollection cylinderModels { get; set; } = new ObservableCollection(); /// /// 阻挡气缸 /// public ObservableCollection BlockCylinders { get; set; } = new ObservableCollection(); /// /// 托盘气缸 /// public ObservableCollection PalletCylinders { get; set; } = new ObservableCollection(); /// /// 其它手动控制气缸 /// public ObservableCollection OtherHandCylinders { get; set; } = new ObservableCollection(); public BPARelayCommand Open { get; set; } public BPARelayCommand Close { get; set; } public BPARelayCommand SystemStart { get; set; } public BPARelayCommand ModelSwitch { get; set; } public string SystemControlText { get { return _mSystemControlText; } set { _mSystemControlText = value; OnPropertyChanged(); } } private string _mSystemControlText; public string ModelSwitchText { get { return _mModelSwitchText; } set { _mModelSwitchText = value; OnPropertyChanged(); } } private string _mModelSwitchText; } public class CylinderModel : NotifyBase { public bool LeftTog { get { return _mLeftTog; } set { _mLeftTog = value; OnPropertyChanged(); } } private bool _mLeftTog; public bool RightTog { get { return _mRightTog; } set { _mRightTog = value; OnPropertyChanged(); } } private bool _mRightTog; public string Name { get { return _mName; } set { _mName = value; OnPropertyChanged(); } } private string _mName; public bool Control { get { return _mControl; } set { _mControl = value; OnPropertyChanged(); } } private bool _mControl; /// /// 气缸默认编号 /// public int Num { get; set; } } }