FestoView.xaml.cs 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. using Aitex.Common.Util;
  2. using Aitex.Core.UI.MVVM;
  3. using Aitex.Core.Util;
  4. using Aitex.Core.Utilities;
  5. using CyberX8_Simulator.Devices;
  6. using MECF.Framework.Common.Device.Festo;
  7. using MECF.Framework.Simulator.Core.Commons;
  8. using System.Collections.ObjectModel;
  9. using System.IO;
  10. using System.Windows;
  11. using System.Windows.Controls;
  12. using System.Windows.Input;
  13. namespace CyberX8_Simulator.Views
  14. {
  15. /// <summary>
  16. /// FestoView.xaml 的交互逻辑
  17. /// </summary>
  18. public partial class FestoView : UserControl
  19. {
  20. public FestoView()
  21. {
  22. InitializeComponent();
  23. this.Loaded += OnViewLoaded;
  24. }
  25. private void OnViewLoaded(object sender, RoutedEventArgs e)
  26. {
  27. (DataContext as TimerViewModelBase)?.Start();
  28. }
  29. }
  30. class FestoViewModel : SocketDeviceViewModel
  31. {
  32. #region 属性
  33. public string Title
  34. {
  35. get { return "Festo Simulator"; }
  36. }
  37. private string _DOSelectedItem;
  38. [IgnorePropertyChange]
  39. public string DOSelectedItem
  40. {
  41. get
  42. {
  43. return _DOSelectedItem;
  44. }
  45. set
  46. {
  47. _DOSelectedItem = value;
  48. }
  49. }
  50. private int _DOInputValue;
  51. [IgnorePropertyChange]
  52. public int DOInputValue
  53. {
  54. get
  55. {
  56. return _DOInputValue;
  57. }
  58. set
  59. {
  60. _DOInputValue = value;
  61. }
  62. }
  63. public ObservableCollection<string> DONameItems { get; set; }
  64. public ObservableCollection<int> DigitalOutputSelected { get; set; }
  65. #endregion
  66. public ICommand SetDOCommand { get; set; }
  67. private FestoSocketSimulator _sim;
  68. public FestoViewModel(string str) : base("FestoViewModel")
  69. {
  70. int.TryParse(str, out int port);
  71. _sim = new FestoSocketSimulator(port);
  72. Init(_sim);
  73. InitData(port);
  74. SetDOCommand = new DelegateCommand<object>(SetDOAction);
  75. }
  76. private void InitData(int port)
  77. {
  78. DigitalOutputSelected = new ObservableCollection<int> { 0, 1 };
  79. string oldXmlPath = PathManager.GetCfgDir();
  80. string newXmlPath = oldXmlPath.Replace("CyberX8_Simulator", "CyberX8_RT") + "Devices\\FestoControllerCfg-Simulator.xml";
  81. FestoControllerCfg cfg = CustomXmlSerializer.Deserialize<FestoControllerCfg>(new FileInfo(newXmlPath));
  82. DONameItems = new ObservableCollection<string>();
  83. foreach (FestoDeviceConfig config in cfg.FestoDeviceConfigs)
  84. {
  85. if (port == config.Port)
  86. {
  87. foreach (FestoDO item in config.FestoDoes)
  88. {
  89. DONameItems.Add(item.Name);
  90. }
  91. }
  92. }
  93. }
  94. private void SetDOAction(object obj)
  95. {
  96. _sim.UpdataDOBytes(DOSelectedItem, DOInputValue);
  97. }
  98. }
  99. }