From 2378f52a7e802cc4f542194249fa37d84c6b62cf Mon Sep 17 00:00:00 2001 From: Thomas Kolb Date: Thu, 16 Jan 2020 14:50:48 +0100 Subject: [PATCH] Reliable reconnect in user script --- example.py | 34 +++++++++++++++++++++++----------- 1 file changed, 23 insertions(+), 11 deletions(-) diff --git a/example.py b/example.py index ea3e874..16d4aed 100644 --- a/example.py +++ b/example.py @@ -1,23 +1,35 @@ import time from sem6000 import SEMSocket -# auto_reconnect_timeout enabled auto reconnect if sending a command fails. Valid values: -# None (default): everything that fails throws NotConnectedException's -# -1: infinite retries -# integer: seconds before exception is thrown +import bluepy -socket = SEMSocket('f0:c7:7f:0d:e7:17', auto_reconnect_timeout=None) - -#socket.login("1337") -#socket.changePassword("1234") -#socket.login("1234") +socket = None while True: time.sleep(1) try: + if socket == None: + print("Connecting...") + + # auto_reconnect_timeout enabled auto reconnect if sending a command fails. Valid values: + # None (default): everything that fails throws NotConnectedException's + # -1: infinite retries + # integer: seconds before exception is thrown + + socket = SEMSocket('f0:c7:7f:0d:e7:17', auto_reconnect_timeout=None) + print("Connected.") + + #socket.login("1337") + #socket.changePassword("1234") + #socket.login("1234") + socket.getStatus() socket.setStatus(True) print("=== {} ({}) ===".format(socket.mac_address, "on" if socket.powered else "off")) print("\t{}V {}A → {}W@{}Hz".format(socket.voltage, socket.current, socket.power, socket.frequency)) - except SEMSocket.NotConnectedException: - socket.reconnect(-1) #infinite reconnect attempts + except (SEMSocket.NotConnectedException, bluepy.btle.BTLEDisconnectError): + print("Restarting...") + if socket != None: + socket.disconnect() + socket = None + -- 2.45.2