Skip to content

Commit

Permalink
house cleaning (#39)
Browse files Browse the repository at this point in the history
* fix/remove_pages

* 🎉

* tests

* signal breaking change

* restore mycroft.ready

refactor in follow up PR

* docstr

* fix/back_button

* fix/ready race condition

now allows core/gui to restart and load independently of each other

* fix homescreen on boot, it wouldnt show if ovos-gui loaded after ovos-core

refactor classes

* compat

* fix tests

* fix tests

---------

Co-authored-by: JarbasAi <[email protected]>
  • Loading branch information
NeonJarbas and JarbasAl authored Feb 8, 2024
1 parent 64d1f04 commit 676ffd7
Show file tree
Hide file tree
Showing 10 changed files with 263 additions and 265 deletions.
14 changes: 7 additions & 7 deletions ovos_gui/extensions.py
Original file line number Diff line number Diff line change
@@ -1,24 +1,23 @@
from ovos_bus_client import Message, MessageBusClient
from ovos_config.config import Configuration
from ovos_gui.namespace import NamespaceManager
from ovos_utils.log import LOG
from ovos_plugin_manager.gui import OVOSGuiFactory
from ovos_gui.homescreen import HomescreenManager


class ExtensionsManager:
def __init__(self, name: str, bus: MessageBusClient, gui: NamespaceManager):
def __init__(self, name: str, bus: MessageBusClient):
"""
Constructor for the Extension Manager. The Extension Manager is
responsible for managing the extensions that define additional GUI
behaviours for specific platforms.
@param name: Name of the extension manager
@param bus: MessageBus instance
@param gui: GUI instance
"""

self.name = name
self.bus = bus
self.gui = gui
self.homescreen_manager = HomescreenManager(self.bus)
core_config = Configuration()
enclosure_config = core_config.get("gui") or {}
self.active_extension = enclosure_config.get("extension", "generic")
Expand Down Expand Up @@ -53,10 +52,11 @@ def activate_extension(self, extension_id: str):
f"falling back to 'generic'")
cfg["module"] = "generic"
self.extension = OVOSGuiFactory.create(cfg, bus=self.bus)
self.extension.bind_homescreen()

LOG.info(f"Extensions Manager: Activated Extension {extension_id} "
f"({self.extension.__class__})")
self.extension.bind_homescreen(self.homescreen_manager)

LOG.info(f"Extensions Manager - Activated: {extension_id} "
f"({self.extension.__class__.__name__})")
self.bus.emit(
Message("extension.manager.activated", {"id": extension_id}))

Expand Down
109 changes: 34 additions & 75 deletions ovos_gui/homescreen.py
Original file line number Diff line number Diff line change
@@ -1,35 +1,26 @@
from threading import Thread
from typing import List, Optional

from ovos_bus_client import Message, MessageBusClient
from ovos_bus_client.message import dig_for_message
from ovos_config.config import Configuration, update_mycroft_config
from ovos_utils.log import LOG, log_deprecation

from ovos_utils.log import LOG, deprecated, log_deprecation

from ovos_gui.namespace import NamespaceManager
from threading import Thread
from ovos_bus_client import Message, MessageBusClient
from ovos_bus_client.message import dig_for_message


class HomescreenManager(Thread):
def __init__(self, bus: MessageBusClient, gui: NamespaceManager):
def __init__(self, bus: MessageBusClient):
super().__init__()
self.bus = bus
self.gui = gui
self.homescreens: List[dict] = []
self.mycroft_ready = False
# TODO: If service starts after `mycroft_ready`,
# homescreen is never shown

self.bus.on('homescreen.manager.add', self.add_homescreen)
self.bus.on('homescreen.manager.remove', self.remove_homescreen)
self.bus.on('homescreen.manager.list', self.get_homescreens)
self.bus.on("homescreen.manager.get_active",
self.handle_get_active_homescreen)
self.bus.on("homescreen.manager.set_active",
self.handle_set_active_homescreen)
self.bus.on("homescreen.manager.disable_active",
self.disable_active_homescreen)
self.bus.on("mycroft.mark2.register_idle",
self.register_old_style_homescreen)
self.bus.on("homescreen.manager.get_active", self.handle_get_active_homescreen)
self.bus.on("homescreen.manager.set_active", self.handle_set_active_homescreen)
self.bus.on("homescreen.manager.disable_active", self.disable_active_homescreen)
self.bus.on("homescreen.manager.show_active", self.show_homescreen)
self.bus.on("mycroft.ready", self.set_mycroft_ready)

Expand All @@ -43,10 +34,9 @@ def add_homescreen(self, message: Message):
"""
Handle `homescreen.manager.add` and add the requested homescreen if it
has not yet been added.
@param message: Message containing homescreen id/class to add
@param message: Message containing homescreen id to add
"""
homescreen_id = message.data["id"]
homescreen_class = message.data["class"]

if any((homescreen['id'] == homescreen_id
for homescreen in self.homescreens)):
Expand All @@ -55,7 +45,7 @@ def add_homescreen(self, message: Message):
LOG.info(f"Homescreen Manager: Adding Homescreen {homescreen_id}")
self.homescreens.append(message.data)

self.show_homescreen_on_add(homescreen_id, homescreen_class)
self.show_homescreen_on_add(homescreen_id)

def remove_homescreen(self, message: Message):
"""
Expand Down Expand Up @@ -103,10 +93,14 @@ def get_active_homescreen(self) -> Optional[dict]:
"""
gui_config = Configuration().get("gui") or {}
active_homescreen = gui_config.get("idle_display_skill")
LOG.debug(f"Homescreen Manager: Active Homescreen {active_homescreen}")
if not active_homescreen:
LOG.info("No homescreen enabled in mycroft.conf")
return
LOG.info(f"Active Homescreen: {active_homescreen}")
for h in self.homescreens:
if h["id"] == active_homescreen:
return active_homescreen
LOG.error(f"{active_homescreen} not loaded!")

def set_active_homescreen(self, homescreen_id: str):
"""
Expand All @@ -126,43 +120,31 @@ def reload_homescreens_list(self):
Emit a request for homescreens to register via the Messagebus
"""
LOG.info("Homescreen Manager: Reloading Homescreen List")
self.collect_old_style_homescreens()
self.bus.emit(Message("homescreen.manager.reload.list"))

def show_homescreen_on_add(self, homescreen_id: str, homescreen_class: str):
def show_homescreen_on_add(self, homescreen_id: str):
"""
Check if a homescreen should be displayed immediately upon addition
@param homescreen_id: ID of added homescreen
@param homescreen_class: "class" (IdleDisplaySkill, MycroftSkill)
of homescreen
"""
if not self.mycroft_ready:
LOG.debug("Not ready yet, don't display")
LOG.debug("Not ready yet, don't display homescreen")
return
LOG.debug(f"Checking {homescreen_id}")
if self.get_active_homescreen() != homescreen_id:
# Added homescreen isn't the configured one, do nothing
return

if homescreen_class == "IdleDisplaySkill":
LOG.debug(f"Displaying Homescreen {homescreen_id}")
self.bus.emit(Message("homescreen.manager.activate.display",
{"homescreen_id": homescreen_id}))
elif homescreen_class == "MycroftSkill":
log_deprecation(f"Homescreen skills should register listeners for "
f"`homescreen.manager.activate.display`. "
f"`{homescreen_id}.idle` messages will be removed.",
"0.1.0")
LOG.debug(f"Displaying Homescreen {homescreen_id}")
self.bus.emit(Message(f"{homescreen_id}.idle"))
LOG.info(f"Displaying Homescreen {homescreen_id}")
self.bus.emit(Message("homescreen.manager.activate.display",
{"homescreen_id": homescreen_id}))

def disable_active_homescreen(self, message: Message):
"""
Handle `homescreen.manager.disable_active` requests by configuring the
`idle_display_skill` as None.
@param message: Message requesting homescreen disable
"""
# TODO: Is this valid behavior?
if Configuration().get("gui", {}).get("idle_display_skill"):
LOG.info(f"Disabling idle_display_skill!")
new_config = {"gui": {"idle_display_skill": None}}
Expand All @@ -174,51 +156,28 @@ def show_homescreen(self, message: Optional[Message] = None):
@param message: Optional `homescreen.manager.show_active` Message
"""
active_homescreen = self.get_active_homescreen()
LOG.debug(f"Requesting activation of {active_homescreen}")
if not active_homescreen:
LOG.info("No active homescreen to display")
return
LOG.info(f"Requesting activation of {active_homescreen}")
for h in self.homescreens:
if h.get("id") == active_homescreen:
LOG.debug(f"matched homescreen skill: {h}")
message = message or dig_for_message() or Message("")
if h["class"] == "IdleDisplaySkill":
LOG.debug(f"Displaying Homescreen {active_homescreen}")
self.bus.emit(message.forward(
"homescreen.manager.activate.display",
{"homescreen_id": active_homescreen}))
elif h["class"] == "MycroftSkill":
LOG.debug(f"Displaying Homescreen {active_homescreen}")
self.bus.emit(message.forward(f"{active_homescreen}.idle"))
else:
LOG.error(f"Requested homescreen has an invalid class: {h}")
return
LOG.warning(f"Requested {active_homescreen} not found in: "
f"{self.homescreens}")
LOG.debug(f"Displaying Homescreen {active_homescreen}")
self.bus.emit(message.forward(
"homescreen.manager.activate.display",
{"homescreen_id": active_homescreen}))
break
else:
LOG.warning(f"Requested {active_homescreen} not found in: "
f"{self.homescreens}")

def set_mycroft_ready(self, message: Message):
"""
Handle `mycroft.ready` and show the homescreen
@param message: `mycroft.ready` Message
"""
self.mycroft_ready = True
self.reload_homescreens_list()
self.show_homescreen()

# Add compabitility with older versions of the Resting Screen Class

def collect_old_style_homescreens(self):
"""Trigger collection of older resting screens."""
# TODO: Deprecate in 0.1.0
self.bus.emit(Message("mycroft.mark2.collect_idle"))

@deprecated("`mycroft.mark2.collect_idle` responses are deprecated",
"0.1.0")
def register_old_style_homescreen(self, message):
if "name" in message.data and "id" in message.data:
super_class_name = "MycroftSkill"
super_class_object = message.data["name"]
skill_id = message.data["id"]
_homescreen_entry = {"class": super_class_name,
"name": super_class_object, "id": skill_id}
LOG.debug(f"Homescreen Manager: Adding OLD Homescreen {skill_id}")
self.add_homescreen(
Message("homescreen.manager.add", _homescreen_entry))
else:
LOG.error("Malformed idle screen registration received")
Loading

0 comments on commit 676ffd7

Please sign in to comment.