2 Slixmpp: The Slick XMPP Library
3 Implementation of xeps for Internet of Things
4 http://wiki.xmpp.org/web/Tech_pages/IoT_systems
5 Copyright (C) 2013 Sustainable Innovation, Joachim.lindborg@sust.se, bjorn.westrom@consoden.se
6 This file is part of Slixmpp.
8 See the file LICENSE for copying permission.
10 from threading
import Thread
, Event
, Timer
13 def TimerReset(*args
, **kwargs
):
14 """ Global function for Timer """
15 return _TimerReset(*args
, **kwargs
)
18 class _TimerReset(Thread
):
19 """Call a function after a specified number of seconds:
21 t = TimerReset(30.0, f, args=[], kwargs={})
23 t.cancel() # stop the timer's action if it's still waiting
26 def __init__(self
, interval
, function
, args
=[], kwargs
={}):
28 self
.interval
= interval
29 self
.function
= function
32 self
.finished
= Event()
36 """Stop the timer if it hasn't finished yet"""
40 #print "Time: %s - timer running..." % time.asctime()
43 #print "Time: %s - timer waiting for timeout in %.2f..." % (time.asctime(), self.interval)
45 self
.finished
.wait(self
.interval
)
47 if not self
.finished
.isSet():
48 self
.function(*self
.args
, **self
.kwargs
)
50 #print "Time: %s - timer finished!" % time.asctime()
52 def reset(self
, interval
=None):
53 """ Reset the timer """
56 #print "Time: %s - timer resetting to %.2f..." % (time.asctime(), interval)
57 self
.interval
= interval
59 #print "Time: %s - timer resetting..." % time.asctime()