[WebIf]: Fix some bugs in timer handling
[enigma2-plugins.git] / pushservice / src / PushService.py
1 #######################################################################
2 #
3 #    Push Service for Enigma-2
4 #    Coded by betonme (c) 2012 <glaserfrank(at)gmail.com>
5 #    Support: http://www.i-have-a-dreambox.com/wbb2/thread.php?threadid=167779
6 #
7 #    This program is free software; you can redistribute it and/or
8 #    modify it under the terms of the GNU General Public License
9 #    as published by the Free Software Foundation; either version 2
10 #    of the License, or (at your option) any later version.
11 #
12 #    This program is distributed in the hope that it will be useful,
13 #    but WITHOUT ANY WARRANTY; without even the implied warranty of
14 #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 #    GNU General Public License for more details.
16 #
17 #######################################################################
18
19 import os
20 import sys, traceback
21 from time import localtime
22
23 from Components.config import config
24 from enigma import eTimer
25 from Tools.BoundFunction import boundFunction
26
27
28 # Plugin internal
29 from . import _
30 from PushServiceBase import PushServiceBase
31
32
33 # States
34 (PSBOOT, PSBOOTRUN, PSFIRST, PSFIRSTRUN, PSCYCLE) = range(5)
35 #MABE use an enum http://stackoverflow.com/questions/36932/whats-the-best-way-to-implement-an-enum-in-python
36
37
38 #######################################################
39 # Logical part
40 class PushService(PushServiceBase):
41
42         def __init__(self):
43                 PushServiceBase.__init__(self)
44                 
45                 self.state = PSBOOT if config.pushservice.runonboot.value else PSFIRST
46                 
47                 self.timer = eTimer()
48                 self.timer.callback.append(self.do)
49                 
50                 # Read XML file, parse it and instantiate configured plugins
51                 self.load()
52                 
53                 #TODO Run in a new thread
54
55
56         ######################################
57         # Statemachine and timer
58         def start(self):
59                 print "[PushService] start"
60                 self.stopTimer()
61                 
62                 self.begin()
63                 self.next()
64
65         def stop(self):
66                 print "[PushService] stop"
67                 self.stopTimer()
68                 
69                 self.end()
70                 self.state = PSFIRST
71
72         def next(self, state = None):
73                 if state: self.state = state
74                 print "[PushService] next", self.state
75                 
76                 if self.state == PSBOOT:
77                         self.startTimer( 10, PSBOOTRUN )
78                 
79                 elif self.state == PSBOOTRUN \
80                         or self.state == PSFIRST:
81                         cltime = config.pushservice.time.value
82                         lotime = localtime()
83                         ltime = lotime[3]*60 + lotime[4]
84                         ctime = cltime[0]*60 + cltime[1]
85                         seconds = 60 * abs(ctime - ltime)
86                         self.startTimer( seconds, PSFIRSTRUN )
87                 
88                 elif self.state == PSFIRSTRUN \
89                         or self.state == PSCYCLE:
90                         period = config.pushservice.period.value
91                         if period > 0:
92                                 self.startTimer( int(period)*60*60, PSCYCLE )
93
94         def do(self):
95                 self.run()
96                 self.next()
97
98         def startTimer(self, seconds, state=None):
99                 if state: self.state = state
100                 self.timer.startLongTimer( seconds )
101
102         def stopTimer(self):
103                 if self.timer.isActive():
104                         self.timer.stop()