private void Search()
        {
            currentSearchTokenSource = new CancellationTokenSource();

            var appPreferences = dispatcher.Dispatch(AppPreferencesActions.GetAppPreferences());

            foreach (var ipAddress in appPreferences.GetIpAddressRange())
            {
                try
                {
                    if (pinger.IsResponding(ipAddress) && fadeCandyPinger.IsFadecandyDevice(ipAddress))
                    {
                        var hostName = pinger.GetHostName(ipAddress);
                        discoveredDevices.Enqueue(new DiscoveredDevice
                        {
                            Name      = hostName,
                            IpAddress = ipAddress
                        });
                    }
                }
                catch (Exception ex)
                {
                    logger.LogException(ex);
                }
            }
        }
Ejemplo n.º 2
0
        private async void ProcessJobs(CancellationToken cancellationToken)
        {
            var consumerTask = Task.Run(() => ConsumeJobs(cancellationToken), cancellationToken);

            var jobsLastAddedById = new List <int>();

            while (!cancellationToken.IsCancellationRequested)
            {
                Thread.Sleep(this.pollPeriodInMilliseconds);

                var newJobs = dispatcher.Dispatch(BackgroundJobActions.GetAllJobs(includeCompleted: false))
                              .Where(j => !jobQueue.Select(qj => qj.Id).Contains(j.Id))
                              .Where(j => !jobsLastAddedById.Contains(j.Id))
                              .OrderBy(j => j.Id);

                if (newJobs.Any())
                {
                    jobsLastAddedById = new List <int>();
                }

                foreach (var job in newJobs)
                {
                    jobsLastAddedById.Add(job.Id);
                    jobQueue.Add(job);
                }
            }

            await consumerTask;
        }
Ejemplo n.º 3
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 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.º 5
0
        public VideoReference ConsumeAndFinishUpload()
        {
            var timeoutStopwatch = new Stopwatch();

            timeoutStopwatch.Start();

            while (timeoutStopwatch.ElapsedMilliseconds < int.Parse(config.AppSettings["VideoUploadTimeoutMilliseconds"]))
            {
                if (!isUploading)
                {
                    var audio = AudioTrack.FromVideo(Path.Combine(UploadDirectory(), fileName));

                    return(dispatcher.Dispatch(VideoActions.CreateVideoReference(new VideoReference
                    {
                        FilePath = Path.Combine(UploadDirectory(), fileName),
                        AudioFilePath = audio.AudioFilePath
                    })));
                }
            }

            isUploading = false;
            throw new Exception("Video upload timed out");
        }
Ejemplo n.º 6
0
        public GlobalJsonResult <IEnumerable <VideoReference> > GetVideoReferences()
        {
            var result = dispatcher.Dispatch(VideoActions.GetVideoReferences());

            return(GlobalJsonResult <IEnumerable <VideoReference> > .Success(System.Net.HttpStatusCode.OK, result));
        }
Ejemplo n.º 7
0
        public Pinger(IDataAccessDispatcher dispatcher)
        {
            var appPreferences = dispatcher.Dispatch(AppPreferencesActions.GetAppPreferences());

            pingTimeout = appPreferences.PingTimeout;
        }
Ejemplo n.º 8
0
        public GlobalJsonResult <IEnumerable <Project> > GetAll()
        {
            var projects = dispatcher.Dispatch(ProjectActions.GetAllProjects());

            return(GlobalJsonResult <IEnumerable <Project> > .Success(System.Net.HttpStatusCode.OK, projects));
        }
Ejemplo n.º 9
0
        public GlobalJsonResult <Build> GetBuild(string id)
        {
            var result = dataAccessDispatcher.Dispatch(BuildActions.GetBuild(id));

            return(GlobalJsonResult <Build> .Success(System.Net.HttpStatusCode.OK, result));
        }
Ejemplo n.º 10
0
        public GlobalJsonResult <IEnumerable <Solution> > GetAll()
        {
            var result = dataAccessDispatcher.Dispatch(SolutionActions.GetAll());

            return(GlobalJsonResult <IEnumerable <Solution> > .Success(System.Net.HttpStatusCode.OK, result));
        }
Ejemplo n.º 11
0
        public GlobalJsonResult <IEnumerable <BackgroundJob> > GetAllJobs(bool includeCompleted = false)
        {
            var result = dispatcher.Dispatch(BackgroundJobActions.GetAllJobs(includeCompleted));

            return(GlobalJsonResult <IEnumerable <BackgroundJob> > .Success(System.Net.HttpStatusCode.OK, result));
        }
        public GlobalJsonResult <IEnumerable <ProjectDevice> > GetProjectDevices(int projectId)
        {
            var result = dispatcher.Dispatch(ProjectDeviceActions.GetProjectDevices(projectId));

            return(GlobalJsonResult <IEnumerable <ProjectDevice> > .Success(System.Net.HttpStatusCode.OK, result));
        }
        public GlobalJsonResult <AppPreferences> GetAll()
        {
            var result = dispatcher.Dispatch(AppPreferencesActions.GetAppPreferences());

            return(GlobalJsonResult <AppPreferences> .Success(System.Net.HttpStatusCode.OK, result));
        }
Ejemplo n.º 14
0
        public GlobalJsonResult <ProjectDevice> AddDiscoveredDeviceToProject(int projectId, [FromBody] DiscoveredDevice device)
        {
            var result = dispatcher.Dispatch(DeviceActions.AddDeviceToProject(projectId, device.Map()));

            return(GlobalJsonResult <ProjectDevice> .Success(HttpStatusCode.OK, result));
        }
Ejemplo n.º 15
0
        public Version GetCurrentVersion()
        {
            var solution = dataAccessDispatcher.Dispatch(SolutionActions.GetSolutionByName(CrmConstants.DefaultSolutionSettings.Name));

            return(Version.Parse(solution.Version));
        }
Ejemplo n.º 16
0
        public void ProcessProjectDeviceVersion(BackgroundJob job)
        {
            var projectDeviceVersion = dispatcher.Dispatch(ProjectDeviceActions.GetProjectDeviceVersion(job.ProjectDeviceVersionId));

            Guard.This(projectDeviceVersion).AgainstDefaultValue(string.Format("Could not find project device version '{0}'", job.ProjectDeviceVersionId));

            var device = dispatcher.Dispatch(DeviceActions.GetProjectDevice(projectDeviceVersion.ProjectDeviceId));

            Guard.This(device).AgainstDefaultValue(string.Format("Could not find project device with project device Id '{0}'", projectDeviceVersion.ProjectDeviceId));
            Guard.This(device).WithRule(d => deviceStatusService.IsOnline(device), string.Format("Device {0} (with IP Address {1}) is not online", device.Name, device.IpAddress));

            var project = dispatcher.Dispatch(ProjectActions.GetProjectFromProjectDevice(projectDeviceVersion.ProjectDeviceId));

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

            var video = dispatcher.Dispatch(VideoActions.GetVideoForProject(project.Id));

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

            using (var videoProcessor = videoProcessorInstantiator())
            {
                var videoMetadata = videoProcessor.StartReadingVideo(project.Id, projectDeviceVersion.ProjectDeviceId);
                var client        = piClientFactory.ForDevice(device);

                // Update video metadata (use project device id instead of video id with client (could have the same video in multiple configurations)
                var existingVideosOnPi = client.GetAllVideoMetadata();
                var existingVideoOnPi  = existingVideosOnPi.SingleOrDefault(v => v.Id == projectDeviceVersion.ProjectDeviceId);

                if (existingVideoOnPi == null)
                {
                    client.CreateVideoMetadata(new VideoMetadataCreateRequest
                    {
                        Id        = projectDeviceVersion.ProjectDeviceId,
                        FileName  = video.FilePath,
                        FrameRate = videoMetadata.FrameRate
                    });
                }
                else
                {
                    client.UpdateVideoMetadata(new VideoMetadataPutRequest
                    {
                        Id        = projectDeviceVersion.ProjectDeviceId,
                        FileName  = video.FilePath,
                        FrameRate = videoMetadata.FrameRate,
                    });
                }

                // Clear, then send frames to Pi
                client.ClearFrames(projectDeviceVersion.ProjectDeviceId);

                int framePosition = 1;
                while (true)
                {
                    var read = videoProcessor.ReadNext1000Frames();
                    client.SendFrames(projectDeviceVersion.ProjectDeviceId, new AppendFramesRequest
                    {
                        AppendFrameRequests = read.Frames
                                              .Select(f => new AppendFrameRequest {
                            BinaryData = f, Position = framePosition
                        })
                                              .ToArray()
                    });

                    dispatcher.Dispatch(BackgroundJobActions.MarkPercentageCompletion(job.Id, read.PercentageComplete));

                    if (!read.MoreFrames)
                    {
                        break;
                    }

                    framePosition++;
                }
            }
        }
        public GlobalJsonResult <ProjectDeviceVersion> GetLatestProjectDeviceVersion(int deviceId, int projectId)
        {
            var result = dispatcher.Dispatch(ProjectDeviceActions.GetLatestProjectDeviceVersion(deviceId, projectId));

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