Beispiel #1
0
        static void Main(string[] args)
        {
            Console.WriteLine("--- Plugin test console ---");
            Console.WriteLine();

            int           pluginCount      = 0;
            IPluginReader reader           = new PluginReader();
            var           availablePlugins = new Dictionary <int, IPlugin>();
            var           pluginOptions    = new List <string>();
            var           plugins          = reader.GetAvailablePlugins <IInputDevicePlugin>();

            foreach (var plugin in plugins)
            {
                availablePlugins.Add(pluginCount++, plugin);
                pluginOptions.Add(string.Format("[{0}] {1}", pluginCount, plugin.Configure().PluginDisplayName));
            }

            var pluginSelection = RequestForValidSelection <int>("Select from the following available plugins:", pluginOptions, availablePlugins.Keys.Select(k => (k + 1).ToString()));

            Console.Clear();

            IPluginRunner runner  = new PluginRunner();
            var           context = PluginContextFactory.GenerateInitialInputDevicePluginContext((IInputDevicePlugin)availablePlugins[pluginSelection - 1]);

            foreach (var input in context.GetPluginInputs())
            {
                var param = RequestForValidInput(string.Format("Enter value for parameter '{0}':", input.Key), input.Value);
                context.SetInput(input.Key, param);
            }

            Console.Clear();
            Console.WriteLine("Running plugin...");
            Console.WriteLine("Press ESC to stop");
            Console.WriteLine();

            var cts = new CancellationTokenSource();

            runner.RunInputDevicePlugin((IInputDevicePlugin)availablePlugins[pluginSelection - 1], context,
                                        (c) =>
            {
                Console.WriteLine();
                Console.WriteLine("Output changed: ");
                var outputValues = new List <object>();
                foreach (var output in context.GetPluginOutputs())
                {
                    outputValues.Add(context.GetOutput(output.Key));
                }

                Console.WriteLine(JsonConvert.SerializeObject(outputValues));
            },
                                        (ex) =>
            {
                Console.WriteLine("An error occurred in the plugin: " + ex.Message);
            },
                                        cts.Token);

            while (Console.ReadKey().Key != ConsoleKey.Escape)
            {
                continue;
            }

            cts.Cancel();

            availablePlugins[pluginSelection - 1].Dispose();
        }