def main(ready_hook=on_ready, error_hook=on_error, stopping_hook=on_stopping): """ Main function. Run when file is invoked. """ try: reset_sigint_handler() check_for_signal("isSpeaking") bus = MessageBusClient() # Connect to the Mycroft Messagebus Configuration.set_config_update_handlers(bus) speech.init(bus) LOG.info("Starting Audio Services") bus.on('message', create_echo_function('AUDIO', ['mycroft.audio.service'])) # Connect audio service instance to message bus audio = AudioService(bus) except Exception as e: error_hook(e) else: create_daemon(bus.run_forever) if audio.wait_for_load() and len(audio.service) > 0: # If at least one service exists, report ready ready_hook() wait_for_exit_signal() stopping_hook() else: error_hook('No audio services loaded') speech.shutdown() audio.shutdown()
def __init__(self): # Load full config config = Configuration.get() self.lang = config['lang'] self.config = config.get("enclosure") self.global_config = config # Create Message Bus Client self.bus = MessageBusClient() self.gui = create_gui_service(self, config['gui_websocket']) # This datastore holds the data associated with the GUI provider. Data # is stored in Namespaces, so you can have: # self.datastore["namespace"]["name"] = value # Typically the namespace is a meaningless identifier, but there is a # special "SYSTEM" namespace. self.datastore = {} # self.loaded is a list, each element consists of a namespace named # tuple. # The namespace namedtuple has the properties "name" and "pages" # The name contains the namespace name as a string and pages is a # mutable list of loaded pages. # # [Namespace name, [List of loaded qml pages]] # [ # ["SKILL_NAME", ["page1.qml, "page2.qml", ... , "pageN.qml"] # [...] # ] self.loaded = [] # list of lists in order. self.explicit_move = True # Set to true to send reorder commands # Listen for new GUI clients to announce themselves on the main bus self.active_namespaces = [] self.bus.on("mycroft.gui.connected", self.on_gui_client_connected) self.register_gui_handlers() # First send any data: self.bus.on("gui.value.set", self.on_gui_set_value) self.bus.on("gui.page.show", self.on_gui_show_page) self.bus.on("gui.page.delete", self.on_gui_delete_page) self.bus.on("gui.clear.namespace", self.on_gui_delete_namespace) self.bus.on("gui.event.send", self.on_gui_send_event) self.bus.on("gui.status.request", self.handle_gui_status_request) # TODO: this requires the Enclosure to be up and running before the # training is complete. self.bus.on('mycroft.skills.trained', self.is_device_ready)
def send(message_to_send, data_to_send=None): """Send a single message over the websocket. Args: message_to_send (str): Message to send data_to_send (dict): data structure to go along with the message, defaults to empty dict. """ data_to_send = data_to_send or {} # Calculate the standard Mycroft messagebus websocket address config = Configuration.get(cache=False, remote=False) config = config.get("websocket") url = MessageBusClient.build_url( config.get("host"), config.get("port"), config.get("route"), config.get("ssl") ) # Send the provided message/data ws = create_connection(url) packet = Message(message_to_send, data_to_send).serialize() ws.send(packet) ws.close()
def main(): """ Main function. Run when file is invoked. """ reset_sigint_handler() check_for_signal("isSpeaking") bus = MessageBusClient() # Connect to the Mycroft Messagebus Configuration.set_config_update_handlers(bus) speech.init(bus) LOG.info("Starting Audio Services") bus.on('message', create_echo_function('AUDIO', ['mycroft.audio.service'])) audio = AudioService(bus) # Connect audio service instance to message bus create_daemon(bus.run_forever) wait_for_exit_signal() speech.shutdown() audio.shutdown()
def __init__(self): # Establish Enclosure's websocket connection to the messagebus self.bus = MessageBusClient() # Load full config Configuration.set_config_update_handlers(self.bus) config = Configuration.get() self.lang = config['lang'] self.config = config.get("enclosure") self.global_config = config # This datastore holds the data associated with the GUI provider. Data # is stored in Namespaces, so you can have: # self.datastore["namespace"]["name"] = value # Typically the namespace is a meaningless identifier, but there is a # special "SYSTEM" namespace. self.datastore = {} # self.loaded is a list, each element consists of a namespace named # tuple. # The namespace namedtuple has the properties "name" and "pages" # The name contains the namespace name as a string and pages is a # mutable list of loaded pages. # # [Namespace name, [List of loaded qml pages]] # [ # ["SKILL_NAME", ["page1.qml, "page2.qml", ... , "pageN.qml"] # [...] # ] self.loaded = [] # list of lists in order. self.explicit_move = True # Set to true to send reorder commands # Listen for new GUI clients to announce themselves on the main bus self.GUIs = {} # GUIs, either local or remote self.active_namespaces = [] self.bus.on("mycroft.gui.connected", self.on_gui_client_connected) self.register_gui_handlers() # First send any data: self.bus.on("gui.value.set", self.on_gui_set_value) self.bus.on("gui.page.show", self.on_gui_show_page) self.bus.on("gui.page.delete", self.on_gui_delete_page) self.bus.on("gui.clear.namespace", self.on_gui_delete_namespace) self.bus.on("gui.event.send", self.on_gui_send_event)
def connect_to_messagebus(): """ Connect to the mycroft messagebus and launch a thread handling the connection. Returns: WebsocketClient """ bus = MessageBusClient() # Mycroft messagebus connection event_thread = Thread(target=connect, args=[bus]) event_thread.setDaemon(True) event_thread.start() return bus
def start_message_bus_client(service, bus=None, whitelist=None): """Start the bus client daemon and wait for connection. Arguments: service (str): name of the service starting the connection bus (MessageBusClient): an instance of the Mycroft MessageBusClient whitelist (list, optional): List of "type" strings. If defined, only messages in this list will be logged. Returns: A connected instance of the MessageBusClient """ # Local imports to avoid circular importing from mycroft.messagebus.client import MessageBusClient from mycroft.configuration import Configuration # Create a client if one was not provided if bus is None: bus = MessageBusClient() Configuration.set_config_update_handlers(bus) bus_connected = Event() bus.on('message', create_echo_function(service, whitelist)) # Set the bus connected event when connection is established bus.once('open', bus_connected.set) create_daemon(bus.run_forever) # Wait for connection bus_connected.wait() LOG.info('Connected to messagebus') return bus
def setUp(self): """ This sets up for testing the message buss This requires starting the mycroft service and creating two WebsocketClient object to talk with eachother. Not this is threaded and will require cleanup """ # start the mycroft service. and get the pid of the script. self.pid = Popen(["python", "mycroft/messagebus/service/main.py"]).pid # Create the two web clients self.ws1 = MessageBusClient() self.ws2 = MessageBusClient() # init the flags for handler's self.handle1 = False self.handle2 = False # Start threads to handle websockets Thread(target=self.ws1.run_forever).start() Thread(target=self.ws2.run_forever).start() # Setup handlers for each of the messages. self.ws1.on('ws1.message', self.onHandle1) self.ws2.on('ws2.message', self.onHandle2)
def init_display_manager_bus_connection(): """ Connects the display manager to the messagebus """ LOG.info("Connecting display manager to messagebus") # Should remove needs to be an object so it can be referenced in functions # [https://stackoverflow.com/questions/986006/how-do-i-pass-a-variable-by-reference] display_manager = DisplayManager() should_remove = [True] def check_flag(flag): if flag[0] is True: display_manager.remove_active() def set_delay(event=None): should_remove[0] = True Timer(2, check_flag, [should_remove]).start() def set_remove_flag(event=None): should_remove[0] = False def connect(): bus.run_forever() def remove_wake_word(): data = _read_data() if "active_skill" in data and data["active_skill"] == "wakeword": display_manager.remove_active() def set_wakeword_skill(event=None): display_manager.set_active("wakeword") Timer(10, remove_wake_word).start() bus = MessageBusClient() bus.on('recognizer_loop:audio_output_end', set_delay) bus.on('recognizer_loop:audio_output_start', set_remove_flag) bus.on('recognizer_loop:record_begin', set_wakeword_skill) event_thread = Thread(target=connect) event_thread.setDaemon(True) event_thread.start()
def _start_message_bus_client(): """Start the bus client daemon and wait for connection.""" bus = MessageBusClient() Configuration.set_config_update_handlers(bus) bus_connected = Event() bus.on('message', create_echo_function('SKILLS')) # Set the bus connected event when connection is established bus.once('open', bus_connected.set) create_daemon(bus.run_forever) # Wait for connection bus_connected.wait() LOG.info('Connected to messagebus') return bus
def main(ready_hook=on_ready, error_hook=on_error, stopping_hook=on_stopping, watchdog=lambda: None): global bus global loop global config try: reset_sigint_handler() PIDLock("voice") bus = MessageBusClient() # Mycroft messagebus, see mycroft.messagebus Configuration.set_config_update_handlers(bus) config = Configuration.get() # Register handlers on internal RecognizerLoop bus loop = RecognizerLoop(watchdog) connect_loop_events(loop) connect_bus_events(bus) create_daemon(bus.run_forever) create_daemon(loop.run) except Exception as e: error_hook(e) else: ready_hook() wait_for_exit_signal() stopping_hook()
def main(): global bus reset_sigint_handler() # Create PID file, prevent multiple instancesof this service mycroft.lock.Lock('skills') # Connect this Skill management process to the Mycroft Messagebus bus = MessageBusClient() Configuration.init(bus) config = Configuration.get() # Set the active lang to match the configured one set_active_lang(config.get('lang', 'en-us')) bus.on('message', create_echo_function('SKILLS')) # Startup will be called after the connection with the Messagebus is done bus.once('open', _starting_up) create_daemon(bus.run_forever) wait_for_exit_signal() shutdown()
def main(): global bus global loop global config reset_sigint_handler() PIDLock("voice") bus = MessageBusClient() # Mycroft messagebus, see mycroft.messagebus Configuration.set_config_update_handlers(bus) config = Configuration.get() # Register handlers on internal RecognizerLoop bus loop = RecognizerLoop() loop.on('recognizer_loop:utterance', handle_utterance) loop.on('recognizer_loop:speech.recognition.unknown', handle_unknown) loop.on('speak', handle_speak) loop.on('recognizer_loop:record_begin', handle_record_begin) loop.on('recognizer_loop:awoken', handle_awoken) loop.on('recognizer_loop:wakeword', handle_wakeword) loop.on('recognizer_loop:record_end', handle_record_end) loop.on('recognizer_loop:no_internet', handle_no_internet) # Register handlers for events on main Mycroft messagebus bus.on('open', handle_open) bus.on('complete_intent_failure', handle_complete_intent_failure) bus.on('recognizer_loop:sleep', handle_sleep) bus.on('recognizer_loop:wake_up', handle_wake_up) bus.on('mycroft.mic.mute', handle_mic_mute) bus.on('mycroft.mic.unmute', handle_mic_unmute) bus.on('mycroft.mic.get_status', handle_mic_get_status) bus.on("mycroft.paired", handle_paired) bus.on('recognizer_loop:audio_output_start', handle_audio_start) bus.on('recognizer_loop:audio_output_end', handle_audio_end) bus.on('mycroft.stop', handle_stop) bus.on('message', create_echo_function('VOICE')) create_daemon(bus.run_forever) create_daemon(loop.run) wait_for_exit_signal()
class TestMessagebusMethods(unittest.TestCase): """This class is for testing the messsagebus. It currently only tests send and receive. The tests could include more. """ def setUp(self): """ This sets up for testing the message buss This requires starting the mycroft service and creating two WebsocketClient object to talk with eachother. Not this is threaded and will require cleanup """ # start the mycroft service. and get the pid of the script. self.pid = Popen(["python", "mycroft/messagebus/service/main.py"]).pid # Create the two web clients self.ws1 = MessageBusClient() self.ws2 = MessageBusClient() # init the flags for handler's self.handle1 = False self.handle2 = False # Start threads to handle websockets Thread(target=self.ws1.run_forever).start() Thread(target=self.ws2.run_forever).start() # Setup handlers for each of the messages. self.ws1.on('ws1.message', self.onHandle1) self.ws2.on('ws2.message', self.onHandle2) def onHandle1(self, event): """This is the handler for ws1.message This for now simply sets a flag to true when received. Args: event(Message): this is the message received """ self.handle1 = True def onHandle2(self, event): """This is the handler for ws2.message This for now simply sets a flag to true when received. Args: event(Message): this is the message received """ self.handle2 = True def tearDown(self): """This is the clean up for the tests This will close the websockets ending the threads then kill the mycroft service that was started in setUp. """ self.ws1.close() self.ws2.close() retcode = call(["kill", "-9", str(self.pid)]) def test_ClientServer(self): """This is the test to send a message from each of the websockets to the other. """ # Send the messages self.ws2.emit(Message('ws1.message')) self.ws1.emit(Message('ws2.message')) # allow time for messages to be processed time.sleep(0.2) # Check that both of the handlers were called. self.assertTrue(self.handle1) self.assertTrue(self.handle2)
def test_build_url(self): url = MessageBusClient.build_url('localhost', 1337, '/core', False) assert url == 'ws://localhost:1337/core' ssl_url = MessageBusClient.build_url('sslhost', 443, '/core', True) assert ssl_url == 'wss://sslhost:443/core'
class Enclosure: def __init__(self): # Establish Enclosure's websocket connection to the messagebus self.bus = MessageBusClient() # Load full config Configuration.set_config_update_handlers(self.bus) config = Configuration.get() self.lang = config['lang'] self.config = config.get("enclosure") self.global_config = config # This datastore holds the data associated with the GUI provider. Data # is stored in Namespaces, so you can have: # self.datastore["namespace"]["name"] = value # Typically the namespace is a meaningless identifier, but there is a # special "SYSTEM" namespace. self.datastore = {} # self.loaded is a list, each element consists of a namespace named # tuple. # The namespace namedtuple has the properties "name" and "pages" # The name contains the namespace name as a string and pages is a # mutable list of loaded pages. # # [Namespace name, [List of loaded qml pages]] # [ # ["SKILL_NAME", ["page1.qml, "page2.qml", ... , "pageN.qml"] # [...] # ] self.loaded = [] # list of lists in order. self.explicit_move = True # Set to true to send reorder commands # Listen for new GUI clients to announce themselves on the main bus self.GUIs = {} # GUIs, either local or remote self.active_namespaces = [] self.bus.on("mycroft.gui.connected", self.on_gui_client_connected) self.register_gui_handlers() # First send any data: self.bus.on("gui.value.set", self.on_gui_set_value) self.bus.on("gui.page.show", self.on_gui_show_page) self.bus.on("gui.page.delete", self.on_gui_delete_page) self.bus.on("gui.clear.namespace", self.on_gui_delete_namespace) self.bus.on("gui.event.send", self.on_gui_send_event) def run(self): try: self.bus.run_forever() except Exception as e: LOG.error("Error: {0}".format(e)) self.stop() ###################################################################### # GUI client API def send(self, *args, **kwargs): """ Send to all registered GUIs. """ for gui in self.GUIs.values(): if gui.socket: gui.socket.send(*args, **kwargs) else: LOG.error('GUI connection {} has no socket!'.format(gui)) def on_gui_send_event(self, message): """ Send an event to the GUIs. """ try: data = {'type': 'mycroft.events.triggered', 'namespace': message.data.get('__from'), 'event_name': message.data.get('event_name'), 'params': message.data.get('params')} self.send(data) except Exception as e: LOG.error('Could not send event ({})'.format(repr(e))) def on_gui_set_value(self, message): data = message.data namespace = data.get("__from", "") # Pass these values on to the GUI renderers for key in data: if key not in RESERVED_KEYS: try: self.set(namespace, key, data[key]) except Exception as e: LOG.exception(repr(e)) def set(self, namespace, name, value): """ Perform the send of the values to the connected GUIs. """ if namespace not in self.datastore: self.datastore[namespace] = {} if self.datastore[namespace].get(name) != value: self.datastore[namespace][name] = value # If the namespace is loaded send data to GUI if namespace in [l.name for l in self.loaded]: msg = {"type": "mycroft.session.set", "namespace": namespace, "data": {name: value}} self.send(msg) def on_gui_delete_page(self, message): """ Bus handler for removing pages. """ page, namespace, _ = _get_page_data(message) try: with namespace_lock: self.remove_pages(namespace, page) except Exception as e: LOG.exception(repr(e)) def on_gui_delete_namespace(self, message): """ Bus handler for removing namespace. """ try: namespace = message.data['__from'] with namespace_lock: self.remove_namespace(namespace) except Exception as e: LOG.exception(repr(e)) def on_gui_show_page(self, message): try: page, namespace, index = _get_page_data(message) # Pass the request to the GUI(s) to pull up a page template with namespace_lock: self.show(namespace, page, index) except Exception as e: LOG.exception(repr(e)) def __find_namespace(self, namespace): for i, skill in enumerate(self.loaded): if skill[0] == namespace: return i return None def __insert_pages(self, namespace, pages): """ Insert pages into the namespace Args: namespace (str): Namespace to add to pages (list): Pages (str) to insert """ LOG.debug("Inserting new pages") if not isinstance(pages, list): raise ValueError('Argument must be list of pages') self.send({"type": "mycroft.gui.list.insert", "namespace": namespace, "position": len(self.loaded[0].pages), "data": [{"url": p} for p in pages] }) # Insert the pages into local reprensentation as well. updated = Namespace(self.loaded[0].name, self.loaded[0].pages + pages) self.loaded[0] = updated def __remove_page(self, namespace, pos): """ Delete page. Args: namespace (str): Namespace to remove from pos (int): Page position to remove """ LOG.debug("Deleting {} from {}".format(pos, namespace)) self.send({"type": "mycroft.gui.list.remove", "namespace": namespace, "position": pos, "items_number": 1 }) # Remove the page from the local reprensentation as well. self.loaded[0].pages.pop(pos) def __insert_new_namespace(self, namespace, pages): """ Insert new namespace and pages. This first sends a message adding a new namespace at the highest priority (position 0 in the namespace stack) Args: namespace (str): The skill namespace to create pages (str): Pages to insert (name matches QML) """ LOG.debug("Inserting new namespace") self.send({"type": "mycroft.session.list.insert", "namespace": "mycroft.system.active_skills", "position": 0, "data": [{"skill_id": namespace}] }) # Load any already stored Data data = self.datastore.get(namespace, {}) for key in data: msg = {"type": "mycroft.session.set", "namespace": namespace, "data": {key: data[key]}} self.send(msg) LOG.debug("Inserting new page") self.send({"type": "mycroft.gui.list.insert", "namespace": namespace, "position": 0, "data": [{"url": p} for p in pages] }) # Make sure the local copy is updated self.loaded.insert(0, Namespace(namespace, pages)) def __move_namespace(self, from_pos, to_pos): """ Move an existing namespace to a new position in the stack. Args: from_pos (int): Position in the stack to move from to_pos (int): Position to move to """ LOG.debug("Activating existing namespace") # Seems like the namespace is moved to the top automatically when # a page change is done. Deactivating this for now. if self.explicit_move: LOG.debug("move {} to {}".format(from_pos, to_pos)) self.send({"type": "mycroft.session.list.move", "namespace": "mycroft.system.active_skills", "from": from_pos, "to": to_pos, "items_number": 1}) # Move the local representation of the skill from current # position to position 0. self.loaded.insert(to_pos, self.loaded.pop(from_pos)) def __switch_page(self, namespace, pages): """ Switch page to an already loaded page. Args: pages (list): pages (str) to switch to namespace (str): skill namespace """ try: num = self.loaded[0].pages.index(pages[0]) except Exception as e: LOG.exception(repr(e)) num = 0 LOG.debug('Switching to already loaded page at ' 'index {} in namespace {}'.format(num, namespace)) self.send({"type": "mycroft.events.triggered", "namespace": namespace, "event_name": "page_gained_focus", "data": {"number": num}}) def show(self, namespace, page, index): """ Show a page and load it as needed. Args: page (str or list): page(s) to show namespace (str): skill namespace index (int): ??? TODO: Unused in code ??? TODO: - Update sync to match. - Separate into multiple functions/methods """ LOG.debug("GUIConnection activating: " + namespace) pages = page if isinstance(page, list) else [page] # find namespace among loaded namespaces try: index = self.__find_namespace(namespace) if index is None: # This namespace doesn't exist, insert them first so they're # shown. self.__insert_new_namespace(namespace, pages) return else: # Namespace exists if index > 0: # Namespace is inactive, activate it by moving it to # position 0 self.__move_namespace(index, 0) # Find if any new pages needs to be inserted new_pages = [p for p in pages if p not in self.loaded[0].pages] if new_pages: self.__insert_pages(namespace, new_pages) else: # No new pages, just switch self.__switch_page(namespace, pages) except Exception as e: LOG.exception(repr(e)) def remove_namespace(self, namespace): """ Remove namespace. Args: namespace (str): namespace to remove """ index = self.__find_namespace(namespace) if index is None: return else: LOG.debug("Removing namespace {} at {}".format(namespace, index)) self.send({"type": "mycroft.session.list.remove", "namespace": "mycroft.system.active_skills", "position": index, "items_number": 1 }) # Remove namespace from loaded namespaces self.loaded.pop(index) def remove_pages(self, namespace, pages): """ Remove the listed pages from the provided namespace. Args: namespace (str): The namespace to modify pages (list): List of page names (str) to delete """ try: index = self.__find_namespace(namespace) if index is None: return else: # Remove any pages that doesn't exist in the namespace pages = [p for p in pages if p in self.loaded[index].pages] # Make sure to remove pages from the back indexes = [self.loaded[index].pages.index(p) for p in pages] indexes = sorted(indexes) indexes.reverse() for page_index in indexes: self.__remove_page(namespace, page_index) except Exception as e: LOG.exception(repr(e)) ###################################################################### # GUI client socket # # The basic mechanism is: # 1) GUI client announces itself on the main messagebus # 2) Mycroft prepares a port for a socket connection to this GUI # 3) The port is announced over the messagebus # 4) The GUI connects on the socket # 5) Connection persists for graphical interaction indefinitely # # If the connection is lost, it must be renegotiated and restarted. def on_gui_client_connected(self, message): # GUI has announced presence LOG.debug("on_gui_client_connected") gui_id = message.data.get("gui_id") # Spin up a new communication socket for this GUI if gui_id in self.GUIs: # TODO: Close it? pass self.GUIs[gui_id] = GUIConnection(gui_id, self.global_config, self.callback_disconnect, self) LOG.debug("Heard announcement from gui_id: {}".format(gui_id)) # Announce connection, the GUI should connect on it soon self.bus.emit(Message("mycroft.gui.port", {"port": self.GUIs[gui_id].port, "gui_id": gui_id})) def callback_disconnect(self, gui_id): LOG.info("Disconnecting!") # TODO: Whatever is needed to kill the websocket instance LOG.info(self.GUIs.keys()) LOG.info('deleting: {}'.format(gui_id)) if gui_id in self.GUIs: del self.GUIs[gui_id] else: LOG.warning('ID doesn\'t exist') def register_gui_handlers(self): # TODO: Register handlers for standard (Mark 1) events # self.bus.on('enclosure.eyes.on', self.on) # self.bus.on('enclosure.eyes.off', self.off) # self.bus.on('enclosure.eyes.blink', self.blink) # self.bus.on('enclosure.eyes.narrow', self.narrow) # self.bus.on('enclosure.eyes.look', self.look) # self.bus.on('enclosure.eyes.color', self.color) # self.bus.on('enclosure.eyes.level', self.brightness) # self.bus.on('enclosure.eyes.volume', self.volume) # self.bus.on('enclosure.eyes.spin', self.spin) # self.bus.on('enclosure.eyes.timedspin', self.timed_spin) # self.bus.on('enclosure.eyes.reset', self.reset) # self.bus.on('enclosure.eyes.setpixel', self.set_pixel) # self.bus.on('enclosure.eyes.fill', self.fill) # self.bus.on('enclosure.mouth.reset', self.reset) # self.bus.on('enclosure.mouth.talk', self.talk) # self.bus.on('enclosure.mouth.think', self.think) # self.bus.on('enclosure.mouth.listen', self.listen) # self.bus.on('enclosure.mouth.smile', self.smile) # self.bus.on('enclosure.mouth.viseme', self.viseme) # self.bus.on('enclosure.mouth.text', self.text) # self.bus.on('enclosure.mouth.display', self.display) # self.bus.on('enclosure.mouth.display_image', self.display_image) # self.bus.on('enclosure.weather.display', self.display_weather) # self.bus.on('recognizer_loop:record_begin', self.mouth.listen) # self.bus.on('recognizer_loop:record_end', self.mouth.reset) # self.bus.on('recognizer_loop:audio_output_start', self.mouth.talk) # self.bus.on('recognizer_loop:audio_output_end', self.mouth.reset) pass
def test_create_client(self, mock_conf): mc = MessageBusClient() assert mc.client.url == 'ws://testhost:1337/core'
def __init__(self, bus=None, timeout=5): if bus is None: bus = MessageBusClient() create_daemon(bus.run_forever) self.bus = bus self.timeout = timeout
class Enclosure: def __init__(self): # Load full config config = Configuration.get() self.lang = config['lang'] self.config = config.get("enclosure") self.global_config = config # Create Message Bus Client self.bus = MessageBusClient() self.gui = create_gui_service(self, config['gui_websocket']) # This datastore holds the data associated with the GUI provider. Data # is stored in Namespaces, so you can have: # self.datastore["namespace"]["name"] = value # Typically the namespace is a meaningless identifier, but there is a # special "SYSTEM" namespace. self.datastore = {} # self.loaded is a list, each element consists of a namespace named # tuple. # The namespace namedtuple has the properties "name" and "pages" # The name contains the namespace name as a string and pages is a # mutable list of loaded pages. # # [Namespace name, [List of loaded qml pages]] # [ # ["SKILL_NAME", ["page1.qml, "page2.qml", ... , "pageN.qml"] # [...] # ] self.loaded = [] # list of lists in order. self.explicit_move = True # Set to true to send reorder commands # Listen for new GUI clients to announce themselves on the main bus self.active_namespaces = [] self.bus.on("mycroft.gui.connected", self.on_gui_client_connected) self.register_gui_handlers() # First send any data: self.bus.on("gui.value.set", self.on_gui_set_value) self.bus.on("gui.page.show", self.on_gui_show_page) self.bus.on("gui.page.delete", self.on_gui_delete_page) self.bus.on("gui.clear.namespace", self.on_gui_delete_namespace) self.bus.on("gui.event.send", self.on_gui_send_event) self.bus.on("gui.status.request", self.handle_gui_status_request) # TODO: this requires the Enclosure to be up and running before the # training is complete. self.bus.on('mycroft.skills.trained', self.is_device_ready) def is_device_ready(self, message): is_ready = False # Bus service assumed to be alive if messages sent and received # Enclosure assumed to be alive if this method is running services = {'audio': False, 'speech': False, 'skills': False} start = time.monotonic() while not is_ready: is_ready = self.check_services_ready(services) if is_ready: break elif time.monotonic() - start >= 60: raise Exception('Timeout waiting for services start.') else: time.sleep(3) if is_ready: LOG.info("Mycroft is all loaded and ready to roll!") self.bus.emit(Message('mycroft.ready')) return is_ready def check_services_ready(self, services): """Report if all specified services are ready. services (iterable): service names to check. """ for ser in services: services[ser] = False response = self.bus.wait_for_response( Message('mycroft.{}.is_ready'.format(ser))) if response and response.data['status']: services[ser] = True return all([services[ser] for ser in services]) def run(self): """Start the Enclosure after it has been constructed.""" # Allow exceptions to be raised to the Enclosure Service # if they may cause the Service to fail. start_message_bus_client("ENCLOSURE", self.bus) def stop(self): """Perform any enclosure shutdown processes.""" pass ###################################################################### # GUI client API @property def gui_connected(self): """Returns True if at least 1 gui is connected, else False""" return len(GUIWebsocketHandler.clients) > 0 def handle_gui_status_request(self, message): """Reply to gui status request, allows querying if a gui is connected using the message bus""" self.bus.emit( message.reply("gui.status.request.response", {"connected": self.gui_connected})) def send(self, msg_dict): """ Send to all registered GUIs. """ for connection in GUIWebsocketHandler.clients: try: connection.send(msg_dict) except Exception as e: LOG.exception(repr(e)) def on_gui_send_event(self, message): """ Send an event to the GUIs. """ try: data = { 'type': 'mycroft.events.triggered', 'namespace': message.data.get('__from'), 'event_name': message.data.get('event_name'), 'params': message.data.get('params') } self.send(data) except Exception as e: LOG.error('Could not send event ({})'.format(repr(e))) def on_gui_set_value(self, message): data = message.data namespace = data.get("__from", "") # Pass these values on to the GUI renderers for key in data: if key not in RESERVED_KEYS: try: self.set(namespace, key, data[key]) except Exception as e: LOG.exception(repr(e)) def set(self, namespace, name, value): """ Perform the send of the values to the connected GUIs. """ if namespace not in self.datastore: self.datastore[namespace] = {} if self.datastore[namespace].get(name) != value: self.datastore[namespace][name] = value # If the namespace is loaded send data to GUI if namespace in [l.name for l in self.loaded]: msg = { "type": "mycroft.session.set", "namespace": namespace, "data": { name: value } } self.send(msg) def on_gui_delete_page(self, message): """ Bus handler for removing pages. """ page, namespace, _ = _get_page_data(message) try: with namespace_lock: self.remove_pages(namespace, page) except Exception as e: LOG.exception(repr(e)) def on_gui_delete_namespace(self, message): """ Bus handler for removing namespace. """ try: namespace = message.data['__from'] with namespace_lock: self.remove_namespace(namespace) except Exception as e: LOG.exception(repr(e)) def on_gui_show_page(self, message): try: page, namespace, index = _get_page_data(message) # Pass the request to the GUI(s) to pull up a page template with namespace_lock: self.show(namespace, page, index) except Exception as e: LOG.exception(repr(e)) def __find_namespace(self, namespace): for i, skill in enumerate(self.loaded): if skill[0] == namespace: return i return None def __insert_pages(self, namespace, pages): """ Insert pages into the namespace Args: namespace (str): Namespace to add to pages (list): Pages (str) to insert """ LOG.debug("Inserting new pages") if not isinstance(pages, list): raise ValueError('Argument must be list of pages') self.send({ "type": "mycroft.gui.list.insert", "namespace": namespace, "position": len(self.loaded[0].pages), "data": [{ "url": p } for p in pages] }) # Insert the pages into local reprensentation as well. updated = Namespace(self.loaded[0].name, self.loaded[0].pages + pages) self.loaded[0] = updated def __remove_page(self, namespace, pos): """ Delete page. Args: namespace (str): Namespace to remove from pos (int): Page position to remove """ LOG.debug("Deleting {} from {}".format(pos, namespace)) self.send({ "type": "mycroft.gui.list.remove", "namespace": namespace, "position": pos, "items_number": 1 }) # Remove the page from the local reprensentation as well. self.loaded[0].pages.pop(pos) # Add a check to return any display to idle from position 0 if (pos == 0 and len(self.loaded[0].pages) == 0): self.bus.emit(Message("mycroft.device.show.idle")) def __insert_new_namespace(self, namespace, pages): """ Insert new namespace and pages. This first sends a message adding a new namespace at the highest priority (position 0 in the namespace stack) Args: namespace (str): The skill namespace to create pages (str): Pages to insert (name matches QML) """ LOG.debug("Inserting new namespace") self.send({ "type": "mycroft.session.list.insert", "namespace": "mycroft.system.active_skills", "position": 0, "data": [{ "skill_id": namespace }] }) # Load any already stored Data data = self.datastore.get(namespace, {}) for key in data: msg = { "type": "mycroft.session.set", "namespace": namespace, "data": { key: data[key] } } self.send(msg) LOG.debug("Inserting new page") self.send({ "type": "mycroft.gui.list.insert", "namespace": namespace, "position": 0, "data": [{ "url": p } for p in pages] }) # Make sure the local copy is updated self.loaded.insert(0, Namespace(namespace, pages)) def __move_namespace(self, from_pos, to_pos): """ Move an existing namespace to a new position in the stack. Args: from_pos (int): Position in the stack to move from to_pos (int): Position to move to """ LOG.debug("Activating existing namespace") # Seems like the namespace is moved to the top automatically when # a page change is done. Deactivating this for now. if self.explicit_move: LOG.debug("move {} to {}".format(from_pos, to_pos)) self.send({ "type": "mycroft.session.list.move", "namespace": "mycroft.system.active_skills", "from": from_pos, "to": to_pos, "items_number": 1 }) # Move the local representation of the skill from current # position to position 0. self.loaded.insert(to_pos, self.loaded.pop(from_pos)) def __switch_page(self, namespace, pages): """ Switch page to an already loaded page. Args: pages (list): pages (str) to switch to namespace (str): skill namespace """ try: num = self.loaded[0].pages.index(pages[0]) except Exception as e: LOG.exception(repr(e)) num = 0 LOG.debug('Switching to already loaded page at ' 'index {} in namespace {}'.format(num, namespace)) self.send({ "type": "mycroft.events.triggered", "namespace": namespace, "event_name": "page_gained_focus", "data": { "number": num } }) def show(self, namespace, page, index): """ Show a page and load it as needed. Args: page (str or list): page(s) to show namespace (str): skill namespace index (int): ??? TODO: Unused in code ??? TODO: - Update sync to match. - Separate into multiple functions/methods """ LOG.debug("GUIConnection activating: " + namespace) pages = page if isinstance(page, list) else [page] # find namespace among loaded namespaces try: index = self.__find_namespace(namespace) if index is None: # This namespace doesn't exist, insert them first so they're # shown. self.__insert_new_namespace(namespace, pages) return else: # Namespace exists if index > 0: # Namespace is inactive, activate it by moving it to # position 0 self.__move_namespace(index, 0) # Find if any new pages needs to be inserted new_pages = [p for p in pages if p not in self.loaded[0].pages] if new_pages: self.__insert_pages(namespace, new_pages) else: # No new pages, just switch self.__switch_page(namespace, pages) except Exception as e: LOG.exception(repr(e)) def remove_namespace(self, namespace): """ Remove namespace. Args: namespace (str): namespace to remove """ index = self.__find_namespace(namespace) if index is None: return else: LOG.debug("Removing namespace {} at {}".format(namespace, index)) self.send({ "type": "mycroft.session.list.remove", "namespace": "mycroft.system.active_skills", "position": index, "items_number": 1 }) # Remove namespace from loaded namespaces self.loaded.pop(index) def remove_pages(self, namespace, pages): """ Remove the listed pages from the provided namespace. Args: namespace (str): The namespace to modify pages (list): List of page names (str) to delete """ try: index = self.__find_namespace(namespace) if index is None: return else: # Remove any pages that doesn't exist in the namespace pages = [p for p in pages if p in self.loaded[index].pages] # Make sure to remove pages from the back indexes = [self.loaded[index].pages.index(p) for p in pages] indexes = sorted(indexes) indexes.reverse() for page_index in indexes: self.__remove_page(namespace, page_index) except Exception as e: LOG.exception(repr(e)) ###################################################################### # GUI client socket # # The basic mechanism is: # 1) GUI client announces itself on the main messagebus # 2) Mycroft prepares a port for a socket connection to this GUI # 3) The port is announced over the messagebus # 4) The GUI connects on the socket # 5) Connection persists for graphical interaction indefinitely # # If the connection is lost, it must be renegotiated and restarted. def on_gui_client_connected(self, message): # GUI has announced presence LOG.info('GUI HAS ANNOUNCED!') port = self.global_config["gui_websocket"]["base_port"] LOG.debug("on_gui_client_connected") gui_id = message.data.get("gui_id") LOG.debug("Heard announcement from gui_id: {}".format(gui_id)) # Announce connection, the GUI should connect on it soon self.bus.emit( Message("mycroft.gui.port", { "port": port, "gui_id": gui_id })) def register_gui_handlers(self): # TODO: Register handlers for standard (Mark 1) events # self.bus.on('enclosure.eyes.on', self.on) # self.bus.on('enclosure.eyes.off', self.off) # self.bus.on('enclosure.eyes.blink', self.blink) # self.bus.on('enclosure.eyes.narrow', self.narrow) # self.bus.on('enclosure.eyes.look', self.look) # self.bus.on('enclosure.eyes.color', self.color) # self.bus.on('enclosure.eyes.level', self.brightness) # self.bus.on('enclosure.eyes.volume', self.volume) # self.bus.on('enclosure.eyes.spin', self.spin) # self.bus.on('enclosure.eyes.timedspin', self.timed_spin) # self.bus.on('enclosure.eyes.reset', self.reset) # self.bus.on('enclosure.eyes.setpixel', self.set_pixel) # self.bus.on('enclosure.eyes.fill', self.fill) # self.bus.on('enclosure.mouth.reset', self.reset) # self.bus.on('enclosure.mouth.talk', self.talk) # self.bus.on('enclosure.mouth.think', self.think) # self.bus.on('enclosure.mouth.listen', self.listen) # self.bus.on('enclosure.mouth.smile', self.smile) # self.bus.on('enclosure.mouth.viseme', self.viseme) # self.bus.on('enclosure.mouth.text', self.text) # self.bus.on('enclosure.mouth.display', self.display) # self.bus.on('enclosure.mouth.display_image', self.display_image) # self.bus.on('enclosure.weather.display', self.display_weather) # self.bus.on('recognizer_loop:record_begin', self.mouth.listen) # self.bus.on('recognizer_loop:record_end', self.mouth.reset) # self.bus.on('recognizer_loop:audio_output_start', self.mouth.talk) # self.bus.on('recognizer_loop:audio_output_end', self.mouth.reset) pass
# use the audio file as the audio source r = sr.Recognizer() with sr.AudioFile(wave_file) as source: audio = r.record(source) return audio class FileConsumer: def __init__(self, emitter=None): super(FileConsumer, self).__init__() self.stt = None self.emitter = emitter LOG.info("Creating SST interface") self.stt = STTFactory.create() def handle_audio(self, audiofile): audio = read_wave_file(audiofile) text = self.stt.execute(audio).lower().strip() self.emitter.emit( Message("recognizer_loop:utterance", {"utterances": [text]}, {"source": "wav_client"})) ws = MessageBusClient() config = Configuration.get() Configuration.set_config_update_handlers(ws) event_thread = Thread(target=connect) event_thread.setDaemon(True) event_thread.start() file_consumer = FileConsumer(emitter=ws)