예제 #1
0
        public IHttpActionResult GetCameraLastThumbPrint(long deviceID)
        {
            using (var dbContext = new DataContext())
            {
                var camera = dbContext.Cameras.Find(deviceID);
                if (camera == null)
                {
                    return(this.BadRequestEx(Error.DeviceNotFound));
                }

                var cameraLastStatistic = dbContext.CameraLastStatistics.SingleOrDefault(f => f.CameraID == deviceID);
                if (cameraLastStatistic == null)
                {
                    return(this.BadRequestEx(ErrorBase.PopulateUnexpectedException(new Exception("Statistic for device does not exist."))));
                }

                if (string.IsNullOrWhiteSpace(cameraLastStatistic.LastInfingementTime) || cameraLastStatistic.LastInfingementTime.Contains("--"))
                {
                    return(this.BadRequestEx(ErrorBase.PopulateUnexpectedException(new Exception("No last Infrngement time recorded."))));
                }

                var ipAddress            = ExtractIpAddress(camera.ConfigJson);
                var lastInfringementTime = cameraLastStatistic.ModifiedTimeStamp.ToString("yyyy-MM-dd") + " " + cameraLastStatistic.LastInfingementTime;

                var model = new CameraThumbNailModel();
                model.DeviceID = deviceID;

                // Do a 3s delay check
                for (var x = 0; x < 3; x++)
                {
                    var epoch    = GetLatestEpoch(lastInfringementTime, x);
                    var document = GetImageAsBase64Url(string.Format("http://{0}/timestamp_thumb.jpg?{1}", ipAddress, epoch));

                    if (!string.IsNullOrWhiteSpace(document))
                    {
                        model.Document = document;
                        return(Ok(model));
                    }
                }

                return(Ok(model));
            }
        }
        public override void OnActionExecuting(HttpActionContext actionContext)
        {
            var modelState = actionContext.ModelState;

            if (!modelState.IsValid)
            {
                var errorlist = modelState.Values.Aggregate("Validation errors : ",
                                                            (current1, value) =>
                                                            value.Errors.Aggregate(current1, (current, error) => current + (error.ErrorMessage)));

                actionContext.Response = actionContext.Request.CreateResponse(HttpStatusCode.BadRequest, ErrorBase.PopulateUnexpectedException(new Exception(errorlist)));
            }
        }