Simu_CometRFView.xaml.cs 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. using System;
  2. using System.Collections.ObjectModel;
  3. using System.Windows;
  4. using System.Windows.Controls;
  5. using System.Windows.Input;
  6. using Aitex.Core.UI.MVVM;
  7. using MECF.Framework.Simulator.Core.Commons;
  8. using MECF.Framework.Simulator.Core.Driver;
  9. using EfemDualSimulator.Devices;
  10. using EfemDualSimulator.Instances;
  11. namespace EfemDualSimulator.Views
  12. {
  13. /// <summary>
  14. /// Simu_CometRFView.xaml 的交互逻辑
  15. /// </summary>
  16. public partial class Simu_CometRFView : UserControl
  17. {
  18. public Simu_CometRFView(string port)
  19. {
  20. InitializeComponent();
  21. this.DataContext = new CometRFSimulatorViewModel("CometRFViewModel", port);
  22. this.Loaded += OnViewLoaded;
  23. }
  24. private void OnViewLoaded(object sender, RoutedEventArgs e)
  25. {
  26. (DataContext as TimerViewModelBase)?.Start();
  27. }
  28. }
  29. class CometRFSimulatorViewModel : TimerViewModelBase
  30. {
  31. //int port = 502;
  32. public ICommand ClearLogCommand { get; set; }
  33. public ICommand EnableCommand { get; set; }
  34. public ICommand DisableCommand { get; set; }
  35. public string Title
  36. {
  37. get { return "CometRF Simulator"; }
  38. }
  39. public ObservableCollection<TransactionLogItem> TransactionLogItems { get; set; }
  40. private CometRFSocketServer _cometRFServer;
  41. public CometRFSimulatorViewModel(string name,string port) : base(name)
  42. {
  43. _cometRFServer = new CometRFSocketServer(int.Parse(port));
  44. ClearLogCommand = new DelegateCommand<string>(ClearLog);
  45. EnableCommand = new DelegateCommand<string>(Enable);
  46. DisableCommand = new DelegateCommand<string>(Disable);
  47. TransactionLogItems = new ObservableCollection<TransactionLogItem>();
  48. _cometRFServer.MessageOut += _simulator_MessageOut;
  49. _cometRFServer.MessageIn += _simulator_MessageIn;
  50. }
  51. private void Disable(string obj)
  52. {
  53. }
  54. private void Enable(string obj)
  55. {
  56. }
  57. private void ClearLog(string obj)
  58. {
  59. TransactionLogItems.Clear();
  60. }
  61. private void _simulator_MessageIn(string obj)
  62. {
  63. Application.Current.Dispatcher.Invoke(new Action(() =>
  64. {
  65. TransactionLogItems.Add(new TransactionLogItem() { Incoming = obj, OccurTime = DateTime.Now.ToString("HH:mm:ss.fff") });
  66. }));
  67. }
  68. private void _simulator_MessageOut(string obj)
  69. {
  70. Application.Current.Dispatcher.Invoke(new Action(() =>
  71. {
  72. TransactionLogItems.Add(new TransactionLogItem() { Outgoing = obj, OccurTime = DateTime.Now.ToString("HH:mm:ss.fff") });
  73. }));
  74. }
  75. protected override void Poll()
  76. {
  77. Application.Current.Dispatcher.Invoke(new Action(() =>
  78. {
  79. InvokeAllPropertyChanged();
  80. }));
  81. }
  82. }
  83. }