Compare commits

..

No commits in common. "ff73d81d2d6599fe554055490297f85b67c2071f" and "777d79aec8a6d6c2809709e735f069a22d335466" have entirely different histories.

8 changed files with 49 additions and 158 deletions

View File

@ -1,25 +0,0 @@
#!/usr/bin/env python
"Background Daemon"
import time
import logging
from mycroft_bus_client import MessageBusClient
import config
from daemon import Daemon
from plugins import plugins
def main():
"Main function"
logging.basicConfig(level=logging.DEBUG)
mycroft = MessageBusClient()
mycroft.run_in_thread()
daemon = Daemon(config.OSD_URL, config.CAR_API_URL, mycroft)
for plugin in plugins:
daemon.register_plugin(plugin)
while True:
daemon.check_all()
time.sleep(config.DELAY)
if __name__ == "__main__":
main()

25
daemon.py Executable file
View File

@ -0,0 +1,25 @@
#!/usr/bin/env python
import time
import requests
import config
from plugins import plugins
def emit(event, data):
json = {
"event": event,
"data": data
}
print(json)
return requests.post(config.OSD_URL, json=json)
def main():
data = requests.get(config.CAR_API_URL)
for plugin in plugins:
plugin.check(data.json(), emit)
if __name__ == "__main__":
while True:
main()
time.sleep(config.DELAY)

View File

@ -1,2 +0,0 @@
from .daemon import Daemon
from .daemon_plugin import DaemonPlugin

View File

@ -1,40 +0,0 @@
"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)

View File

@ -1,30 +0,0 @@
"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"

View File

@ -1,2 +1,2 @@
from .fuel_check import FuelCheck from . import fuel_check
plugins = [FuelCheck] plugins = [fuel_check]

View File

@ -1,28 +0,0 @@
"AirBags"
import time
from daemon import DaemonPlugin
class AirBags(DaemonPlugin):
"Checks AirBags and sends relevant warnings"
last_message = None
start = None
def initialize(self):
"Initialize plugin"
self.start = time.monotonic()
def check(self, data):
"Checks if the air bags are deployed"
if "AirBags-Deploy" not in data:
return
if data["AirBags-Deploy"]:
if self.last_message is None:
self.emit("switchPlugin", { "plugin": "accident" })
message = ("Airbags were released. Calling ambulance in 20 "
"seconds. Tap screen to cancel.")
self.speak(message)
self.last_message = time.monotonic()
else:
self.last_message = None

View File

@ -1,35 +1,26 @@
"Fuel Check"
import time import time
from daemon import DaemonPlugin start = time.monotonic()
last_message = None
threshold = 5
class FuelCheck(DaemonPlugin): def check(data, emit):
start = None "Checks if the fuel ratio is below a certain threshold"
last_message = None global last_message
threshold = 5 if "FuelRatio" not in data:
return
def initialize(self): ratio = data["FuelRatio"]
"Initialize plugin" if ratio <= threshold:
self.start = time.monotonic() if last_message is None:
emit("switchPlugin", {
def check(self, data): "plugin": "warning",
"Checks if the fuel ratio is below a certain threshold" "data": {
if "FuelRatio" not in data: "title": "Low Fuel",
return "description": f"Only {int(ratio)}% fuel left. Please refill the tank."
},
ratio = data["FuelRatio"] "time": 5000
if ratio <= self.threshold: })
if self.last_message is None: last_message = time.monotonic()
desc = f"Only {int(ratio)}% fuel left. Please refill the tank." else:
self.speak(desc) last_message = None
self.emit("switchPlugin", {
"plugin": "warning",
"data": {
"title": "Low Fuel",
"description": desc
},
"time": 5000
})
self.last_message = time.monotonic()
else:
self.last_message = None