From 128a61706e5211b73eeb4cc5bbf4be4122b68a57 Mon Sep 17 00:00:00 2001 From: Franziska Kunsmann Date: Thu, 28 Nov 2024 08:27:36 +0100 Subject: [PATCH] bundles/infobeamer-monitor: some more improvements in status display --- bundles/infobeamer-monitor/files/monitor.py | 39 +++++++-------------- 1 file changed, 13 insertions(+), 26 deletions(-) diff --git a/bundles/infobeamer-monitor/files/monitor.py b/bundles/infobeamer-monitor/files/monitor.py index 5a253e3..e5755c2 100644 --- a/bundles/infobeamer-monitor/files/monitor.py +++ b/bundles/infobeamer-monitor/files/monitor.py @@ -25,7 +25,8 @@ logging.basicConfig( ) LOG = logging.getLogger("main") -MLOG = logging.getLogger("mqtt") +TZ = ZoneInfo("Europe/Berlin") +DUMP_TIME = "0900" state = None @@ -68,13 +69,12 @@ def mqtt_dump_state(device): device=device, ) +def is_dump_time(): + return datetime.now(TZ).strftime("%H%M") == DUMP_TIME mqtt_out("Monitor starting up") while True: try: - online_devices = set() - available_credits = None - try: r = get( "https://info-beamer.com/api/v1/device/list", @@ -83,9 +83,9 @@ while True: r.raise_for_status() ib_state = r.json()["devices"] except RequestException as e: - LOG.exception("Could not get data from info-beamer") + LOG.exception("Could not get device data from info-beamer") mqtt_out( - f"Could not get data from info-beamer: {e!r}", + f"Could not get device data from info-beamer: {e!r}", level="WARN", ) else: @@ -98,7 +98,8 @@ while True: continue new_state[did] = device - must_dump_state = False + # force information output for every online device at 09:00 CE(S)T + must_dump_state = is_dump_time() if state is not None: if did not in state: @@ -161,14 +162,6 @@ while True: else: LOG.info("adding device {} to empty state".format(device["id"])) - if device["is_online"]: - online_devices.add( - "{} ({})".format( - device["id"], - device["description"], - ) - ) - state = new_state try: @@ -179,13 +172,16 @@ while True: r.raise_for_status() ib_account = r.json() except RequestException as e: - LOG.exception("Could not get data from info-beamer") + LOG.exception("Could not get account data from info-beamer") mqtt_out( - f"Could not get data from info-beamer: {e!r}", + f"Could not get account data from info-beamer: {e!r}", level="WARN", ) else: available_credits = ib_account["balance"] + if is_dump_time(): + mqtt_out(f"Available Credits: {available_credits}") + if available_credits < 50: mqtt_out( f"balance has dropped below 50 credits! (available: {available_credits})", @@ -211,15 +207,6 @@ while True: level="WARN", ) - if datetime.now(ZoneInfo("Europe/Berlin")).strftime("%H%M") == "0900": - if available_credits is not None: - mqtt_out(f"Available Credits: {available_credits}") - - if online_devices: - mqtt_out( - "Online Devices: {}".format(", ".join(sorted(online_devices))) - ) - sleep(60) except KeyboardInterrupt: break