public CommandResult Switch(string buzzerId) { SwitchBuzzerCommand command = new SwitchBuzzerCommand(); command.SetBuzzerId(buzzerId); return(Execute <SwitchBuzzerCommand, CommandResult>(command)); }
public async Task <CommandResult> Handle(SwitchBuzzerCommand command) { CommandResult result = new CommandResult(); ObjectId buzzerId = new ObjectId(); if (!ObjectId.TryParse(command.BuzzerId, out buzzerId)) { AddNotification(nameof(command.BuzzerId), ENotifications.InvalidFormat); } if (Valid) { Buzzer buzzer = _buzzerRepository.Get(buzzerId); if (buzzer == null && _buzzerRepository.Valid) { AddNotification(nameof(command.BuzzerId), ENotifications.NotFound); } if (Valid) { buzzer = await UpdateState(buzzer); buzzer.Switch(); if (await _arduinoCommunicationService.Buzzers.Switch(new SwitchBuzzerQuery(buzzer.PinPort, !buzzer.State))) { _buzzerRepository.Update(buzzer); if (_arduinoCommunicationService.Buzzers.Valid) { result = new CommandResult(HttpStatusCode.OK); } } } else { result = new CommandResult(HttpStatusCode.BadRequest, Notifications); } } else { result = new CommandResult(HttpStatusCode.BadRequest, Notifications); } return(result); }