custom-scripts/timelapse/tl-control

163 lines
3.7 KiB
Plaintext
Raw Permalink Normal View History

2024-06-24 21:40:34 +00:00
#!/bin/bash
# Interactive front end for timelapse on Pi via webcam
# Configuration Variables
2024-06-24 21:46:00 +00:00
INSTALL_DIR="/usr/local/bin"
2024-06-25 14:16:08 +00:00
FILE_LIST=("tl-capture" "tl-control")
IMG_DIR="/tmp/TL"
SYNC_DES_PATH="/mnt/media/TL/to-process"
2024-06-24 21:40:34 +00:00
# Cron
CRON_DIR="/etc/cron.d"
CRON_FILE="$CRON_DIR/timelapse"
CRON_FREQUENCY="" # Set by prompt
CRON_LOG_FILE="/var/log/${FILE_LIST[0]}.log"
CRON_JOB="root $INSTALL_DIR/${FILE_LIST[0]} >> $CRON_LOG_FILE 2>&1"
2024-06-24 21:40:34 +00:00
# Check for root/sudo
if [ "$EUID" -ne 0 ]; then
echo "This script must be run as root or with sudo."
exit 1
else
echo "Running as root or with sudo."
fi
# Functions
# Start Time Lapse
start_tl() {
# Check if image directory exists and create if not
if [[ ! -d $IMG_DIR ]]; then
echo "Image directory doesn't exist, creating it now..."
mkdir -p $IMG_DIR
if [[ $? -eq 0 ]]; then
echo "Success!"
else
echo "Unable to create directory, exiting!"
exit 1
fi
fi
read -p "Image interval in minutes (1-59): " interval
# Make sure the interval is valid 1-59
if [[ "$interval" =~ ^[1-9]$|^[1-5][0-9]$ ]]; then
CRON_FREQUENCY="*/$interval * * * *"
else
echo "Invalid interval!"
return 1
fi
2024-06-24 21:40:34 +00:00
echo "Creating cron file: $CRON_FILE"
touch "$CRON_FILE"
if [[ $? -eq 0 ]]; then
echo "Success"
else
echo "Unable to create the file. Exiting"
exit 1
fi
echo -e "Writing job to file: $CRON_FILE\n$CRON_JOB"
cat <<EOF > $CRON_FILE
# Take an image every 15 minutes and save it for a time lapse
$CRON_FREQUENCY $CRON_JOB
2024-06-24 21:40:34 +00:00
EOF
if [[ $? -eq 0 ]]; then
echo "Success"
else
echo "Unable to write to the file. Exiting"
exit 1
fi
}
# Stop Time Lapse
stop_tl() {
echo "Removing cron file: $CRON_FILE"
rm $CRON_FILE
if [[ $? -eq 0 ]]; then
echo "Success"
else
echo "Unable to remove to the file. Exiting"
exit 1
fi
echo "Moving log file: $CRON_LOG_FILE"
mv "$CRON_LOG_FILE" "$CRON_LOG_FILE-bak"
if [[ $? -eq 0 ]]; then
echo "Success"
else
echo "Unable to move to the file. Exiting"
exit 1
fi
2024-06-24 21:40:34 +00:00
}
# NAS Sync
sync_nas() {
echo "Syncing $IMG_DIR to $SYNC_DES_PATH..."
rsync -av --progress --remove-source-files "$IMG_DIR" "$SYNC_DES_PATH" $1
2024-06-24 21:40:34 +00:00
if [[ $? -eq 0 ]]; then
echo "Success"
else
echo "Unable to complete sync. Exiting"
exit 1
fi
}
# Delete synced images
clean_pi() {
echo "Are you sure??"
read -n 1 -S
echo "Sike!"
}
# Menu Prompt
while true; do
# Empty the screen
# clear
# Print the prompt
echo "[ Time Lapse Control Panel ]"
echo ""
echo "[1] Start Time Lapse"
echo "[2] Stop Time Lapse"
echo "[3] Dry Run Sync to NAS"
echo "[4] Sync to NAS"
echo "[5] !! Delete ALL synced images !!"
echo ""
echo "[0] Exit"
# Wait for input
read -p "Option: " opt
# Execute the correct commands based on input.
case "$opt" in
1)
# Start Time Lapse
clear
start_tl
;;
2)
# Stop Time Lapse
clear
stop_tl
;;
3)
# Dry Run NAS Sync
clear
sync_nas --dry-run
# echo "Untested and not working yet!"
;;
4)
# NAS Sync
clear
sync_nas
;;
5)
# !! Delete ALL synced images on Pi !!
clear
clean_pi
;;
0)
# Exit
clear
exit
;;
*)
# clear
echo "!! Invalid Option !!"
;;
esac
done