Module watchdogTimer
[hide private]
[frames] | no frames]

Source Code for Module watchdogTimer

  1  #!/usr/bin/env python 
  2   
  3  import sys 
  4  import time 
  5   
  6  from python_qt_binding import QtCore, QtGui; 
  7  from QtCore import QTimer; 
  8  from QtGui import QApplication; 
  9   
10 -class WatchdogTimer(QTimer):
11
12 - def __init__(self, timeout=1, callback=None, callbackArg=None, selfTest=False):
13 ''' 14 Create watchdog. 15 @param timeout: number of seconds that are set each time the timer is kicked. 16 @type timeout: float 17 @param callback: callable to invoke when timer expires. 18 @type callback: Python callable 19 @param callbackArg: any argument to pass to the callback 20 @type callbackArg: any 21 @param selfTest: set to true if testing the class 22 @type selfTest: boolean 23 ''' 24 super(WatchdogTimer,self).__init__(); 25 # Use leading underscore to avoid 26 # overwriting parent's 'timeout' signal. 27 # Internally, work with milliseconds: 28 self._timeout = int(1000 * timeout); 29 self.callback = callback; 30 self.callbackArg = callbackArg; 31 32 self.timedout = True; 33 self.stopped = True; 34 35 self.setSingleShot(True); 36 self.timeout.connect(self.timerExpiredHandler); 37 38 if selfTest: 39 self.runSelfTest();
40 #sys.exit(); 41
42 - def timedOut(self):
43 return self.timedout;
44
45 - def kick(self, _timeout=None, callback=None, callbackArg=None):
46 ''' 47 (Re)-start the timer 48 @param _timeout: timeout in seconds 49 @type _timeout: float 50 @param callback: Python callable to invoke when timer expires. 51 @type callback: callable 52 @param callbackArg: argument to pass to callback 53 @type callbackArg: any 54 ''' 55 self.timedout = False; 56 if _timeout is None: 57 _timeout = self._timeout; 58 else: 59 _timeout = int(1000 * _timeout); 60 if callback is not None: 61 self.callback = callback; 62 if callbackArg is not None: 63 self.callbackArg = callbackArg; 64 self.stopped = False; 65 self.start(_timeout);
66
67 - def stop(self):
68 super(WatchdogTimer, self).stop(); 69 self.timedout = False; 70 self.stopped = True;
71
72 - def changeTimeout(self, newTimeout):
73 self._timeout = int(1000 * newTimeout);
74
75 - def changeCallback(self, newCallback):
76 self.callback = newCallback;
77
78 - def changeCallbackArg(self, newArg):
79 self.callbackArg = newArg;
80
81 - def timerExpiredHandler(self):
82 self.stop(); 83 self.timedout = True; 84 if self.callback is not None: 85 if self.callbackArg is None: 86 self.callback(); 87 else: 88 self.callback(self.callbackArg);
89 90 # ----------------------------- Testing ---------------------------- 91
92 - def runSelfTest(self):
93 self.changeCallback(self.testTimeoutHandler); 94 self.kick();
95
96 - def testTimeoutHandler(self):
97 print("Timed out."); 98 self.kick(); 99 print("Stop test: stopped == %s" % self.stopped); 100 self.kick(); 101 print("After kick: stopped == %s" % self.stopped); 102 self.stop(); 103 if self.stopped: 104 print("Timer successfully stopped."); 105 else: 106 print("Bad: timer not stopped.");
107 108 if __name__ == '__main__': 109 110 app = QtGui.QApplication(sys.argv) 111 dog = WatchdogTimer(selfTest=True) 112 sys.exit(app.exec_()) 113