Linux-DVB-DVBT-Apps-QuartzPVR
view release on metacpan or search on metacpan
lib/Linux/DVB/DVBT/Apps/QuartzPVR/Schedule.pm view on Meta::CPAN
print "\n==[ Phase 3 ]=============================================\n" ;
$tvreport->display_unschedule_by_adap($schedule_aref, $unscheduled_aref) ;
print "\n==========================================================\n" ;
}
# report
$tvreport->scheduling($schedule_aref, $unscheduled_aref) ;
$this->prt_data("Schedule3=", $schedule_aref, "Unscheduled3=", $unscheduled_aref) if $this->debug >= 4 ;
}
}
## Need to propogate the DVB adapter down from the 'multiplex' container into each sub-prog
$this->update_multiplex($schedule_aref) ;
# update the array passed in
@$requested_rec_aref = @$recording_schedule_aref ;
Linux::DVB::DVBT::Apps::QuartzPVR::Base::DbgProf::endfn() ;
my $ok = scalar(@$unscheduled_aref) ? 0 : 1 ;
return ($ok) ;
}
#---------------------------------------------------------------------
# Remove any dummy "fuzzy" recordings
#
sub remove_dummies
{
my $this = shift ;
my ($recording_aref) = @_ ;
# TODO: Change scheduling to not schedule these in the first place!
Linux::DVB::DVBT::Apps::QuartzPVR::Base::DbgTrace::startfn('remove dummies') ;
Linux::DVB::DVBT::Apps::QuartzPVR::Base::DbgProf::startfn() ;
my @removed ;
foreach my $prog_href (@$recording_aref)
{
if ($prog_href->{'pid'} == $Linux::DVB::DVBT::Apps::QuartzPVR::Base::Constants::FUZZY_PID)
{
# delete
}
else
{
push @removed, $prog_href ;
}
}
Linux::DVB::DVBT::Apps::QuartzPVR::Base::DbgProf::endfn() ;
return @removed ;
}
#---------------------------------------------------------------------
# Update cron jobs
#
sub update_cron
{
my $this = shift ;
my ($schedule_aref) = @_ ;
Linux::DVB::DVBT::Apps::QuartzPVR::Base::DbgTrace::startfn('update cron') ;
Linux::DVB::DVBT::Apps::QuartzPVR::Base::DbgProf::startfn() ;
## Set cron
Linux::DVB::DVBT::Apps::QuartzPVR::Crontab::update($schedule_aref) ;
Linux::DVB::DVBT::Apps::QuartzPVR::Base::DbgProf::endfn() ;
}
#---------------------------------------------------------------------
# Commit the final schedule (both to database and cron jobs)
#
sub commit
{
my $this = shift ;
my ($schedule_aref, $test) = @_ ;
Linux::DVB::DVBT::Apps::QuartzPVR::Base::DbgTrace::startfn('commit') ;
Linux::DVB::DVBT::Apps::QuartzPVR::Base::DbgProf::startfn() ;
## Set cron
Linux::DVB::DVBT::Apps::QuartzPVR::Crontab::commit() unless $test ;
## Set database
my $tvsql = $this->_tvsql ;
$tvsql->update_schedule_table($schedule_aref) unless $test>=2 ;
## Update recorded files
unless ($test >= 2)
{
my $recorded_files_href = Linux::DVB::DVBT::Apps::QuartzPVR::Crontab::recorded_files() ;
print "Schedule: Update recorded table\n" if $this->debug ;
## Check for exsistence of recrded entries
foreach my $idx (sort {
my $a_href = $recorded_files_href->{$a} ;
my $b_href = $recorded_files_href->{$b} ;
$a_href->{'start_dt_mins'} <=> $b_href->{'start_dt_mins'} ||
$a_href->{'rid'} <=> $b_href->{'rid'} ||
$a_href->{'rectype'} cmp $b_href->{'rectype'}
} keys %$recorded_files_href)
{
print " + $recorded_files_href->{$idx}{pid} ($recorded_files_href->{$idx}{rectype}) $recorded_files_href->{$idx}{title}\n" if $this->debug ;
my $exsisting_aref = $tvsql->select_recorded($recorded_files_href->{$idx}) ;
## If not exsits, then create a new one
if (!@$exsisting_aref)
{
print " + + Does not exist : creating new\n" if $this->debug ;
$tvsql->insert_recorded($recorded_files_href->{$idx}) ;
}
else
lib/Linux/DVB/DVBT/Apps/QuartzPVR/Schedule.pm view on Meta::CPAN
if ( ($rec_href->{'start_dt_mins'} < $max_time) && ($rec_href->{'end_dt_mins'} >= $min_time) )
{
++$locked ;
print " + + locked\n" if $this->debug ;
}
if ($locked)
{
$rec_href->{'locked'} = 1 ;
foreach my $mux_href (@{$rec_href->{'multiplex'}})
{
$mux_href->{'locked'} = 1 ;
}
}
}
}
#$this->prt_data("Schedule locked:", $schedule_aref) if $this->debug>=2 ;
print "lock_multiplexes() - DONE\n" if $this->debug ;
}
#---------------------------------------------------------------------
# Input:
# $schedule_aref - ARRAY ref of scheduled recordings (with any multiplex recordings)
#
# Output:
# $schedule_aref - ARRAY ref of scheduled recordings (without any multiplex recordings)
#
sub de_multiplex
{
my $this = shift ;
my ($schedule_aref, %options) = @_ ;
my $demux_aref = [] ;
foreach my $rec_href (@$schedule_aref)
{
if ($rec_href->{'type'} eq 'multiplex')
{
push @$demux_aref, @{$rec_href->{'multiplex'}} ;
}
else
{
push @$demux_aref, $rec_href ;
}
}
return $demux_aref ;
}
#---------------------------------------------------------------------
# Input:
# $schedule_aref - ARRAY ref of schedule format
#
# Propogates the DVB adapter number & the multiplex ID down into all progs contained
# in a multiplex recording
#
sub update_multiplex
{
my $this = shift ;
my ($schedule_aref, %options) = @_ ;
my @FIELDS = qw/multid adapter timeslip/ ;
foreach my $rec_href (@$schedule_aref)
{
# ensure defaults are set
foreach my $field (@FIELDS)
{
$rec_href->{$field} ||= 0 ;
}
# propogate from top-level multiplex container down to the individual programs
if ($rec_href->{'type'} eq 'multiplex')
{
foreach my $mux_href (@{$rec_href->{'multiplex'}})
{
foreach my $field (@FIELDS)
{
$mux_href->{$field} = $rec_href->{$field} ;
}
}
}
}
}
#---------------------------------------------------------------------
# Input:
# $schedule_aref - ARRAY ref in schedule format (read from SQL table)
#
# Output:
# $schedule_aref - ARRAY ref of schedule format (with multiplex recordings fixed)
#
#
sub fix_multiplex
{
my $this = shift ;
my ($schedule_aref) = @_ ;
my @mux_schedule = () ;
my %multiplex ;
foreach my $rec_href (@$schedule_aref)
{
my $multid = $rec_href->{'multid'} ;
if ($multid > 0)
{
# create new entry if required
if (!exists($multiplex{$multid}))
{
# create new multiplex entry based on this prog's info
my $mux_href = $this->_new_multiplex($rec_href, $multid) ;
push @mux_schedule, $mux_href ;
$multiplex{$multid} = $mux_href ;
}
else
{
# get multiplex & update the start/end/duration/priority based on this new prog
my $mux_href = $multiplex{$multid} ;
push @{$mux_href->{'multiplex'}}, $rec_href ;
if ($mux_href->{'start_dt_mins'} > $rec_href->{'start_dt_mins'})
{
$mux_href->{'date'} = $rec_href->{'date'} ;
$mux_href->{'start'} = $rec_href->{'start'} ;
$mux_href->{'start_dt_mins'} = $rec_href->{'start_dt_mins'} ;
}
$mux_href->{'end_dt_mins'} = $rec_href->{'end_dt_mins'} if ($mux_href->{'end_dt_mins'} < $rec_href->{'end_dt_mins'}) ;
$mux_href->{'priority'} = $rec_href->{'priority'} if ($mux_href->{'priority'} > $rec_href->{'priority'}) ;
}
}
else
{
push @mux_schedule, $rec_href ;
}
}
## Now ensure all multiplex times/durations etc are correct
foreach my $rec_href (@mux_schedule)
{
if ($rec_href->{'type'} eq 'multiplex')
{
my $duration = $rec_href->{'end_dt_mins'} - $rec_href->{'start_dt_mins'} ;
$rec_href->{'duration'} = Linux::DVB::DVBT::Apps::QuartzPVR::Time::mins2time($duration) ;
Linux::DVB::DVBT::Apps::QuartzPVR::Prog::set_times($rec_href) ;
}
}
return @mux_schedule ;
}
#---------------------------------------------------------------------
# Replace all rids set to NEW_RID with the specified new value
#
sub update_rid
{
my $this = shift ;
my ($new_rid, $schedule_aref) = @_ ;
foreach my $href (@$schedule_aref)
{
my @progs = ($href) ;
if ($href->{'type'} eq 'multiplex')
{
@progs = @{$href->{'multiplex'}} ;
}
foreach my $rec_href (@progs)
{
if ($rec_href->{'rid'} == $Linux::DVB::DVBT::Apps::QuartzPVR::Base::Constants::NEW_RID)
{
$rec_href->{'rid'} = $new_rid ;
}
}
}
}
#---------------------------------------------------------------------
# Check the schedule list for overlapped programs.
# Assumes the other programs are non-overlapping and checks to ensure
# that this new one doesn't interfere with existing ones. Also assumes
# that the schedule list is for the same DVB adapter
#
sub can_schedule
{
my $this = shift ;
my ($schedule_aref, $prog_href) = @_ ;
print "can_sched($prog_href->{title} $prog_href->{date} $prog_href->{start})\n" if $this->debug ;
my $ok = 1 ;
# Start with the new entry, moving through the schedule list to see if it overlaps
# For each entry, need to check
# (a) it starts AFTER the end of the previous program
#
# (prev) end
# ----------|
# ^
# |---------------.....
# start (current)
#
#
# (b) it ends BEFORE the start of the next program
#
# start (next)
# |--------....
# ^
# |-------------------|
# start (current) end
#
#
# NOTE: May we have:
#
# start end
( run in 2.163 seconds using v1.01-cache-2.11-cpan-437f7b0c052 )