-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
a little bit of refactoring in the CLI project
- Loading branch information
1 parent
420df70
commit 7fc4265
Showing
23 changed files
with
32,419 additions
and
32,278 deletions.
There are no files selected for viewing
63 changes: 63 additions & 0 deletions
63
LtAmpDotNet/LtAmpDotNet.Cli/Commands/BaseCommandDefinition.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
using LtAmpDotNet.Lib; | ||
using LtAmpDotNet.Tests.Mock; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.CommandLine; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
|
||
namespace LtAmpDotNet.Cli.Commands | ||
{ | ||
internal abstract class BaseCommandDefinition | ||
{ | ||
internal virtual Command CommandDefinition { get => throw new NotImplementedException(); set => throw new NotImplementedException(); } | ||
|
||
internal LtAmplifier? Amp; | ||
|
||
internal virtual void Open() | ||
{ | ||
if (!Console.IsOutputRedirected) | ||
{ | ||
Console.Write($"Connecting to device {0}..."); | ||
} | ||
Amp = new LtAmplifier(new MockHidDevice(), false); | ||
WaitForEvent(() => { Amp.Open(); }, handler => Amp.AmplifierConnected += handler, 5); | ||
} | ||
|
||
/// <summary>Executes the command, and waits for the event to respond before continuing.</summary> | ||
/// <param name="action">the action to run</param> | ||
/// <param name="eventHandler">the event to wait for</param> | ||
/// <param name="waitTime">timeout (in seconds)</param> | ||
internal static EventArgs? WaitForEvent(Action action, Action<EventHandler> eventHandler, int waitTime = 5) | ||
{ | ||
EventArgs? returnVal = null; | ||
var wait = new AutoResetEvent(false); | ||
eventHandler((sender, eventArgs) => { | ||
returnVal = eventArgs; | ||
wait.Set(); | ||
}); | ||
action.Invoke(); | ||
wait.WaitOne(TimeSpan.FromSeconds(waitTime)); | ||
return returnVal; | ||
} | ||
|
||
/// <summary>Executes the command, and waits for the event to respond before continuing.</summary> | ||
/// <param name="action">the action to run</param> | ||
/// <param name="eventHandler">the event to wait for</param> | ||
/// <param name="waitTime">timeout (in seconds)</param> | ||
internal static T? WaitForEvent<T>(Action action, Action<EventHandler<T>> eventHandler, int waitTime = 5) | ||
{ | ||
T? returnVal = default; | ||
var wait = new AutoResetEvent(false); | ||
eventHandler((sender, eventArgs) => { | ||
returnVal = eventArgs; | ||
wait.Set(); | ||
}); | ||
action.Invoke(); | ||
wait.WaitOne(TimeSpan.FromSeconds(waitTime)); | ||
return returnVal; | ||
} | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
14 changes: 14 additions & 0 deletions
14
LtAmpDotNet/LtAmpDotNet.Cli/Commands/ICommandDefinition.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.CommandLine; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
|
||
namespace LtAmpDotNet.Cli.Commands | ||
{ | ||
internal interface ICommandDefinition | ||
{ | ||
Command CommandDefinition { get; set; } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 44 additions & 0 deletions
44
LtAmpDotNet/LtAmpDotNet.Cli/Commands/OtherCommandDefinition.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
using Google.Protobuf; | ||
using LtAmpDotNet.Lib.Models.Protobuf; | ||
using Newtonsoft.Json; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.CommandLine; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
|
||
namespace LtAmpDotNet.Cli.Commands | ||
{ | ||
internal class OtherCommandDefinition : BaseCommandDefinition | ||
{ | ||
internal override Command CommandDefinition { get; set; } | ||
internal OtherCommandDefinition() | ||
{ | ||
var terminalCommand = new Command("term", "Terminal"); | ||
terminalCommand.SetHandler(Terminal); | ||
CommandDefinition = terminalCommand; | ||
} | ||
|
||
internal void Terminal() | ||
{ | ||
Open(); | ||
if(Amp != null) | ||
{ | ||
IMessage definition = (IMessage)Activator.CreateInstance(typeof(FenderMessageLT))!; | ||
string? input; | ||
Amp.MessageReceived += Amp_MessageReceived; | ||
while ((input = Console.ReadLine()) != null) | ||
{ | ||
var message = (FenderMessageLT)JsonParser.Default.Parse(input, definition?.Descriptor); | ||
Amp.SendMessage(message); | ||
} | ||
} | ||
} | ||
|
||
internal void Amp_MessageReceived(object? sender, Lib.Events.FenderMessageEventArgs e) | ||
{ | ||
Console.WriteLine(e.Message); | ||
} | ||
} | ||
} |
Oops, something went wrong.