public static void Execute(this IDbCommandExecutor executor, CreateCommandRequest request, Action <IDbCommand> execute)
    {
        Assert.IsNotNull(executor);
        var requests = new ExecuteCommandRequest(request, execute).ItemToArray();

        executor.Execute(requests);
    }
        public string Post([FromBody] ExecuteCommandRequest commandRequest)
        {
            var command = commandRequest.Command.ToLower();

            try
            {
                ICommandValidator commandValidator = new CommandValidator();
                commandValidator.ValidateCommand(command);
            }
            catch (InvalidCommandException e)
            {
                return($"{e.GetType()} occured. Message: {e.Message}");
            }

            try
            {
                ICommandParser commandParser = new CommandParser();
                string         result        = string.Empty;
                if (commandParser.TryParse(command, out var parsedCommand))
                {
                    return(parsedCommand.Execute());
                }
                else
                {
                    return($"Could not parse command: {command}");
                }
            }
            catch (Exception exception)
            {
                return($"Failure with exception {exception}");
            }
        }
Exemple #3
0
        public ActionResult <string> Post(ExecuteCommandRequest commandRequest)
        {
            var command = commandRequest.Command.ToLower();

            try
            {
                ICommandValidator commandValidator = new CommandValidator();
                commandValidator.ValidateCommand(command);
            }
            catch (InvalidCommandException e)
            {
                return($"{e.GetType()} occured. Message: {e.Message}");
            }

            try
            {
                ICommandParser commandParser = new CommandParser();
                ICommand       parsedCommand;
                if (commandParser.TryParse(command, out parsedCommand))
                {
                    parsedCommand.Execute();
                }
                else
                {
                    return($"Could not parse command: {command}");
                }
                return("SUCCESS");
            }
            catch (Exception exception)
            {
                return($"Failure with exception {exception}");
            }
        }
    public static void Execute(this IDbCommandExecutor executor, CreateCommandRequest request, Action <IDbCommand> execute)
    {
        ArgumentNullException.ThrowIfNull(executor);
        var requests = new ExecuteCommandRequest(request, execute).ItemToArray();

        executor.Execute(requests);
    }
        private ExecuteCommandResponse ExecuteCommandCore(ExecuteCommandRequest request)
        {
            Trace.WriteLine(string.Format("Executing command {0} on {1}", request.CommandText, request.ClientName));

            IClientCallback callback = _registry.Get(request.ClientName);
            var             response = callback.Execute(request);

            Trace.WriteLine(string.Format("Command completed. Output: {0}\r\n", response.CommandOutput));
            return(response);
        }
Exemple #6
0
        public async Task DeviceCommand(string deviceName, ExecuteCommandRequest commandRequest)
        {
            var device  = deviceRepo.GetByName(deviceName);
            var manager = _dcf.GetDeviceManager(device);

            var command = new ha.sdk.Command {
                Device      = device,
                CommandName = commandRequest.Command,
                Parameters  = commandRequest.Parameters
            };

            await manager.ExecuteCommand(command);
        }
Exemple #7
0
        public async Task <string> Command(ExecuteCommandRequest request)
        {
            var chandnel = await _channelService.Get(request.Id);

            if (chandnel == null)
            {
                return("命令发送失败");
            }
            List <CommandDto> data = new List <CommandDto>();

            if (request.CommandType == CommandTypeEnum.Stop)
            {
                data.Add(new CommandDto {
                    ChanneId = request.Id, CommandType = request.CommandType, FactoryCode = chandnel.FactoryCode
                });
            }
            else
            {
                data = _pullInfoService.GetPullInfoPageList(new PullInfoSearchDto {
                    ChannelId = request.Id, ResultCount = 999
                })
                       .Items.Select(s => new CommandDto
                {
                    ChanneId    = request.Id,
                    CommandType = request.CommandType,
                    FactoryCode = chandnel.FactoryCode,
                    PullAddress = s.Url,
                    PushAddress = chandnel.PushUrl
                }).ToList();
            }
            if (data.Count > 0)
            {
                var model = LiveChannelManager.Data.FirstOrDefault(m => m.Id == request.Id);
                if (model != null)
                {
                    _commandService.ExecuteCommand(data[model.PullIndex]);
                }
                else
                {
                    _commandService.ExecuteCommand(data.FirstOrDefault());
                }
                return("命令已发送");
            }
            else
            {
                return("命令发送失败,为查询到对应的数据");
            }
        }
        public override async Task RequestCommand(Unit request, IServerStreamWriter <ExecuteCommandRequest> responseStream, ServerCallContext context)
        {
            RequestHostCommandImpl = async cmd =>
            {
                var req = new ExecuteCommandRequest {
                    Command = cmd.Command
                };

                // woof
                req.Args.AddRange(cmd.Args);

                await responseStream.WriteAsync(req);
            };

            await new TaskCompletionSource <bool>(context.CancellationToken).Task;
        }
        public IActionResult ExecuteCommand(ExecuteCommandRequest request)
        {
            if (string.IsNullOrEmpty(request?.Input))
            {
                return(BadRequest());
            }

            try
            {
                var text = _commandInvoker.Execute(request.Input);

                return(new JsonResult(new { text = text }));
            }
            catch
            {
                return(StatusCode((int)HttpStatusCode.InternalServerError));
            }
        }
Exemple #10
0
        public override async Task <Outcome> Exec(ExecuteCommandRequest request, ServerCallContext context)
        {
            try
            {
                return(await _reloadManager.ExecuteCommand(new CommandRequest(request.Command, request.Args.ToList())));
            }
            catch (Exception ex)
            {
                _log($"An error occurred when attempting to exec command: {request.Command} with args {request.Args}");

                return(new Outcome
                {
                    Success = false,
                    Messages = { new Message {
                                     Message_ = ex.ToString()
                                 } }
                });
            }
        }
        public ExecuteCommandResponse Execute(ExecuteCommandRequest request)
        {
            var    sb        = new StringBuilder();
            string command   = string.Format("/C {0} {1}", request.CommandText, request.CommandParameters);
            var    startInfo = new ProcessStartInfo
            {
                FileName               = "cmd.exe",
                Arguments              = command,
                UseShellExecute        = false,
                RedirectStandardOutput = true,
                RedirectStandardError  = true
            };

            using (var process = new Process {
                StartInfo = startInfo, EnableRaisingEvents = true
            })
            {
                Console.WriteLine();
                Console.WriteLine(new string('-', 20));
                Console.WriteLine("Start command: {0} {1}", request.CommandText, request.CommandParameters);

                var thread = new Thread(Read);
                process.Start();
                thread.Start(new { sb, process });

                const int timeout = 30000;
                process.WaitForExit(timeout);
                thread.Join(timeout);

                Console.WriteLine(sb.ToString());
                Console.WriteLine(" Completed.");
            }

            return(new ExecuteCommandResponse {
                CommandOutput = sb.ToString()
            });
        }
 public ExecuteCommandResponse ExecuteCommand(ExecuteCommandRequest request)
 {
     return(this.HandleAndReturn(request, ExecuteCommandCore));
 }
 public Task <ExecuteCommandResponse> ExecuteCommandAsync(ExecuteCommandRequest request)
 {
     return(GetClient().ExecuteCommandAsync(request));
 }
 public ExecuteCommandResponse ExecuteCommand(ExecuteCommandRequest request)
 {
     return(GetClient().ExecuteCommand(request));
 }