Ejemplo n.º 1
0
        public async Task <TakeTimelapseResponse> StartTimelapseAsync(TakeTimelapseRequest req)
        {
            if (IsTakingTimelapse())
            {
                return(new TakeTimelapseResponse()
                {
                    ErrorMessage = $"Timelapse {currentTimelapseId?.ToString()} is being taken",
                });
            }

            var cameraUsed = false;

            try
            {
                var path = EnsureLocalDirectoryExists();
                currentTimelapseId = Guid.NewGuid().ToString();
                var pathForImages = Path.Combine(path, currentTimelapseId);

                await cameraInUse.WaitAsync();

                cameraUsed = true;

                // This example will take an image every 10 seconds for 4 hours
                var imgCaptureHandler = new ImageStreamCaptureHandler(pathForImages, "jpg");
                timelapseCts = new CancellationTokenSource(TimeSpan.FromSeconds(req.Duration));
                var tl = new Timelapse {
                    Mode = TimelapseMode.Second, CancellationToken = timelapseCts.Token, Value = req.Interval
                };

                Logger.Log($"Starting timelapse {currentTimelapseId}");
                _ = camera.TakePictureTimelapse(imgCaptureHandler, MMALEncoding.JPEG, MMALEncoding.I420, tl)
                    .ContinueWith(async(t) => await PrepareTimelapseVideoAsync(t, imgCaptureHandler, pathForImages));

                return(new TakeTimelapseResponse()
                {
                    Id = currentTimelapseId,
                    Duration = req.Duration,
                    Interval = req.Interval,
                });
            }
            catch
            {
                if (cameraUsed)
                {
                    cameraInUse.Release();
                }

                throw;
            }
        }
Ejemplo n.º 2
0
        private static async Task <MethodResponse> StartTimelapseAsync(MethodRequest methodRequest, ModuleClient moduleClient)
        {
            try
            {
                var req      = TakeTimelapseRequest.FromJson(methodRequest.DataAsJson);
                var response = await camera.StartTimelapseAsync(req);

                var responseAsBytes = GetJsonBytes(response);
                return(new MethodResponse(
                           responseAsBytes,
                           response.Succeded ? (int)HttpStatusCode.OK : (int)HttpStatusCode.Conflict
                           ));
            }
            catch (Exception ex)
            {
                Logger.LogError(ex, "Failed to start new timelapse");

                return(new MethodResponse(
                           Encoding.UTF8.GetBytes($"Error starting timelapse: {ex.Message}"),
                           (int)HttpStatusCode.InternalServerError));
            }
        }