sdm630_mqtt/printout.py

123 lines
3 KiB
Python
Raw Permalink Normal View History

2024-07-27 15:26:51 +00:00
import logging
2024-07-30 16:32:03 +00:00
from sys import argv, exit
2024-07-27 15:26:51 +00:00
from time import sleep
2024-07-30 16:32:03 +00:00
from tomllib import load
2024-07-27 15:26:51 +00:00
import paho.mqtt.client as mqtt
from rich.align import Align
from rich.layout import Layout
from rich.live import Live
from rich.panel import Panel
from rich.table import Table
2024-07-30 16:32:03 +00:00
try:
with open(argv[1], "rb") as f:
config = load(f)
TITLE = config["printout"]["title"]
2024-07-30 16:44:25 +00:00
MQTT_SUB = "{}/".format(config["mqtt"]["prefix"])
2024-07-30 16:32:03 +00:00
except Exception as e:
print(f"Usage: {argv[0]} config.toml")
exit(1)
2024-07-27 15:26:51 +00:00
TABLE_LAYOUT = {
"row1": {
"voltage": {
"name": "Voltage",
"suffix": "V",
},
"current": {
"name": "Current",
"suffix": "A",
},
"current-demand": {
"name": "Current Demand",
"suffix": "A",
},
},
"row2": {
2024-07-30 14:55:30 +00:00
"active-power": {
2024-07-30 16:52:06 +00:00
"name": "Active Power",
2024-07-27 15:26:51 +00:00
"suffix": "W",
},
2024-07-30 16:52:06 +00:00
"apparent-power": {
"name": "Apparent Power",
"suffix": "VA",
2024-07-27 15:26:51 +00:00
},
"power-factor": {
"name": "Power Factor",
"suffix": "",
},
},
}
2024-07-30 16:42:29 +00:00
logging.basicConfig(level=logging.DEBUG)
2024-07-27 15:26:51 +00:00
LOG = logging.getLogger(__name__)
mqtt_data = {}
def on_connect(client, userdata, flags, rc):
LOG.info(f"Connected to mqtt server")
2024-07-30 16:44:25 +00:00
mqtt.subscribe(f"{MQTT_SUB}#")
2024-07-30 16:42:29 +00:00
LOG.info(f"subscribing to {MQTT_SUB}")
2024-07-27 15:26:51 +00:00
def on_disconnect(client, userdata, rc):
LOG.info(f"Disconnected from mqtt server")
def on_message(client, userdata, msg):
value = msg.payload.decode()
try:
value = round(float(value), 2)
except Exception:
pass
mqtt_data[msg.topic[len(MQTT_SUB) :]] = value
def generate_layout():
layout = Layout()
layout.split_column(
Layout(Panel(Align.center(TITLE)), name="header"),
Layout(name="main"),
)
layout["header"].size = 3
layout["main"].split_column(*[Layout(name=k) for k in TABLE_LAYOUT.keys()])
for k, i in TABLE_LAYOUT.items():
layout["main"][k].split_row(*[Layout(name=v) for v in i.keys()])
for k, i in TABLE_LAYOUT.items():
for k2, v in i.items():
table = Table(
title=v["name"],
show_header=False,
box=None,
)
for topic, value in sorted(mqtt_data.items()):
if topic.startswith(f"{k2}/"):
table.add_row(topic[len(f"{k2}/") :], f"{value} {v['suffix']}")
layout["main"][k][k2].update(Panel(Align.center(table)))
return layout
try:
2024-07-30 16:44:25 +00:00
LOG.info("mooooin!")
2024-07-27 15:26:51 +00:00
mqtt = mqtt.Client()
mqtt.on_connect = on_connect
mqtt.on_disconnect = on_disconnect
mqtt.on_message = on_message
2024-07-30 16:32:03 +00:00
mqtt.connect(config["mqtt"]["host"], config["mqtt"]["port"], 10)
2024-07-27 15:26:51 +00:00
2024-07-30 16:44:25 +00:00
LOG.info("starting loop")
2024-07-30 16:42:29 +00:00
mqtt.loop_start()
2024-07-27 15:26:51 +00:00
with Live(generate_layout(), screen=True) as live:
while True:
live.update(generate_layout())
2024-07-31 12:56:26 +00:00
sleep(0.1)
2024-07-30 16:42:29 +00:00
mqtt.loop_stop()
2024-07-30 16:44:25 +00:00
LOG.info("bye")
2024-07-27 15:26:51 +00:00
except Exception:
LOG.exception("oops")