From becfb7646483edc829f9e7eecd7a2589449f8e26 Mon Sep 17 00:00:00 2001 From: Gunter Tim Date: Sun, 25 Oct 2020 12:41:04 -0700 Subject: [PATCH] Added support for context management --- meshtastic/__init__.py | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/meshtastic/__init__.py b/meshtastic/__init__.py index 340adc0..e9c2f84 100644 --- a/meshtastic/__init__.py +++ b/meshtastic/__init__.py @@ -105,6 +105,16 @@ class MeshInterface: if not noProto: self._startConfig() + def __enter__(self): + return self + + def __exit__(self, exc_type, exc_value, traceback): + if exc_type is not None and exc_value is not None: + logging.error(f'An exception of type {exc_type} with value {exc_value} has occurred') + if traceback is not None: + logging.error(f'Traceback: {traceback}') + self.close() + def sendText(self, text, destinationId=BROADCAST_ADDR, wantAck=False, wantResponse=False): """Send a utf8 string to some other node, if the node has a display it will also be shown on the device.