120 lines
4.1 KiB
C#
120 lines
4.1 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Reflection;
|
|
using Tecan.At.Dragonfly.AutomationInterface;
|
|
|
|
namespace Tecan.At.AutomationInterface.SampleApp
|
|
{
|
|
class Program
|
|
{
|
|
static void Main(string[] args)
|
|
{
|
|
var commands = BuildCommands();
|
|
|
|
try
|
|
{
|
|
// Needed in your code
|
|
AutomationInterfaceFactory.Start();
|
|
|
|
SessionData sessionData = null;
|
|
var automationInterfaceAccess = new SampleApp();
|
|
|
|
var quitCommands = new[] { "q", "Q" };
|
|
|
|
while (true)
|
|
{
|
|
RenderMenu(commands);
|
|
|
|
var consoleInput = Console.ReadKey();
|
|
Console.WriteLine();
|
|
if (quitCommands.Any(x => x == consoleInput.KeyChar.ToString()))
|
|
{
|
|
break;
|
|
}
|
|
|
|
var command = '0';
|
|
if (!char.TryParse(consoleInput.KeyChar.ToString(), out command))
|
|
{
|
|
Console.WriteLine($"'{consoleInput}' isn't a valid command key.");
|
|
continue;
|
|
}
|
|
else if (commands.ContainsKey(command))
|
|
{
|
|
if (sessionData == null)
|
|
{
|
|
sessionData = new SessionData();
|
|
}
|
|
|
|
try
|
|
{
|
|
var theCommand = commands[command];
|
|
|
|
Console.WriteLine($"--- Execute command {theCommand.Item1}...");
|
|
|
|
theCommand.Item2.Invoke(automationInterfaceAccess, sessionData);
|
|
|
|
Console.WriteLine("--- done");
|
|
}
|
|
catch (TargetInvocationException e)
|
|
{
|
|
Console.WriteLine($"### Error: {e.InnerException.Message} ###");
|
|
Console.WriteLine();
|
|
Console.WriteLine(e.InnerException.ToString());
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
Console.WriteLine(e.ToString());
|
|
}
|
|
}
|
|
else
|
|
{
|
|
Console.WriteLine($"Unknown command '{command}'.");
|
|
}
|
|
}
|
|
}
|
|
finally
|
|
{
|
|
// Needed in your code
|
|
AutomationInterfaceFactory.Stop();
|
|
}
|
|
}
|
|
|
|
private static void RenderMenu(Dictionary<char, Tuple<string, Action<SampleApp, SessionData>>> commands)
|
|
{
|
|
Console.WriteLine();
|
|
commands.Keys.OrderBy(x => x).ToList().ForEach(c =>
|
|
{
|
|
var actCmd = commands[c];
|
|
Console.WriteLine($"{c}. {actCmd.Item1}");
|
|
});
|
|
Console.WriteLine();
|
|
Console.WriteLine($"Press q to exit.");
|
|
Console.WriteLine();
|
|
Console.Write("> ");
|
|
}
|
|
|
|
private static Dictionary<char, Tuple<string, Action<SampleApp, SessionData>>> BuildCommands()
|
|
{
|
|
var methods = typeof(Program).Assembly.GetTypes()
|
|
.SelectMany(t => t.GetMethods())
|
|
.Where(m => m.GetCustomAttributes(typeof(DemoAttribute), false).Length > 0)
|
|
.Select(x => new { Attribute = (DemoAttribute)x.GetCustomAttributes(typeof(DemoAttribute), false).Single(), Delegate = x })
|
|
.ToArray();
|
|
|
|
var commands = new Dictionary<char, Tuple<string, Action<SampleApp, SessionData>>>(methods.Length);
|
|
|
|
foreach (var actDemo in methods)
|
|
{
|
|
commands.Add(actDemo.Attribute.Key, Tuple.Create<string, Action<SampleApp, SessionData>>(actDemo.Attribute.Name, (aia, sd) => { actDemo.Delegate.Invoke(null, new object[] { aia, sd }); }));
|
|
}
|
|
|
|
Console.WriteLine($"Count: " + methods.Length);
|
|
|
|
return commands;
|
|
}
|
|
|
|
|
|
}
|
|
}
|