2020-12-31 00:11:46 +01:00
|
|
|
"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"
|
2020-12-31 01:32:39 +01:00
|
|
|
return requests.get(self.car_api).json()
|
2020-12-31 00:11:46 +01:00
|
|
|
|
|
|
|
def emit(self, event, data):
|
|
|
|
"Events data to OSD Backend"
|
|
|
|
json = {
|
|
|
|
"event": event,
|
|
|
|
"data": data
|
|
|
|
}
|
2020-12-31 01:32:39 +01:00
|
|
|
logging.info("Emitting to OSD Frontend")
|
|
|
|
logging.debug("%s", json)
|
2020-12-31 00:11:46 +01:00
|
|
|
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:
|
2020-12-31 01:32:39 +01:00
|
|
|
logging.debug("Checking plugin %s", plugin)
|
2020-12-31 00:11:46 +01:00
|
|
|
try:
|
|
|
|
plugin.check(data)
|
|
|
|
except Exception as error: # pylint: disable=broad-except
|
|
|
|
logging.error("Exception raised by %s: %s", plugin, error)
|