2023-12-26 13:50:24 +00:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
|
|
|
|
import logging
|
2024-11-16 13:14:05 +00:00
|
|
|
from datetime import datetime
|
2023-12-26 13:50:24 +00:00
|
|
|
from json import dumps
|
|
|
|
from time import sleep
|
2024-11-16 13:14:05 +00:00
|
|
|
from zoneinfo import ZoneInfo
|
2023-12-26 13:50:24 +00:00
|
|
|
|
|
|
|
import paho.mqtt.client as mqtt
|
|
|
|
from requests import RequestException, get
|
|
|
|
|
|
|
|
try:
|
|
|
|
# python 3.11
|
|
|
|
from tomllib import loads as toml_load
|
|
|
|
except ImportError:
|
|
|
|
from rtoml import load as toml_load
|
|
|
|
|
|
|
|
with open("config.toml") as f:
|
|
|
|
CONFIG = toml_load(f.read())
|
|
|
|
|
|
|
|
|
|
|
|
logging.basicConfig(
|
|
|
|
format="[%(levelname)s %(name)s] %(message)s",
|
|
|
|
level=logging.INFO,
|
|
|
|
)
|
|
|
|
|
|
|
|
LOG = logging.getLogger("main")
|
2024-11-28 07:27:36 +00:00
|
|
|
TZ = ZoneInfo("Europe/Berlin")
|
|
|
|
DUMP_TIME = "0900"
|
2023-12-26 13:50:24 +00:00
|
|
|
|
|
|
|
state = None
|
|
|
|
|
|
|
|
client = mqtt.Client()
|
|
|
|
client.username_pw_set(CONFIG["mqtt"]["user"], CONFIG["mqtt"]["password"])
|
|
|
|
client.connect(CONFIG["mqtt"]["host"], 1883, 60)
|
|
|
|
client.loop_start()
|
|
|
|
|
|
|
|
|
|
|
|
def mqtt_out(message, level="INFO", device=None):
|
|
|
|
key = "infobeamer"
|
|
|
|
if device:
|
|
|
|
key += f"/{device['id']}"
|
2024-12-08 14:49:19 +00:00
|
|
|
if device["description"]:
|
|
|
|
message = f"[{device['description']}] {message}"
|
|
|
|
else:
|
|
|
|
message = f"[{device['serial']}] {message}"
|
2023-12-26 13:50:24 +00:00
|
|
|
|
|
|
|
client.publish(
|
|
|
|
CONFIG["mqtt"]["topic"],
|
|
|
|
dumps(
|
|
|
|
{
|
|
|
|
"level": level,
|
|
|
|
"component": key,
|
|
|
|
"msg": message,
|
|
|
|
}
|
|
|
|
),
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
def mqtt_dump_state(device):
|
2023-12-27 13:55:15 +00:00
|
|
|
if not device["is_online"]:
|
|
|
|
return
|
2024-03-25 13:52:51 +00:00
|
|
|
|
|
|
|
out = []
|
|
|
|
if device["location"]:
|
|
|
|
out.append("Location: {}".format(device["location"]))
|
|
|
|
out.append("Setup: {} ({})".format(device["setup"]["name"], device["setup"]["id"]))
|
|
|
|
out.append("Resolution: {}".format(device["run"].get("resolution", "unknown")))
|
|
|
|
|
2023-12-26 13:50:24 +00:00
|
|
|
mqtt_out(
|
2024-03-25 13:52:51 +00:00
|
|
|
" - ".join(out),
|
2023-12-26 13:50:24 +00:00
|
|
|
device=device,
|
|
|
|
)
|
|
|
|
|
2024-11-28 07:27:36 +00:00
|
|
|
def is_dump_time():
|
|
|
|
return datetime.now(TZ).strftime("%H%M") == DUMP_TIME
|
2023-12-26 13:50:24 +00:00
|
|
|
|
|
|
|
mqtt_out("Monitor starting up")
|
|
|
|
while True:
|
|
|
|
try:
|
|
|
|
try:
|
2023-12-27 13:55:15 +00:00
|
|
|
r = get(
|
|
|
|
"https://info-beamer.com/api/v1/device/list",
|
|
|
|
auth=("", CONFIG["api_key"]),
|
|
|
|
)
|
2023-12-26 13:50:24 +00:00
|
|
|
r.raise_for_status()
|
|
|
|
ib_state = r.json()["devices"]
|
|
|
|
except RequestException as e:
|
2024-11-28 07:27:36 +00:00
|
|
|
LOG.exception("Could not get device data from info-beamer")
|
2023-12-26 13:50:24 +00:00
|
|
|
mqtt_out(
|
2024-11-28 07:27:36 +00:00
|
|
|
f"Could not get device data from info-beamer: {e!r}",
|
2023-12-26 13:50:24 +00:00
|
|
|
level="WARN",
|
|
|
|
)
|
|
|
|
else:
|
|
|
|
new_state = {}
|
|
|
|
for device in ib_state:
|
|
|
|
did = str(device["id"])
|
|
|
|
|
|
|
|
if did in new_state:
|
|
|
|
mqtt_out("DUPLICATE DETECTED!", level="ERROR", device=device)
|
|
|
|
continue
|
|
|
|
|
|
|
|
new_state[did] = device
|
2024-11-28 07:27:36 +00:00
|
|
|
# force information output for every online device at 09:00 CE(S)T
|
|
|
|
must_dump_state = is_dump_time()
|
2023-12-26 13:50:24 +00:00
|
|
|
|
|
|
|
if state is not None:
|
|
|
|
if did not in state:
|
|
|
|
LOG.info(
|
|
|
|
"new device found: {} [{}]".format(
|
|
|
|
did,
|
|
|
|
device["description"],
|
|
|
|
)
|
|
|
|
)
|
|
|
|
mqtt_out(
|
2023-12-27 13:55:15 +00:00
|
|
|
"new device found!",
|
2023-12-26 13:50:24 +00:00
|
|
|
device=device,
|
|
|
|
)
|
2023-12-27 13:55:15 +00:00
|
|
|
must_dump_state = True
|
2023-12-26 13:50:24 +00:00
|
|
|
|
|
|
|
else:
|
|
|
|
if device["is_online"] != state[did]["is_online"]:
|
|
|
|
online_status = (
|
|
|
|
"online from {}".format(device["run"]["public_addr"])
|
|
|
|
if device["is_online"]
|
|
|
|
else "offline"
|
|
|
|
)
|
|
|
|
|
|
|
|
LOG.info("device {} is now {}".format(did, online_status))
|
|
|
|
mqtt_out(
|
2023-12-27 13:55:15 +00:00
|
|
|
f"status changed to {online_status}",
|
2023-12-26 14:02:56 +00:00
|
|
|
level="INFO" if device["is_online"] else "WARN",
|
2023-12-26 13:50:24 +00:00
|
|
|
device=device,
|
|
|
|
)
|
2023-12-27 13:55:15 +00:00
|
|
|
must_dump_state = True
|
2023-12-26 13:50:24 +00:00
|
|
|
|
2023-12-26 22:15:18 +00:00
|
|
|
if device["description"] != state[did]["description"]:
|
2023-12-27 13:55:15 +00:00
|
|
|
LOG.info(
|
|
|
|
"device {} changed name to {}".format(
|
|
|
|
did, device["description"]
|
|
|
|
)
|
|
|
|
)
|
2023-12-26 22:15:18 +00:00
|
|
|
must_dump_state = True
|
|
|
|
|
2023-12-26 13:50:24 +00:00
|
|
|
if device["is_online"]:
|
|
|
|
if device["maintenance"]:
|
|
|
|
mqtt_out(
|
2024-11-03 16:40:46 +00:00
|
|
|
"maintenance required: {}".format(
|
|
|
|
" ".join(sorted(device["maintenance"]))
|
|
|
|
),
|
2023-12-26 13:50:24 +00:00
|
|
|
level="WARN",
|
|
|
|
device=device,
|
|
|
|
)
|
|
|
|
|
|
|
|
if (
|
2024-11-03 16:40:46 +00:00
|
|
|
device["location"] != state[did]["location"]
|
2023-12-26 22:15:18 +00:00
|
|
|
or device["setup"]["id"] != state[did]["setup"]["id"]
|
2023-12-26 13:50:24 +00:00
|
|
|
or device["run"].get("resolution")
|
|
|
|
!= state[did]["run"].get("resolution")
|
|
|
|
):
|
|
|
|
must_dump_state = True
|
|
|
|
|
|
|
|
if must_dump_state:
|
|
|
|
mqtt_dump_state(device)
|
|
|
|
else:
|
|
|
|
LOG.info("adding device {} to empty state".format(device["id"]))
|
|
|
|
|
|
|
|
state = new_state
|
2023-12-31 07:50:45 +00:00
|
|
|
|
2024-11-03 16:40:46 +00:00
|
|
|
try:
|
|
|
|
r = get(
|
|
|
|
"https://info-beamer.com/api/v1/account",
|
|
|
|
auth=("", CONFIG["api_key"]),
|
|
|
|
)
|
|
|
|
r.raise_for_status()
|
|
|
|
ib_account = r.json()
|
|
|
|
except RequestException as e:
|
2024-11-28 07:27:36 +00:00
|
|
|
LOG.exception("Could not get account data from info-beamer")
|
2024-11-03 16:40:46 +00:00
|
|
|
mqtt_out(
|
2024-11-28 07:27:36 +00:00
|
|
|
f"Could not get account data from info-beamer: {e!r}",
|
2024-11-03 16:40:46 +00:00
|
|
|
level="WARN",
|
|
|
|
)
|
|
|
|
else:
|
|
|
|
available_credits = ib_account["balance"]
|
2024-11-28 07:27:36 +00:00
|
|
|
if is_dump_time():
|
|
|
|
mqtt_out(f"Available Credits: {available_credits}")
|
|
|
|
|
2024-11-03 16:40:46 +00:00
|
|
|
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",
|
|
|
|
)
|
|
|
|
|
|
|
|
sleep(60)
|
2023-12-26 13:50:24 +00:00
|
|
|
except KeyboardInterrupt:
|
|
|
|
break
|
|
|
|
|
|
|
|
mqtt_out("Monitor exiting")
|