From 398f432e1a02d21ce50424db9e1cd7bad7670e17 Mon Sep 17 00:00:00 2001 From: Ceda EI Date: Thu, 31 Dec 2020 06:02:03 +0530 Subject: [PATCH] Update plugins to new class-based structure --- plugins/__init__.py | 4 ++-- plugins/fuel_check.py | 53 +++++++++++++++++++++++++------------------ 2 files changed, 33 insertions(+), 24 deletions(-) diff --git a/plugins/__init__.py b/plugins/__init__.py index 1cebcf3..ca676a8 100644 --- a/plugins/__init__.py +++ b/plugins/__init__.py @@ -1,2 +1,2 @@ -from . import fuel_check -plugins = [fuel_check] +from .fuel_check import FuelCheck +plugins = [FuelCheck] diff --git a/plugins/fuel_check.py b/plugins/fuel_check.py index 3abdba4..535b77c 100644 --- a/plugins/fuel_check.py +++ b/plugins/fuel_check.py @@ -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