scripts/mycroft/client/enclosure/generic/__init__.py
Go to the documentation of this file.
1 # Copyright 2017 Mycroft AI Inc.
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
6 #
7 # http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14 #
15 import subprocess
16 import time
17 import sys
18 from alsaaudio import Mixer
19 from threading import Thread, Timer
20 
21 import mycroft.dialog
22 from mycroft.client.enclosure.base import Enclosure
23 from mycroft.api import has_been_paired
24 from mycroft.audio import wait_while_speaking
26  init_display_manager_bus_connection
27 from mycroft.messagebus.message import Message
28 from mycroft.util import connected
29 from mycroft.util.log import LOG
30 
31 
33  """
34  Serves as a communication interface between a simple text frontend and
35  Mycroft Core. This is used for Picroft or other headless systems,
36  and/or for users of the CLI.
37  """
38 
39  _last_internet_notification = 0
40 
41  def __init__(self):
42  super().__init__()
43 
44  # Notifications from mycroft-core
45  self.bus.on("enclosure.notify.no_internet", self.on_no_internet)
46 
47  # initiates the web sockets on display manager
48  # NOTE: this is a temporary place to connect the display manager
50 
51  # verify internet connection and prompt user on bootup if needed
52  if not connected():
53  # We delay this for several seconds to ensure that the other
54  # clients are up and connected to the messagebus in order to
55  # receive the "speak". This was sometimes happening too
56  # quickly and the user wasn't notified what to do.
57  Timer(5, self._do_net_check).start()
58 
59  def on_no_internet(self, event=None):
60  if connected():
61  # One last check to see if connection was established
62  return
63 
64  if time.time() - Enclosure._last_internet_notification < 30:
65  # don't bother the user with multiple notifications with 30 secs
66  return
67 
68  Enclosure._last_internet_notification = time.time()
69 
70  # TODO: This should go into EnclosureMark1 subclass of Enclosure.
71  if has_been_paired():
72  # Handle the translation within that code.
73  self.bus.emit(Message("speak", {
74  'utterance': "This device is not connected to the Internet. "
75  "Either plug in a network cable or set up your "
76  "wifi connection."}))
77  else:
78  # enter wifi-setup mode automatically
79  self.bus.emit(Message('system.wifi.setup', {'lang': self.lang}))
80 
81  def speak(self, text):
82  self.bus.emit(Message("speak", {'utterance': text}))
83 
84  def _handle_pairing_complete(self, Message):
85  """
86  Handler for 'mycroft.paired', unmutes the mic after the pairing is
87  complete.
88  """
89  self.bus.emit(Message("mycroft.mic.unmute"))
90 
91  def _do_net_check(self):
92  # TODO: This should live in the derived Enclosure, e.g. EnclosureMark1
93  LOG.info("Checking internet connection")
94  if not connected(): # and self.conn_monitor is None:
95  if has_been_paired():
96  # TODO: Enclosure/localization
97  self.speak("This unit is not connected to the Internet. "
98  "Either plug in a network cable or setup your "
99  "wifi connection.")
100  else:
101  # Begin the unit startup process, this is the first time it
102  # is being run with factory defaults.
103 
104  # TODO: This logic should be in EnclosureMark1
105  # TODO: Enclosure/localization
106 
107  # Don't listen to mic during this out-of-box experience
108  self.bus.emit(Message("mycroft.mic.mute"))
109  # Setup handler to unmute mic at the end of on boarding
110  # i.e. after pairing is complete
111  self.bus.once('mycroft.paired', self._handle_pairing_complete)
112 
113  self.speak(mycroft.dialog.get('mycroft.intro'))
115  time.sleep(2) # a pause sounds better than just jumping in
116 
117  # Kick off wifi-setup automatically
118  data = {'allow_timeout': False, 'lang': self.lang}
119  self.bus.emit(Message('system.wifi.setup', data))
def get(phrase, lang=None, context=None)


mycroft_ros
Author(s):
autogenerated on Mon Apr 26 2021 02:35:40