Files
carputer/templates/timelapse_service.sh.j2
2025-07-28 06:21:45 -07:00

84 lines
3.3 KiB
Django/Jinja

#!/bin/bash
trap "source {{ working_folder }}/create_timelapse.sh" SIGINT SIGTERM
# initialize all the variables
# basic things
BEGIN=$(date +%Y%m%d-%H%M%S)
WORKING_DIR="{{ working_folder }}/storage/$BEGIN"
# 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)
RUN_FILE={{ working_folder }}/run
# Generate Status Report
mkdir -p $WORKING_DIR
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
# 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
i=1
while [ -e $RUN_FILE ]
do
FILENAME=$(printf "img-%05d" "$i")
# old API based latlon
#LATLON=$(curl -s http://10.18.1.1:8184/where_is?api_key={{ tesla_api_key }} )
#LON=$(echo $LATLON | jq .lon)
#LAT=$(echo $LATLON | jq .lat)
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 -lt 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
# 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'll make one every 5 seconds
# 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
if (( $i % 5 == 0 )); then
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
fi
sleep 1
((i++))
done