コード例 #1
0
        public CommandResult Switch(string lightId)
        {
            SwitchLightCommand command = new SwitchLightCommand();

            command.SetLightId(lightId);

            return(Execute <SwitchLightCommand, CommandResult>(command));
        }
コード例 #2
0
        public async Task <CommandResult> Handle(SwitchLightCommand command)
        {
            CommandResult result = new CommandResult();

            ObjectId lightId = new ObjectId();

            if (!ObjectId.TryParse(command.LightId, out lightId))
            {
                AddNotification(nameof(command.LightId), ENotifications.InvalidFormat);
            }

            if (Valid)
            {
                Light light = _lightRepository.Get(lightId);

                if (light == null && _lightRepository.Valid)
                {
                    AddNotification(nameof(command.LightId), ENotifications.NotFound);
                }

                if (Valid)
                {
                    light = await UpdateState(light);

                    light.Switch();

                    if (await _arduinoCommunicationService.Lights.Switch(new SwitchLightQuery(light.PinPort, light.State)))
                    {
                        _lightRepository.Update(light);

                        if (_lightRepository.Valid)
                        {
                            result = new CommandResult(HttpStatusCode.OK);
                        }
                    }
                }

                else
                {
                    result = new CommandResult(HttpStatusCode.BadRequest, Notifications);
                }
            }

            else
            {
                result = new CommandResult(HttpStatusCode.BadRequest, Notifications);
            }

            return(result);
        }