public byte[] ProcessBitmap(Bitmap bitmap, ProjectDeviceVersion projectDeviceVersion)
        {
            Guard.This(bitmap).AgainstDefaultValue("Bitmap cannot be null");

            var mappings = dispatcher.Dispatch(ProjectDeviceActions.GetProjectDeviceMappings(projectDeviceVersion.Id));

            var data = new List <byte>
            {
                0, 0, 0, 0
            };

            var xPercentagePixelGap = (double)(projectDeviceVersion.HorizontalPercentage) / (double)(projectDeviceVersion.NumberOfHorizontalPixels - 1);
            var yPercentagePixelGap = (double)(projectDeviceVersion.VerticalPercentage) / (double)(projectDeviceVersion.NumberOfVerticalPixels - 1);

            foreach (var mapping in mappings.OrderBy(m => m.MappingOrder))
            {
                var xPercent = projectDeviceVersion.StartAtHorizontalPercentage + (mapping.HorizontalPosition - 1) * xPercentagePixelGap;
                var yPercent = projectDeviceVersion.StartAtVerticalPercentage + (mapping.VerticalPosition - 1) * yPercentagePixelGap;

                var x = (int)Math.Round(xPercent * bitmap.Width / 100, 0);
                var y = (int)Math.Round(yPercent * bitmap.Height / 100, 0);

                x = x >= bitmap.Width ? bitmap.Width - 1 : x;
                y = y >= bitmap.Height ? bitmap.Height - 1 : y;

                try
                {
                    var pixelColor = bitmap.GetPixel(x, y);

                    data.Add(pixelColor.R);
                    data.Add(pixelColor.G);
                    data.Add(pixelColor.B);
                }
                catch
                {
                    throw;
                }
            }

            return(data.ToArray());
        }
Ejemplo n.º 2
0
        public VideoMetadata StartReadingVideo(int projectId, int projectDeviceId)
        {
            var project = dispatcher.Dispatch(ProjectActions.GetProject(projectId));

            Guard.This(project).AgainstDefaultValue(string.Format("Could not find project with project id '{0}'", projectId));

            var video = dispatcher.Dispatch(VideoActions.GetVideoForProject(projectId));

            Guard.This(video).AgainstDefaultValue(string.Format("Could not find video for project '{0}' (id '{1}')", project.Name, project.Id));

            var projectDeviceVersion = dispatcher.Dispatch(ProjectDeviceActions.GetLatestProjectDeviceVersion(projectDeviceId));

            Guard.This(projectDeviceVersion).AgainstDefaultValue(string.Format("Could not get latest project device version for project '{0}' (id '{1}')", project.Name, project.Id));

            reader = videoFileReader();
            reader.Open(video.FilePath);

            framesRead                = 0;
            framesToRead              = reader.FrameCount;
            this.videoReference       = video;
            this.projectDeviceVersion = projectDeviceVersion;

            return(new VideoMetadata(reader.FrameCount, reader.FrameRate));
        }
        public static DataAccessAction <ProjectDeviceVersion> SetLatestProjectDeviceVersion(ProjectDeviceVersion projectDeviceVersion)
        {
            return(new DataAccessAction <ProjectDeviceVersion>((IDbConnection conn) =>
            {
                projectDeviceVersion.Validate();

                var currentVersion = conn.Query <ProjectDeviceVersion>("SELECT * FROM ProjectDeviceVersion WHERE projectdeviceid = @projectdeviceid ORDER BY Version DESC LIMIT 1",
                                                                       new { projectDeviceVersion.ProjectDeviceId })
                                     .SingleOrDefault();

                var id = conn.GetNextId <ProjectDeviceVersion>();

                projectDeviceVersion.Id = id;
                projectDeviceVersion.Version = currentVersion == null ? 1 : currentVersion.Version + 1;

                conn.Insert(projectDeviceVersion);

                // Update mappings to exist for new version
                if (currentVersion != null)
                {
                    conn.Execute("UPDATE ProjectDeviceVersionMapping SET projectdeviceversionid = @newprojectdeviceversionid WHERE projectdeviceversionid = @currentprojectdeviceversionid",
                                 new { NewProjectDeviceVersionId = id, CurrentProjectDeviceVersionId = currentVersion.Id });
                }


                // Always create a new background job when the project device version changes
                BackgroundJobActions.CreateJob(projectDeviceVersion.Id).Function(conn);

                return projectDeviceVersion;
            }));
        }
Ejemplo n.º 4
0
        public static DataAccessAction <ProjectDevice> AddDeviceToProject(int projectId, Device device)
        {
            return(new DataAccessAction <ProjectDevice>((IDbConnection conn) =>
            {
                device.Validate();

                var project = conn.Get <Project>(projectId);
                Guard.This(project).AgainstDefaultValue(string.Format("No project exists with id '{0}'", projectId));

                var existingDevice = conn
                                     .Query <Device>("SELECT * FROM Device WHERE ipaddress = @ipaddress", new { device.IpAddress })
                                     .SingleOrDefault();

                using (var transaction = conn.BeginTransaction())
                {
                    try
                    {
                        if (existingDevice != null)
                        {
                            device.Id = existingDevice.Id;
                            conn.Update(device, transaction);
                        }
                        else
                        {
                            device.Id = conn.GetNextId <Device>();
                            conn.Insert(device, transaction);
                        }

                        var existingProjectDevices = conn
                                                     .Query <ProjectDevice>("SELECT * FROM ProjectDevice WHERE deviceid=@deviceid AND projectid=@projectid", new { deviceId = device.Id, projectId });

                        foreach (var existingProjectDevice in existingProjectDevices)
                        {
                            conn.Delete(existingProjectDevice, transaction);
                        }

                        var projectDevice = new ProjectDevice {
                            DeviceId = device.Id, ProjectId = project.Id
                        };
                        projectDevice.Id = conn.GetNextId <ProjectDevice>();

                        conn.Insert(projectDevice, transaction);

                        var projectDeviceVersionId = conn.GetNextId <ProjectDeviceVersion>();
                        var projectDeviceVersion = new ProjectDeviceVersion
                        {
                            Id = projectDeviceVersionId,
                            ProjectDeviceId = projectDevice.Id,
                        };

                        conn.Insert(projectDeviceVersion, transaction);

                        transaction.Commit();

                        return projectDevice;
                    }
                    catch
                    {
                        transaction.Rollback();
                        throw;
                    }
                }
            }));
        }
        public GlobalJsonResult <ProjectDeviceVersion> SetLatestProjectDeviceVersion(int projectDeviceId, [FromBody] ProjectDeviceVersion projectDeviceVersion)
        {
            var result = dispatcher.Dispatch(ProjectDeviceActions.SetLatestProjectDeviceVersion(projectDeviceVersion));

            return(GlobalJsonResult <ProjectDeviceVersion> .Success(System.Net.HttpStatusCode.Created, result));
        }