Background-Daemon/daemon/daemon.py

41 lines
1.2 KiB
Python

"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).json()
def emit(self, event, data):
"Events data to OSD Backend"
json = {
"event": event,
"data": data
}
logging.info("Emitting to OSD Frontend")
logging.debug("%s", 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:
logging.debug("Checking plugin %s", plugin)
try:
plugin.check(data)
except Exception as error: # pylint: disable=broad-except
logging.error("Exception raised by %s: %s", plugin, error)