Reliable reconnect in user script
This commit is contained in:
parent
a91eb91ac6
commit
2378f52a7e
22
example.py
22
example.py
|
@ -1,23 +1,35 @@
|
||||||
import time
|
import time
|
||||||
from sem6000 import SEMSocket
|
from sem6000 import SEMSocket
|
||||||
|
|
||||||
|
import bluepy
|
||||||
|
|
||||||
|
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:
|
# auto_reconnect_timeout enabled auto reconnect if sending a command fails. Valid values:
|
||||||
# None (default): everything that fails throws NotConnectedException's
|
# None (default): everything that fails throws NotConnectedException's
|
||||||
# -1: infinite retries
|
# -1: infinite retries
|
||||||
# integer: seconds before exception is thrown
|
# integer: seconds before exception is thrown
|
||||||
|
|
||||||
socket = SEMSocket('f0:c7:7f:0d:e7:17', auto_reconnect_timeout=None)
|
socket = SEMSocket('f0:c7:7f:0d:e7:17', auto_reconnect_timeout=None)
|
||||||
|
print("Connected.")
|
||||||
|
|
||||||
#socket.login("1337")
|
#socket.login("1337")
|
||||||
#socket.changePassword("1234")
|
#socket.changePassword("1234")
|
||||||
#socket.login("1234")
|
#socket.login("1234")
|
||||||
|
|
||||||
while True:
|
|
||||||
time.sleep(1)
|
|
||||||
try:
|
|
||||||
socket.getStatus()
|
socket.getStatus()
|
||||||
socket.setStatus(True)
|
socket.setStatus(True)
|
||||||
print("=== {} ({}) ===".format(socket.mac_address, "on" if socket.powered else "off"))
|
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))
|
print("\t{}V {}A → {}W@{}Hz".format(socket.voltage, socket.current, socket.power, socket.frequency))
|
||||||
except SEMSocket.NotConnectedException:
|
except (SEMSocket.NotConnectedException, bluepy.btle.BTLEDisconnectError):
|
||||||
socket.reconnect(-1) #infinite reconnect attempts
|
print("Restarting...")
|
||||||
|
if socket != None:
|
||||||
|
socket.disconnect()
|
||||||
|
socket = None
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue