update moved to rsv class, epg update-lookup-tab, new methods inline, strip new prefix

This commit is contained in:
Bob Buxton 2020-05-08 14:56:11 +01:00
parent ac9e2ce8e8
commit bf4c5341d9
1 changed files with 170 additions and 91 deletions

View File

@ -197,33 +197,62 @@ proc rsvscan {} {
set slot [$resv get ulslot]
set lasttime $now
set dresv "$ds [clock format $d -format {%H:%M}] === slot $slot === $name === [$resv channel_name]"
# add any missing episodes
if {[$resv isseries]} {
miss_epsd $resv $dresv
set s [$resv start]
set d [$resv get nduration]
set e $($s + $d)
}
lassign [$resv padded 1] sp ep
set rstatus [$resv status]
# Has event passed
if {$now > $e + $ep} {
if {$now > $e + $ep
&& $rstatus == "unknown" } {
set ended 1
incr num_ended
} else {
set ended 0
}
if {!$ended} {
log "Reservation - $dresv" 2
if {$s - $sp < $now} {
#
# Should be recording now, check if it is
#
log "Already started? Status = $rstatus - $dresv" 1
if {$::opts(nonotrec)} {
log "-noNotRec - Not checking for failed recording" 2
continue
}
# need to check whether it has actually started and schedule alternate if not recording
if {$rstatus=="recording"} {
# Recording but is it actually growing
# How to associate recording with slot?22
puts [system nugget recordings]
}
# don't check active recordings further
continue
}
# add any missing episodes
if {[$resv isseries]} {
miss_epsd $resv $dresv
set s [$resv start]
set d [$resv get nduration]
set e $($s + $d)
}
if {$s > $thresh} {
log "Start > Threshold - $dresv" 2
continue
}
set elist [$resv aul]
set enum -1
set ecrids [split [$resv get szEventToRecord] "|"]
set ecrid [$resv get szCRID]
set ecrids [split [string toupper [$resv get szEventToRecord]] "|"]
set ecrid [string toupper [$resv get szCRID]]
set ps $s
foreach epsd $elist {
lassign $epsd service_id start end event_id
if {$start < $ps} {
log "AUL not in time order - $dresv" 0
}
set ps $start
}
if {[llength $elist] > 0} {
lassign [lindex $elist 0] service_id start end event_id
@ -264,7 +293,6 @@ proc rsvscan {} {
break
}
chkEpisode $resv $epsd $ecrid
if {$start < $now} { break }
}
} else {
@ -302,24 +330,7 @@ proc chkEpisode {resv epsd ecrid} {
set deps "[clock format $start -format {%d/%m/%y %H:%M}] [clock format $dur -format {%H:%M}] === slot $slot === $name === [$resv channel_name]"
if {$start < $now} {
#
# Should be recording now, check if it is
#
log "Already started? Status = [$resv status ] - $deps" 1
if {$::opts(nonotrec)} {
log "-noNotRec - Not checking for failed recording" 2
return
}
# need to check whether it has actually started and schedule alternate if not recording
if {[$resv status]=="recording"} {
# Recording but is it actually growing
# How to associate recording with slot?22
puts [system nugget recordings]
}
return
}
set svc $svcmap($service_id)
# Retrieve epg record for the episode
@ -341,7 +352,7 @@ proc chkEpisode {resv epsd ecrid} {
set others [epg dbfetch dump -crid $epgcrid -sort "start" ]
if {$others == {}} {
# No luck by crid, try for same name/time/channel
set others [epg dbfetch dump -term $name -service $svc -time "$(($start+$end)/2)" -sort "start" -debug 1 ]
set others [epg dbfetch dump -term [strip_new $name] -service $svc -time "$(($start+$end)/2)" -sort "start" -debug 1 ]
}
#puts $others
set other [findAlternate $resv $others $start $def 1]
@ -510,6 +521,17 @@ proc scanAlternates {resv others start definition sametime samedef} {
return ""
}
# Strip new: prefix from name prior to search
proc strip_new {name} {
foreach pfx {"new:" "new series." new} {
if {[string first $pfx [string tolower $name]] == 0} {
set name [string range $name [string length $pfx] end]
break
}
}
return [string trim $name]
}
# Check for prossible changes to series CRID
# Unscheduled series at same time/channel as an existing ulSeries
# Also one-off programmes matching series name
@ -527,7 +549,7 @@ proc scrid_chg {resv desc lasttime} {
set start [$resv start]
set timeslot [clock format $start -format "%H:%M"]
set others [epg dbfetch dump -service $svc -term $name -after $lasttime -before $thresh -sort "start"]
set others [epg dbfetch dump -service $svc -term [strip_new $name] -after $lasttime -before $thresh -sort "start"]
foreach other $others {
set ostart [$other get start]
@ -661,6 +683,11 @@ proc schedule {event desc {reason ""} {type 1} } {
log "*** Already scheduled *** $desc === $reason" 0
return 1
}
# mark new event as scheduled
set service_id [$event get channel_hsvc]
set event_id [$event get event_id]
epg update_lookup_tab service_id event_id 1
if {$::opts(test)} {
log "*** Test mode =scheduled *** $desc === $reason" 0
return 1
@ -701,6 +728,11 @@ proc refresh {resv desc {reason ""}} {
# Cancel reservation
proc cancel {resv desc {reason ""}} {
# mark event as not scheduled
set service_id [$resv get hsvc]
set event_id [$resv get usevtid]
epg update_lookup_tab service_id event_id 0
if {$::opts(test)} {
log "*** Test mode =cancelled *** $desc === $reason" 0
return 1
@ -761,67 +793,119 @@ proc replaceskip {resv skipcrid desc {reason ""}} {
}
}
rsv method update_aul {new_event old_aul} {
# Add new_event (optional) to the aulEventToRecordInfo and szEventToRecord
# remove old_aul (optional) from " "
# Maintain list in time order
set curauls [$self aul]
set ecrids [split $szEventToRecord "|"]
set evtaul {}
set insert 1
set start 0
# Do we have new event to add?
if {$new_event != 0 && $new_event !={}} {
set service_id [$new_event get channel_hsvc]
set start [$new_event get start]
set end [$new_event end]
set evtid [$new_event get event_id]
set evtaul [list $service_id $start $end $evtid]
set evtcrid [string toupper "1[$new_event get channel_crid][$new_event get event_crid]"]
set insert 0
}
# rebuild aul and events list with new event in correct order
lmap aul $curauls ecrid $ecrids {
#puts "$aul $ecrid"
if {$aul == $old_aul} {continue; #remove current entry}
if {$aul == ""} {continue; #null entry}
if {!$insert && $start <= [lindex $aul 1]} {
set insert 1
lappend newaul $evtaul
append newevnts "$evtcrid|"
}
lappend newaul $aul
append newevnts "$ecrid|"
}
# add to end if not inserted
if {!$insert} {
set insert 1
lappend newaul $evtaul
append newevnts "$evtcrid|"
}
set action 5
$self setaul [$self buildaul $newaul]
set szEventToRecord $newevnts
}
rsv method set_next_event {} {
# Update reservation next event info in ucVolume
if {[string length $aulEventToRecordInfo] >0} {
binary scan [string range $aulEventToRecordInfo 0 15]\
iiii service_id start end event_id
set dur $($end-$start)
if {$hsvc != $service_id ||
$nsttime != $start ||
$nduration != $dur ||
$usevtid != $event_id} {
set nduration $dur
set ucVolume "$service_id:$event_id:$start"
}
} else {
# The next event is not yet in the EPG
set time $([clock seconds] - 86400)
set ucVolume "$hsvc:$usLastRecordedEvtId:$time"
}
}
rsv method reset_next_event {} {
# Ensure our copy matches the updated reservation
if {$ucVolume!=0} {
lassign [split $ucVolume ":"] service_id event_id start
set hsvc $service_id
set nsttime $start
set usevtid $event_id
set ucVolume 0
}
}
proc {epg update_lookup_tab} {svcid evtid {add true} {type "S"} } {
global rsvlookup
# Update the reservations look up table (if present) with single entry
if {$rsvlookup==""} {return}
set key $svcid:$evtid
if {$add} {
# add new entry
dict set $rsvlookup $key $type
} else {
# remove existing entry
if {[dict exists $rsvlookup $key]} {
dict unset $rsvlookup $key
}
}
}
# Update reservation with new event details
proc update_event {resv other origaul desc {reason ""}} {
if {$origaul != {}} {
# mark event not scheduled
lassign $origaul service_id start end event_id
epg update_lookup_tab service_id event_id 0
}
if {$other != {}} {
# mark new event as scheduled
set service_id [$other get channel_hsvc]
set event_id [$other get event_id]
epg update_lookup_tab service_id event_id 1
}
if {$::opts(test)} {
log "*** Test mode =Updated *** $desc === $reason" 0
return 1
}
set curauls [$resv aul]
set ecrids [split [$resv get szEventToRecord] "|"]
set othaul {}
set insert 1
set start 0
if {$other != 0 && $other !={}} {
set hsvc [$other get channel_hsvc]
set start [$other get start]
set end [$other end]
set evtid [$other get event_id]
set othaul [list $hsvc $start $end $evtid]
set othcrid [string toupper "1[$other get channel_crid][$other get event_crid]"]
set insert 0
}
set newaul {}
set newevnts ""
log "$origaul-- $othaul -- $curauls $ecrids " 3
# rebuild aul and events list with new event in correct order
lmap aul $curauls ecrid $ecrids {
#puts "$aul $ecrid"
if {$aul == $origaul} {continue; #remove current entry}
if {$aul == ""} {continue; #null entry}
if {!$insert && $start <= [lindex $aul 1]} {
set insert 1
lappend newaul $othaul
append newevnts "$othcrid|"
}
lappend newaul $aul
append newevnts "$ecrid|"
}
# add to end if not inserted
if {!$insert} {
set insert 1
lappend newaul $othaul
append newevnts "$othcrid|"
}
log "$newevnts -- $newaul" 3
$resv update_aul $other $origaul
# update header with next event
lassign [lindex $newaul 0] service_id start end event_id
set dur $($end-$start)
if {[$resv get hsvc] != $service_id ||
[$resv get nsttime] != $start ||
[$resv get nduration] != $dur ||
[$resv get usevtid] != $event_id} {
$resv set nduration $($end-$start)
$resv set ucVolume "$service_id:$event_id:$start"
log "$desc update next episode [$resv get ucVolume]" 2
}
$resv set_next_event
if {[catch {
$resv set action 5
$resv setaul [$resv buildaul $newaul]
$resv set szEventToRecord $newevnts
$resv insert
} msg]} {
log "+++ Error while updating: $msg ++++ $desc === $reason" 0
@ -830,12 +914,7 @@ proc update_event {resv other origaul desc {reason ""}} {
log "*** Updated *** $desc === $reason" 0
system restartpending
# Ensure our copy matches the updated
$resv set hsvc $service_id
$resv set nsttime $start
$resv set nduration $($end-$start)
$resv set usevtid $event_id
$resv set ucVolume 0
return 1
$resv reset_next_event
}
}