Exemplo n.º 1
0
        /// <summary>
        /// Registriert diese Aufzeichnung in einer Planungsinstanz.
        /// </summary>
        /// <param name="scheduler">Die zu verwendende Planungsinstanz.</param>
        /// <param name="job">Der zugehörige Auftrag.</param>
        /// <param name="devices">Die Liste der Geräte, auf denen die Aufzeichnung ausgeführt werden darf.</param>
        /// <param name="findSource">Dient zum Prüfen einer Quelle.</param>
        /// <param name="disabled">Alle deaktivierten Aufträge.</param>
        /// <param name="context">Die aktuelle Planungsumgebung.</param>
        /// <exception cref="ArgumentNullException">Es wurden nicht alle Parameter angegeben.</exception>
        public void AddToScheduler(RecordingScheduler scheduler, VCRJob job, IScheduleResource[] devices, Func <SourceSelection, SourceSelection> findSource, Func <Guid, bool> disabled, PlanContext context)
        {
            // Validate
            if (scheduler == null)
            {
                throw new ArgumentNullException(nameof(scheduler));
            }
            if (job == null)
            {
                throw new ArgumentNullException(nameof(job));
            }
            if (findSource == null)
            {
                throw new ArgumentNullException(nameof(findSource));
            }

            // Let VCR.NET choose a profile to do the work
            if (job.AutomaticResourceSelection)
            {
                devices = null;
            }

            // Create the source selection
            var persistedSource = Source ?? job.Source;
            var selection       = findSource(persistedSource);

            // Station no longer available
            if (selection == null)
            {
                if (persistedSource != null)
                {
                    selection =
                        new SourceSelection
                    {
                        DisplayName = persistedSource.DisplayName,
                        ProfileName = persistedSource.ProfileName,
                        Location    = persistedSource.Location,
                        Group       = persistedSource.Group,
                        Source      =
                            new Station
                        {
                            TransportStream = persistedSource.Source?.TransportStream ?? 0,
                            Network         = persistedSource.Source?.Network ?? 0,
                            Service         = persistedSource.Source?.Service ?? 0,
                            Name            = persistedSource.DisplayName,
                        },
                    }
                }
            }
            ;

            // See if we are allowed to process
            var identifier = UniqueID.Value;

            if (disabled != null)
            {
                if (disabled(identifier))
                {
                    return;
                }
            }

            // Load all
            var name          = string.IsNullOrEmpty(Name) ? job.Name : $"{job.Name} ({Name})";
            var source        = ProfileScheduleResource.CreateSource(selection);
            var duration      = TimeSpan.FromMinutes(Duration);
            var noStartBefore = NoStartBefore;
            var start         = FirstStart;

            // Check repetition
            var repeat = CreateRepeatPattern();

            if (repeat == null)
            {
                // Only if not being recorded
                if (!noStartBefore.HasValue)
                {
                    scheduler.Add(RecordingDefinition.Create(this, name, identifier, devices, source, start, duration));
                }
            }
            else
            {
                // See if we have to adjust the start day
                if (noStartBefore.HasValue)
                {
                    // Attach to the limit - actually we shift it a bit further assuming that we did have no large exception towards the past and the duration is moderate
                    var startAfter    = noStartBefore.Value.AddHours(12);
                    var startAfterDay = startAfter.ToLocalTime().Date;

                    // Localize the start time
                    var startTime = start.ToLocalTime().TimeOfDay;

                    // First adjust
                    start = (startAfterDay + startTime).ToUniversalTime();

                    // One more day
                    if (start < startAfter)
                    {
                        start = (startAfterDay.AddDays(1) + startTime).ToUniversalTime();
                    }
                }

                // Read the rest
                var exceptions = Exceptions.Select(e => e.ToPlanException(duration)).ToArray();
                var endDay     = LastDay.GetValueOrDefault(MaxMovableDay);

                // A bit more complex
                if (start.Date <= endDay.Date)
                {
                    scheduler.Add(RecordingDefinition.Create(this, name, identifier, devices, source, start, duration, endDay, repeat), exceptions);
                }
            }
        }
        /// <summary>
        /// Erstellt eine neue Planung.
        /// </summary>
        /// <param name="site">Die zugehörige Arbeitsumgebung.</param>
        private RecordingPlanner(IRecordingPlannerSite site)
        {
            // Remember
            m_site = site;

            // Process all profiles
            foreach (var profileName in site.ProfileNames)
            {
                // Look up the profile
                var profile = ProfileManager.FindProfile(profileName);
                if (profile == null)
                {
                    continue;
                }

                // Create the resource for it
                var profileResource = ProfileScheduleResource.Create(profileName);

                // Remember
                m_resources.Add(profileName, profileResource);

                // See if this is a leaf profile
                if (!string.IsNullOrEmpty(profile.UseSourcesFrom))
                {
                    continue;
                }

                // See if we should process guide updates
                var guideTask = site.CreateProgramGuideTask(profileResource, profile);
                if (guideTask != null)
                {
                    m_tasks.Add(guideTask);
                }

                // See if we should update the source list
                var scanTask = site.CreateSourceScanTask(profileResource, profile);
                if (scanTask != null)
                {
                    m_tasks.Add(scanTask);
                }
            }

            // Make sure we report all errors
            try
            {
                // Create the manager
                m_manager = ResourceManager.Create(site.ScheduleRulesPath, ProfileManager.ProfileNameComparer);
            }
            catch (Exception e)
            {
                // Report
                VCRServer.LogError(Properties.Resources.BadRuleFile, e.Message);

                // Use standard rules
                m_manager = ResourceManager.Create(ProfileManager.ProfileNameComparer);
            }

            // Safe configure it
            try
            {
                // All all resources
                foreach (var resource in m_resources.Values)
                {
                    m_manager.Add(resource);
                }
            }
            catch (Exception e)
            {
                // Cleanup
                Dispose();

                // Report
                VCRServer.Log(e);
            }
        }