Add proper messages when the backend replies incorrectly

pull/1508/head
Matthew D. Scholefield 2018-03-27 18:51:30 -05:00
parent 8e48d441c3
commit eb100706df
6 changed files with 70 additions and 38 deletions

View File

@ -15,20 +15,28 @@
from copy import copy
import requests
from requests import HTTPError
from requests import HTTPError, RequestException
from mycroft.configuration import Configuration
from mycroft.configuration.config import DEFAULT_CONFIG, SYSTEM_CONFIG, \
USER_CONFIG
from mycroft.identity import IdentityManager
from mycroft.version import VersionManager
from mycroft.util import get_arch
from mycroft.util import get_arch, connected, LOG
# python 2/3 compatibility
from future.utils import iteritems
_paired_cache = False
class BackendDown(RequestException):
pass
class InternetDown(RequestException):
pass
class Api(object):
""" Generic object to wrap web APIs """
@ -332,7 +340,7 @@ def has_been_paired():
return id.uuid is not None and id.uuid != ""
def is_paired():
def is_paired(ignore_errors=True):
""" Determine if this device is actively paired with a web backend
Determines if the installation of Mycroft has been paired by the user
@ -354,5 +362,13 @@ def is_paired():
_paired_cache = api.identity.uuid is not None and \
api.identity.uuid != ""
return _paired_cache
except:
except HTTPError as e:
if e.response.status_code == 401:
return False
except Exception as e:
LOG.warning('Could not get device infO: ' + repr(e))
if ignore_errors:
return False
if connected():
raise BackendDown
raise InternetDown

View File

@ -20,13 +20,14 @@ from pyee import EventEmitter
from requests import RequestException, HTTPError
from requests.exceptions import ConnectionError
import mycroft.dialog
from mycroft import dialog
from mycroft.client.speech.hotword_factory import HotWordFactory
from mycroft.client.speech.mic import MutableMicrophone, ResponsiveRecognizer
from mycroft.configuration import Configuration
from mycroft.metrics import MetricsAggregator, Stopwatch, report_timing
from mycroft.session import SessionManager
from mycroft.stt import STTFactory
from mycroft.util import connected
from mycroft.util.log import LOG
if sys.version_info[0] < 3:
from Queue import Queue, Empty
@ -159,20 +160,21 @@ class AudioConsumer(Thread):
'stt': self.stt.__class__.__name__})
def transcribe(self, audio):
text = None
try:
# Invoke the STT engine on the audio clip
text = self.stt.execute(audio).lower().strip()
LOG.debug("STT: " + text)
return text
except sr.RequestError as e:
LOG.error("Could not request Speech Recognition {0}".format(e))
except ConnectionError as e:
LOG.error("Connection Error: {0}".format(e))
self.emitter.emit("recognizer_loop:no_internet")
except HTTPError as e:
if e.response.status_code == 401:
text = "pair my device" # phrase to start the pairing process
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:
@ -184,7 +186,12 @@ class AudioConsumer(Thread):
else:
LOG.error(e)
LOG.error("Speech Recognition could not understand audio")
return text
return None
if connected():
dialog_name = 'backend.down'
else:
dialog_name = 'not connected to the internet'
self.emitter.emit('speak', {'utterance': dialog.get(dialog_name)})
def __speak(self, utterance):
payload = {

View File

@ -37,7 +37,7 @@ class IdentityManager(object):
try:
with FileSystemAccess('identity').open('identity2.json', 'r') as f:
IdentityManager.__identity = DeviceIdentity(**json.load(f))
except:
except Exception:
IdentityManager.__identity = DeviceIdentity()
return IdentityManager.__identity

View File

@ -0,0 +1,4 @@
I'm having trouble communicating with the Mycroft servers. Please give me a few minutes before trying to speak to me.
I'm having trouble communicating with the Mycroft servers. Please wait few minutes before trying to speak to me.
It seems I cannot connect to the Mycroft servers. Please give me a few minutes before trying to speak to me.
It seems I cannot connect to the Mycroft servers. Please wait few minutes before trying to speak to me.

View File

@ -1,4 +1,5 @@
It seems I'm not connected to the Internet
I don't seem to be connected to the internet
I can't reach the internet right now
I'm unable to reach the internet
It seems I'm not connected to the Internet, please check your network connection.
I don't seem to be connected to the internet, please check your network connection.
I can't reach the internet right now, please check your network connection.
I'm unable to reach the internet, please check your network connection.
I'm having trouble reaching the internet right now, please check your network connection.

View File

@ -12,21 +12,20 @@
# See the License for the specific language governing permissions and
# limitations under the License.
#
import gc
import json
import os
import subprocess
import sys
import time
import monotonic
from threading import Timer, Thread, Event, Lock
import gc
import os
import monotonic
from os.path import exists, join
import mycroft.dialog
import mycroft.lock
from mycroft import MYCROFT_ROOT_PATH
from mycroft.api import is_paired
from mycroft import MYCROFT_ROOT_PATH, dialog
from mycroft.api import is_paired, BackendDown
from mycroft.configuration import Configuration
from mycroft.messagebus.client.ws import WebsocketClient
from mycroft.messagebus.message import Message
@ -131,14 +130,14 @@ def check_connection():
if is_paired():
# Skip the sync message when unpaired because the prompt to go to
# home.mycrof.ai will be displayed by the pairing skill
enclosure.mouth_text(mycroft.dialog.get("message_synching.clock"))
enclosure.mouth_text(dialog.get("message_synching.clock"))
# Force a sync of the local clock with the internet
config = Configuration.get()
platform = config['enclosure'].get("platform", "unknown")
if platform in ['mycroft_mark_1', 'picroft']:
ws.emit(Message("system.ntp.sync"))
time.sleep(15) # TODO: Generate/listen for a message response...
time.sleep(15) # TODO: Generate/listen for a message response...
# Check if the time skewed significantly. If so, reboot
skew = abs((monotonic.monotonic() - start_ticks) -
@ -148,11 +147,11 @@ def check_connection():
# prevent weird things from occcurring due to the 'time warp'.
#
ws.emit(Message("speak", {'utterance':
mycroft.dialog.get("time.changed.reboot")}))
dialog.get("time.changed.reboot")}))
wait_while_speaking()
# provide visual indicators of the reboot
enclosure.mouth_text(mycroft.dialog.get("message_rebooting"))
enclosure.mouth_text(dialog.get("message_rebooting"))
enclosure.eyes_color(70, 65, 69) # soft gray
enclosure.eyes_spin()
@ -168,17 +167,22 @@ def check_connection():
ws.emit(Message('mycroft.internet.connected'))
# check for pairing, if not automatically start pairing
if not is_paired():
# begin the process
payload = {
'utterances': ["pair my device"],
'lang': "en-us"
}
ws.emit(Message("recognizer_loop:utterance", payload))
else:
from mycroft.api import DeviceApi
api = DeviceApi()
api.update_version()
try:
if not is_paired(ignore_errors=False):
payload = {
'utterances': ["pair my device"],
'lang': "en-us"
}
ws.emit(Message("recognizer_loop:utterance", payload))
else:
from mycroft.api import DeviceApi
api = DeviceApi()
api.update_version()
except BackendDown:
data = {'utterance': dialog.get("backend.down")}
ws.emit(Message("speak", data))
ws.emit(Message("backend.down"))
else:
thread = Timer(1, check_connection)
thread.daemon = True
@ -264,7 +268,7 @@ class SkillManager(Thread):
# to home.mycrof.ai will be displayed by the pairing skill
if not is_paired():
self.enclosure.mouth_text(
mycroft.dialog.get("message_updating"))
dialog.get("message_updating"))
else:
LOG.info('Skills will be updated at a later time')
self.next_download = time.time() + 60 * MINUTES
@ -312,13 +316,13 @@ class SkillManager(Thread):
if res == 0 and speak:
self.ws.emit(Message("speak", {'utterance':
mycroft.dialog.get("skills updated")}))
dialog.get("skills updated")}))
return True
elif not connected():
LOG.error('msm failed, network connection not available')
if speak:
self.ws.emit(Message("speak", {
'utterance': mycroft.dialog.get(
'utterance': dialog.get(
"not connected to the internet")}))
self.next_download = time.time() + 5 * MINUTES
return False
@ -328,7 +332,7 @@ class SkillManager(Thread):
res, output))
if speak:
self.ws.emit(Message("speak", {
'utterance': mycroft.dialog.get(
'utterance': dialog.get(
"sorry I couldn't install default skills")}))
self.next_download = time.time() + 5 * MINUTES
return False