its mandatory to call deleteDialog for the pip screen
[enigma2-plugins.git] / epgrefresh / src / EPGRefreshTimer.py
1 from __future__ import print_function
2
3 # To check if in Standby
4 import Screens.Standby
5
6 # Base Class
7 import timer
8
9 # To see if in Timespan and to determine begin of timespan
10 from time import localtime, mktime, time, strftime
11
12 # Config
13 from Components.config import config
14
15 def checkTimespan(begin, end):
16         # Get current time
17         time = localtime()
18
19         # Check if we span a day
20         if begin[0] > end[0] or (begin[0] == end[0] and begin[1] >= end[1]):
21                 # Check if begin of event is later than our timespan starts
22                 if time.tm_hour > begin[0] or (time.tm_hour == begin[0] and time.tm_min >= begin[1]):
23                         # If so, event is in our timespan
24                         return True
25                 # Check if begin of event is earlier than our timespan end
26                 if time.tm_hour < end[0] or (time.tm_hour == end[0] and time.tm_min <= end[1]):
27                         # If so, event is in our timespan
28                         return True
29                 return False
30         else:
31                 # Check if event begins earlier than our timespan starts
32                 if time.tm_hour < begin[0] or (time.tm_hour == begin[0] and time.tm_min < begin[1]):
33                         # Its out of our timespan then
34                         return False
35                 # Check if event begins later than our timespan ends
36                 if time.tm_hour > end[0] or (time.tm_hour == end[0] and time.tm_min > end[1]):
37                         # Its out of our timespan then
38                         return False
39                 return True
40
41 class EPGRefreshTimerEntry(timer.TimerEntry):
42         """TimerEntry ..."""
43         def __init__(self, begin, tocall, nocheck = False):
44                 timer.TimerEntry.__init__(self, int(begin), int(begin))
45
46                 self.function = tocall
47                 self.nocheck = nocheck
48                 if nocheck:
49                         self.state = self.StatePrepared
50
51         def getNextActivation(self):
52                 # We delay our activation so we won't rush into reprocessing a repeating one
53                 return self.begin+1
54
55         def activate(self):
56                 if self.state == self.StateWaiting:
57                         # Check if in timespan
58                         if checkTimespan(config.plugins.epgrefresh.begin.value, config.plugins.epgrefresh.end.value):
59                                 print("[EPGRefresh] In Timespan, will check if we're in Standby and have no Recordings running next")
60                                 # Do we realy want to check nav?
61                                 from NavigationInstance import instance
62                                 if config.plugins.epgrefresh.force.value or (Screens.Standby.inStandby and instance is not None and not instance.RecordTimer.isRecording()):
63                                         return True
64                                 else:
65                                         print("[EPGRefresh] Box still in use, rescheduling")
66
67                                         # Recheck later
68                                         self.begin = time() + config.plugins.epgrefresh.delay_standby.value*60
69                                         return False
70                         else:
71                                 print("[EPGRefresh] Not in timespan, ending timer")
72                                 self.state = self.StateEnded
73                                 return False
74                 elif self.state == self.StateRunning:
75                         self.function()
76
77                 return True
78
79         def resetState(self):
80                 self.state = self.StateWaiting
81                 self.cancelled = False
82                 self.timeChanged()
83
84         def timeChanged(self):
85                 if self.nocheck and self.state < self.StateRunning:
86                         self.state = self.StatePrepared
87
88         def shouldSkip(self):
89                 return False
90
91         def __repr__(self):
92                 return ''.join((
93                                 "<EPGRefreshTimerEntry (",
94                                 ', '.join((
95                                         strftime("%c", localtime(self.begin)),
96                                         str(self.repeated),
97                                         str(self.function)
98                                 )),
99                                 ")>"
100                         ))
101
102 class EPGRefreshTimer(timer.Timer):
103         def __init__(self):
104                 timer.Timer.__init__(self)
105
106         def remove(self, entry):
107                 print("[EPGRefresh] Timer removed " + str(entry))
108
109                 # avoid re-enqueuing
110                 entry.repeated = False
111
112                 # abort timer.
113                 # this sets the end time to current time, so timer will be stopped.
114                 entry.abort()
115
116                 if entry.state != entry.StateEnded:
117                         self.timeChanged(entry)
118
119                 print("state: ", entry.state)
120                 print("in processed: ", entry in self.processed_timers)
121                 print("in running: ", entry in self.timer_list)
122                 # now the timer should be in the processed_timers list. remove it from there.
123                 self.processed_timers.remove(entry)
124
125         def setRefreshTimer(self, tocall):
126                 # Add refresh Timer
127                 now = localtime()
128                 begin = mktime(
129                         (now.tm_year, now.tm_mon, now.tm_mday,
130                         config.plugins.epgrefresh.begin.value[0],
131                         config.plugins.epgrefresh.begin.value[1],
132                         0, now.tm_wday, now.tm_yday, now.tm_isdst)
133                 )
134
135                 # If the last scan was finished before our timespan begins/began and
136                 # timespan began in the past fire the timer once (timer wouldn't do so
137                 # by itself)
138                 if config.plugins.epgrefresh.lastscan.value < begin and begin < time():
139                         tocall()
140
141                 refreshTimer = EPGRefreshTimerEntry(begin, tocall, nocheck = True)
142
143                 i = 0
144                 while i < 7:
145                         refreshTimer.setRepeated(i)
146                         i += 1
147
148                 # We can be sure that whenever this function is called the timer list
149                 # was wiped, so just add a new timer
150                 self.addTimerEntry(refreshTimer)
151
152         def add(self, entry):
153                 entry.timeChanged()
154                 print("[EPGRefresh] Timer added " + str(entry))
155                 self.addTimerEntry(entry)
156
157         def clear(self):
158                 self.timer_list = []
159
160         def isActive(self):
161                 return len(self.timer_list) > 0
162               
163         def calcNextActivation(self):
164                 # FIXME FIXME FIXME
165                 # Dr.Best 09-04-2014 --> good solution?
166                 # if eDVBLocalTimeHandler is updating the transponder time (negativ), the timewarp code in the orginal calcNextActivation() method is the reason why epgrefresh triggers all remaining refresh timers at once.
167                 # I believe the timewarp code is not necessary here in the EPGRefreshTimer class, so I removed it.
168                 # FIXME FIXME FIXME
169                 
170                 self.processActivation()
171                 self.lastActivation = time()
172         
173                 min = int(time()) + self.MaxWaitTime
174                 
175                 # calculate next activation point
176                 timer_list = [ t for t in self.timer_list if not t.disabled ]
177                 if timer_list:
178                         w = timer_list[0].getNextActivation()
179                         if w < min:
180                                 min = w
181                         else:
182                                 print ("[EPGRefreshTimer] next real activation is %s" % (strftime("%c", localtime(w))))
183                 
184                 self.setNextActivation(min)      
185
186 epgrefreshtimer = EPGRefreshTimer()