diff --git a/mycroft/messagebus/client/client.py b/mycroft/messagebus/client/client.py index 5748f4399e6..5fe62ff7acc 100644 --- a/mycroft/messagebus/client/client.py +++ b/mycroft/messagebus/client/client.py @@ -1,50 +1,6 @@ -# Copyright 2019 Mycroft AI Inc. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# - -from ovos_bus_client.client import MessageBusClient as _MessageBusClient +from ovos_bus_client.client.client import MessageBusClient, echo from ovos_bus_client.client import MessageWaiter - from mycroft.messagebus.load_config import load_message_bus_config -import mycroft.util.process_utils - - -class MessageBusClient(_MessageBusClient): - # minimize reading of the .conf - _config_cache = None - - def __init__(self, host=None, port=None, route=None, ssl=None, cache=False): - config_overrides = dict(host=host, port=port, route=route, ssl=ssl) - if cache and self._config_cache: - config = self._config_cache - else: - config = load_message_bus_config(**config_overrides) - if cache: - MessageBusClient._config_cache = config - super().__init__(config.host, config.port, config.route, config.ssl) - - -def echo(): - message_bus_client = MessageBusClient() - - def repeat_utterance(message): - message.msg_type = 'speak' - message_bus_client.emit(message) - - message_bus_client.on('message', mycroft.util.process_utils.create_echo_function(None)) - message_bus_client.on('recognizer_loop:utterance', repeat_utterance) - message_bus_client.run_forever() if __name__ == "__main__": diff --git a/mycroft/messagebus/load_config.py b/mycroft/messagebus/load_config.py index 9573d865a73..639063cce9f 100644 --- a/mycroft/messagebus/load_config.py +++ b/mycroft/messagebus/load_config.py @@ -1,53 +1 @@ -# Copyright 2019 Mycroft AI Inc. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# -"""Message bus configuration loader. - -The message bus event handler and client use basically the same configuration. -This code is re-used in both to load config values. -""" -from collections import namedtuple - -from ovos_config.config import Configuration -from mycroft.util.log import LOG - -MessageBusConfig = namedtuple( - 'MessageBusConfig', - ['host', 'port', 'route', 'ssl'] -) - - -def load_message_bus_config(**overrides): - """Load the bits of device configuration needed to run the message bus.""" - LOG.info('Loading message bus configs') - config = Configuration() - - try: - websocket_configs = config['websocket'] - except KeyError as ke: - LOG.error('No websocket configs found ({})'.format(repr(ke))) - raise - else: - mb_config = MessageBusConfig( - host=overrides.get('host') or websocket_configs.get('host'), - port=overrides.get('port') or websocket_configs.get('port'), - route=overrides.get('route') or websocket_configs.get('route'), - ssl=overrides.get('ssl') or config.get('ssl') - ) - if not all([mb_config.host, mb_config.port, mb_config.route]): - error_msg = 'Missing one or more websocket configs' - LOG.error(error_msg) - raise ValueError(error_msg) - - return mb_config +from ovos_bus_client.conf import load_message_bus_config, client_from_config, MessageBusConfig diff --git a/mycroft/messagebus/message.py b/mycroft/messagebus/message.py index a887a0e2ac1..89e662c35e0 100644 --- a/mycroft/messagebus/message.py +++ b/mycroft/messagebus/message.py @@ -1,41 +1 @@ -# Copyright 2017 Mycroft AI Inc. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# -import re - -from mycroft.util.parse import normalize -from ovos_bus_client.message import dig_for_message -import ovos_bus_client - - -class Message(ovos_bus_client.Message): - """Mycroft specific Message class.""" - - def utterance_remainder(self): - """ - For intents get the portion not consumed by Adapt. - - For example: if they say 'Turn on the family room light' and there are - entity matches for "turn on" and "light", then it will leave behind - " the family room " which is then normalized to "family room". - - Returns: - str: Leftover words or None if not an utterance. - """ - utt = normalize(self.data.get("utterance", "")) - if utt and "__tags__" in self.data: - for token in self.data["__tags__"]: - # Substitute only whole words matching the token - utt = re.sub(r'\b' + token.get("key", "") + r"\b", "", utt) - return normalize(utt) +from ovos_bus_client.message import dig_for_message, Message diff --git a/mycroft/messagebus/send.py b/mycroft/messagebus/send.py index 1a250e3cfa2..4ee0cda70b6 100644 --- a/mycroft/messagebus/send.py +++ b/mycroft/messagebus/send.py @@ -15,7 +15,7 @@ import sys import json -from mycroft.messagebus.send_func import send +from ovos_bus_client.send_func import send def main(): diff --git a/mycroft/messagebus/send_func.py b/mycroft/messagebus/send_func.py index abc26313807..f1f1d5a88a3 100644 --- a/mycroft/messagebus/send_func.py +++ b/mycroft/messagebus/send_func.py @@ -1,46 +1 @@ -# Copyright 2019 Mycroft AI Inc. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# -from websocket import create_connection - -from ovos_config.config import Configuration -from mycroft.messagebus.client import MessageBusClient -from mycroft.messagebus.message import Message - - -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() - 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() +from ovos_bus_client.send_func import send diff --git a/requirements/minimal.txt b/requirements/minimal.txt index f5e2cebc844..15cfaea27c9 100644 --- a/requirements/minimal.txt +++ b/requirements/minimal.txt @@ -3,7 +3,7 @@ mycroft-messagebus-client~=0.9,!=0.9.2,!=0.9.3 combo-lock~=0.2 ovos-utils~=0.0, >=0.0.26 ovos-plugin-manager~=0.0, >=0.0.20a1 -ovos-bus-client~=0.0, >=0.0.1 +ovos-bus-client~=0.0, >=0.0.2 ovos-config~=0.0,>=0.0.5 python-dateutil~=2.6 ovos-lingua-franca==0.4.6a1 diff --git a/requirements/requirements.txt b/requirements/requirements.txt index cb2fc70e47b..38ed56faf96 100644 --- a/requirements/requirements.txt +++ b/requirements/requirements.txt @@ -8,7 +8,7 @@ combo-lock~=0.2 PyYAML~=5.4 watchdog -ovos-bus-client~=0.0, >=0.0.1 +ovos-bus-client~=0.0, >=0.0.2 ovos_backend_client~=0.0, >=0.0.5 ovos-config~=0.0,>=0.0.5a5 ovos-utils~=0.0, >=0.0.26