Beispiel #1
0
        private void ExecuteRemoteCommand(IRemoteCommand command)
        {
            Analyzer.Serial.SendPacket(command.GetBytes());

            timer.Restart();

            bool commandExecuted = false;

            while (!commandExecuted)
            {
                if (timer.ElapsedMilliseconds >= timeToWaitResponse)
                {
                    //Logger.Info("[Command executor] - Слишком долгое ожидание ответа от устройства.");

                    Analyzer.Serial.SendPacket(command.GetBytes());

                    timer.Restart();
                }

                if (Protocol.CommandTypes.SIMPLE_COMMAND == command.GetType())
                {
                    commandExecuted = CheckCommandStatus(command,
                                                         CommandStateResponse.CommandStates.COMMAND_EXECUTE_STARTED);
                }
                else if (Protocol.CommandTypes.WAITING_COMMAND == command.GetType())
                {
                    commandExecuted = CheckCommandStatus(command,
                                                         CommandStateResponse.CommandStates.COMMAND_EXECUTE_FINISHED);
                }
            }

            timer.Stop();
        }
        private ContextMenuStrip GetContextMenuStrip()
        {
            var            notifyContextMenu     = new ContextMenuStrip();
            bool           hasConfiguredCommands = false;
            IRemoteCommand previous = null;

            foreach (IRemoteCommand command in _commandsManager.Cast <IRemoteCommand>().OrderBy(c => c.GetType().ToString()))
            {
                if (command.Configured)
                {
                    if (previous != null && !string.Equals(previous.GetType().ToString(), command.GetType().ToString()))
                    {
                        notifyContextMenu.Items.Add("-");
                    }
                    var commandButton = new ToolStripMenuItem(command.CommandTitle)
                    {
                        Image = command.TrayIcon
                    };
                    commandButton.Click += (s, e) => command.RunCommand();
                    notifyContextMenu.Items.Add(commandButton);
                    hasConfiguredCommands = true;
                    previous = command;
                }
            }
            if (hasConfiguredCommands)
            {
                notifyContextMenu.Items.Add("-");
            }

            var settings = new ToolStripMenuItem("Settings")
            {
                Image = Resources.Settings.ToBitmap()
            };

            settings.Click += SettingsClick;
            notifyContextMenu.Items.Add(settings);

            notifyContextMenu.Items.Add("-");

            var exit = new ToolStripMenuItem("Exit")
            {
                Image = Resources.Exit.ToBitmap()
            };

            exit.Click += Exit;
            notifyContextMenu.Items.Add(exit);

            return(notifyContextMenu);
        }
        public void Execute(AutomationProvider provider, IEnumerable <RemoteCommandDetails> commands)
        {
            CommandManager manager = new CommandManager(provider);
            Assembly       asm     = typeof(RemoteCommandManager).Assembly;

            try
            {
                // force remote execution to false, don't want loops of RemoteCommands!
                manager.EnableRemoteExecution = false;
                manager.Record();

                var browserList = new List <BrowserType>();

                foreach (var command in commands)
                {
                    // attempt to locate mapper
                    // TODO: Get rid of the 'magic string' Commands part, make this work with loaded assemblies
                    var type = asm.GetType(string.Format("{0}.{1}.{2}", typeof(RemoteCommandManager).Namespace, "Commands", command.Name));
                    if (type == null)
                    {
                        throw new ArgumentException(string.Format("Unable to locate available command: {0}", command.Name));
                    }

                    CommandArgumentsTypeAttribute commandArgs = (CommandArgumentsTypeAttribute)type.GetCustomAttributes(typeof(CommandArgumentsTypeAttribute), false).FirstOrDefault();
                    if (commandArgs == null)
                    {
                        provider.Cleanup();
                        throw new ArgumentException(string.Format("Unable to locate command arguments handler for command: {0}", command.Name));
                    }

                    IRemoteCommand cmd = (IRemoteCommand)Activator.CreateInstance(type);

                    IRemoteCommandArguments args = null;
                    try
                    {
                        args = DeserializeArguments(commandArgs.ArgsType, command.Arguments);
                    }
                    catch (Exception ex)
                    {
                        throw new ArgumentException(string.Format("An error occurred while processing the arguments provided for command: {0}", command.Name), ex);
                    }

                    if (cmd.GetType() == typeof(Commands.Use))
                    {
                        var useArgs = (Commands.UseArguments)args;
                        Guard.ArgumentExpressionTrueForCommand <Commands.Use>(() => useArgs.BrowserType.Count > 0);

                        browserList.AddRange(useArgs.BrowserType);
                    }
                    else
                    {
                        cmd.Execute(manager, args);
                    }
                }

                if (browserList.Count == 0)
                {
                    browserList.Add(BrowserType.Chrome);
                }

                manager.Execute(browserList.ToArray());
            }
            catch (FluentAutomation.API.Exceptions.AssertException)
            {
                throw;
            }
            catch (ArgumentException)
            {
                throw;
            }
            catch (Exception ex)
            {
                throw new Exception("An error occurred while executing the specified commands.", ex);
            }
            finally
            {
                provider.Cleanup();
            }
        }
        public static TestDetails GetRemoteCommands(IEnumerable <RemoteCommandDetails> commands)
        {
            TestDetails testDetails = new TestDetails();
            Assembly    asm         = typeof(IRemoteCommand).Assembly;

            try
            {
                foreach (var command in commands)
                {
                    // attempt to locate mapper
                    // TODO: Get rid of the 'magic string' Commands part, make this work with loaded assemblies
                    var type = asm.GetType(string.Format("{0}.{1}.{2}", typeof(RemoteCommandManager).Namespace, "Commands", command.Name));
                    if (type == null)
                    {
                        throw new ArgumentException(string.Format("Unable to locate available command: {0}", command.Name));
                    }

                    CommandArgumentsTypeAttribute commandArgs = (CommandArgumentsTypeAttribute)type.GetCustomAttributes(typeof(CommandArgumentsTypeAttribute), false).FirstOrDefault();
                    if (commandArgs == null)
                    {
                        throw new ArgumentException(string.Format("Unable to locate command arguments handler for command: {0}", command.Name));
                    }

                    IRemoteCommand          cmd  = (IRemoteCommand)Activator.CreateInstance(type);
                    IRemoteCommandArguments args = null;
                    try
                    {
                        args = DeserializeArguments(commandArgs.ArgsType, command.Arguments);
                    }
                    catch (Exception ex)
                    {
                        throw new ArgumentException(string.Format("An error occurred while processing the arguments provided for command: {0}", command.Name), ex);
                    }

                    if (cmd.GetType() == typeof(Commands.Use))
                    {
                        var useArgs = (Commands.UseArguments)args;
                        Guard.ArgumentExpressionTrueForCommand <Commands.Use>(() => useArgs.BrowserType.Count > 0);

                        testDetails.Browsers.AddRange(useArgs.BrowserType);
                    }
                    else
                    {
                        testDetails.RemoteCommands.Add(cmd, args);
                    }
                }

                if (testDetails.Browsers.Count == 0)
                {
                    testDetails.Browsers.Add(BrowserType.Chrome);
                }
            }
            catch (FluentAutomation.API.Exceptions.AssertException)
            {
                throw;
            }
            catch (ArgumentException)
            {
                throw;
            }
            catch (Exception ex)
            {
                throw new Exception("An error occurred while executing the specified commands.", ex);
            }

            return(testDetails);
        }