Add Daemon and DaemonPlugin classes
This commit is contained in:
		
							
								
								
									
										2
									
								
								daemon/__init__.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										2
									
								
								daemon/__init__.py
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,2 @@ | ||||
| from .daemon import Daemon | ||||
| from .daemon_plugin import DaemonPlugin | ||||
							
								
								
									
										38
									
								
								daemon/daemon.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										38
									
								
								daemon/daemon.py
									
									
									
									
									
										Normal file
									
								
							| @@ -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) | ||||
							
								
								
									
										30
									
								
								daemon/daemon_plugin.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										30
									
								
								daemon/daemon_plugin.py
									
									
									
									
									
										Normal file
									
								
							| @@ -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" | ||||
		Reference in New Issue
	
	Block a user