implemented ConnectionRequestEventArgs to provide Connection result (Sucess or failure) to ProtocolSettingsViewModel

master
Jonas Arnold 3 years ago
parent 585ce8bebd
commit 641e72b1bd
  1. 15
      MultiTerm.Core/ViewModel/TerminalViewModel.cs
  2. 6
      MultiTerm.Protocols/CommunicationProtocol.cs
  3. 25
      MultiTerm.Protocols/ConnectionRequestEventArgs.cs
  4. 2
      MultiTerm.Protocols/ICommunicationProtocol.cs
  5. 2
      MultiTerm.Protocols/IProtocolSettingsViewModel.cs
  6. 19
      MultiTerm.Protocols/ProtocolSettingsViewModel.cs

@ -26,13 +26,12 @@ public abstract partial class TerminalViewModel : ObservableObject, ITerminalVie
// register event handler for connection request from viewmodel
if(value != null)
{
protocolSettings!.ConnectRequested += OnViewModelRequestedConnect; ;
protocolSettings!.ConnectRequested += OnViewModelRequestedConnect; ; ;
protocolSettings!.DisconnectRequested += OnViewModelRequestedDisconnect;
}
}
}
/// <summary>
/// Method to override if any closing actions are required.
/// Closing can be cancelled using the return value.
@ -49,13 +48,19 @@ public abstract partial class TerminalViewModel : ObservableObject, ITerminalVie
}
}
private void OnViewModelRequestedConnect(object? sender, IProtocolSettings e)
private void OnViewModelRequestedConnect(object? sender, ConnectionRequestEventArgs e)
{
this.CommunicationProtocol?.Connect(e);
// guard uninitialized CommunicationProtocol
if (CommunicationProtocol == null) { throw new Exception($"To call '{nameof(OnViewModelRequestedConnect)}()', CommunicationProtocol must not be null!"); }
e.Success = this.CommunicationProtocol.Connect(e.Settings);
}
private void OnViewModelRequestedDisconnect(object? sender, EventArgs e)
{
this.CommunicationProtocol?.Disconnect();
// guard uninitialized CommunicationProtocol
if (CommunicationProtocol == null) { throw new Exception($"To call '{nameof(OnViewModelRequestedConnect)}()', CommunicationProtocol must not be null!"); }
this.CommunicationProtocol.Disconnect();
}
}

@ -59,13 +59,13 @@ public abstract class CommunicationProtocol : ICommunicationProtocol
/// <param name="ct">cancellation token</param>
protected abstract void InternalRead(CancellationToken ct);
public void Connect(IProtocolSettings settings)
public bool Connect(IProtocolSettings settings)
{
// check if settings are valid, cancel if not
if (settings.AreValid() == false)
{
this.logger.LogError($"'{nameof(Connect)}()' failed since the provided protocol settings are invalid", nameof(CommunicationProtocol));
return;
return false;
}
// try connecting if serttings are valid
@ -76,10 +76,12 @@ public abstract class CommunicationProtocol : ICommunicationProtocol
// start internal reading thread
this.readingThread = new Thread(() => this.InternalRead(this.cancellationTokenSource.Token));
this.readingThread.Start();
return true;
}
else
{
this.logger.LogWarn($"'{nameof(Connect)}()' failed to connect to protocol, did not start reading thread.", nameof(CommunicationProtocol));
return false;
}
}

@ -0,0 +1,25 @@
namespace MultiTerm.Protocols;
public class ConnectionRequestEventArgs : EventArgs
{
/// <summary>
/// Settings object, contains all settings that are required to connect.
/// </summary>
public IProtocolSettings Settings { get; private set; }
/// <summary>
/// Indicates wether the connection could be successfully made.
/// </summary>
public bool Success { get; set; }
/// <summary>
/// Creates instance of event args with given <paramref name="settings"/>.
/// Defaults <see cref="Success"/> to <see cref="true"/>.
/// </summary>
/// <param name="settings">settings object</param>
public ConnectionRequestEventArgs(IProtocolSettings settings)
{
this.Settings = settings;
this.Success = true;
}
}

@ -26,7 +26,7 @@ public interface ICommunicationProtocol
/// Connect to the device.
/// </summary>
/// <param name="settings">settings required to connect and use the protocol</param>
void Connect(IProtocolSettings settings);
bool Connect(IProtocolSettings settings);
/// <summary>
/// Disconnect from the device. Ends all internal activities.

@ -13,7 +13,7 @@ public interface IProtocolSettingsViewModel
/// Event that is thrown when the user requested to connect to the device with the entered settings.
/// Provides protocol settings to use for connection.
/// </summary>
event EventHandler<IProtocolSettings> ConnectRequested;
event EventHandler<ConnectionRequestEventArgs> ConnectRequested;
/// <summary>
/// Event that is thrown when the user requested to disconnect from the device.

@ -16,7 +16,7 @@ public abstract partial class ProtocolSettingsViewModel : ObservableObject, IPro
private const string disconnectedStateButtonText = "Connect";
protected readonly IMessenger messenger;
public event EventHandler<IProtocolSettings>? ConnectRequested;
public event EventHandler<ConnectionRequestEventArgs>? ConnectRequested;
public event EventHandler? DisconnectRequested;
public abstract ProtocolType ProtocolType { get; }
@ -50,9 +50,24 @@ public abstract partial class ProtocolSettingsViewModel : ObservableObject, IPro
{
// CONNECT
this.AreEditable = false;
this.ConnectRequested?.Invoke(this, (IProtocolSettings)this); // implementing class must also implement IProtocolSettings!
// create event args. Important: derivative classes must also implement IProtocolSettings!
var eventArgs = new ConnectionRequestEventArgs((IProtocolSettings)this);
// fire event
this.ConnectRequested?.Invoke(this, eventArgs);
// after invoke: connection was made successfully?
if (eventArgs.Success)
{
// set button to connected state
this.ConnectDisconnectButtonText = connectedStateButtonText;
}
else
{
// rollback
this.AreEditable = true;
}
}
// if currently connected
else if (this.ConnectDisconnectButtonText == connectedStateButtonText)
{

Loading…
Cancel
Save