mirror of
https://github.com/alexbelgium/hassio-addons.git
synced 2026-06-06 15:45:56 +02:00
battybirdnet-pi
This commit is contained in:
87
battybirdnet-pi/rootfs/etc/cont-init.d/01-structure.sh
Normal file
87
battybirdnet-pi/rootfs/etc/cont-init.d/01-structure.sh
Normal file
@@ -0,0 +1,87 @@
|
||||
#!/command/with-contenv bashio
|
||||
# shellcheck shell=bash
|
||||
set -e
|
||||
|
||||
###############
|
||||
# SET /CONFIG #
|
||||
###############
|
||||
|
||||
echo " "
|
||||
bashio::log.info "Ensuring the file structure is correct :"
|
||||
|
||||
# Define structure
|
||||
echo "... creating default files"
|
||||
touch /config/include_species_list.txt # Should be null
|
||||
for files in apprise.txt exclude_species_list.txt IdentifiedSoFar.txt disk_check_exclude.txt confirmed_species_list.txt blacklisted_images.txt; do
|
||||
if [ ! -f /config/"$files" ]; then
|
||||
echo "" > /config/"$files"
|
||||
fi
|
||||
done
|
||||
|
||||
# Get BirdSongs folder locations
|
||||
BIRDSONGS_FOLDER="/config/BirdSongs"
|
||||
if bashio::config.has_value "BIRDSONGS_FOLDER"; then
|
||||
BIRDSONGS_FOLDER_OPTION="$(bashio::config "BIRDSONGS_FOLDER")"
|
||||
echo "... BIRDSONGS_FOLDER set to $BIRDSONGS_FOLDER_OPTION"
|
||||
mkdir -p "$BIRDSONGS_FOLDER_OPTION" || bashio::log.fatal "...... folder couldn't be created"
|
||||
chown -R pi:pi "$BIRDSONGS_FOLDER_OPTION" || bashio::log.fatal "...... folder couldn't be given permissions for 1000:1000"
|
||||
if [ -d "$BIRDSONGS_FOLDER_OPTION" ] && [ "$(stat -c '%u:%g' "$BIRDSONGS_FOLDER_OPTION")" == "1000:1000" ]; then
|
||||
BIRDSONGS_FOLDER="$BIRDSONGS_FOLDER_OPTION"
|
||||
else
|
||||
bashio::log.yellow "BIRDSONGS_FOLDER reverted to /config/BirdSongs"
|
||||
fi
|
||||
fi
|
||||
|
||||
# Create BirdSongs folder
|
||||
echo "... creating default folders ; it is highly recommended to store those on a ssd"
|
||||
mkdir -p "$BIRDSONGS_FOLDER"/By_Date
|
||||
mkdir -p "$BIRDSONGS_FOLDER"/Charts
|
||||
|
||||
# If tmpfs is installed, use it
|
||||
if df -T /tmp | grep -q "tmpfs"; then
|
||||
echo "... tmpfs detected, using it for StreamData and Processed to reduce disk wear"
|
||||
mkdir -p /tmp/StreamData
|
||||
mkdir -p /tmp/Processed
|
||||
rm -r "$HOME"/BirdSongs/StreamData
|
||||
rm -r "$HOME"/BirdSongs/Processed
|
||||
sudo -u pi ln -fs /tmp/StreamData "$HOME"/BirdSongs/StreamData
|
||||
sudo -u pi ln -fs /tmp/Processed "$HOME"/BirdSongs/Processed
|
||||
fi
|
||||
|
||||
# Permissions for created files and folders
|
||||
echo "... set permissions to user pi"
|
||||
chown -R pi:pi /config /etc/birdnet "$BIRDSONGS_FOLDER" /tmp
|
||||
chmod -R 755 /config /config /etc/birdnet "$BIRDSONGS_FOLDER" /tmp
|
||||
|
||||
# Save default birdnet.conf to perform sanity check
|
||||
cp "$HOME"/BirdNET-Pi/birdnet.conf "$HOME"/BirdNET-Pi/birdnet.bak
|
||||
|
||||
# Symlink files
|
||||
echo "... creating symlink"
|
||||
for files in "$HOME/BirdNET-Pi/birdnet.conf" "$HOME/BirdNET-Pi/blacklisted_images.txt" "$HOME/BirdNET-Pi/scripts/birds.db" "$HOME/BirdNET-Pi/BirdDB.txt" "$HOME/BirdNET-Pi/scripts/disk_check_exclude.txt" "$HOME/BirdNET-Pi/apprise.txt" "$HOME/BirdNET-Pi/exclude_species_list.txt" "$HOME/BirdNET-Pi/include_species_list.txt" "$HOME/BirdNET-Pi/IdentifiedSoFar.txt" "$HOME/BirdNET-Pi/confirmed_species_list.txt"; do
|
||||
filename="${files##*/}"
|
||||
if [ ! -f /config/"$filename" ]; then
|
||||
if [ -f "$files" ]; then
|
||||
echo "... copying $filename" && sudo -u pi mv "$files" /config/
|
||||
else
|
||||
touch /config/"$filename"
|
||||
fi
|
||||
fi
|
||||
if [ -e "$files" ]; then rm "$files"; fi
|
||||
sudo -u pi ln -fs /config/"$filename" "$files" || bashio::log.fatal "Symlink creation failed for $filename"
|
||||
sudo -u pi ln -fs /config/"$filename" /etc/birdnet/"$filename" || bashio::log.fatal "Symlink creation failed for $filename"
|
||||
done
|
||||
|
||||
# Symlink folders
|
||||
for folders in By_Date Charts; do
|
||||
echo "... creating symlink for $BIRDSONGS_FOLDER/$folders"
|
||||
rm -r "$HOME/BirdSongs/Extracted/${folders:?}"
|
||||
sudo -u pi ln -fs "$BIRDSONGS_FOLDER"/"$folders" "$HOME/BirdSongs/Extracted/$folders"
|
||||
done
|
||||
|
||||
# Permissions for created files and folders
|
||||
echo "... check permissions"
|
||||
chmod -R 755 /config/*
|
||||
chmod 777 /config
|
||||
|
||||
echo " "
|
||||
@@ -0,0 +1,24 @@
|
||||
#!/usr/bin/with-contenv bashio
|
||||
# shellcheck shell=bash
|
||||
|
||||
# Check if there are files in "$HOME"/BirdSongs/StreamData and move them to /data/StreamData
|
||||
if [ -d /data/StreamData ] && [ "$(ls -A /data/StreamData/)" ]; then
|
||||
|
||||
bashio::log.warning "Container was stopped while files were still being analysed, restoring them"
|
||||
|
||||
# Copy files
|
||||
if [ "$(ls -A /data/StreamData)" ]; then
|
||||
mv -v /data/StreamData/* "$HOME"/BirdSongs/StreamData/
|
||||
fi
|
||||
echo "... done"
|
||||
echo ""
|
||||
|
||||
# Setting permissions
|
||||
chown -R pi:pi "$HOME"/BirdSongs
|
||||
chmod -R 755 "$HOME"/BirdSongs
|
||||
|
||||
# Cleaning folder
|
||||
rm -r /data/StreamData
|
||||
|
||||
fi
|
||||
|
||||
54
battybirdnet-pi/rootfs/etc/cont-init.d/31-checks.sh
Normal file
54
battybirdnet-pi/rootfs/etc/cont-init.d/31-checks.sh
Normal file
@@ -0,0 +1,54 @@
|
||||
#!/command/with-contenv bashio
|
||||
# shellcheck shell=bash
|
||||
set -e
|
||||
|
||||
######################
|
||||
# CHECK BIRDNET.CONF #
|
||||
######################
|
||||
|
||||
echo " "
|
||||
bashio::log.info "Checking your birndet.conf file integrity"
|
||||
|
||||
# Set variables
|
||||
configcurrent="$HOME"/BirdNET-Pi/birdnet.conf
|
||||
configtemplate="$HOME"/BirdNET-Pi/birdnet.bak
|
||||
|
||||
# Extract variable names from config template and read each one
|
||||
grep -o '^[^#=]*=' "$configtemplate" | sed 's/=//' | while read -r var; do
|
||||
# Check if the variable is in configcurrent, if not, append it
|
||||
if ! grep -q "^$var=" "$configcurrent"; then
|
||||
# At which line was the variable in the initial file
|
||||
bashio::log.yellow "...$var was missing from your birdnet.conf file, it was re-added"
|
||||
grep "^$var=" "$configtemplate" >> "$configcurrent"
|
||||
fi
|
||||
# Check for duplicates
|
||||
if [ "$(grep -c "^$var=" "$configcurrent")" -gt 1 ]; then
|
||||
bashio::log.error "Duplicate variable $var found in $configcurrent, all were commented out expect for the first one"
|
||||
awk -v var="$var" '{ if ($0 ~ "^[[:blank:]]*"var && c++ > 0) print "#" $0; else print $0; }' "$configcurrent" > temp && mv temp "$configcurrent"
|
||||
fi
|
||||
done
|
||||
|
||||
################
|
||||
# CHECK AMIXER #
|
||||
################
|
||||
|
||||
# If default capture is set at 0%, increase it to 50%
|
||||
# current_volume="$(amixer sget Capture | grep -oP '\[\d+%]' | tr -d '[]%' | head -1)" 2>/dev/null || true
|
||||
# current_volume="${current_volume:-100}"
|
||||
|
||||
# Set the default microphone volume to 50% if it's currently at 0%
|
||||
# if [[ "$current_volume" -eq 0 ]]; then
|
||||
# amixer sset Capture 70%
|
||||
# bashio::log.warning "Microphone was off, volume set to 70%."
|
||||
# fi
|
||||
|
||||
##############
|
||||
# CHECK PORT #
|
||||
##############
|
||||
|
||||
if [[ "$(bashio::addon.port "80")" == 3000 ]]; then
|
||||
bashio::log.fatal "This is crazy but your port is set to 3000 and streamlit doesn't accept this port! You need to change it from the addon options and restart. Thanks"
|
||||
sleep infinity
|
||||
fi
|
||||
|
||||
echo " "
|
||||
47
battybirdnet-pi/rootfs/etc/cont-init.d/33-mqtt.sh
Normal file
47
battybirdnet-pi/rootfs/etc/cont-init.d/33-mqtt.sh
Normal file
@@ -0,0 +1,47 @@
|
||||
#!/usr/bin/with-contenv bashio
|
||||
# shellcheck shell=bash
|
||||
set -e
|
||||
|
||||
if bashio::services.available 'mqtt' && ! bashio::config.true 'MQTT_DISABLED' ; then
|
||||
bashio::log.green "---"
|
||||
bashio::log.blue "MQTT addon is active on your system! battybirdnet-pi is now automatically configured to send its ouptut 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")"
|
||||
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
|
||||
sed -i "s|%%mqtt_server%%|$(bashio::services "mqtt" "host")|g" /helpers/birdnet_to_mqtt.py
|
||||
sed -i "s|%%mqtt_port%%|$(bashio::services "mqtt" "port")|g" /helpers/birdnet_to_mqtt.py
|
||||
sed -i "s|%%mqtt_user%%|$(bashio::services "mqtt" "username")|g" /helpers/birdnet_to_mqtt.py
|
||||
sed -i "s|%%mqtt_pass%%|$(bashio::services "mqtt" "password")|g" /helpers/birdnet_to_mqtt.py
|
||||
|
||||
# Copy script
|
||||
cp /helpers/birdnet_to_mqtt.py /usr/bin/birdnet_to_mqtt.py
|
||||
cp /helpers/birdnet_to_mqtt.sh /custom-services.d
|
||||
chmod 777 /usr/bin/birdnet_to_mqtt.py
|
||||
chmod 777 /custom-services.d/birdnet_to_mqtt.sh
|
||||
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 "battybirdnet-pi is now automatically configured to send its ouptut 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
|
||||
sed -i "s|%%mqtt_server%%|$(bashio::config "MQTT_HOST_manual")|g" /helpers/birdnet_to_mqtt.py
|
||||
sed -i "s|%%mqtt_port%%|$(bashio::config "MQTT_PORT_manual")|g" /helpers/birdnet_to_mqtt.py
|
||||
sed -i "s|%%mqtt_user%%|$(bashio::config "MQTT_USER_manual")|g" /helpers/birdnet_to_mqtt.py
|
||||
sed -i "s|%%mqtt_pass%%|$(bashio::config "MQTT_PASSWORD_manual")|g" /helpers/birdnet_to_mqtt.py
|
||||
|
||||
# Copy script
|
||||
cp /helpers/birdnet_to_mqtt.py /usr/bin/birdnet_to_mqtt.py
|
||||
cp /helpers/birdnet_to_mqtt.sh /custom-services.d
|
||||
chmod +x /usr/bin/birdnet_to_mqtt.py
|
||||
chmod +x /custom-services.d/birdnet_to_mqtt.sh
|
||||
fi
|
||||
127
battybirdnet-pi/rootfs/etc/cont-init.d/71-newfeatures.sh
Normal file
127
battybirdnet-pi/rootfs/etc/cont-init.d/71-newfeatures.sh
Normal file
@@ -0,0 +1,127 @@
|
||||
#!/command/with-contenv bashio
|
||||
# shellcheck shell=bash
|
||||
set -e
|
||||
|
||||
################
|
||||
# ADD FEATURES #
|
||||
################
|
||||
|
||||
echo " "
|
||||
bashio::log.info "Adding optional features"
|
||||
|
||||
# Denoiser
|
||||
#if bashio::config.true "DENOISER_ANALYSIS_ENABLED"; then
|
||||
# sed -i "s|ar 48000|ar 48000 -af \"arnndn=m=sample.rnnn\"|g" "$HOME"/BirdNET-Pi/scripts/birdnet_recording.sh
|
||||
# sed -i "s|ar 48000|ar 48000 -af afftdn=nr=30:nt=w:om=o|g" "$HOME"/BirdNET-Pi/scripts/birdnet_recording.sh
|
||||
#fi
|
||||
|
||||
# Add species conversion system
|
||||
###############################
|
||||
if bashio::config.true "SPECIES_CONVERTER_ENABLED"; then
|
||||
echo "... adding feature of SPECIES_CONVERTER, a new tab is added to your Tools"
|
||||
touch /config/convert_species_list.txt
|
||||
chown pi:pi /config/convert_species_list.txt
|
||||
sudo -u pi ln -fs /config/convert_species_list.txt "$HOME"/BirdNET-Pi/
|
||||
sudo -u pi ln -fs /config/convert_species_list.txt "$HOME"/BirdNET-Pi/scripts/
|
||||
# Not useful
|
||||
sed -i "/exclude_species_list.txt/a sudo -u pi ln -fs /config/convert_species_list.txt $HOME/BirdNET-Pi/scripts/" "$HOME"/BirdNET-Pi/scripts/clear_all_data.sh
|
||||
sed -i "/exclude_species_list.txt/a sudo -u pi ln -fs /config/convert_species_list.txt $HOME/BirdNET-Pi/scripts/" "$HOME"/BirdNET-Pi/scripts/install_services.sh
|
||||
# Modify views.php if not already done
|
||||
if ! grep -q "Converted" "$HOME"/BirdNET-Pi/homepage/views.php; then
|
||||
# Add button
|
||||
# shellcheck disable=SC2016
|
||||
sed -i '/Excluded Species List/a\ <button type=\\"submit\\" name=\\"view\\" value=\\"Converted\\" form=\\"views\\">Converted Species List</button>' "$HOME"/BirdNET-Pi/homepage/views.php
|
||||
# Flag to indicate whether we've found the target line
|
||||
found_target=false
|
||||
# Read the original file line by line
|
||||
while IFS= read -r line; do
|
||||
if [[ $line == *"if(\$_GET['view'] == \"File\"){"* ]]; then
|
||||
found_target=true
|
||||
fi
|
||||
if $found_target; then
|
||||
echo "$line" >> "$HOME"/BirdNET-Pi/homepage/views.php.temp
|
||||
fi
|
||||
done < "$HOME"/BirdNET-Pi/homepage/views.php
|
||||
# Remove the extracted lines from the original file
|
||||
# shellcheck disable=SC2016
|
||||
sed -i '/if(\$_GET\['\''view'\''\] == "File"){/,$d' "$HOME"/BirdNET-Pi/homepage/views.php
|
||||
# Add new text
|
||||
cat "/helpers/views.add" >> "$HOME"/BirdNET-Pi/homepage/views.php
|
||||
cat "$HOME"/BirdNET-Pi/homepage/views.php.temp >> "$HOME"/BirdNET-Pi/homepage/views.php
|
||||
# Clean up: Remove the temporary file
|
||||
rm "$HOME"/BirdNET-Pi/homepage/views.php.temp
|
||||
fi
|
||||
|
||||
# Add the converter script
|
||||
if [ ! -f "$HOME"/BirdNET-Pi/scripts/convert_list.php ]; then
|
||||
mv -f /helpers/convert_list.php "$HOME"/BirdNET-Pi/scripts/convert_list.php
|
||||
chown pi:pi "$HOME"/BirdNET-Pi/scripts/convert_list.php
|
||||
chmod 664 "$HOME"/BirdNET-Pi/scripts/convert_list.php
|
||||
fi
|
||||
|
||||
# Change server
|
||||
if ! grep -q "converted_entry" "$HOME"/BirdNET-Pi/scripts/server.py; then
|
||||
sed -i "/INTERPRETER, M_INTERPRETER, INCLUDE_LIST, EXCLUDE_LIST/c INTERPRETER, M_INTERPRETER, INCLUDE_LIST, EXCLUDE_LIST, CONVERT_LIST = (None, None, None, None, None)" "$HOME"/BirdNET-Pi/scripts/server.py
|
||||
sed -i "/global INCLUDE_LIST, EXCLUDE_LIST/c\ global INCLUDE_LIST, EXCLUDE_LIST, CONVERT_LIST, CONVERT_DICT" "$HOME"/BirdNET-Pi/scripts/server.py
|
||||
sed -i "/exclude_species_list.txt/a\ CONVERT_DICT = {row.split(';')[0]: row.split(';')[1] for row in CONVERT_LIST}" "$HOME"/BirdNET-Pi/scripts/server.py
|
||||
sed -i "/exclude_species_list.txt/a\ CONVERT_LIST = loadCustomSpeciesList(os.path.expanduser(\"~/BirdNET-Pi/convert_species_list.txt\"))" "$HOME"/BirdNET-Pi/scripts/server.py
|
||||
sed -i "s|entry\[0\]|converted_entry|g" "$HOME"/BirdNET-Pi/scripts/server.py
|
||||
sed -i "s|if converted_entry in|if entry\[0\] in|g" "$HOME"/BirdNET-Pi/scripts/server.py
|
||||
sed -i "/for entry in entries/a\ converted_entry = entry[0]" "$HOME"/BirdNET-Pi/scripts/server.py
|
||||
sed -i "/for entry in entries/a\ else :" "$HOME"/BirdNET-Pi/scripts/server.py
|
||||
sed -i "/for entry in entries/a\ log.info('WARNING : ' + entry[0] + ' converted to ' + converted_entry)" "$HOME"/BirdNET-Pi/scripts/server.py
|
||||
sed -i "/for entry in entries/a\ converted_entry = CONVERT_DICT.get(entry[0], entry[0])" "$HOME"/BirdNET-Pi/scripts/server.py
|
||||
sed -i "/for entry in entries/a\ if entry[0] in CONVERT_DICT:" "$HOME"/BirdNET-Pi/scripts/server.py
|
||||
sed -i "/for entry in entries/a\ if entry[1] >= conf.getfloat('CONFIDENCE'):" "$HOME"/BirdNET-Pi/scripts/server.py
|
||||
sed -i "/converted_entry in INCLUDE_LIST or len(INCLUDE_LIST)/c\ if ((converted_entry in INCLUDE_LIST or len(INCLUDE_LIST) == 0)" "$HOME"/BirdNET-Pi/scripts/server.py
|
||||
sed -i "s| d = Detection| d = Detection|g" "$HOME"/BirdNET-Pi/scripts/server.py
|
||||
sed -i "s| confident_detections| confident_detections|g" "$HOME"/BirdNET-Pi/scripts/server.py
|
||||
fi
|
||||
fi || true
|
||||
|
||||
# Enable the Processed folder
|
||||
#############################
|
||||
|
||||
if bashio::config.true "PROCESSED_FOLDER_ENABLED" && ! grep -q "processed_size" "$HOME"/BirdNET-Pi/scripts/birdnet_analysis.py; then
|
||||
echo "... Enabling the Processed folder : the last 15 wav files will be stored there"
|
||||
# Adapt config.php
|
||||
sed -i "/GET\[\"info_site\"\]/a\ \$processed_size = \$_GET\[\"processed_size\"\];" "$HOME"/BirdNET-Pi/scripts/config.php
|
||||
sed -i "/\$contents = file_get_contents/a\ \$contents = preg_replace\(\"/PROCESSED_SIZE=\.\*/\", \"PROCESSED_SIZE=\$processed_size\", \$contents\);" "$HOME"/BirdNET-Pi/scripts/config.php
|
||||
sed -i "/\"success\"/i <table class=\"settingstable\"><tr><td>" "$HOME"/BirdNET-Pi/scripts/config.php
|
||||
sed -i "/\"success\"/i <h2>Processed folder management </h2>" "$HOME"/BirdNET-Pi/scripts/config.php
|
||||
sed -i "/\"success\"/i <label for=\"processed_size\">Amount of files to keep after analysis :</label>" "$HOME"/BirdNET-Pi/scripts/config.php
|
||||
sed -i "/\"success\"/i <input name=\"processed_size\" type=\"number\" style=\"width:6em;\" max=\"90\" min=\"0\" step=\"1\" value=\"<\?php print(\$config\['PROCESSED_SIZE'\]);?>\"/>" "$HOME"/BirdNET-Pi/scripts/config.php
|
||||
sed -i "/\"success\"/i </td></tr><tr><td>" "$HOME"/BirdNET-Pi/scripts/config.php
|
||||
sed -i "/\"success\"/i Processed is the directory where the formerly 'Analyzed' files are moved after extractions, mostly for troubleshooting purposes.<br>" "$HOME"/BirdNET-Pi/scripts/config.php
|
||||
sed -i "/\"success\"/i This value defines the maximum amount of files that are kept before replacement with new files.<br>" "$HOME"/BirdNET-Pi/scripts/config.php
|
||||
sed -i "/\"success\"/i </td></tr></table>" "$HOME"/BirdNET-Pi/scripts/config.php
|
||||
sed -i "/\"success\"/i\ <br>" "$HOME"/BirdNET-Pi/scripts/config.php
|
||||
# Adapt birdnet_analysis.py - move_to_processed
|
||||
sed -i "/log.info('handle_reporting_queue done')/a\ os.remove(files.pop(0))" "$HOME"/BirdNET-Pi/scripts/birdnet_analysis.py
|
||||
sed -i "/log.info('handle_reporting_queue done')/a\ while len(files) > processed_size:" "$HOME"/BirdNET-Pi/scripts/birdnet_analysis.py
|
||||
sed -i "/log.info('handle_reporting_queue done')/a\ files.sort(key=os.path.getmtime)" "$HOME"/BirdNET-Pi/scripts/birdnet_analysis.py
|
||||
sed -i "/log.info('handle_reporting_queue done')/a\ files = glob.glob(os.path.join(processed_dir, '*'))" "$HOME"/BirdNET-Pi/scripts/birdnet_analysis.py
|
||||
sed -i "/log.info('handle_reporting_queue done')/a\ os.rename(file_name, os.path.join(processed_dir, os.path.basename(file_name)))" "$HOME"/BirdNET-Pi/scripts/birdnet_analysis.py
|
||||
sed -i "/log.info('handle_reporting_queue done')/a\ processed_dir = os.path.join(get_settings()['RECS_DIR'], 'Processed')" "$HOME"/BirdNET-Pi/scripts/birdnet_analysis.py
|
||||
sed -i "/log.info('handle_reporting_queue done')/a\def move_to_processed(file_name, processed_size):" "$HOME"/BirdNET-Pi/scripts/birdnet_analysis.py
|
||||
sed -i "/log.info('handle_reporting_queue done')/a\ " "$HOME"/BirdNET-Pi/scripts/birdnet_analysis.py
|
||||
# Adapt birdnet_analysis.py - get_processed_size
|
||||
sed -i "/log.info('handle_reporting_queue done')/a\ return 0" "$HOME"/BirdNET-Pi/scripts/birdnet_analysis.py
|
||||
sed -i "/log.info('handle_reporting_queue done')/a\ except (ValueError, TypeError):" "$HOME"/BirdNET-Pi/scripts/birdnet_analysis.py
|
||||
sed -i "/log.info('handle_reporting_queue done')/a\ return processed_size if isinstance(processed_size, int) else 0" "$HOME"/BirdNET-Pi/scripts/birdnet_analysis.py
|
||||
sed -i "/log.info('handle_reporting_queue done')/a\ processed_size = get_settings().getint('PROCESSED_SIZE')" "$HOME"/BirdNET-Pi/scripts/birdnet_analysis.py
|
||||
sed -i "/log.info('handle_reporting_queue done')/a\ try:" "$HOME"/BirdNET-Pi/scripts/birdnet_analysis.py
|
||||
sed -i "/log.info('handle_reporting_queue done')/a\def get_processed_size():" "$HOME"/BirdNET-Pi/scripts/birdnet_analysis.py
|
||||
sed -i "/log.info('handle_reporting_queue done')/a\ " "$HOME"/BirdNET-Pi/scripts/birdnet_analysis.py
|
||||
# Modify calls
|
||||
sed -i "/from subprocess import CalledProcessError/a\import glob" "$HOME"/BirdNET-Pi/scripts/birdnet_analysis.py
|
||||
sed -i "/from subprocess import CalledProcessError/a\import time" "$HOME"/BirdNET-Pi/scripts/birdnet_analysis.py
|
||||
# Modify main code
|
||||
sed -i "/os.remove(file.file_name)/i\ processed_size = get_processed_size()" "$HOME"/BirdNET-Pi/scripts/birdnet_analysis.py
|
||||
sed -i "/os.remove(file.file_name)/i\ if processed_size > 0:" "$HOME"/BirdNET-Pi/scripts/birdnet_analysis.py
|
||||
sed -i "/os.remove(file.file_name)/i\ move_to_processed(file.file_name, processed_size)" "$HOME"/BirdNET-Pi/scripts/birdnet_analysis.py
|
||||
sed -i "/os.remove(file.file_name)/i\ else:" "$HOME"/BirdNET-Pi/scripts/birdnet_analysis.py
|
||||
sed -i "/os.remove(file.file_name)/c\ os.remove(file.file_name)" "$HOME"/BirdNET-Pi/scripts/birdnet_analysis.py
|
||||
fi || true
|
||||
|
||||
echo " "
|
||||
66
battybirdnet-pi/rootfs/etc/cont-init.d/81-modifications.sh
Normal file
66
battybirdnet-pi/rootfs/etc/cont-init.d/81-modifications.sh
Normal file
@@ -0,0 +1,66 @@
|
||||
#!/command/with-contenv bashio
|
||||
# shellcheck shell=bash
|
||||
set -e
|
||||
|
||||
################
|
||||
# MODIFY WEBUI #
|
||||
################
|
||||
|
||||
echo " "
|
||||
bashio::log.info "Adapting webui"
|
||||
|
||||
# Remove services tab
|
||||
echo "... removing System Controls from webui as should be used from HA"
|
||||
sed -i '/>System Controls/d' "$HOME"/BirdNET-Pi/homepage/views.php
|
||||
|
||||
# Remove services tab
|
||||
echo "... removing Ram drive from webui as it is handled from HA"
|
||||
sed -i '/Ram drive/{n;s/center"/center" style="display: none;"/;}' "$HOME"/BirdNET-Pi/scripts/service_controls.php
|
||||
sed -i '/Ram drive/d' "$HOME"/BirdNET-Pi/scripts/service_controls.php
|
||||
|
||||
# Correct services to start as user pi
|
||||
echo "... correct services to start as pi"
|
||||
for file in $(find "$HOME"/BirdNET-Pi/templates/birdnet*.service -print0 | xargs -0 basename -a) livestream.service chart_viewer.service chart_viewer.service spectrogram_viewer.service; do
|
||||
if [[ "$file" != "birdnet_log.service" ]]; then
|
||||
sed -i "s|ExecStart=|ExecStart=/usr/bin/sudo -u pi |g" "$HOME/BirdNET-Pi/templates/$file"
|
||||
fi
|
||||
done
|
||||
|
||||
# Send services log to container logs
|
||||
echo "... send services log to container logs"
|
||||
for file in $(find "$HOME"/BirdNET-Pi/templates/birdnet*.service -print0 | xargs -0 basename -a) livestream.service chart_viewer.service chart_viewer.service spectrogram_viewer.service; do
|
||||
sed -i "/Service/a StandardError=append:/proc/1/fd/1" "$HOME/BirdNET-Pi/templates/$file"
|
||||
sed -i "/Service/a StandardOutput=append:/proc/1/fd/1" "$HOME/BirdNET-Pi/templates/$file"
|
||||
done
|
||||
|
||||
# Avoid preselection in include and exclude lists
|
||||
echo "... avoid preselecting options in include and exclude lists"
|
||||
sed -i "s|option selected|option disabled|g" "$HOME"/BirdNET-Pi/scripts/include_list.php
|
||||
sed -i "s|option selected|option disabled|g" "$HOME"/BirdNET-Pi/scripts/exclude_list.php
|
||||
|
||||
# Correct log services to show /proc/1/fd/1
|
||||
echo "... show container logs in /logs"
|
||||
sed -i "/User=pi/d" "$HOME/BirdNET-Pi/templates/birdnet_log.service"
|
||||
sed -i "s|birdnet_log.sh|cat /proc/1/fd/1|g" "$HOME/BirdNET-Pi/templates/birdnet_log.service"
|
||||
|
||||
# Make sure config is correctly formatted.
|
||||
echo "... caddyfile modifications"
|
||||
#Correct instructions
|
||||
caddy fmt --overwrite /etc/caddy/Caddyfile
|
||||
#Change port to leave 80 free for certificate requests
|
||||
sed -i "s|http://|http://:8081|g" /etc/caddy/Caddyfile
|
||||
sed -i "s|http://|http://:8081|g" "$HOME"/BirdNET-Pi/scripts/update_caddyfile.sh
|
||||
#Remove default file that blocks 80
|
||||
if [ -f /etc/caddy/Caddyfile.original ]; then rm /etc/caddy/Caddyfile.original; fi
|
||||
|
||||
# Improve webui paths to facilitate ingress
|
||||
echo "... correcting webui paths"
|
||||
sed -i "s|/stats|/stats/|g" "$HOME"/BirdNET-Pi/homepage/views.php
|
||||
sed -i "s|/log|/log/|g" "$HOME"/BirdNET-Pi/homepage/views.php
|
||||
|
||||
# If port 80 is enabled, make sure it is still 80
|
||||
if [ -n "$(bashio::addon.port 80)" ] && [ "$(bashio::addon.port 80)" != 80 ]; then
|
||||
bashio::log.fatal "The port 80 is enabled, but should still be 80 if you want the automatic ssl certificates generation to work"
|
||||
fi
|
||||
|
||||
echo " "
|
||||
37
battybirdnet-pi/rootfs/etc/cont-init.d/91-nginx_ingress.sh
Normal file
37
battybirdnet-pi/rootfs/etc/cont-init.d/91-nginx_ingress.sh
Normal file
@@ -0,0 +1,37 @@
|
||||
#!/usr/bin/with-contenv bashio
|
||||
# shellcheck shell=bash
|
||||
set -e
|
||||
|
||||
#################
|
||||
# NGINX SETTING #
|
||||
#################
|
||||
|
||||
declare ingress_interface
|
||||
declare ingress_port
|
||||
declare ingress_entry
|
||||
|
||||
# Variables
|
||||
ingress_port=$(bashio::addon.ingress_port)
|
||||
ingress_interface=$(bashio::addon.ip_address)
|
||||
ingress_entry=$(bashio::addon.ingress_entry)
|
||||
|
||||
# Quits if ingress not active
|
||||
if [ -z "$ingress_entry" ]; then exit 0; fi
|
||||
|
||||
echo " "
|
||||
bashio::log.info "Adapting for ingress"
|
||||
echo "... setting up nginx"
|
||||
sed -i "s/%%port%%/${ingress_port}/g" /etc/nginx/servers/ingress.conf
|
||||
sed -i "s/%%interface%%/${ingress_interface}/g" /etc/nginx/servers/ingress.conf
|
||||
sed -i "s|%%ingress_entry%%|${ingress_entry}|g" /etc/nginx/servers/ingress.conf
|
||||
|
||||
echo "... ensuring restricted area access"
|
||||
echo "${ingress_entry}" > /ingress_url
|
||||
sed -i "/function is_authenticated/a if (strpos(\$_SERVER['HTTP_REFERER'], '/api/hassio_ingress') !== false && strpos(\$_SERVER['HTTP_REFERER'], trim(file_get_contents('/ingress_url'))) !== false) { \$ret = true; return \$ret; }" "$HOME"/BirdNET-Pi/scripts/common.php
|
||||
|
||||
echo "... adapt Caddyfile for ingress"
|
||||
chmod +x /helpers/caddy_ingress.sh
|
||||
/./helpers/caddy_ingress.sh
|
||||
sed -i "/sudo caddy fmt --overwrite/i /./helpers/caddy_ingress.sh" "$HOME"/BirdNET-Pi/scripts/update_caddyfile.sh
|
||||
|
||||
echo " "
|
||||
20
battybirdnet-pi/rootfs/etc/cont-init.d/92-ssl.sh
Normal file
20
battybirdnet-pi/rootfs/etc/cont-init.d/92-ssl.sh
Normal file
@@ -0,0 +1,20 @@
|
||||
#!/usr/bin/with-contenv bashio
|
||||
# shellcheck shell=bash
|
||||
set -e
|
||||
|
||||
###############
|
||||
# SSL SETTING #
|
||||
###############
|
||||
|
||||
if bashio::config.true 'ssl'; then
|
||||
bashio::log.info "Ssl is enabled using addon options, setting up nginx"
|
||||
bashio::config.require.ssl
|
||||
certfile=$(bashio::config 'certfile')
|
||||
keyfile=$(bashio::config 'keyfile')
|
||||
sed -i "2a\ tls /ssl/${certfile} /ssl/${keyfile}" /etc/caddy/Caddyfile
|
||||
sed -i "s|http://:8081|https://:8081|g" /etc/caddy/Caddyfile
|
||||
sed -i "s|http://:8081|https://:8081|g" "$HOME"/BirdNET-Pi/scripts/update_caddyfile.sh
|
||||
sed -i "/https:/a tls /ssl/${certfile} /ssl/${keyfile}" "$HOME"/BirdNET-Pi/scripts/update_caddyfile.sh
|
||||
fi
|
||||
|
||||
echo " "
|
||||
72
battybirdnet-pi/rootfs/etc/cont-init.d/99-run.sh
Normal file
72
battybirdnet-pi/rootfs/etc/cont-init.d/99-run.sh
Normal file
@@ -0,0 +1,72 @@
|
||||
#!/command/with-contenv bashio
|
||||
# shellcheck shell=bash
|
||||
set -e
|
||||
|
||||
##############
|
||||
# SET SYSTEM #
|
||||
##############
|
||||
|
||||
echo " "
|
||||
bashio::log.info "Setting password for the user pi"
|
||||
echo "pi:$(bashio::config "pi_password")" | sudo chpasswd
|
||||
echo "... done"
|
||||
|
||||
echo " "
|
||||
bashio::log.info "Starting system services"
|
||||
|
||||
# Set TZ
|
||||
if bashio::config.has_value 'TZ'; then
|
||||
TIMEZONE=$(bashio::config 'TZ')
|
||||
echo "... setting timezone to $TIMEZONE"
|
||||
ln -snf /usr/share/zoneinfo/"$TIMEZONE" /etc/localtime
|
||||
echo "$TIMEZONE" >/etc/timezone
|
||||
fi || (bashio::log.fatal "Error : $TIMEZONE not found. Here is a list of valid timezones : https://manpages.ubuntu.com/manpages/focal/man3/DateTime::TimeZone::Catalog.3pm.html")
|
||||
|
||||
# Correcting systemctl
|
||||
echo "... correcting systemctl"
|
||||
mv /helpers/systemctl3.py /bin/systemctl
|
||||
chmod a+x /bin/systemctl
|
||||
|
||||
# Correcting systemctl
|
||||
echo "... correcting datetimectl"
|
||||
mv /helpers/timedatectl /usr/bin/timedatectl
|
||||
chmod a+x /usr/bin/timedatectl
|
||||
|
||||
# Correct language labels
|
||||
export "$(grep "^DATABASE_LANG" /config/birdnet.conf)"
|
||||
# Saving default of en
|
||||
cp "$HOME"/BirdNET-Pi/model/labels.txt "$HOME"/BirdNET-Pi/model/labels.bak
|
||||
# Adapt to new language
|
||||
echo "... adapting labels according to birdnet.conf file to $DATABASE_LANG"
|
||||
/."$HOME"/BirdNET-Pi/scripts/install_language_label_nm.sh -l "$DATABASE_LANG"
|
||||
|
||||
echo "... starting cron"
|
||||
systemctl start cron
|
||||
|
||||
# Starting dbus
|
||||
echo "... starting dbus"
|
||||
service dbus start
|
||||
|
||||
# Starting journald
|
||||
# echo "... starting journald"
|
||||
# systemctl start systemd-journald
|
||||
|
||||
# Starting services
|
||||
echo ""
|
||||
bashio::log.info "Starting battybirdnet-pi services"
|
||||
chmod +x "$HOME"/BirdNET-Pi/scripts/restart_services.sh
|
||||
"$HOME"/BirdNET-Pi/scripts/restart_services.sh
|
||||
|
||||
if bashio::config.true LIVESTREAM_BOOT_ENABLED; then
|
||||
echo "... starting livestream"
|
||||
sudo systemctl enable icecast2
|
||||
sudo systemctl start icecast2.service
|
||||
sudo systemctl enable --now livestream.service
|
||||
fi
|
||||
|
||||
# Correct the phpsysinfo for the correct gotty service
|
||||
gottyservice="$(pgrep -l "gotty" | awk '{print $NF}' | head -n 1)"
|
||||
echo "... using $gottyservice in phpsysinfo"
|
||||
sed -i "s/,gotty,/,${gottyservice:-gotty},/g" "$HOME"/BirdNET-Pi/templates/phpsysinfo.ini
|
||||
|
||||
echo " "
|
||||
Reference in New Issue
Block a user