Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix/scheduled_event message.context #197

Closed
wants to merge 1 commit into from
Closed
Changes from all 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
55 changes: 4 additions & 51 deletions ovos_utils/messagebus.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,17 +30,9 @@ def dig_for_message():
class FakeBus:
def __init__(self, *args, **kwargs):
self.started_running = False
self.session_id = "default"
self.ee = kwargs.get("emitter") or BaseEventEmitter()
self.ee.on("error", self.on_error)
self.on_open()
try:
self.session_id = kwargs["session"].session_id
except:
pass # don't care

self.on("ovos.session.update_default",
self.on_default_session_update)

def on(self, msg_type, handler):
self.ee.on(msg_type, handler)
Expand All @@ -49,47 +41,8 @@ def once(self, msg_type, handler):
self.ee.once(msg_type, handler)

def emit(self, message):
if "session" not in message.context:
try: # replicate side effects
from ovos_bus_client.session import Session, SessionManager
sess = SessionManager.sessions.get(self.session_id) or \
Session(self.session_id)
message.context["session"] = sess.serialize()
except ImportError: # don't care
message.context["session"] = {"session_id": self.session_id}
self.ee.emit("message", message.serialize())
self.ee.emit(message.msg_type, message)
self.on_message(message.serialize())

def on_message(self, *args):
"""
Handle an incoming websocket message
@param args:
message (str): serialized Message
"""
if len(args) == 1:
message = args[0]
else:
message = args[1]
parsed_message = Message.deserialize(message)
try: # replicate side effects
from ovos_bus_client.session import Session, SessionManager
sess = Session.from_message(parsed_message)
if sess.session_id != "default":
# 'default' can only be updated by core
SessionManager.update(sess)
except ImportError:
pass # don't care

def on_default_session_update(self, message):
try: # replicate side effects
from ovos_bus_client.session import Session, SessionManager
new_session = message.data["session_data"]
sess = Session.deserialize(new_session)
SessionManager.update(sess, make_default=True)
LOG.debug("synced default_session")
except ImportError:
pass # don't care

def wait_for_message(self, message_type, timeout=3.0):
"""Wait for a message of a specific type.
Expand Down Expand Up @@ -182,13 +135,13 @@ def __instancecheck__(self, instance):
from ovos_bus_client.message import Message as _MycroftMessage
if isinstance(instance, _MycroftMessage):
return True
except ImportError:
except:
pass
try:
from mycroft_bus_client.message import Message as _MycroftMessage
if isinstance(instance, _MycroftMessage):
return True
except ImportError:
except:
pass
return super().__instancecheck__(instance)

Expand All @@ -201,12 +154,12 @@ def __new__(cls, *args, **kwargs):
try: # most common case
from ovos_bus_client import Message as _M
return _M(*args, **kwargs)
except ImportError:
except:
pass
try: # some old install that upgraded during migration period
from mycroft_bus_client import Message as _M
return _M(*args, **kwargs)
except ImportError: # FakeMessage
except: # FakeMessage
return super().__new__(cls)

def __init__(self, msg_type, data=None, context=None):
Expand Down