EDMC-FuelStatus/load.py

61 lines
1.8 KiB
Python
Raw Permalink Normal View History

2020-12-05 16:58:46 +01:00
import tkinter as tk
import logging
import l10n
import functools
import os
2020-12-05 16:58:46 +01:00
from typing import Optional, Tuple, Dict, Any
2020-12-05 16:58:46 +01:00
from config import appname
plugin_name = os.path.basename(os.path.dirname(__file__))
logger = logging.getLogger(f'{appname}.{plugin_name}')
_ = functools.partial(l10n.Translations.translate, context=__file__)
label: Optional[tk.Label]
status: Optional[tk.Label]
main_tank: Optional[float] = None
reservoir: Optional[float] = None
2020-12-05 16:58:46 +01:00
2020-12-07 00:26:50 +01:00
def plugin_start3(plugin_dir: str) -> str:
2020-12-05 16:58:46 +01:00
logger.debug('fuelstatus plugin loaded')
2020-12-05 18:20:14 +01:00
return "FuelStatus"
2020-12-05 16:58:46 +01:00
def plugin_stop() -> None:
pass
def prefs_changed(cmdr: str, is_beta: bool) -> None:
update_status()
2020-12-05 16:58:46 +01:00
def plugin_app(parent) -> Tuple[tk.Label,tk.Label]:
global label, status
label = tk.Label(parent, text="")
status = tk.Label(parent, text="")
update_status()
return (label, status)
def dashboard_entry(cmdr: str, is_beta: bool, entry: Dict[str, Any]) -> None:
global main_tank, reservoir
main_tank = None
2020-12-05 16:58:46 +01:00
reservoir = None
if "Fuel" in entry:
if "FuelMain" in entry["Fuel"]:
main_tank = entry["Fuel"]["FuelMain"]
2020-12-05 16:58:46 +01:00
if "FuelReservoir" in entry["Fuel"]:
reservoir = entry["Fuel"]["FuelReservoir"]
update_status()
def update_status() -> None:
global label, status
2020-12-05 16:58:46 +01:00
label["text"] = f'{_("Fuel levels")}:'
if main_tank is None or reservoir is None:
if main_tank is None and reservoir is None:
2020-12-05 16:58:46 +01:00
status["text"] = _("waiting for data …")
else:
# maybe add error handling for this weird edge case, should it ever exist …
status["text"] = _("ERROR")
logger.error("One of main tank and reservoir fuel levels is None, the other isnt … WTF?")
else:
status["text"] = f'{round(main_tank,3)}t ({_("main")}), {round(reservoir,3)}t ({_("reservoir")})'