Simu_CometRFView.xaml.cs 3.6 KB

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