Skip to content
This repository has been archived by the owner on Sep 8, 2024. It is now read-only.

Refactor - common bus connection method #2792

Merged
merged 6 commits into from
Jan 18, 2021
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 10 additions & 12 deletions mycroft/audio/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,12 @@

This handles playback of audio and speech
"""
from mycroft.configuration import Configuration
from mycroft.messagebus.client import MessageBusClient
from mycroft.util import reset_sigint_handler, wait_for_exit_signal, \
create_daemon, create_echo_function, check_for_signal
from mycroft.util import (
check_for_signal,
reset_sigint_handler,
start_message_bus_client,
wait_for_exit_signal
)
from mycroft.util.log import LOG

import mycroft.audio.speech as speech
Expand All @@ -39,24 +41,20 @@ def on_stopping():


def main(ready_hook=on_ready, error_hook=on_error, stopping_hook=on_stopping):
""" Main function. Run when file is invoked. """
"""Start the Audio Service and connect to the Message Bus"""
LOG.info("Starting Audio Service")
try:
reset_sigint_handler()
check_for_signal("isSpeaking")
bus = MessageBusClient() # Connect to the Mycroft Messagebus
Configuration.set_config_update_handlers(bus)
whitelist = ['mycroft.audio.service']
bus = start_message_bus_client("AUDIO", whitelist=whitelist)
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()
Expand Down
3 changes: 1 addition & 2 deletions mycroft/client/enclosure/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@

from mycroft.configuration import Configuration
from mycroft.messagebus.client import MessageBusClient
from mycroft.services import start_message_bus_client
from mycroft.util import create_daemon
from mycroft.util import create_daemon, start_message_bus_client
from mycroft.util.log import LOG

import json
Expand Down
16 changes: 8 additions & 8 deletions mycroft/client/speech/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,13 @@
from mycroft.configuration import Configuration
from mycroft.identity import IdentityManager
from mycroft.lock import Lock as PIDLock # Create/Support PID locking file
from mycroft.messagebus.client import MessageBusClient
from mycroft.messagebus.message import Message
from mycroft.util import create_daemon, wait_for_exit_signal, \
reset_sigint_handler, create_echo_function
from mycroft.util import (
create_daemon,
reset_sigint_handler,
start_message_bus_client,
wait_for_exit_signal
)
from mycroft.util.log import LOG

bus = None # Mycroft messagebus connection
Expand Down Expand Up @@ -208,7 +211,6 @@ def connect_bus_events(bus):
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'))


def main(ready_hook=on_ready, error_hook=on_error, stopping_hook=on_stopping,
Expand All @@ -219,15 +221,13 @@ def main(ready_hook=on_ready, error_hook=on_error, stopping_hook=on_stopping,
try:
reset_sigint_handler()
PIDLock("voice")
bus = MessageBusClient() # Mycroft messagebus, see mycroft.messagebus
Configuration.set_config_update_handlers(bus)
config = Configuration.get()
bus = start_message_bus_client("VOICE")
connect_bus_events(bus)

# 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)
Expand Down
15 changes: 0 additions & 15 deletions mycroft/services/__init__.py

This file was deleted.

45 changes: 0 additions & 45 deletions mycroft/services/util.py

This file was deleted.

23 changes: 2 additions & 21 deletions mycroft/skills/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,11 @@
from mycroft.audio import wait_while_speaking
from mycroft.enclosure.api import EnclosureAPI
from mycroft.configuration import Configuration
from mycroft.messagebus.client import MessageBusClient
from mycroft.messagebus.message import Message
from mycroft.util import (
connected,
create_echo_function,
create_daemon,
reset_sigint_handler,
start_message_bus_client,
wait_for_exit_signal
)
from mycroft.util.lang import set_active_lang
Expand Down Expand Up @@ -194,7 +192,7 @@ def main(ready_hook=on_ready, error_hook=on_error, stopping_hook=on_stopping,
set_active_lang(config.get('lang', 'en-us'))

# Connect this process to the Mycroft message bus
bus = _start_message_bus_client()
bus = start_message_bus_client("SKILLS")
_register_intent_services(bus)
event_scheduler = EventScheduler(bus)
skill_manager = _initialize_skill_manager(bus, watchdog)
Expand All @@ -215,23 +213,6 @@ def main(ready_hook=on_ready, error_hook=on_error, stopping_hook=on_stopping,
shutdown(skill_manager, event_scheduler)


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 _register_intent_services(bus):
"""Start up the all intent services and connect them as needed.

Expand Down
3 changes: 2 additions & 1 deletion mycroft/util/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@
curate_cache, get_cache_directory)
from .network_utils import connected
from .process_utils import (reset_sigint_handler, create_daemon,
wait_for_exit_signal, create_echo_function)
wait_for_exit_signal, create_echo_function,
start_message_bus_client)
from .log import LOG
from .parse import extract_datetime, extract_number, normalize
from .signal import check_for_signal, create_signal, get_ipc_directory
Expand Down
33 changes: 32 additions & 1 deletion mycroft/util/process_utils.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import json
import logging
import signal as sig
from threading import Thread
from threading import Event, Thread
from time import sleep

from .log import LOG
Expand Down Expand Up @@ -121,3 +121,34 @@ def echo(message):
# Listen for messages and echo them for logging
LOG(name).info("BUS: {}".format(message))
return echo


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