Beispiel #1
0
        public async Task <IActionResult> Execute(int id)
        {
            if (id == 0)
            {
                return(BadRequest());
            }

            var procedure = _procedureService.GetAllProcedures().FirstOrDefault(p => p.Id == id);

            if (procedure == null)
            {
                return(NotFound());
            }

            var user = await _userManager.GetUserAsync(User);

            if (procedure.UserId != user.Id)
            {
                return(Forbid());
            }

            try
            {
                await _procedureService.ExecuteAsync(procedure);
            }
            catch (DeviceNotFoundException)
            {
                return(StatusCode(500, "Failed to connect with the device"));
            }
            catch (CloudToDeviceMethodInvocationFailedException)
            {
                return(StatusCode(500, "Connected to the device, but failed to execute the procedure"));
            }
            catch (IotHubCommunicationException)
            {
                return(StatusCode(500, "IoTHub communication error"));
            }

            return(NoContent());
        }