From b19a62404f9e3c67cb72c8326c27e320b26525fa Mon Sep 17 00:00:00 2001 From: Alexandre <44178713+alexbelgium@users.noreply.github.com> Date: Tue, 27 Aug 2024 17:10:05 +0200 Subject: [PATCH] Clean --- birdnet-pi/rootfs/etc/cont-init.d/33-mqtt.sh | 30 +++++++++----------- 1 file changed, 14 insertions(+), 16 deletions(-) diff --git a/birdnet-pi/rootfs/etc/cont-init.d/33-mqtt.sh b/birdnet-pi/rootfs/etc/cont-init.d/33-mqtt.sh index 82ed8a2ea..51f4c3c91 100755 --- a/birdnet-pi/rootfs/etc/cont-init.d/33-mqtt.sh +++ b/birdnet-pi/rootfs/etc/cont-init.d/33-mqtt.sh @@ -2,37 +2,34 @@ # shellcheck shell=bash set -e +# Function to perform common setup steps common_steps () { - # Attempt to connect to the MQTT broker TOPIC="birdnet" - mosquitto_pub -h "$MQTT_HOST" -p "$MQTT_PORT" -t "$TOPIC" -m "test" -u "$MQTT_USER" -P "$MQTT_PASS" -q 1 -d --will-topic "$TOPIC" --will-payload "Disconnected" --will-qos 1 --will-retain > /dev/null 2>&1 - - # If connected - if [ $? -eq 0 ]; then - # Adapt script + if mosquitto_pub -h "$MQTT_HOST" -p "$MQTT_PORT" -t "$TOPIC" -m "test" -u "$MQTT_USER" -P "$MQTT_PASS" -q 1 -d --will-topic "$TOPIC" --will-payload "Disconnected" --will-qos 1 --will-retain > /dev/null 2>&1; then + # Adapt script with MQTT settings sed -i "s|%%mqtt_server%%|$MQTT_HOST|g" /helpers/birdnet_to_mqtt.py sed -i "s|%%mqtt_port%%|$MQTT_PORT|g" /helpers/birdnet_to_mqtt.py sed -i "s|%%mqtt_user%%|$MQTT_USER|g" /helpers/birdnet_to_mqtt.py sed -i "s|%%mqtt_pass%%|$MQTT_PASS|g" /helpers/birdnet_to_mqtt.py - # Copy script + # Copy script to the appropriate directory cp /helpers/birdnet_to_mqtt.py "$HOME"/BirdNET-Pi/scripts/utils/birdnet_to_mqtt.py chown pi:pi "$HOME"/BirdNET-Pi/scripts/utils/birdnet_to_mqtt.py chmod +x "$HOME"/BirdNET-Pi/scripts/utils/birdnet_to_mqtt.py - # Add hooks + # Add hooks to the main analysis script sed -i "/load_global_model, run_analysis/a from utils.birdnet_to_mqtt import automatic_mqtt_publish" "$HOME"/BirdNET-Pi/scripts/birdnet_analysis.py - sed -i '/write_to_db(/a\ automatic_mqtt_publish(file, detection,os.path.basename(detection.file_name_extr))' "$HOME"/BirdNET-Pi/scripts/birdnet_analysis.py + sed -i '/write_to_db(/a\ automatic_mqtt_publish(file, detection, os.path.basename(detection.file_name_extr))' "$HOME"/BirdNET-Pi/scripts/birdnet_analysis.py else bashio::log.fatal "MQTT connection failed, it will not be configured" fi - } -if bashio::services.available 'mqtt' && ! bashio::config.true 'MQTT_DISABLED' ; then +# Check if MQTT service is available and not disabled +if bashio::services.available 'mqtt' && ! bashio::config.true 'MQTT_DISABLED'; then bashio::log.green "---" - bashio::log.blue "MQTT addon is active on your system! Birdnet-pi is now automatically configured to send its ouptut to MQTT" + bashio::log.blue "MQTT addon is active on your system! Birdnet-pi is now automatically configured to send its output to MQTT" bashio::log.blue "MQTT user : $(bashio::services "mqtt" "username")" bashio::log.blue "MQTT password : $(bashio::services "mqtt" "password")" bashio::log.blue "MQTT broker : tcp://$(bashio::services "mqtt" "host"):$(bashio::services "mqtt" "port")" @@ -47,25 +44,26 @@ if bashio::services.available 'mqtt' && ! bashio::config.true 'MQTT_DISABLED' ; MQTT_USER="$(bashio::services "mqtt" "username")" MQTT_PASS="$(bashio::services "mqtt" "password")" - # Common steps + # Perform common setup steps common_steps +# Check if manual MQTT configuration is provided elif bashio::config.has_value "MQTT_HOST_manual" && bashio::config.has_value "MQTT_PORT_manual"; then bashio::log.green "---" bashio::log.blue "MQTT is manually configured in the addon options" - bashio::log.blue "Birdnet-pi is now automatically configured to send its ouptut to MQTT" + bashio::log.blue "Birdnet-pi is now automatically configured to send its output to MQTT" bashio::log.green "---" bashio::log.blue "Data will be posted to the topic : 'birdnet'" bashio::log.blue "Json data : {'Date', 'Time', 'ScientificName', 'CommonName', 'Confidence', 'SpeciesCode', 'ClipName', 'url'}" bashio::log.blue "---" - # Apply MQTT settings + # Apply manual MQTT settings MQTT_HOST="$(bashio::config "MQTT_HOST_manual")" MQTT_PORT="$(bashio::config "MQTT_PORT_manual")" MQTT_USER="$(bashio::config "MQTT_USER_manual")" MQTT_PASS="$(bashio::config "MQTT_PASSWORD_manual")" - # Common steps + # Perform common setup steps common_steps fi