using System;
using System.Collections.ObjectModel;
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 EfemDualSimulator.Devices;
using EfemDualSimulator.Instances;
namespace EfemDualSimulator.Views
{
///
/// Simu_CometRFView.xaml 的交互逻辑
///
public partial class Simu_CometRFView : UserControl
{
public Simu_CometRFView(string port)
{
InitializeComponent();
this.DataContext = new CometRFSimulatorViewModel("CometRFViewModel", port);
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;
public CometRFSimulatorViewModel(string name,string port) : base(name)
{
_cometRFServer = new CometRFSocketServer(int.Parse(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") });
}));
}
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") });
}));
}
protected override void Poll()
{
Application.Current.Dispatcher.Invoke(new Action(() =>
{
InvokeAllPropertyChanged();
}));
}
}
}