bundles/infobeamer-monitor: add account data monitoring

This commit is contained in:
Franzi 2024-11-03 17:40:46 +01:00
parent 2c83a5c4fc
commit 72638e0856
Signed by: kunsi
GPG key ID: 12E3D2136B818350

View file

@ -61,8 +61,6 @@ def mqtt_dump_state(device):
out.append("Location: {}".format(device["location"])) out.append("Location: {}".format(device["location"]))
out.append("Setup: {} ({})".format(device["setup"]["name"], device["setup"]["id"])) out.append("Setup: {} ({})".format(device["setup"]["name"], device["setup"]["id"]))
out.append("Resolution: {}".format(device["run"].get("resolution", "unknown"))) out.append("Resolution: {}".format(device["run"].get("resolution", "unknown")))
if not device["is_synced"]:
out.append("syncing ...")
mqtt_out( mqtt_out(
" - ".join(out), " - ".join(out),
@ -73,6 +71,9 @@ def mqtt_dump_state(device):
mqtt_out("Monitor starting up") mqtt_out("Monitor starting up")
while True: while True:
try: try:
online_devices = set()
available_credits = None
try: try:
r = get( r = get(
"https://info-beamer.com/api/v1/device/list", "https://info-beamer.com/api/v1/device/list",
@ -88,7 +89,6 @@ while True:
) )
else: else:
new_state = {} new_state = {}
online_devices = set()
for device in ib_state: for device in ib_state:
did = str(device["id"]) did = str(device["id"])
@ -140,16 +140,15 @@ while True:
if device["is_online"]: if device["is_online"]:
if device["maintenance"]: if device["maintenance"]:
mqtt_out( mqtt_out(
"maintenance required: {}".format(' '.join( "maintenance required: {}".format(
sorted(device["maintenance"]) " ".join(sorted(device["maintenance"]))
)), ),
level="WARN", level="WARN",
device=device, device=device,
) )
if ( if (
device["is_synced"] != state[did]["is_synced"] device["location"] != state[did]["location"]
or device["location"] != state[did]["location"]
or device["setup"]["id"] != state[did]["setup"]["id"] or device["setup"]["id"] != state[did]["setup"]["id"]
or device["run"].get("resolution") or device["run"].get("resolution")
!= state[did]["run"].get("resolution") != state[did]["run"].get("resolution")
@ -171,13 +170,56 @@ while True:
state = new_state state = new_state
if ( try:
datetime.now(timezone.utc).strftime("%H%M") == "1312" r = get(
and online_devices "https://info-beamer.com/api/v1/account",
and int(datetime.now(timezone.utc).strftime("%S")) < 30 auth=("", CONFIG["api_key"]),
): )
mqtt_out("Online Devices: {}".format(", ".join(sorted(online_devices)))) r.raise_for_status()
sleep(30) ib_account = r.json()
except RequestException as e:
LOG.exception("Could not get data from info-beamer")
mqtt_out(
f"Could not get data from info-beamer: {e!r}",
level="WARN",
)
else:
available_credits = ib_account["balance"]
if available_credits < 50:
mqtt_out(
f"balance has dropped below 50 credits! (available: {available_credits})",
level="ERROR",
)
elif available_credits < 100:
mqtt_out(
f"balance has dropped below 100 credits! (available: {available_credits})",
level="WARN",
)
for quota_name, quota_config in sorted(ib_account["quotas"].items()):
value = quota_config["count"]["value"]
limit = quota_config["count"]["limit"]
if value > limit * 0.9:
mqtt_out(
f"quota {quota_name} is over 90% (limit {limit}, value {value})",
level="ERROR",
)
elif value > limit * 0.8:
mqtt_out(
f"quota {quota_name} is over 80% (limit {limit}, value {value})",
level="WARN",
)
if datetime.now(timezone.utc).strftime("%H%M") == "1312":
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: except KeyboardInterrupt:
break break