private static async Task <MethodResponse> TakePhotoAsync(MethodRequest methodRequest, ModuleClient moduleClient) { try { var req = TakePhotoRequest.FromJson(methodRequest.DataAsJson); var response = await camera.TakePhotoAsync(req); var responseAsBytes = GetJsonBytes(response); if (configuration.OutputEvents) { await SendEventAsync(moduleClient, responseAsBytes); } return(new MethodResponse( responseAsBytes, response.Succeded ? (int)HttpStatusCode.OK : (int)HttpStatusCode.Conflict )); } catch (Exception ex) { Logger.LogError(ex, "Failed to take new photo"); return(new MethodResponse( Encoding.UTF8.GetBytes($"Error taking new photo: {ex.Message}"), (int)HttpStatusCode.InternalServerError)); } }
public async Task <TakePhotoResponse> TakePhotoAsync(TakePhotoRequest takePhotoRequest) { var cameraWasUsed = false; if (IsTakingTimelapse()) { return(new TakePhotoResponse() { ErrorMessage = $"Timelapse {currentTimelapseId?.ToString()} is being taken", }); } try { var path = EnsureLocalDirectoryExists(); await cameraInUse.WaitAsync(); cameraWasUsed = true; using (var imgCaptureHandler = new ImageStreamCaptureHandler(path, takePhotoRequest.ImageType)) { var stopwatch = Stopwatch.StartNew(); if (takePhotoRequest.QuickMode) { await camera.TakeRawPicture(imgCaptureHandler); } else { await camera.TakePicture(imgCaptureHandler, takePhotoRequest.GetImageEncoding(), takePhotoRequest.GetPixelFormatEncoding()); } stopwatch.Stop(); var localFilePath = imgCaptureHandler.GetFilepath(); var blobName = await UploadFileAsync("photos", localFilePath); var fi = new FileInfo(localFilePath); Logger.Log($"New photo: {fi.Length} bytes, in {stopwatch.ElapsedMilliseconds}ms"); if (takePhotoRequest.DeleteLocalFile) { File.Delete(localFilePath); } return(new TakePhotoResponse() { BlobName = blobName, LocalFilePath = localFilePath, DeleteLocalFile = takePhotoRequest.DeleteLocalFile, PixelFormat = takePhotoRequest.PixelFormat, ImageType = takePhotoRequest.ImageType, QuickMode = takePhotoRequest.QuickMode, }); } } finally { if (cameraWasUsed) { cameraInUse.Release(); } } }