2 #from time import datetime
3 from Tools import Directories, Notifications
5 from Components.config import config
9 from enigma import eEPGCache, getBestPlayableServiceReference, \
10 eServiceReference, iRecordableService, quitMainloop
12 from Screens.MessageBox import MessageBox
14 import NavigationInstance
16 import Screens.Standby
18 from time import localtime
20 from Tools.XMLTools import elementsWithTag, mergeText, stringToXML
21 from ServiceReference import ServiceReference
23 # ok, for descriptions etc we have:
24 # service reference (to get the service name)
26 # description (description)
27 # event data (ONLY for time adjustments etc.)
30 # parses an event, and gives out a (begin, end, name, duration, eit)-tuple.
31 # begin and end will be corrected
32 def parseEvent(ev, description = True):
34 name = ev.getEventName()
35 description = ev.getShortDescription()
39 begin = ev.getBeginTime()
40 end = begin + ev.getDuration()
42 begin -= config.recording.margin_before.value * 60
43 end += config.recording.margin_after.value * 60
44 return (begin, end, name, description, eit)
51 # please do not translate log messages
52 class RecordTimerEntry(timer.TimerEntry, object):
53 ######### the following static methods and members are only in use when the box is in (soft) standby
54 receiveRecordEvents = False
61 def staticGotRecordEvent(recservice, event):
62 if event == iRecordableService.evEnd:
63 print "RecordTimer.staticGotRecordEvent(iRecordableService.evEnd)"
64 recordings = NavigationInstance.instance.getRecordings()
65 if not len(recordings): # no more recordings exist
66 rec_time = NavigationInstance.instance.RecordTimer.getNextRecordingTime()
67 if rec_time > 0 and (rec_time - time.time()) < 360:
68 print "another recording starts in", rec_time - time.time(), "seconds... do not shutdown yet"
70 print "no starting records in the next 360 seconds... immediate shutdown"
71 RecordTimerEntry.shutdown() # immediate shutdown
72 elif event == iRecordableService.evStart:
73 print "RecordTimer.staticGotRecordEvent(iRecordableService.evStart)"
76 def stopTryQuitMainloop():
77 print "RecordTimer.stopTryQuitMainloop"
78 NavigationInstance.instance.record_event.remove(RecordTimerEntry.staticGotRecordEvent)
79 RecordTimerEntry.receiveRecordEvents = False
82 def TryQuitMainloop(default_yes = True):
83 if not RecordTimerEntry.receiveRecordEvents:
84 print "RecordTimer.TryQuitMainloop"
85 NavigationInstance.instance.record_event.append(RecordTimerEntry.staticGotRecordEvent)
86 RecordTimerEntry.receiveRecordEvents = True
87 # send fake event.. to check if another recordings are running or
88 # other timers start in a few seconds
89 RecordTimerEntry.staticGotRecordEvent(None, iRecordableService.evEnd)
90 # send normal notification for the case the user leave the standby now..
91 Notifications.AddNotification(Screens.Standby.TryQuitMainloop, 1, onSessionOpenCallback=RecordTimerEntry.stopTryQuitMainloop, default_yes = default_yes)
92 #################################################################
94 def __init__(self, serviceref, begin, end, name, description, eit, disabled = False, justplay = False, afterEvent = AFTEREVENT.NONE, checkOldTimers = False, dirname = None):
95 timer.TimerEntry.__init__(self, int(begin), int(end))
97 if checkOldTimers == True:
98 if self.begin < time.time() - 1209600:
99 self.begin = int(time.time())
101 if self.end < self.begin:
102 self.end = self.begin
104 assert isinstance(serviceref, ServiceReference)
106 self.service_ref = serviceref
108 self.dontSave = False
110 self.description = description
111 self.disabled = disabled
113 self.__record_service = None
114 self.start_prepare = 0
115 self.justplay = justplay
116 self.afterEvent = afterEvent
117 self.dirname = dirname
118 self.dirnameHadToFallback = False
120 self.log_entries = []
123 def log(self, code, msg):
124 self.log_entries.append((int(time.time()), code, msg))
127 def calculateFilename(self):
128 service_name = self.service_ref.getServiceName()
129 begin_date = time.strftime("%Y%m%d %H%M", time.localtime(self.begin))
131 print "begin_date: ", begin_date
132 print "service_name: ", service_name
133 print "name:", self.name
134 print "description: ", self.description
136 filename = begin_date + " - " + service_name
138 filename += " - " + self.name
140 if self.dirname and not Directories.pathExists(self.dirname):
141 self.dirnameHadToFallback = True
142 self.Filename = Directories.getRecordingFilename(filename, None)
144 self.Filename = Directories.getRecordingFilename(filename, self.dirname)
145 self.log(0, "Filename calculated as: '%s'" % self.Filename)
146 #begin_date + " - " + service_name + description)
148 def tryPrepare(self):
152 self.calculateFilename()
153 rec_ref = self.service_ref and self.service_ref.ref
154 if rec_ref and rec_ref.flags & eServiceReference.isGroup:
155 rec_ref = getBestPlayableServiceReference(rec_ref, eServiceReference())
157 self.log(1, "'get best playable service for group... record' failed")
160 self.record_service = rec_ref and NavigationInstance.instance.recordService(rec_ref)
162 if not self.record_service:
163 self.log(1, "'record service' failed")
167 epgcache = eEPGCache.getInstance()
168 queryTime=self.begin+(self.end-self.begin)/2
169 evt = epgcache.lookupEventTime(rec_ref, queryTime)
171 self.description = evt.getShortDescription()
172 event_id = evt.getEventId()
180 prep_res=self.record_service.prepare(self.Filename + ".ts", self.begin, self.end, event_id)
182 self.log(2, "'prepare' failed: error %d" % prep_res)
183 NavigationInstance.instance.stopRecordService(self.record_service)
184 self.record_service = None
187 self.log(3, "prepare ok, writing meta information to %s" % self.Filename)
189 f = open(self.Filename + ".ts.meta", "w")
190 f.write(rec_ref.toString() + "\n")
191 f.write(self.name + "\n")
192 f.write(self.description + "\n")
193 f.write(str(self.begin) + "\n")
196 self.log(4, "failed to write meta information")
197 NavigationInstance.instance.stopRecordService(self.record_service)
198 self.record_service = None
202 def do_backoff(self):
203 if self.backoff == 0:
207 if self.backoff > 100:
209 self.log(10, "backoff: retry in %d seconds" % self.backoff)
212 next_state = self.state + 1
213 self.log(5, "activating state %d" % next_state)
215 if next_state == self.StatePrepared:
216 if self.tryPrepare():
217 self.log(6, "prepare ok, waiting for begin")
218 # fine. it worked, resources are allocated.
219 self.next_activation = self.begin
223 self.log(7, "prepare failed")
224 if self.first_try_prepare:
225 self.first_try_prepare = False
226 if not config.recording.asktozap.value:
227 self.log(8, "asking user to zap away")
228 Notifications.AddNotificationWithCallback(self.failureCB, MessageBox, _("A timer failed to record!\nDisable TV and try again?\n"), timeout=20)
229 else: # zap without asking
230 self.log(9, "zap without asking")
231 Notifications.AddNotification(MessageBox, _("In order to record a timer, the TV was switched to the recording service!\n"), type=MessageBox.TYPE_INFO, timeout=20)
236 self.start_prepare = time.time() + self.backoff
238 elif next_state == self.StateRunning:
239 # if this timer has been cancelled, just go to "end" state.
244 if Screens.Standby.inStandby:
245 self.log(11, "wakeup and zap")
246 #set service to zap after standby
247 Screens.Standby.inStandby.prev_running_service = self.service_ref.ref
249 Screens.Standby.inStandby.Power()
251 self.log(11, "zapping")
252 NavigationInstance.instance.playService(self.service_ref.ref)
255 self.log(11, "start recording")
256 record_res = self.record_service.start()
259 self.log(13, "start record returned %d" % record_res)
262 self.begin = time.time() + self.backoff
266 elif next_state == self.StateEnded:
267 self.log(12, "stop recording")
268 if not self.justplay:
269 NavigationInstance.instance.stopRecordService(self.record_service)
270 self.record_service = None
271 if self.afterEvent == AFTEREVENT.STANDBY:
272 if not Screens.Standby.inStandby: # not already in standby
273 Notifications.AddNotificationWithCallback(self.sendStandbyNotification, MessageBox, _("A finished record timer wants to set your\nDreambox to standby. Do that now?"), timeout = 20)
274 elif self.afterEvent == AFTEREVENT.DEEPSTANDBY:
275 if not Screens.Standby.inTryQuitMainloop: # not a shutdown messagebox is open
276 if Screens.Standby.inStandby: # in standby
277 RecordTimerEntry.TryQuitMainloop() # start shutdown handling without screen
279 Notifications.AddNotificationWithCallback(self.sendTryQuitMainloopNotification, MessageBox, _("A finished record timer wants to shut down\nyour Dreambox. Shutdown now?"), timeout = 20)
282 def sendStandbyNotification(self, answer):
284 Notifications.AddNotification(Screens.Standby.Standby)
286 def sendTryQuitMainloopNotification(self, answer):
288 Notifications.AddNotification(Screens.Standby.TryQuitMainloop, 1)
290 def getNextActivation(self):
291 if self.state == self.StateEnded:
294 next_state = self.state + 1
296 return {self.StatePrepared: self.start_prepare,
297 self.StateRunning: self.begin,
298 self.StateEnded: self.end }[next_state]
300 def failureCB(self, answer):
302 self.log(13, "ok, zapped away")
303 #NavigationInstance.instance.stopUserServices()
304 NavigationInstance.instance.playService(self.service_ref.ref)
306 self.log(14, "user didn't want to zap away, record will probably fail")
308 def timeChanged(self):
309 old_prepare = self.start_prepare
310 self.start_prepare = self.begin - self.prepare_time
313 if int(old_prepare) != int(self.start_prepare):
314 self.log(15, "record time changed, start prepare is now: %s" % time.ctime(self.start_prepare))
316 def gotRecordEvent(self, record, event):
317 # TODO: this is not working (never true), please fix. (comparing two swig wrapped ePtrs)
318 if self.__record_service.__deref__() != record.__deref__():
320 self.log(16, "record event %d" % event)
321 if event == iRecordableService.evRecordWriteError:
322 print "WRITE ERROR on recording, disk full?"
323 # show notification. the 'id' will make sure that it will be
324 # displayed only once, even if more timers are failing at the
325 # same time. (which is very likely in case of disk fullness)
326 Notifications.AddPopup(text = _("Write error while recording. Disk full?\n"), type = MessageBox.TYPE_ERROR, timeout = 0, id = "DiskFullMessage")
327 # ok, the recording has been stopped. we need to properly note
328 # that in our state, with also keeping the possibility to re-try.
329 # TODO: this has to be done.
330 elif event == iRecordableService.evStart:
331 text = _("A record has been started:\n%s") % self.name
332 if self.dirnameHadToFallback:
333 text = '\n'.join([text, _("Please note that the previously selected media could not be accessed and therefore the default directory is being used instead.")])
335 # maybe this should be configurable?
336 Notifications.AddPopup(text = text, type = MessageBox.TYPE_INFO, timeout = 3)
338 # we have record_service as property to automatically subscribe to record service events
339 def setRecordService(self, service):
340 if self.__record_service is not None:
341 print "[remove callback]"
342 NavigationInstance.instance.record_event.remove(self.gotRecordEvent)
344 self.__record_service = service
346 if self.__record_service is not None:
347 print "[add callback]"
348 NavigationInstance.instance.record_event.append(self.gotRecordEvent)
350 record_service = property(lambda self: self.__record_service, setRecordService)
352 def createTimer(xml):
353 begin = int(xml.getAttribute("begin"))
354 end = int(xml.getAttribute("end"))
355 serviceref = ServiceReference(xml.getAttribute("serviceref").encode("utf-8"))
356 description = xml.getAttribute("description").encode("utf-8")
357 repeated = xml.getAttribute("repeated").encode("utf-8")
358 disabled = long(xml.getAttribute("disabled") or "0")
359 justplay = long(xml.getAttribute("justplay") or "0")
360 afterevent = str(xml.getAttribute("afterevent") or "nothing")
361 afterevent = { "nothing": AFTEREVENT.NONE, "standby": AFTEREVENT.STANDBY, "deepstandby": AFTEREVENT.DEEPSTANDBY }[afterevent]
362 if xml.hasAttribute("eit") and xml.getAttribute("eit") != "None":
363 eit = long(xml.getAttribute("eit"))
366 if xml.hasAttribute("location") and xml.getAttribute("location") != "None":
367 location = xml.getAttribute("location").encode("utf-8")
371 name = xml.getAttribute("name").encode("utf-8")
372 #filename = xml.getAttribute("filename").encode("utf-8")
373 entry = RecordTimerEntry(serviceref, begin, end, name, description, eit, disabled, justplay, afterevent, dirname = location)
374 entry.repeated = int(repeated)
376 for l in elementsWithTag(xml.childNodes, "log"):
377 time = int(l.getAttribute("time"))
378 code = int(l.getAttribute("code"))
379 msg = mergeText(l.childNodes).strip().encode("utf-8")
380 entry.log_entries.append((time, code, msg))
384 class RecordTimer(timer.Timer):
386 timer.Timer.__init__(self)
388 self.Filename = Directories.resolveFilename(Directories.SCOPE_CONFIG, "timers.xml")
393 print "unable to load timers from file!"
395 def isRecording(self):
397 for timer in self.timer_list:
398 if timer.isRunning() and not timer.justplay:
405 doc = xml.dom.minidom.parse(self.Filename)
406 except xml.parsers.expat.ExpatError:
407 from Tools.Notifications import AddPopup
408 from Screens.MessageBox import MessageBox
410 AddPopup(_("The timer file (timers.xml) is corrupt and could not be loaded."), type = MessageBox.TYPE_ERROR, timeout = 0, id = "TimerLoadFailed")
412 print "timers.xml failed to load!"
415 os.rename(self.Filename, self.Filename + "_old")
417 print "renaming broken timer failed"
420 root = doc.childNodes[0]
421 for timer in elementsWithTag(root.childNodes, "timer"):
422 self.record(createTimer(timer))
425 #doc = xml.dom.minidom.Document()
426 #root_element = doc.createElement('timers')
427 #doc.appendChild(root_element)
428 #root_element.appendChild(doc.createTextNode("\n"))
430 #for timer in self.timer_list + self.processed_timers:
431 # some timers (instant records) don't want to be saved.
435 #t = doc.createTextNode("\t")
436 #root_element.appendChild(t)
437 #t = doc.createElement('timer')
438 #t.setAttribute("begin", str(int(timer.begin)))
439 #t.setAttribute("end", str(int(timer.end)))
440 #t.setAttribute("serviceref", str(timer.service_ref))
441 #t.setAttribute("repeated", str(timer.repeated))
442 #t.setAttribute("name", timer.name)
443 #t.setAttribute("description", timer.description)
444 #t.setAttribute("eit", str(timer.eit))
446 #for time, code, msg in timer.log_entries:
447 #t.appendChild(doc.createTextNode("\t\t"))
448 #l = doc.createElement('log')
449 #l.setAttribute("time", str(time))
450 #l.setAttribute("code", str(code))
451 #l.appendChild(doc.createTextNode(msg))
453 #t.appendChild(doc.createTextNode("\n"))
455 #root_element.appendChild(t)
456 #t = doc.createTextNode("\n")
457 #root_element.appendChild(t)
460 #file = open(self.Filename, "w")
467 list.append('<?xml version="1.0" ?>\n')
468 list.append('<timers>\n')
470 for timer in self.timer_list + self.processed_timers:
474 list.append('<timer')
475 list.append(' begin="' + str(int(timer.begin)) + '"')
476 list.append(' end="' + str(int(timer.end)) + '"')
477 list.append(' serviceref="' + stringToXML(str(timer.service_ref)) + '"')
478 list.append(' repeated="' + str(int(timer.repeated)) + '"')
479 list.append(' name="' + str(stringToXML(timer.name)) + '"')
480 list.append(' description="' + str(stringToXML(timer.description)) + '"')
481 list.append(' afterevent="' + str(stringToXML({ AFTEREVENT.NONE: "nothing", AFTEREVENT.STANDBY: "standby", AFTEREVENT.DEEPSTANDBY: "deepstandby" }[timer.afterEvent])) + '"')
482 if timer.eit is not None:
483 list.append(' eit="' + str(timer.eit) + '"')
484 if timer.dirname is not None:
485 list.append(' location="' + str(stringToXML(timer.dirname)) + '"')
486 list.append(' disabled="' + str(int(timer.disabled)) + '"')
487 list.append(' justplay="' + str(int(timer.justplay)) + '"')
490 if config.recording.debug.value:
491 for time, code, msg in timer.log_entries:
493 list.append(' code="' + str(code) + '"')
494 list.append(' time="' + str(time) + '"')
496 list.append(str(stringToXML(msg)))
497 list.append('</log>\n')
499 list.append('</timer>\n')
501 list.append('</timers>\n')
503 file = open(self.Filename, "w")
508 def getNextZapTime(self):
510 for timer in self.timer_list:
511 if not timer.justplay or timer.begin < now:
516 def getNextRecordingTime(self):
518 for timer in self.timer_list:
519 if timer.justplay or timer.begin < now:
524 def record(self, entry):
526 print "[Timer] Record " + str(entry)
528 self.addTimerEntry(entry)
531 def isInTimer(self, eventid, begin, duration, service):
535 chktimecmp_end = None
536 end = begin + duration
537 for x in self.timer_list:
538 check = x.service_ref.ref.toCompareString() == str(service)
540 sref = x.service_ref.ref
541 parent_sid = sref.getUnsignedData(5)
542 parent_tsid = sref.getUnsignedData(6)
543 if parent_sid and parent_tsid: # check for subservice
544 sid = sref.getUnsignedData(1)
545 tsid = sref.getUnsignedData(2)
546 sref.setUnsignedData(1, parent_sid)
547 sref.setUnsignedData(2, parent_tsid)
548 sref.setUnsignedData(5, 0)
549 sref.setUnsignedData(6, 0)
550 check = x.service_ref.ref.toCompareString() == str(service)
554 event = eEPGCache.getInstance().lookupEventId(sref, eventid)
555 num = event and event.getNumOfLinkageServices() or 0
556 sref.setUnsignedData(1, sid)
557 sref.setUnsignedData(2, tsid)
558 sref.setUnsignedData(5, parent_sid)
559 sref.setUnsignedData(6, parent_tsid)
560 for cnt in range(num):
561 subservice = event.getLinkageService(sref, cnt)
562 if sref.toCompareString() == subservice.toCompareString():
566 #if x.eit is not None and x.repeated == 0:
567 # if x.eit == eventid:
571 chktime = localtime(begin)
572 chktimecmp = chktime.tm_wday * 1440 + chktime.tm_hour * 60 + chktime.tm_min
573 chktimecmp_end = chktimecmp + (duration / 60)
574 time = localtime(x.begin)
576 if x.repeated & (2 ** y):
577 timecmp = y * 1440 + time.tm_hour * 60 + time.tm_min
578 if timecmp <= chktimecmp < (timecmp + ((x.end - x.begin) / 60)):
579 time_match = ((timecmp + ((x.end - x.begin) / 60)) - chktimecmp) * 60
580 elif chktimecmp <= timecmp < chktimecmp_end:
581 time_match = (chktimecmp_end - timecmp) * 60
582 else: #if x.eit is None:
583 if begin <= x.begin <= end:
585 if time_match < diff:
587 elif x.begin <= begin <= x.end:
589 if time_match < diff:
593 def removeEntry(self, entry):
594 print "[Timer] Remove " + str(entry)
597 entry.repeated = False
600 # this sets the end time to current time, so timer will be stopped.
603 if entry.state != entry.StateEnded:
604 self.timeChanged(entry)
606 print "state: ", entry.state
607 print "in processed: ", entry in self.processed_timers
608 print "in running: ", entry in self.timer_list
609 # now the timer should be in the processed_timers list. remove it from there.
610 self.processed_timers.remove(entry)