/// <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); } } }