1
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
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
26
27
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
41
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
68 super(WatchdogTimer, self).stop();
69 self.timedout = False;
70 self.stopped = True;
71
73 self._timeout = int(1000 * newTimeout);
74
76 self.callback = newCallback;
77
79 self.callbackArg = newArg;
80
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
91
95
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