2016-06-20 10:30:03 +00:00
|
|
|
using System;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.Collections.ObjectModel;
|
|
|
|
using System.Threading;
|
|
|
|
using System.Threading.Tasks;
|
2016-06-18 18:30:12 +00:00
|
|
|
using System.Windows.Input;
|
|
|
|
using PettingZoo.Infrastructure;
|
|
|
|
using PettingZoo.Model;
|
|
|
|
|
|
|
|
namespace PettingZoo.ViewModel
|
|
|
|
{
|
|
|
|
public class MainViewModel : BaseViewModel
|
|
|
|
{
|
2016-06-20 10:30:03 +00:00
|
|
|
private readonly TaskScheduler uiScheduler;
|
2016-06-18 18:30:12 +00:00
|
|
|
private readonly IConnectionInfoBuilder connectionInfoBuilder;
|
|
|
|
private readonly IConnectionFactory connectionFactory;
|
|
|
|
|
|
|
|
private ConnectionInfo connectionInfo;
|
|
|
|
private IConnection connection;
|
2016-06-20 10:30:03 +00:00
|
|
|
private readonly ObservableCollection<MessageInfo> messages;
|
|
|
|
private MessageInfo selectedMessage;
|
2016-06-18 18:30:12 +00:00
|
|
|
|
|
|
|
private readonly DelegateCommand connectCommand;
|
|
|
|
private readonly DelegateCommand disconnectCommand;
|
|
|
|
private readonly DelegateCommand clearCommand;
|
|
|
|
|
|
|
|
|
|
|
|
public ConnectionInfo ConnectionInfo {
|
2016-06-20 10:30:03 +00:00
|
|
|
get { return connectionInfo; }
|
|
|
|
private set
|
2016-06-18 18:30:12 +00:00
|
|
|
{
|
2016-06-20 10:30:03 +00:00
|
|
|
if (value == connectionInfo)
|
|
|
|
return;
|
|
|
|
|
|
|
|
connectionInfo = value;
|
|
|
|
RaisePropertyChanged();
|
2016-06-18 18:30:12 +00:00
|
|
|
}
|
2016-06-20 10:30:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public ObservableCollection<MessageInfo> Messages { get { return messages; } }
|
|
|
|
|
|
|
|
public MessageInfo SelectedMessage
|
|
|
|
{
|
|
|
|
get { return selectedMessage; }
|
|
|
|
set
|
|
|
|
{
|
|
|
|
if (value == selectedMessage)
|
|
|
|
return;
|
|
|
|
|
|
|
|
selectedMessage = value;
|
|
|
|
RaisePropertyChanged();
|
|
|
|
RaiseOtherPropertyChanged("SelectedMessageBody");
|
|
|
|
RaiseOtherPropertyChanged("SelectedMessageProperties");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public string SelectedMessageBody
|
|
|
|
{
|
|
|
|
get
|
2016-06-18 18:30:12 +00:00
|
|
|
{
|
2016-06-20 10:30:03 +00:00
|
|
|
return SelectedMessage != null
|
|
|
|
? MessageBodyRenderer.Render(SelectedMessage.Body, SelectedMessage.ContentType)
|
|
|
|
: "";
|
2016-06-18 18:30:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-20 10:30:03 +00:00
|
|
|
public Dictionary<string, string> SelectedMessageProperties
|
|
|
|
{
|
|
|
|
get { return SelectedMessage != null ? SelectedMessage.Properties : null; }
|
|
|
|
}
|
|
|
|
|
2016-06-18 18:30:12 +00:00
|
|
|
public ICommand ConnectCommand { get { return connectCommand; } }
|
|
|
|
public ICommand DisconnectCommand { get { return disconnectCommand; } }
|
|
|
|
public ICommand ClearCommand { get { return clearCommand; } }
|
|
|
|
|
|
|
|
|
|
|
|
public MainViewModel(IConnectionInfoBuilder connectionInfoBuilder, IConnectionFactory connectionFactory)
|
|
|
|
{
|
2016-06-20 10:30:03 +00:00
|
|
|
uiScheduler = TaskScheduler.FromCurrentSynchronizationContext();
|
|
|
|
|
2016-06-18 18:30:12 +00:00
|
|
|
this.connectionInfoBuilder = connectionInfoBuilder;
|
|
|
|
this.connectionFactory = connectionFactory;
|
|
|
|
|
2016-06-20 10:30:03 +00:00
|
|
|
messages = new ObservableCollection<MessageInfo>();
|
|
|
|
|
2016-06-18 18:30:12 +00:00
|
|
|
connectCommand = new DelegateCommand(ConnectExecute);
|
|
|
|
disconnectCommand = new DelegateCommand(DisconnectExecute, DisconnectCanExecute);
|
|
|
|
clearCommand = new DelegateCommand(ClearExecute, ClearCanExecute);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private void ConnectExecute()
|
|
|
|
{
|
|
|
|
var newInfo = connectionInfoBuilder.Build();
|
|
|
|
if (newInfo == null)
|
|
|
|
return;
|
|
|
|
|
|
|
|
ConnectionInfo = newInfo;
|
|
|
|
connection = connectionFactory.CreateConnection(connectionInfo);
|
2016-06-20 10:30:03 +00:00
|
|
|
connection.MessageReceived += ConnectionMessageReceived;
|
2016-06-18 18:30:12 +00:00
|
|
|
|
|
|
|
disconnectCommand.RaiseCanExecuteChanged();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private void DisconnectExecute()
|
|
|
|
{
|
|
|
|
if (connection != null)
|
|
|
|
{
|
|
|
|
connection.Dispose();
|
|
|
|
connection = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
ConnectionInfo = null;
|
2016-06-20 10:30:03 +00:00
|
|
|
disconnectCommand.RaiseCanExecuteChanged();
|
2016-06-18 18:30:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private bool DisconnectCanExecute()
|
|
|
|
{
|
|
|
|
return connection != null;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private void ClearExecute()
|
|
|
|
{
|
2016-06-20 12:20:37 +00:00
|
|
|
messages.Clear();
|
|
|
|
clearCommand.RaiseCanExecuteChanged();
|
2016-06-18 18:30:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private bool ClearCanExecute()
|
|
|
|
{
|
2016-06-20 12:20:37 +00:00
|
|
|
return messages.Count > 0;
|
2016-06-18 18:30:12 +00:00
|
|
|
}
|
2016-06-20 10:30:03 +00:00
|
|
|
|
|
|
|
|
|
|
|
private void ConnectionMessageReceived(object sender, MessageReceivedEventArgs e)
|
|
|
|
{
|
2016-06-20 12:20:37 +00:00
|
|
|
RunFromUiScheduler(() =>
|
|
|
|
{
|
|
|
|
messages.Add(e.MessageInfo);
|
|
|
|
clearCommand.RaiseCanExecuteChanged();
|
|
|
|
});
|
2016-06-20 10:30:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private void RunFromUiScheduler(Action action)
|
|
|
|
{
|
2016-06-20 12:20:37 +00:00
|
|
|
Task.Factory.StartNew(action, CancellationToken.None, TaskCreationOptions.None, uiScheduler);
|
2016-06-20 10:30:03 +00:00
|
|
|
}
|
2016-06-18 18:30:12 +00:00
|
|
|
}
|
|
|
|
}
|