diff --git a/daemon/__init__.py b/daemon/__init__.py new file mode 100644 index 0000000..c44973f --- /dev/null +++ b/daemon/__init__.py @@ -0,0 +1,2 @@ +from .daemon import Daemon +from .daemon_plugin import DaemonPlugin diff --git a/daemon/daemon.py b/daemon/daemon.py new file mode 100644 index 0000000..46c1817 --- /dev/null +++ b/daemon/daemon.py @@ -0,0 +1,38 @@ +"Daemon class" +import logging +import requests + +class Daemon: + "Daemon " + def __init__(self, osd_backend, car_api, messagebus_client): + self.plugins = [] + self.osd_backend = osd_backend + self.car_api = car_api + self.messagebus_client = messagebus_client + + def register_plugin(self, plugin_class): + "Registers a plugin" + plugin = plugin_class(self) + self.plugins.append(plugin) + + def get_data(self): + "Gets data from car api" + return requests.get(self.car_api) + + def emit(self, event, data): + "Events data to OSD Backend" + json = { + "event": event, + "data": data + } + print(json) + return requests.post(self.osd_backend, json=json) + + def check_all(self): + "Checks all the plugins" + data = self.get_data() + for plugin in self.plugins: + try: + plugin.check(data) + except Exception as error: # pylint: disable=broad-except + logging.error("Exception raised by %s: %s", plugin, error) diff --git a/daemon/daemon_plugin.py b/daemon/daemon_plugin.py new file mode 100644 index 0000000..27282a4 --- /dev/null +++ b/daemon/daemon_plugin.py @@ -0,0 +1,30 @@ +"defines DaemonPlugin base class" +from abc import ABC, abstractmethod +from mycroft_bus_client import Message + +class DaemonPlugin(ABC): + "Abstract class for Plugins to inherit from" + + def __init__(self, daemon): + self.daemon = daemon + self.messagebus_client = daemon.messagebus_client + self.initialize() + + def initialize(self): + """ + Initialize is called after the plugin object has been created. Add + any handlers for messagebus_client here + """ + + def emit(self, event, data): + "Emits a message for OSD frontend" + self.daemon.emit(event, data) + + def speak(self, utterance): + "Speaks the given string" + message = Message("speak", {"utterance": utterance}) + self.messagebus_client.emit(message) + + @abstractmethod + def check(self, data): + "Implement the core checks here"