Skip to content
This repository was archived by the owner on Oct 20, 2024. It is now read-only.

Commit 8eaf7f3

Browse files
committed
cancel timer when done
1 parent 8cff636 commit 8eaf7f3

File tree

1 file changed

+11
-7
lines changed

1 file changed

+11
-7
lines changed

lirc_watcher.py

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ def on_mqtt_connect(mqtt, userdata, flags, rc):
3939

4040

4141
prev_data = None
42-
t = None
42+
timer = None
4343

4444
mqtt = paho.Client(MQTT_ID)
4545
mqtt.on_connect = on_mqtt_connect
@@ -55,7 +55,11 @@ def on_mqtt_connect(mqtt, userdata, flags, rc):
5555

5656

5757
def send_code(priority_data=None):
58-
global prev_data, mqtt
58+
global prev_data, mqtt, timer
59+
60+
if timer is not None and timer.is_alive():
61+
timer.cancel()
62+
timer = None
5963

6064
if priority_data is not None or prev_data is not None:
6165
to_send = priority_data if priority_data is not None else prev_data
@@ -104,7 +108,7 @@ def send_code(priority_data=None):
104108
else:
105109
if new_data:
106110
new_data = new_data.decode("utf-8")
107-
# print("new_data: ", new_data)
111+
print("new_data: ", new_data)
108112
counter_str = new_data.split()
109113

110114
"""
@@ -115,11 +119,11 @@ def send_code(priority_data=None):
115119

116120
prev_data = new_data
117121

118-
if t is not None and t.is_alive():
119-
t.cancel()
122+
if timer is not None and timer.is_alive():
123+
timer.cancel()
120124

121-
t = Timer(READ_TIMEOUT, send_code)
122-
t.start()
125+
timer = Timer(READ_TIMEOUT, send_code)
126+
timer.start()
123127

124128

125129
except KeyboardInterrupt:

0 commit comments

Comments
 (0)