add workaround to make sure box is running when timespam begins,
[enigma2-plugins.git] / epgrefresh / src / plugin.py
1 # Config
2 from Components.config import config, ConfigEnableDisable, ConfigInteger, ConfigSubsection, ConfigClock
3
4 # Calculate default begin/end
5 from time import localtime, mktime
6 now = [x for x in localtime()]
7 now[3] = 20
8 now[4] = 15
9 begin = mktime(now)
10 now[3] = 06
11 now[4] = 30
12 end = mktime(now)
13
14 config.plugins.epgrefresh = ConfigSubsection()
15 config.plugins.epgrefresh.enabled = ConfigEnableDisable(default = False)
16 config.plugins.epgrefresh.begin = ConfigClock(default = begin)
17 config.plugins.epgrefresh.end = ConfigClock(default = end)
18 config.plugins.epgrefresh.interval = ConfigInteger(default = 2, limits=(1, 10))
19 config.plugins.epgrefresh.delay_timespan = ConfigInteger(default = 60, limits=(5, 300))
20 config.plugins.epgrefresh.delay_standby = ConfigInteger(default = 10, limits=(1, 60))
21 config.plugins.epgrefresh.inherit_autotimer = ConfigEnableDisable(default = False)
22 config.plugins.epgrefresh.afterevent = ConfigEnableDisable(default = False)
23 config.plugins.epgrefresh.force = ConfigEnableDisable(default = False)
24 config.plugins.epgrefresh.lastscan = ConfigInteger(default = 0)
25
26 # Plugin
27 from EPGRefresh import epgrefresh
28 from EPGRefreshConfiguration import EPGRefreshConfiguration
29
30 # Plugin definition
31 from Plugins.Plugin import PluginDescriptor
32
33 # Autostart
34 def autostart(reason, **kwargs):
35         if config.plugins.epgrefresh.enabled.value and reason == 0 and kwargs.has_key("session"):
36                 epgrefresh.start(kwargs["session"])
37
38                 # Remove a fake timer we might have created at last shutdown
39                 for record in kwargs["session"].nav.RecordTimer.timer_list:
40                         if record.name == "EPGRefresh Help Timer":
41                                 kwargs["session"].nav.RecordTimer.removeEntry(record)
42                                 break
43         elif reason == 1:
44                 epgrefresh.stop()
45                 if config.plugins.epgrefresh.enabled.value:
46                         # Workaround until a better solution comes up:
47                         # add a fake zap-timer if the box might not be up when our timespan begins
48
49                         #get next record timer start time
50                         nextRecordingTime = session.nav.RecordTimer.getNextRecordingTime()
51                         #get next zap timer start time
52                         nextZapTime = session.nav.RecordTimer.getNextZapTime()
53                         if nextZapTime != -1 and nextRecordingTime != -1:
54                                 startTime = nextZapTime < nextRecordingTime and nextZapTime or nextRecordingTime
55                         else:
56                                 startTime = nextZapTime != -1 and nextZapTime or nextRecordingTime
57
58                         now = localtime()
59                         begin = mktime(
60                                 (now.tm_year, now.tm_mon, now.tm_mday,
61                                 config.plugins.epgrefresh.begin.value[0],
62                                 config.plugins.epgrefresh.begin.value[1],
63                                 0, now.tm_wday, now.tm_yday, now.tm_isdst)
64                         )
65
66                         # If no recording scheduled or next scheduled recording after begin of timespan
67                         if startTime == -1 or startTime > begin:
68                                 from RecordTimer import RecordTimerEntry
69                                 from ServiceReference import ServiceReference
70                                 import NavigationInstance
71                                 fakeEntry = RecordTimerEntry(
72                                         ServiceReference(
73                                                 NavigationInstance.instance.getCurrentlyPlayingServiceReference()
74                                         ),
75                                         begin,
76                                         begin,
77                                         "EPGRefresh Help Timer",
78                                         "EPGRefresh Help Timer",
79                                         -1,
80                                         justplay = True
81                                 )
82                                 NavigationInstance.instance.RecordTimer.record(fakeEntry)
83
84 # Mainfunction
85 def main(session, **kwargs):
86         epgrefresh.stop()
87         session.openWithCallback(
88                 doneConfiguring,
89                 EPGRefreshConfiguration
90         )
91
92 def doneConfiguring(session, **kwargs):
93         if config.plugins.epgrefresh.enabled.value:
94                 epgrefresh.start(session)
95
96 def Plugins(**kwargs):
97         return [
98                 PluginDescriptor(name="EPGRefresh", description = "Automated EPGRefresher", where = [PluginDescriptor.WHERE_AUTOSTART, PluginDescriptor.WHERE_SESSIONSTART], fnc = autostart),
99                 PluginDescriptor(name="EPGRefresh", description = "Automated EPGRefresher", where = PluginDescriptor.WHERE_PLUGINMENU, fnc = main)
100         ]