From 01a8d7a6db1354ca0d7888d7d7c7a6d51ef0518e Mon Sep 17 00:00:00 2001 From: Franziska Kunsmann Date: Sat, 10 Aug 2024 19:10:20 +0200 Subject: [PATCH] add bundle:sdm630_mqtt --- .../sdm630_mqtt/files/sdm630_printout.service | 21 +++++ .../sdm630_mqtt/files/sdm630_to_mqtt.service | 14 ++++ bundles/sdm630_mqtt/items.py | 76 +++++++++++++++++++ bundles/sdm630_mqtt/metadata.py | 38 ++++++++++ 4 files changed, 149 insertions(+) create mode 100644 bundles/sdm630_mqtt/files/sdm630_printout.service create mode 100644 bundles/sdm630_mqtt/files/sdm630_to_mqtt.service create mode 100644 bundles/sdm630_mqtt/items.py create mode 100644 bundles/sdm630_mqtt/metadata.py diff --git a/bundles/sdm630_mqtt/files/sdm630_printout.service b/bundles/sdm630_mqtt/files/sdm630_printout.service new file mode 100644 index 0000000..8ba5a23 --- /dev/null +++ b/bundles/sdm630_mqtt/files/sdm630_printout.service @@ -0,0 +1,21 @@ +[Unit] +Description=SDM630 stats printout +Conflicts=getty@tty1.service +After=systemd-user-sessions.service getty@tty1.service plymouth-quit.service + +[Service] +User=sdm630_mqtt +Group=sdm630_mqtt +ExecStart=/opt/sdm630_mqtt/venv/bin/python printout.py /opt/sdm630_mqtt/config.toml +WorkingDirectory=/opt/sdm630_mqtt/src +Restart=always +RestartSec=10 +StandardInput=tty +StandardOutput=tty +StandardError=journal +TTYPath=/dev/tty1 +TTYReset=yes +TTYVHangup=yes + +[Install] +WantedBy=multi-user.target diff --git a/bundles/sdm630_mqtt/files/sdm630_to_mqtt.service b/bundles/sdm630_mqtt/files/sdm630_to_mqtt.service new file mode 100644 index 0000000..b1e67d7 --- /dev/null +++ b/bundles/sdm630_mqtt/files/sdm630_to_mqtt.service @@ -0,0 +1,14 @@ +[Unit] +Description=SDM630-to-MQTT bridge +After=network.target + +[Service] +User=sdm630_mqtt +Group=sdm630_mqtt +ExecStart=/opt/sdm630_mqtt/venv/bin/python sdm630_mqtt.py /opt/sdm630_mqtt/config.toml +WorkingDirectory=/opt/sdm630_mqtt/src +Restart=always +RestartSec=1 + +[Install] +WantedBy=multi-user.target diff --git a/bundles/sdm630_mqtt/items.py b/bundles/sdm630_mqtt/items.py new file mode 100644 index 0000000..6a691c9 --- /dev/null +++ b/bundles/sdm630_mqtt/items.py @@ -0,0 +1,76 @@ +directories['/opt/sdm630_mqtt/src'] = {} + +git_deploy['/opt/sdm630_mqtt/src'] = { + 'repo': 'https://git.franzi.business/kunsi/sdm630_mqtt.git', + 'rev': 'main', + 'triggers': { + 'action:sdm630_mqtt_install_deps', + }, +} + +actions['sdm630_mqtt_create_virtualenv'] = { + 'command': 'python3 -m virtualenv /opt/sdm630_mqtt/venv', + 'unless': 'test -x /opt/sdm630_mqtt/venv/bin/python3', + 'needs': { + 'directory:/opt/sdm630_mqtt/src', + }, +} + +actions['sdm630_mqtt_install_deps'] = { + 'command': 'cd /opt/sdm630_mqtt/src && /opt/sdm630_mqtt/venv/bin/pip install -r requirements.txt', + 'triggered': True, + 'needs': { + 'action:sdm630_mqtt_create_virtualenv', + }, +} + +users['sdm630_mqtt'] = { + 'home': '/opt/sdm630_mqtt', +} + +files['/opt/sdm630_mqtt/config.toml'] = { + 'content': repo.libs.faults.dict_as_toml(node.metadata.get('sdm630_mqtt/config')), + 'triggers': set(), +} + +if node.has_bundle('telegraf'): + files['/opt/sdm630_mqtt/config.toml']['triggers'].add('svc_systemd:telegraf:restart') + git_deploy['/opt/sdm630_mqtt/src']['triggers'].add('svc_systemd:telegraf:restart') + +if node.metadata.get('sdm630_mqtt/enable_stats_collection', True): + files['/usr/local/lib/systemd/system/sdm630_to_mqtt.service'] = { + 'triggers': { + 'action:systemd-reload', + 'svc_systemd:sdm630_to_mqtt:restart', + }, + } + + svc_systemd['sdm630_to_mqtt'] = { + 'needs': { + 'git_deploy:/opt/sdm630_mqtt/src', + 'action:sdm630_mqtt_install_deps', + 'file:/usr/local/lib/systemd/system/sdm630_to_mqtt.service', + }, + } + + files['/opt/sdm630_mqtt/config.toml']['triggers'].add('svc_systemd:sdm630_to_mqtt:restart') + git_deploy['/opt/sdm630_mqtt/src']['triggers'].add('svc_systemd:sdm630_to_mqtt:restart') + +if node.metadata.get('sdm630_mqtt/enable_local_printout', False): + files['/usr/local/lib/systemd/system/sdm630_printout.service'] = { + 'triggers': { + 'action:systemd-reload', + 'svc_systemd:sdm630_printout:restart', + }, + } + + svc_systemd['sdm630_printout'] = { + 'needs': { + 'git_deploy:/opt/sdm630_mqtt/src', + 'action:sdm630_mqtt_install_deps', + 'file:/usr/local/lib/systemd/system/sdm630_printout.service', + }, + } + + files['/opt/sdm630_mqtt/config.toml']['triggers'].add('svc_systemd:sdm630_printout:restart') + git_deploy['/opt/sdm630_mqtt/src']['triggers'].add('svc_systemd:sdm630_printout:restart') diff --git a/bundles/sdm630_mqtt/metadata.py b/bundles/sdm630_mqtt/metadata.py new file mode 100644 index 0000000..944b8b2 --- /dev/null +++ b/bundles/sdm630_mqtt/metadata.py @@ -0,0 +1,38 @@ +defaults = { + 'sdm630_mqtt': { + 'config': { + 'modbus': { + 'host': '127.0.0.1', + 'port': 501, + 'unit_id': 1, + }, + 'mqtt': { + 'prefix': 'sdm630', + 'host': '127.0.0.1', + 'port': 1883, + }, + 'printout': { + 'title': 'SDM630', + }, + 'telegraf': { + 'identifier': 'unknown', + }, + }, + }, + 'telegraf': { + 'input_plugins': { + 'execd': { + 'sdm630_mqtt': { + 'command': [ + '/opt/sdm630_mqtt/venv/bin/python', + '/opt/sdm630_mqtt/src/telegraf.py', + '/opt/sdm630_mqtt/config.toml', + ], + 'signal': 'none', + 'restart_delay': '1s', + 'data_format': 'influx', + }, + }, + }, + }, +}