Added ability to reconnect to a string or list

This commit is contained in:
Brikwerk 2020-08-11 20:23:50 -07:00
commit d0c109d9aa

View file

@ -55,7 +55,7 @@ class ControllerServer():
:param reconnect_address: The Bluetooth MAC address of a :param reconnect_address: The Bluetooth MAC address of a
previously connected to Nintendo Switch, defaults to None previously connected to Nintendo Switch, defaults to None
:type reconnect_address: string, optional :type reconnect_address: string or list, optional
""" """
self.state["state"] = "initializing" self.state["state"] = "initializing"
@ -70,7 +70,6 @@ class ControllerServer():
self.controller.setup() self.controller.setup()
if reconnect_address: if reconnect_address:
print("reconnecting")
itr, ctrl = self.reconnect(reconnect_address) itr, ctrl = self.reconnect(reconnect_address)
else: else:
itr, ctrl = self.connect() itr, ctrl = self.connect()
@ -84,9 +83,11 @@ class ControllerServer():
self.mainloop(itr, ctrl) self.mainloop(itr, ctrl)
except Exception: except KeyboardInterrupt:
pass
except Exception as e:
self.state["state"] = "crashed" self.state["state"] = "crashed"
self.state["errors"] = traceback.format_exc() self.state["errors"] = str(e)
return self.state return self.state
def mainloop(self, itr, ctrl): def mainloop(self, itr, ctrl):
@ -286,24 +287,52 @@ class ControllerServer():
"""Attempts to reconnect with a Switch at the given address. """Attempts to reconnect with a Switch at the given address.
:param reconnect_address: The Bluetooth MAC address of the Switch :param reconnect_address: The Bluetooth MAC address of the Switch
:type reconnect_address: string :type reconnect_address: string or list
""" """
def recreate_sockets():
# Creating control and interrupt sockets
ctrl = socket.socket(
family=socket.AF_BLUETOOTH,
type=socket.SOCK_SEQPACKET,
proto=socket.BTPROTO_L2CAP)
itr = socket.socket(
family=socket.AF_BLUETOOTH,
type=socket.SOCK_SEQPACKET,
proto=socket.BTPROTO_L2CAP)
return itr, ctrl
self.state["state"] = "reconnecting" self.state["state"] = "reconnecting"
# Creating control and interrupt sockets itr = None
ctrl = socket.socket( ctrl = None
family=socket.AF_BLUETOOTH, if type(reconnect_address) == list:
type=socket.SOCK_SEQPACKET, for address in reconnect_address:
proto=socket.BTPROTO_L2CAP) try:
itr = socket.socket( test_itr, test_ctrl = recreate_sockets
family=socket.AF_BLUETOOTH,
type=socket.SOCK_SEQPACKET,
proto=socket.BTPROTO_L2CAP)
# Setting up HID interrupt/control sockets # Setting up HID interrupt/control sockets
ctrl.connect((reconnect_address, 17)) test_ctrl.connect((reconnect_address, 17))
itr.connect((reconnect_address, 19)) test_itr.connect((reconnect_address, 19))
itr = test_itr
ctrl = test_ctrl
except OSError:
pass
elif type(reconnect_address) == str:
test_itr, test_ctrl = recreate_sockets()
# Setting up HID interrupt/control sockets
test_ctrl.connect((reconnect_address, 17))
test_itr.connect((reconnect_address, 19))
itr = test_itr
ctrl = test_ctrl
if not itr and not ctrl:
raise OSError("Unable to reconnect to sockets at the given address(es)",
reconnect_address)
fcntl.fcntl(itr, fcntl.F_SETFL, os.O_NONBLOCK) fcntl.fcntl(itr, fcntl.F_SETFL, os.O_NONBLOCK)