Permalink
Fetching contributors…
Cannot retrieve contributors at this time
executable file 69 lines (55 sloc) 2.03 KB
import psmove
import os
import jm_dbus
class Pair():
"""
Manage paring move controllers to the server
"""
def __init__(self):
"""Use DBus to find bluetooth controllers"""
self.hci_dict = jm_dbus.get_hci_dict()
devices = self.hci_dict.values()
self.bt_devices = {}
for device in devices:
self.bt_devices[device] = []
self.pre_existing_devices()
def pre_existing_devices(self):
"""
Enumerate known devices
For each device on each adapter, add the device's address to it's adapter's
list of known devices
"""
for hci, addr in self.hci_dict.items():
proxy = jm_dbus.get_adapter_proxy(hci)
devices = jm_dbus.get_node_child_names(proxy)
self.bt_devices[addr] = jm_dbus.get_attached_addresses(hci)
def update_adapters(self):
"""
Rescan for bluetooth adapters that may not have existed on program launch
"""
self.hci_dict = jm_dbus.get_hci_dict()
for addr in self.hci_dict.values():
if addr not in self.bt_devices.keys():
self.bt_devices[addr] = []
self.pre_existing_devices()
def check_if_not_paired(self, addr):
for devs in self.bt_devices.keys():
if addr in self.bt_devices[devs]:
return False
return True
def get_lowest_bt_device(self):
num = 9999999
print(self.bt_devices)
for dev in self.bt_devices.keys():
if len(self.bt_devices[dev]) < num:
num = len(self.bt_devices[dev])
for dev in self.bt_devices.keys():
if len(self.bt_devices[dev]) == num:
return dev
return ''
def pair_move(self, move):
if move and move.get_serial():
if move.connection_type == psmove.Conn_USB:
self.pre_existing_devices()
if self.check_if_not_paired(move.get_serial().upper()):
move.pair_custom(self.get_lowest_bt_device())