diff --git a/scripts/update_birdnet_snippets.sh b/scripts/update_birdnet_snippets.sh index a6fef3bf..c0e95197 100755 --- a/scripts/update_birdnet_snippets.sh +++ b/scripts/update_birdnet_snippets.sh @@ -26,6 +26,9 @@ chmod g+r $HOME # remove world-writable perms chmod -R o-w ~/BirdNET-Pi/templates/* +# update database schema +$my_dir/update_db.sh + APT_UPDATED=0 PIP_UPDATED=0 @@ -147,6 +150,29 @@ if grep -q 'birdnet_server.service' "$HOME/BirdNET-Pi/templates/birdnet_analysis systemctl daemon-reload && restart_services.sh fi + +# Ensure networkd-dispatcher is installed +if ! dpkg -s networkd-dispatcher >/dev/null 2>&1; then + echo "networkd-dispatcher is not installed. Installing it now..." + sudo apt update -qq + sudo apt install -qqy networkd-dispatcher +fi + +# Add BirdWeather past publication service if not already installed +export PYTHON_VIRTUAL_ENV="$HOME/BirdNET-Pi/birdnet/bin/python3" +BIRDWEATHER_PAST_DISPATCHER_SCRIPT="$HOME/BirdNET-Pi/templates/50-birdweather-past-publication" +BIRDWEATHER_PAST_SERVICE_FILE="/usr/lib/systemd/system/birdweather_past_publication@.service" +if [ ! -f "$BIRDWEATHER_PAST_DISPATCHER_SCRIPT" ] || [ ! -f "$BIRDWEATHER_PAST_SERVICE_FILE" ]; then + echo "Installing BirdWeather past publication service..." + install_birdweather_past_publication +fi +# Set ownership to root for the birdweather publication networkd-dispatcher script +if [ -f "$BIRDWEATHER_PAST_DISPATCHER_SCRIPT" ]; then + sudo chown root:root "$BIRDWEATHER_PAST_DISPATCHER_SCRIPT" + sudo chmod 755 "$BIRDWEATHER_PAST_DISPATCHER_SCRIPT" +fi + + TMP_MOUNT=$(systemd-escape -p --suffix=mount "$RECS_DIR/StreamData") if ! [ -f "$HOME/BirdNET-Pi/templates/$TMP_MOUNT" ]; then install_birdnet_mount diff --git a/scripts/update_db.sh b/scripts/update_db.sh new file mode 100755 index 00000000..2dd07f40 --- /dev/null +++ b/scripts/update_db.sh @@ -0,0 +1,44 @@ +#!/usr/bin/env bash + +DB_PATH="$HOME/BirdNET-Pi/scripts/birds.db" + +echo "Checking database schema for updates" + +# Check if the tables exist +DETECTIONS_TABLE_EXISTS=$(sqlite3 "$DB_PATH" "SELECT name FROM sqlite_master WHERE type='table' AND name='detections';") +SCRIPTS_MTD_TABLE_EXISTS=$(sqlite3 "$DB_PATH" "SELECT name FROM sqlite_master WHERE type='table' AND name='scripts_metadata';") + +if [ -z "$DETECTIONS_TABLE_EXISTS" ]; then + echo "Table 'detections' does not exist. Creating table..." + sqlite3 "$DB_PATH" << EOF + CREATE TABLE IF NOT EXISTS detections ( + Date DATE, + Time TIME, + Sci_Name VARCHAR(100) NOT NULL, + Com_Name VARCHAR(100) NOT NULL, + Confidence FLOAT, + Lat FLOAT, + Lon FLOAT, + Cutoff FLOAT, + Week INT, + Sens FLOAT, + Overlap FLOAT, + File_Name VARCHAR(100) NOT NULL); + CREATE INDEX "detections_Com_Name" ON "detections" ("Com_Name"); + CREATE INDEX "detections_Date_Time" ON "detections" ("Date" DESC, "Time" DESC); +EOF + echo "Table 'detections' created successfully." +elif [ -z "$SCRIPTS_MTD_TABLE_EXISTS" ]; then + echo "Table 'scripts_metadata' does not exist. Creating table..." + sqlite3 "$DB_PATH" << EOF + CREATE TABLE IF NOT EXISTS scripts_metadata ( + script_name TEXT PRIMARY KEY, + last_run DATETIME + ); +EOF + echo "Table 'scripts_metadata' created successfully." +else + echo "Tables 'detections' and 'scripts_metadata' already exist. No changes made." +fi + +echo "Database schema update complete."