using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.Linq; using System.Windows; using System.Windows.Controls; using System.Windows.Input; using Aitex.Core.UI.MVVM; using MECF.Framework.Simulator.Core.Commons; using MECF.Framework.Simulator.Core.Driver; using CyberX8_Simulator.Devices; using CyberX8_Simulator.Instances; namespace CyberX8_Simulator.Views { /// /// Simu_CometRFView.xaml 的交互逻辑 /// public partial class Simu_CometRFView : UserControl { public Simu_CometRFView() { InitializeComponent(); this.DataContext = new CometRFSimulatorViewModel("CometRFViewModel"); this.Loaded += OnViewLoaded; } private void OnViewLoaded(object sender, RoutedEventArgs e) { (DataContext as TimerViewModelBase)?.Start(); } } class CometRFSimulatorViewModel : TimerViewModelBase { int port = 502; public ICommand ClearLogCommand { get; set; } public ICommand EnableCommand { get; set; } public ICommand DisableCommand { get; set; } public string Title { get { return "CometRF Simulator"; } } public ObservableCollection TransactionLogItems { get; set; } private CometRFSocketServer _cometRFServer; private int maxItemNumber = 200; public CometRFSimulatorViewModel(string name) : base(name) { _cometRFServer = new CometRFSocketServer(port); ClearLogCommand = new DelegateCommand(ClearLog); EnableCommand = new DelegateCommand(Enable); DisableCommand = new DelegateCommand(Disable); TransactionLogItems = new ObservableCollection(); _cometRFServer.MessageOut += _simulator_MessageOut; _cometRFServer.MessageIn += _simulator_MessageIn; } private void Disable(string obj) { } private void Enable(string obj) { } private void ClearLog(string obj) { TransactionLogItems.Clear(); } private void _simulator_MessageIn(string obj) { Application.Current.Dispatcher.Invoke(new Action(() => { TransactionLogItems.Add(new TransactionLogItem() { Incoming = obj, OccurTime = DateTime.Now.ToString("HH:mm:ss.fff") }); if (TransactionLogItems.Count > maxItemNumber) TransactionLogItems = new ObservableCollection(TransactionLogItems.Skip(1).Take(maxItemNumber).ToList()); Poll(); })); } private void _simulator_MessageOut(string obj) { Application.Current.Dispatcher.Invoke(new Action(() => { TransactionLogItems.Add(new TransactionLogItem() { Outgoing = obj, OccurTime = DateTime.Now.ToString("HH:mm:ss.fff") }); if (TransactionLogItems.Count > maxItemNumber) TransactionLogItems = new ObservableCollection(TransactionLogItems.Skip(1).Take(maxItemNumber).ToList()); Poll(); })); } protected override void Poll() { Application.Current.Dispatcher.Invoke(new Action(() => { InvokeAllPropertyChanged(); })); } } }