Пример #1
0
 public void Initialize()
 {
     if (!_repository.Get().Any())
     {
         _repository.Add(new Command {
             Description = "window-select notepad"
         });
         _repository.Add(new Command {
             Description = "mouse-move 150;150"
         });
         _repository.Add(new Command {
             Description = "mouse-click Single"
         });
         _repository.Add(new Command {
             Description = "keyboard-sendkeys Hello World!!!"
         });
     }
 }
        private async void PlayCommand_Execute(object parameter)
        {
            var cmd = new RobotCommand(SelectedRobotIndex, SelectedCommand, Double.Parse(Payload));
            await RobotCommandExecuter.ExecuteAsync(cmd);

            //add to in memory repo
            CommandRepository.Add(cmd);

            //update UI
            ExecutedCommandText.Add(GetCommandText(cmd));
        }
Пример #3
0
        protected void given_command(string verb, string noun, params Action <MemoryCommandDescriptor>[] configurators)
        {
            var command = new MemoryCommandDescriptor {
                Noun = noun, Verb = verb
            };

            foreach (var c in configurators)
            {
                c(command);
            }
            Repository.Add(command);
        }
Пример #4
0
 private void addNewButton_Click(object sender, RoutedEventArgs e)
 {
     if (!string.IsNullOrEmpty(newActionTxt.Text))
     {
         repository.Add(new Command()
         {
             Description = newActionTxt.Text
         });
     }
     commands = repository.Get();
     CommandsGrid.ItemsSource = commands;
 }
Пример #5
0
        /// <summary>
        /// Здесь мы реализуем паттерн Команда
        /// </summary>
        static void Main()
        {
            var CommandRepositorys = new CommandRepository();

            CommandRepositorys.Add(new CommandClass1(new Class1()
            {
                Value = 1
            }));

            CommandRepositorys.ExecuteCommand(0);
            CommandRepositorys.ExecuteCommand(0);

            CommandRepositorys.Undo();
            CommandRepositorys.Undo();
            CommandRepositorys.Undo();
            CommandRepositorys.Undo();

            CommandRepositorys.ExecuteCommand(0);
            CommandRepositorys.ExecuteCommand(0);
            CommandRepositorys.ExecuteCommand(0);
            CommandRepositorys.Undo();
            CommandRepositorys.ExecuteCommand(0);
            CommandRepositorys.ExecuteCommand(0);
        }
Пример #6
0
        public async Task Command(object command)
        {
            var telemetryProps = new Dictionary <string, string> {
                { "connection", this.Context.ConnectionId },
                { "user", this.Context.User.Identity.Name }
            };

            // In this method only one message for the user can be running at the time. Not very efficient nor
            // reliable way of doing this, but with the low volumes of this app this should be more than fine.
            if (this.userMutex.Contains(this.Context.ConnectionId))
            {
                this.logger.LogError($"Simultaneous commands for user {this.Context.ConnectionId}");
                tc.TrackEvent("parallelcommand", telemetryProps);
                await Clients.Client(Context.ConnectionId).InvokeAsync("error", "Palvelimella käsitellään vielä edellistä pyyntöäsi, uutta pyyntöä ei voida vastaanottaa");

                return;
            }

            this.userMutex.Add(this.Context.ConnectionId);

            try
            {
                // Can't use built in model mapping as it is too limited for receiving various
                // kinds of commands at the same endpoint.
                // We know that the incoming model is always JSON object:
                var jsonObject = (JObject)command;

                // All commands have these base properties, this is
                // the way to get exact type for next conversion.
                var jsCommand = jsonObject.ToObject <GenericJSCommand>();

                this.logger.LogInformation($"{jsCommand.Id}:{jsCommand.Name} Received");

                telemetryProps.Add("type", jsCommand.Name);
                tc.TrackEvent("command", telemetryProps);

                Type targetCommandType = this.commands.CreateCommandTypeFromCommandName(jsCommand.Name);

                if (targetCommandType == null)
                {
                    this.logger.LogError($"{jsCommand.Id}:{jsCommand.Name} Could not find command type");
                    tc.TrackEvent("commandtypenotfound", telemetryProps);

                    // Acknowledge, as the message was received but could not be handled properly - this is a
                    // configuration error that won't be fixed by retrying --> ack the message
                    await Clients.Client(Context.ConnectionId).InvokeAsync("ack", jsCommand.Id);

                    await Clients.Client(Context.ConnectionId).InvokeAsync("error", $"Toimintoa ei löytynyt, toimintoa {jsCommand.Name} ei voitu suorittaa.");

                    return;
                }

                var baseCommand = (ICommand)jsonObject.ToObject(targetCommandType);

                // Copy message properties to command context
                baseCommand.Context.User            = Context.User.Identity.Name;
                baseCommand.Context["ConnectionId"] = Context.ConnectionId;
                baseCommand.Context["Timestamp"]    = jsCommand.Timestamp;
                baseCommand.Context["MessageType"]  = targetCommandType.FullName;

                if (await commandRepository.Contains(baseCommand))
                {
                    // This command was already in the data store, exit
                    this.logger.LogWarning($"{jsCommand.Id}:{jsCommand.Name} Already handled, skipping");
                    await Clients.Client(Context.ConnectionId).InvokeAsync("ack", baseCommand.Id);

                    return;
                }

                // Process the command, fail if no one handled it
                // Reflection based call must know the exact type of the handler, thus RedirectToWhen-logic
                // is in each command handler separately. Not pretty but works.
                // Only one handler should handle a command --> exit loop on first match
                var handled = commandHandlerFactory.All.Any(handler => handler.TryExecute((ICommand)baseCommand));
                if (!handled)
                {
                    var s = $"{jsCommand.Id}:{jsCommand.Name} Handler not found for command type {targetCommandType.FullName}";
                    this.logger.LogError(s);
                    this.tc.TrackEvent("handlernotfound", telemetryProps);
                    throw new InvalidOperationException(s);
                }

                // Save command to repository
                await commandRepository.Add(baseCommand, baseCommand.Context.User);

                // Tell client that this command has been received, no need to resend. This was more useful when all
                // operations were async and went through cloud queues.
                await Clients.Client(Context.ConnectionId).InvokeAsync("ack", baseCommand.Id);
            }
            catch (Exception e)
            {
                this.tc.TrackException(e, telemetryProps);
                this.logger.LogError("Dispatching command failed: " + e.ToString());

                try
                {
                    await Clients.Client(Context.ConnectionId).InvokeAsync("error", "Toiminto kaatui tuntemattomaan virheeseen.");
                }
                catch
                {
                    this.logger.LogError("Sending error to SignalR client failed");
                }
            }
            finally
            {
                this.userMutex.Remove(this.Context.ConnectionId);
            }
        }
Пример #7
0
        public async Task <MethodResultValue <BsVerifyViewModel> > AddBsItem(BsCreateViewModel item)
        {
            var result     = new MethodResultValue <BsVerifyViewModel>();
            var reasonCode = await QueryRepository.GetReasonCodeAsync(item.ReportReasonCode);

            if (reasonCode == null)
            {
                reasonCode = new BsReasonCode
                {
                    ReasonCode     = BsButtonQueryRepository.OtherValue,
                    ReasonCodeGuid = Guid.NewGuid(),
                    ReasonCodeText = BsButtonQueryRepository.OtherValue
                };
                var addResult = await CommandRepository.Add <BsReasonCode>(reasonCode);

                var saveBsReasonCodeResult = await CommandRepository.SaveChangesAsync();

                reasonCode = addResult.ReturnValue;
            }
            var reason = new BsReason
            {
                ReasonCodeId = reasonCode.ReasonCodeId,
                ReasonGuid   = Guid.NewGuid(),
                ReasonText   = item.ReportReason
            };
            var reasonResult = await CommandRepository.Add(reason);

            var reasonSaveResult = await CommandRepository.SaveChangesAsync();

            var socialMediaSource = await QueryRepository.GetSocialMediaSource(item.ReportedFrom);

            if (socialMediaSource == null)
            {
                socialMediaSource = new BsSocialMediaSource()
                {
                    SourceCodeGuid = Guid.NewGuid(),
                    SourceCodeName = BsButtonQueryRepository.OtherValue
                };
                var addSocialResult = await CommandRepository.Add <BsSocialMediaSource>(socialMediaSource);

                var saveSocialRResult = await CommandRepository.SaveChangesAsync();

                socialMediaSource = addSocialResult.ReturnValue;
            }

            var source = new BsSource
            {
                SourceGuid = Guid.NewGuid(), SocialMediaSourceId = socialMediaSource.SourceCodeId, SourceUrl = item.ReportedFromUrl
            };
            var sourceResult = await CommandRepository.Add(source);

            var sourceSaveResult = await CommandRepository.SaveChangesAsync();

            var unconfirmedReport = new BsUnconfirmedReport
            {
                ReportGuid           = Guid.NewGuid(),
                SourceId             = source.SocialMediaSourceId,
                ReasonId             = reason.ReasonCodeId,
                ReportReason         = item.ReportReason,
                ReportedDateTime     = item.ReportedDateTime,
                ReportText           = item.ReportText,
                ReporterUserName     = item.ReporterUserName,
                ReportedNameOfPoster = item.ReportedNameOfPoster
            };
            var addReportResult = await CommandRepository.Add(unconfirmedReport);

            var saveResult = await CommandRepository.SaveChangesAsync();

            var vm = new BsVerifyViewModel {
                ReportReason = addReportResult.ReturnValue.ReportReason
            };

            result.ReturnValue = vm;
            return(result);
        }