kete/prompt: implement pairing

we have to tweak a little the Searcher class for it to not prompt the
request whether to pair to the device or not.
This commit is contained in:
Benjamin Tissoires 2018-01-26 13:50:14 +01:00 committed by Peter Hutterer
parent c1a11280c0
commit dd84145b3b
1 changed files with 72 additions and 4 deletions

View File

@ -317,6 +317,7 @@ class Worker(GObject.Object):
class Searcher(Worker): class Searcher(Worker):
need_mainloop = True need_mainloop = True
interactive = True
def __init__(self, manager, args): def __init__(self, manager, args):
super(Searcher, self).__init__(manager) super(Searcher, self).__init__(manager)
@ -346,12 +347,10 @@ class Searcher(Worker):
def _on_notify_search(self, manager, pspec): def _on_notify_search(self, manager, pspec):
if not manager.searching: if not manager.searching:
logger.info('Search cancelled') logger.info('Search cancelled')
if not self.is_pairing: if not self.is_pairing and self.interactive:
self.stop() self.stop()
def _on_pairable_device(self, manager, device): def _on_pairable_device_interactive(self, manager, device):
print('Pairable device: {}'.format(device))
if self.address is None: if self.address is None:
print('Connect to device? [y/N] ', end='') print('Connect to device? [y/N] ', end='')
sys.stdout.flush() sys.stdout.flush()
@ -367,6 +366,12 @@ class Searcher(Worker):
self.is_pairing = True self.is_pairing = True
device.pair() device.pair()
def _on_pairable_device(self, manager, device):
logger.info('Pairable device: {}'.format(device))
if self.interactive:
self._on_pairable_device_interactive(manager, device)
class Listener(Worker): class Listener(Worker):
need_mainloop = True need_mainloop = True
@ -677,6 +682,69 @@ class TuhiKeteShell(cmd.Cmd):
wargs.index = index wargs.index = index
self.start_worker(Fetcher, wargs) self.start_worker(Fetcher, wargs)
def help_search(self):
self.do_search('-h')
def do_search(self, args):
'''Start/Stop listening for devices in pairable mode'''
parser = argparse.ArgumentParser(prog='search',
description='Start/Stop listening for devices in pairable mode.',
add_help=False)
parser.add_argument('-h', action='help', help=argparse.SUPPRESS)
parser.add_argument('mode', choices=['on', 'off'], nargs='?',
const='on', default='on')
try:
parsed_args = parser.parse_args(args.split())
except SystemExit:
return
if parsed_args.mode == 'off':
self._manager.stop_search()
return
Searcher.interactive = False
wargs = Args()
wargs.address = None
self.start_worker(Searcher, wargs)
def help_pair(self):
self.do_pair('-h')
def do_pair(self, args):
'''Pair a specific device in pairable mode'''
if not self._manager.searching and '-h' not in args.split():
print("please call search first")
return
parser = argparse.ArgumentParser(prog='pair',
description='Pair a specific device in pairable mode.',
add_help=False)
parser.add_argument('-h', action='help', help=argparse.SUPPRESS)
parser.add_argument('address', metavar='12:34:56:AB:CD:EF',
type=TuhiKeteDevice.is_device_address,
default=None,
help='the address of the device to pair')
try:
parsed_args = parser.parse_args(args.split())
except SystemExit:
return
address = parsed_args.address
device = None
for d in self._manager.devices:
if d.address == address:
device = d
break
else:
logger.error("{}: device not found".format(address))
return
device.pair()
class TuhiKeteShellWorker(Worker): class TuhiKeteShellWorker(Worker):
def __init__(self, manager, args): def __init__(self, manager, args):