Update plugins to new class-based structure

This commit is contained in:
Ceda EI 2020-12-31 06:02:03 +05:30
parent c55899cd8a
commit 398f432e1a
2 changed files with 33 additions and 24 deletions

View File

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

View File

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