try to avoid multiple refreshs per day,
[enigma2-plugins.git] / epgrefresh / src / EPGRefreshTimer.py
1 # To check if in Standby
2 import Screens.Standby
3
4 # Base Class
5 import timer
6
7 # To see if in Timespan and to determine begin of timespan
8 from time import localtime, mktime, time
9
10 # Config
11 from Components.config import config
12
13 def checkTimespan(begin, end):
14         # Get current time
15         time = localtime()
16
17         # Check if we span a day
18         if begin[0] > end[0] or (begin[0] == end[0] and begin[1] >= end[1]):
19                 # Check if begin of event is later than our timespan starts
20                 if time[3] > begin[0] or (time[3] == begin[0] and time[4] >= begin[1]):
21                         # If so, event is in our timespan
22                         return True
23                 # Check if begin of event is earlier than our timespan end
24                 if time[3] < end[0] or (time[3] == end[0] and time[4] <= end[1]):
25                         # If so, event is in our timespan
26                         return True
27                 return False
28         else:
29                 # Check if event begins earlier than our timespan starts 
30                 if time[3] < begin[0] or (time[3] == begin[0] and time[4] <= begin[1]):
31                         # Its out of our timespan then
32                         return False
33                 # Check if event begins later than our timespan ends
34                 if time[3] > end[0] or (time[3] == end[0] and time[4] >= end[1]):
35                         # Its out of our timespan then
36                         return False
37                 return True
38
39 class EPGRefreshTimerEntry(timer.TimerEntry):
40         """TimerEntry ..."""
41         def __init__(self, begin, tocall, nocheck = False):
42                 timer.TimerEntry.__init__(self, int(begin), int(begin))
43
44                 self.prepare_time = 0
45                 self.function = tocall
46                 self.nocheck = nocheck
47
48         def getNextActivation(self):
49                 return self.begin
50
51         def activate(self):
52                 if self.state == self.StateRunning:
53                         # Just execute function and signalize success if told to
54                         if self.nocheck:
55                                 self.function()
56                                 return True
57
58                         # Check if in timespan
59                         if checkTimespan(config.plugins.epgrefresh.begin.value, config.plugins.epgrefresh.end.value):
60                                 print "[EPGRefresh] In Timespan, will check if we're in Standby and have no Recordings running next"
61                                 # Do we realy want to check nav?
62                                 from NavigationInstance import instance
63                                 if config.plugins.epgrefresh.force.value or (Screens.Standby.inStandby and instance is not None and not instance.RecordTimer.isRecording()):
64                                         self.function()
65                                         return True
66                                 else:
67                                         print "[EPGRefresh] Box still in use, rescheduling"     
68
69                                         # Recheck later
70                                         self.begin = time() + config.plugins.epgrefresh.delay_standby.value*60
71                                         return False
72                         else:
73                                 print "[EPGRefresh] Not in timespan, ending timer"
74                                 return True
75
76                 return True
77
78         def shouldSkip(self):
79                 return False
80
81 class EPGRefreshTimer(timer.Timer):
82         def __init__(self):
83                 timer.Timer.__init__(self)
84
85         def remove(self, entry):
86                 print "[EPGRefresh] Timer removed " + str(entry)
87
88                 # avoid re-enqueuing
89                 entry.repeated = False
90
91                 # abort timer.
92                 # this sets the end time to current time, so timer will be stopped.
93                 entry.abort()
94
95                 if entry.state != entry.StateEnded:
96                         self.timeChanged(entry)
97
98                 print "state: ", entry.state
99                 print "in processed: ", entry in self.processed_timers
100                 print "in running: ", entry in self.timer_list
101                 # now the timer should be in the processed_timers list. remove it from there.
102                 self.processed_timers.remove(entry)
103
104         def setRefreshTimer(self, tocall):
105                 # Add refresh Timer
106                 begin = [x for x in localtime()]
107                 begin[3] = config.plugins.epgrefresh.begin.value[0]
108                 begin[4] = config.plugins.epgrefresh.begin.value[1]
109                 begin = mktime(begin)
110
111                 # If the last scan was finished before our timespan begins/began and
112                 # timespan began in the past fire the timer once (timer wouldn't do so
113                 # by itself)
114                 if config.plugins.epgrefresh.lastscan.value < begin and begin < time():
115                         tocall()
116
117                 refreshTimer = EPGRefreshTimerEntry(begin, tocall, nocheck = True)
118
119                 for x in range(0,7):
120                         refreshTimer.setRepeated(x)
121
122                 # We can be sure that whenever this function is called the timer list
123                 # was wiped, so just add a new timer
124                 self.addTimerEntry(refreshTimer)
125
126         def add(self, entry):
127                 entry.timeChanged()
128                 print "[EPGRefresh] Timer added " + str(entry)
129                 self.addTimerEntry(entry)
130
131         def clear(self):
132                 self.timer_list = []
133
134         def isActive(self):
135                 return len(self.timer_list) > 0
136
137 epgrefreshtimer = EPGRefreshTimer()