Move pairing check to the relevant stt backends

The pairing trigger should only be triggered by the 401 status for the
Mycroft hosted STT backends. Doing it for other STT's is only confusing.

This creates the decorator 'requires_pairing' that can be applied to the STT modules where it's relevant.

This adds the decorator to the MycroftSTT and the MycroftDeepSpeechSTT
pull/2248/head
Åke Forslund 2019-08-02 18:16:29 +02:00
parent b357874c25
commit f727c00430
2 changed files with 25 additions and 8 deletions

View File

@ -16,7 +16,7 @@ import time
from threading import Thread
import speech_recognition as sr
from pyee import EventEmitter
from requests import RequestException, HTTPError
from requests import RequestException
from requests.exceptions import ConnectionError
from mycroft import dialog
@ -215,12 +215,6 @@ class AudioConsumer(Thread):
LOG.error("Connection Error: {0}".format(e))
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:
LOG.error(e.__class__.__name__ + ': ' + str(e))
except Exception as e:

View File

@ -20,7 +20,7 @@ from speech_recognition import Recognizer
from queue import Queue
from threading import Thread
from mycroft.api import STTApi
from mycroft.api import STTApi, HTTPError
from mycroft.configuration import Configuration
from mycroft.util.log import LOG
@ -118,11 +118,33 @@ class IBMSTT(BasicSTT):
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):
"""Default mycroft STT."""
def __init__(self):
super(MycroftSTT, self).__init__()
self.api = STTApi("stt")
@requires_pairing
def execute(self, audio, language=None):
self.lang = language or self.lang
try:
@ -138,6 +160,7 @@ class MycroftDeepSpeechSTT(STT):
super(MycroftDeepSpeechSTT, self).__init__()
self.api = STTApi("deepspeech")
@requires_pairing
def execute(self, audio, language=None):
language = language or self.lang
if not language.startswith("en"):