Skip to content

Commit e86a495

Browse files
authored
Merge pull request #899 from dim5x/master
Refactor the Meshtastic TCP pub/sub example to ensure proper resource cleanup and clearer exception handling.
2 parents 5dde67e + 3ea1994 commit e86a495

1 file changed

Lines changed: 9 additions & 6 deletions

File tree

examples/pub_sub_example2.py

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,9 @@
55
import sys
66
import time
77

8-
from pubsub import pub
8+
from meshtastic.tcp_interface import TCPInterface
99

10-
import meshtastic
11-
import meshtastic.tcp_interface
10+
from pubsub import pub
1211

1312
# simple arg check
1413
if len(sys.argv) < 2:
@@ -29,11 +28,15 @@ def onConnection(interface, topic=pub.AUTO_TOPIC): # pylint: disable=unused-arg
2928

3029
pub.subscribe(onReceive, "meshtastic.receive")
3130
pub.subscribe(onConnection, "meshtastic.connection.established")
31+
32+
iface=None
3233
try:
33-
iface = meshtastic.tcp_interface.TCPInterface(hostname=sys.argv[1])
34+
iface = TCPInterface(hostname=sys.argv[1])
3435
while True:
3536
time.sleep(1000)
36-
iface.close()
3737
except Exception as ex:
3838
print(f"Error: Could not connect to {sys.argv[1]} {ex}")
39-
sys.exit(1)
39+
raise
40+
finally:
41+
if iface:
42+
iface.close()

0 commit comments

Comments
 (0)