Files
carputer/templates/timelapse_service.sh.j2
2025-08-03 17:18:58 -07:00

87 lines
3.3 KiB
Django/Jinja

#!/bin/bash
trap "source {{ working_folder }}/create_timelapse.sh" SIGINT SIGTERM
echo "Start"
# initialize all the variables
# basic things
BEGIN=$(date +%Y%m%d-%H%M%S)
echo $BEGIN
WORKING_DIR="{{ working_folder }}/storage/$BEGIN"
mkdir -p $WORKING_DIR
if ( nc -w 3 -z 10.18.1.1 8184 2>&1 >/dev/null ); then
# be greedy about API calls
WHERES_GALI=$(curl -s http://10.18.1.1:8184/wheres_gali?api_key={{ tesla_api_key }})
# parse the one API call
CITY=$(echo $WHERES_GALI | jq .city)
STATE=$(echo $WHERES_GALI | jq .state)
ZIPCODE=$(echo $WHERES_GALI | jq .postcode)
DISPLAY_NAME=$(echo $WHERES_GALI | jq .display_name)
# Generate Status Report
echo Timelapse Initiated at $BEGIN >> $WORKING_DIR/info.txt
echo Shuttlecraft Galileo located at $CITY, $STATE $ZIPCODE >> $WORKING_DIR/info.txt
echo $DISPLAY_NAME >> $WORKING_DIR/info.txt
else
echo "No Gali API Data Available"
fi
RUN_FILE={{ working_folder }}/run
# timelapse creation helpers
#echo "To create timelapse, here's the ffpmeg script" >> $WORKING_DIR/info.txt
#echo "/bin/ffmpeg -r 30 -pattern_type glob -i "$WORKING_DIR/*.jpg" \\" >> $WORKING_DIR/info.txt
#echo "-vf "scale=1920x1080" -vcodec libx264 /$WORKING_DIR/00-timelapse.mp4" >> $WORKING_DIR/info.txt
# there should be a script that can do them all
#echo "/bin/ffmpeg -r 30 -pattern_type glob -i "$WORKING_DIR/*.jpg" \\" >> {{ working_folder }}/generate_timelapses.sh
#echo "-vf "scale=1920x1080" -vcodec libx264 /$WORKING_DIR/00-timelapse.mp4" >> {{ working_folder }}/generate_timelapses.sh
# capture time
touch $RUN_FILE
echo $RUN_FILE
i=1
while [ -e $RUN_FILE ]
do
FILENAME=$(printf "img-%05d" "$i")
LAT=$(cat {{ gps_service_directory }}/gps_data | grep lat | cut -d ":" -f 2 | awk '{printf("%.5f\n", $1)}')
LON=$(cat {{ gps_service_directory }}/gps_data | grep lon | cut -d ":" -f 2 | awk '{printf("%.5f\n", $1)}')
SPEED_KPH=$(cat {{ gps_service_directory }}/gps_data | grep speed | cut -d ":" -f 2)
if [ $SPEED_KPH > 1 ]; then
SPEED_MPH=$(echo "scale=3; $SPEEK_KPH * 0.62" | bc)
else
SPEED_MPH=0
fi
COORDINATES="$LAT, $LON, $SPEED_MPH mph"
curl http://127.0.0.1:7123/snapshot --output $WORKING_DIR/$FILENAME.jpg
echo $FILENAME
echo $COORDINATES
# This variable is for the timestamp
TIME=$(date +%c)
# Save the initial image in a temp file
convert $WORKING_DIR/$FILENAME.jpg -gravity SouthWest -pointsize 44 \
-fill yellow -annotate +30+30 "$TIME" $WORKING_DIR/$FILENAME-temp1.jpg
# Add the second annotation to the temp file
convert $WORKING_DIR/$FILENAME-temp1.jpg -gravity SouthEast -pointsize 44 \
-fill yellow -annotate +30+30 "$COORDINATES" $WORKING_DIR/$FILENAME-temp2.jpg
# Append the two images and save the final result
convert $WORKING_DIR/$FILENAME-temp2.jpg -append $WORKING_DIR/$FILENAME.jpg
# Optionally, remove intermediate files if you don't need them
rm $WORKING_DIR/$FILENAME-temp1.jpg $WORKING_DIR/$FILENAME-temp2.jpg
# This is for the tiny pic
# I have 1340x580. i think i will truncate the top
# Calculate the height to trim based on the aspect ratios of the input and output images
NOW=$(date +%Y%m%d%H%M%S)
convert $WORKING_DIR/$FILENAME.jpg -gravity North -crop 1920x830+0+260 +repage -resize 1340x580 {{ working_folder }}/small_thumbs/$NOW.jpg
sleep 1
((i++))
done
# end the loop