Merge pull request #2248 from forslund/feature/pairing-check-in-stt

Move pairing check to the relevant stt backends
pull/2257/head
David Wagner 2019-08-13 14:56:49 -05:00 committed by GitHub
commit 5f6db0941b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 25 additions and 8 deletions

View File

@ -16,7 +16,7 @@ import time
from threading import Thread from threading import Thread
import speech_recognition as sr import speech_recognition as sr
from pyee import EventEmitter from pyee import EventEmitter
from requests import RequestException, HTTPError from requests import RequestException
from requests.exceptions import ConnectionError from requests.exceptions import ConnectionError
from mycroft import dialog from mycroft import dialog
@ -215,12 +215,6 @@ class AudioConsumer(Thread):
LOG.error("Connection Error: {0}".format(e)) LOG.error("Connection Error: {0}".format(e))
self.emitter.emit("recognizer_loop:no_internet") self.emitter.emit("recognizer_loop:no_internet")
except HTTPError as e:
if e.response.status_code == 401:
LOG.warning("Access Denied at mycroft.ai")
return "pair my device" # phrase to start the pairing process
else:
LOG.error(e.__class__.__name__ + ': ' + str(e))
except RequestException as e: except RequestException as e:
LOG.error(e.__class__.__name__ + ': ' + str(e)) LOG.error(e.__class__.__name__ + ': ' + str(e))
except Exception as e: except Exception as e:

View File

@ -20,7 +20,7 @@ from speech_recognition import Recognizer
from queue import Queue from queue import Queue
from threading import Thread from threading import Thread
from mycroft.api import STTApi from mycroft.api import STTApi, HTTPError
from mycroft.configuration import Configuration from mycroft.configuration import Configuration
from mycroft.util.log import LOG from mycroft.util.log import LOG
@ -118,11 +118,33 @@ class IBMSTT(BasicSTT):
self.password, self.lang) self.password, self.lang)
def requires_pairing(func):
"""Decorator kicking of pairing sequence if client is not allowed access.
Checks the http status of the response if an HTTP error is recieved. If
a 401 status is detected returns "pair my device" to trigger the pairing
skill.
"""
def wrapper(*args, **kwargs):
try:
return func(*args, **kwargs)
except HTTPError as e:
if e.response.status_code == 401:
LOG.warning('Access Denied at mycroft.ai')
# phrase to start the pairing process
return 'pair my device'
else:
raise
return wrapper
class MycroftSTT(STT): class MycroftSTT(STT):
"""Default mycroft STT."""
def __init__(self): def __init__(self):
super(MycroftSTT, self).__init__() super(MycroftSTT, self).__init__()
self.api = STTApi("stt") self.api = STTApi("stt")
@requires_pairing
def execute(self, audio, language=None): def execute(self, audio, language=None):
self.lang = language or self.lang self.lang = language or self.lang
try: try:
@ -138,6 +160,7 @@ class MycroftDeepSpeechSTT(STT):
super(MycroftDeepSpeechSTT, self).__init__() super(MycroftDeepSpeechSTT, self).__init__()
self.api = STTApi("deepspeech") self.api = STTApi("deepspeech")
@requires_pairing
def execute(self, audio, language=None): def execute(self, audio, language=None):
language = language or self.lang language = language or self.lang
if not language.startswith("en"): if not language.startswith("en"):