8000 Configurable init state for timers. add start function. · safufude/lib-python@5dca229 · GitHub
[go: up one dir, main page]

Skip to content

Commit 5dca229

Browse files
author
amorozenko
committed
Configurable init state for timers. add start function.
1 parent 60744c5 commit 5dca229

File tree

1 file changed

+11
-3
lines changed

1 file changed

+11
-3
lines changed

blynktimer.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,13 +55,14 @@ def register(blynk, *args, **kwargs):
5555
# kwargs with defaults are used cause PEP 3102 no supported by Python2
5656
interval = kwargs.pop('interval', DEFAULT_INTERVAL)
5757
run_once = kwargs.pop('run_once', False)
58+
stopped = kwargs.pop('stopped', False)
5859

5960
class Deco(object):
6061
def __init__(self, func):
6162
self.func = func
6263
if len(list(Timer.timers.keys())) >= MAX_TIMERS:
6364
raise TimerError('Max allowed timers num={}'.format(MAX_TIMERS))
64-
_timer = _Timer(interval, func, run_once, *args, **kwargs)
65+
_timer = _Timer(interval, func, run_once, stopped, *args, **kwargs)
6566
Timer.timers['{}_{}'.format(len(list(Timer.timers.keys())), blynk._get_func_name(func))] = _timer
6667

6768
def __call__(self, *f_args, **f_kwargs):
@@ -76,6 +77,13 @@ def stop(t_id):
7677
raise TimerError('Timer id={} not found'.format(t_id))
7778
Timer.timers[t_id].stopped = True
7879

80+
@staticmethod
81+
def start(t_id):
82+
timer = Timer.timers.get(t_id, None)
83+
if timer is None:
84+
raise TimerError('Timer id={} not found'.format(t_id))
85+
Timer.timers[t_id].stopped = False
86+
7987
@staticmethod
8088
def is_stopped(t_id):
8189
timer = Timer.timers.get(t_id, None)
@@ -96,15 +104,15 @@ def run(self):
96104

97105

98106
class _Timer(object):
99-
def __init__(self, interval, deco, run_once, *args, **kwargs):
107+
def __init__(self, interval, deco, run_once, stopped, *args, **kwargs):
100108
self.interval = interval
101109
self.deco = deco
102110
self.args = args
103111
self.run_once = run_once
104112
self.kwargs = kwargs
105113
self.fire_time = None
106114
self.fire_time_prev = None
107-
self.stopped = False
115+
self.stopped = stopped
108116

109117
def run(self):
110118
timer_real_interval = 0

0 commit comments

Comments
 (0)
0