Skip to content

Commit

Permalink
deprecate bus utils
Browse files Browse the repository at this point in the history
  • Loading branch information
JarbasAl committed Dec 28, 2023
1 parent 72a7579 commit 1e8bc20
Show file tree
Hide file tree
Showing 2 changed files with 1 addition and 175 deletions.
2 changes: 1 addition & 1 deletion ovos_utils/gui.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from typing import List

from ovos_utils.log import LOG
from ovos_utils.messagebus import wait_for_reply
from ovos_bus_client.util import wait_for_reply
from ovos_utils.system import is_installed, has_screen, is_process_running

_default_gui_apps = (
Expand Down
174 changes: 0 additions & 174 deletions ovos_utils/messagebus.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,6 @@
from ovos_utils.json_helper import merge_dict
from ovos_utils.log import LOG, log_deprecation

_DEFAULT_WS_CONFIG = {"host": "0.0.0.0",
"port": 8181,
"route": "/core",
"ssl": False}


def dig_for_message():
try:
Expand Down Expand Up @@ -352,172 +347,3 @@ def __int__(self, *args, **kwargs):
"0.1.0")
FakeMessage.__init__(self, *args, **kwargs)


def get_message_lang(message=None):
"""Get the language from the message or the default language.
Args:
message: message to check for language code.
Returns:
The language code from the message or the default language.
"""
try:
from ovos_config.locale import get_default_lang
default_lang = get_default_lang()
except ImportError:
LOG.warning("ovos_config not available. Using default lang en-us")
default_lang = "en-us"
message = message or dig_for_message()
if not message:
return default_lang
lang = message.data.get("lang") or message.context.get("lang") or default_lang
return lang.lower()


def get_websocket(host, port, route='/', ssl=False, threaded=True):
"""
Returns a connection to a websocket
"""
from ovos_bus_client import MessageBusClient

client = MessageBusClient(host, port, route, ssl)
if threaded:
client.run_in_thread()
return client


def get_mycroft_bus(host: str = None, port: int = None, route: str = None,
ssl: bool = None):
"""
Returns a connection to the mycroft messagebus
"""
try:
from ovos_config.config import read_mycroft_config
config = read_mycroft_config().get('websocket') or dict()
except ImportError:
LOG.warning("ovos_config not available. Falling back to default WS")
config = dict()
host = host or config.get('host') or _DEFAULT_WS_CONFIG['host']
port = port or config.get('port') or _DEFAULT_WS_CONFIG['port']
route = route or config.get('route') or _DEFAULT_WS_CONFIG['route']
if ssl is None:
ssl = config.get('ssl') if 'ssl' in config else \
_DEFAULT_WS_CONFIG['ssl']
return get_websocket(host, port, route, ssl)


def listen_for_message(msg_type, handler, bus=None):
"""
Continuously listens and reacts to a specific messagetype on the mycroft messagebus
NOTE: when finished you should call bus.remove(msg_type, handler)
"""
bus = bus or get_mycroft_bus()
bus.on(msg_type, handler)
return bus


def listen_once_for_message(msg_type, handler, bus=None):
"""
listens and reacts once to a specific messagetype on the mycroft messagebus
"""
auto_close = bus is None
bus = bus or get_mycroft_bus()

def _handler(message):
handler(message)
if auto_close:
bus.close()

bus.once(msg_type, _handler)
return bus


def wait_for_reply(message, reply_type=None, timeout=3.0, bus=None):
"""Send a message and wait for a response.
Args:
message (FakeMessage or str or dict): message object or type to send
reply_type (str): the message type of the expected reply.
Defaults to "<message.type>.response".
timeout: seconds to wait before timeout, defaults to 3
Returns:
The received message or None if the response timed out
"""
auto_close = bus is None
bus = bus or get_mycroft_bus()
if isinstance(message, str):
try:
message = json.loads(message)
except:
pass
if isinstance(message, str):
message = FakeMessage(message)
elif isinstance(message, dict):
message = FakeMessage(message["type"],
message.get("data"),
message.get("context"))
elif not isinstance(message, FakeMessage):
raise ValueError
response = bus.wait_for_response(message, reply_type, timeout)
if auto_close:
bus.close()
return response


def send_message(message, data=None, context=None, bus=None):
auto_close = bus is None
bus = bus or get_mycroft_bus()
if isinstance(message, str):
if isinstance(data, dict) or isinstance(context, dict):
message = FakeMessage(message, data, context)
else:
try:
message = json.loads(message)
except:
message = FakeMessage(message)
if isinstance(message, dict):
message = FakeMessage(message["type"],
message.get("data"),
message.get("context"))
if not isinstance(message, FakeMessage):
raise ValueError
bus.emit(message)
if auto_close:
bus.close()


def send_binary_data_message(binary_data, msg_type="mycroft.binary.data",
msg_data=None, msg_context=None, bus=None):
msg_data = msg_data or {}
msg = {
"type": msg_type,
"data": merge_dict(msg_data, {"binary": binary_data.hex()}),
"context": msg_context or None
}
send_message(msg, bus=bus)


def send_binary_file_message(filepath, msg_type="mycroft.binary.file",
msg_context=None, bus=None):
with open(filepath, 'rb') as f:
binary_data = f.read()
msg_data = {"path": filepath}
send_binary_data_message(binary_data, msg_type=msg_type, msg_data=msg_data,
msg_context=msg_context, bus=bus)


def decode_binary_message(message):
if isinstance(message, str):
try: # json string
message = json.loads(message)
binary_data = message.get("binary") or message["data"]["binary"]
except: # hex string
binary_data = message
elif isinstance(message, dict):
# data field or serialized message
binary_data = message.get("binary") or message["data"]["binary"]
else:
# message object
binary_data = message.data["binary"]
# decode hex string
return bytearray.fromhex(binary_data)

0 comments on commit 1e8bc20

Please sign in to comment.