using Aitex.Common.Util; using Aitex.Core.UI.MVVM; using Aitex.Core.Util; using Aitex.Core.Utilities; using CyberX8_Simulator.Devices; using MECF.Framework.Common.Device.Festo; using MECF.Framework.Simulator.Core.Commons; using System.Collections.ObjectModel; using System.IO; using System.Windows; using System.Windows.Controls; using System.Windows.Input; namespace CyberX8_Simulator.Views { /// /// FestoView.xaml 的交互逻辑 /// public partial class FestoView : UserControl { public FestoView() { InitializeComponent(); this.Loaded += OnViewLoaded; } private void OnViewLoaded(object sender, RoutedEventArgs e) { (DataContext as TimerViewModelBase)?.Start(); } } class FestoViewModel : SocketDeviceViewModel { #region 属性 public string Title { get { return "Festo Simulator"; } } private string _DOSelectedItem; [IgnorePropertyChange] public string DOSelectedItem { get { return _DOSelectedItem; } set { _DOSelectedItem = value; } } private int _DOInputValue; [IgnorePropertyChange] public int DOInputValue { get { return _DOInputValue; } set { _DOInputValue = value; } } public ObservableCollection DONameItems { get; set; } public ObservableCollection DigitalOutputSelected { get; set; } #endregion public ICommand SetDOCommand { get; set; } private FestoSocketSimulator _sim; public FestoViewModel(string str) : base("FestoViewModel") { int.TryParse(str, out int port); _sim = new FestoSocketSimulator(port); Init(_sim); InitData(port); SetDOCommand = new DelegateCommand(SetDOAction); } private void InitData(int port) { DigitalOutputSelected = new ObservableCollection { 0, 1 }; string oldXmlPath = PathManager.GetCfgDir(); string newXmlPath = oldXmlPath.Replace("CyberX8_Simulator", "CyberX8_RT") + "Devices\\FestoControllerCfg-Simulator.xml"; FestoControllerCfg cfg = CustomXmlSerializer.Deserialize(new FileInfo(newXmlPath)); DONameItems = new ObservableCollection(); foreach (FestoDeviceConfig config in cfg.FestoDeviceConfigs) { if (port == config.Port) { foreach (FestoDO item in config.FestoDoes) { DONameItems.Add(item.Name); } } } } private void SetDOAction(object obj) { _sim.UpdataDOBytes(DOSelectedItem, DOInputValue); } } }