ansible-role-motion/files/process_motion_event.sh

270 lines
7 KiB
Bash

#!/usr/bin/env bash
#shellcheck disable=SC1091
test -e /var/lib/from_repos/scripts/shared_functions.sh && source /var/lib/from_repos/scripts/shared_functions.sh
remove_picture=false
signal_alert=false
mail_alert=false
move_to=''
motion_host="$(hostname -f)"
data="\"date\": \"$(date +%s)\""
while [ $# -gt 0 ]
do
case "$1" in
"--event-type"|"-e")
shift
event_type="$1"
data="${data}, \"event_type\": \"${event_type}\""
shift
;;
"--camera-id"|"-t")
shift
camera_id="$1"
data="${data}, \"camera_id\": \"${camera_id}\""
shift
;;
"--image-width"|"-w")
shift
image_width="${1}"
data="${data}, \"image_width\": \"${image_width}\""
shift
;;
"--height-motion"|"-J")
shift
height_motion="$1"
data="${data}, \"height_motion\": \"${height_motion}\""
shift
;;
"--width-motion"|"-i")
shift
width_motion="$1"
data="${data}, \"width_motion\": \"${width_motion}\""
shift
;;
"--text-event"|"-C")
shift
text_event="${1}"
data="${data}, \"text_event\": \"${text_event}\""
shift
;;
"--threshold"|"-o")
shift
threshold="${1}"
data="${data}, \"threshold\": \"${threshold}\""
shift
;;
"--camera-name"|"-c")
shift
camera_name="${1}"
data="${data}, \"camera_name\": \"${camera_name}\""
shift
;;
"--event"|"-v")
shift
event="${1}"
data="${data}, \"event\": \"${event}\""
shift
;;
"--changed-pixels"|"-D")
shift
changed_pixels="${1}"
data="${data}, \"changed_pixels\": \"${changed_pixels}\""
shift
;;
"--image-height"|"-h")
shift
image_height="${1}"
data="${data}, \"image_height\": \"${image_height}\""
shift
;;
"--motion-center-x"|"-K")
shift
motion_center_x="${1}"
data="${data}, \"motion_center_x\": \"${motion_center_x}\""
shift
;;
"--motion-center-y"|"-L")
shift
motion_center_y="${1}"
data="${data}, \"motion_center_y\": \"${motion_center_y}\""
shift
;;
"--file"|"-f")
shift
file="${1}"
data="${data}, \"file\": \"${file}\""
shift
;;
"--despeckle-labels"|"-Q")
shift
despeckle_labels="${1}"
data="${data}, \"despeckle_labels\": \"${despeckle_labels}\""
shift
;;
"--frames-per-second"|"-F")
shift
fps="${1}"
data="${data}, \"fps\": \"${fps}\""
shift
;;
"--frame-number"|"-q")
shift
frame_number="${1}"
data="${data}, \"frame_number\": \"${frame_number}\""
shift
;;
"--noise-level"|"-N")
shift
noise_level="${1}"
data="${data}, \"noise_level\": \"${noise_level}\""
shift
;;
"--filetype-number"|"-n")
shift
filetype_number="${1}"
data="${data}, \"filetype_number\": \"${filetype_number}\""
shift
;;
"--host"|"-h")
shift
motion_host="${1}"
data="${data}, \"motion_host\": \"${motion_host}\""
shift
;;
"--signal-destination"|"-s")
shift
signal_destination="${1}"
shift
;;
"--home-assistant-token"|"-T")
shift
ha_token="${1}"
shift
;;
"--home-assistant-url"|"-U")
shift
ha_url="${1}"
shift
;;
"--email-destination"|"-E")
shift
email_destination="${1}"
shift
;;
"--remove-picture"|"-r")
shift
remove_picture=true
;;
"--signal-alert"|"-S")
shift
signal_alert=true
;;
"--mail-alert"|"-m")
shift
mail_alert=true
;;
"--move-to"|"-M")
shift
move_to="${1}"
shift
;;
"--move-to-port")
shift
move_to_port="${1}"
shift
;;
"--ntfy-server")
shift
ntfy_server="${1}"
shift
;;
"--ntfy-user")
shift
ntfy_user="${1}"
shift
;;
"--ntfy-password-file")
shift
ntfy_password_file="${1}"
shift
;;
*)
;;
esac
done
if [ -n "${file}" ]; then
file_uri=$(echo "${file}" | sed 's|/srv/expendable_data|https://ficheros.koti.site|g')
data="${data}, \"file_uri\": \"${file_uri}\""
if [ -n "${move_to}" ]; then
message "Moving file '${file}' to '${move_to}'..."
scp -P "${move_to_port}" "${file}" "${move_to}/" && rm -rf "${file}"
return_code=$?
if [ $return_code != 0 ]; then
message "Error ${return_code} moving file."
fi
fi
fi
json_data="{${data}}"
if [ "${signal_alert}" == "true" ]; then
message "Sending to Signal destination '${signal_destination}'..."
/var/lib/from_repos/signal_scripts/send_msg.sh --destination "${signal_destination}" \
--message "Clip termino en camara '${camera_name}' (${motion_host}). Evento: #${event}. File: ${file_uri}"
return_code=$?
if [ $return_code != 0 ]; then
message "Error ${return_code} sending to signal."
fi
fi
if [ "${mail_alert}" == "true" ]; then
message "Sending email to '${email_destination}'..."
echo "${json_data}" | jq '.' | mail -s "Motion event '${event_type}' on host '${motion_host}' camera '${camera_name}'" "${email_destination}"
return_code=$?
if [ $return_code != 0 ]; then
message "Error ${return_code} sending email"
fi
fi
if [ -n "${ntfy_server}" ]; then
ntfy_password="$(cat "${ntfy_password_file}")"
message "Sending event to ntfy server '${ntfy_server}/motion_detection' ..."
if [ -n "${file}" ]; then
temp_file="$(mktemp /tmp/tmp.XXXXX.mkv)"
result=$(/usr/bin/ffmpeg -y -filter_complex '[0:v] fps=12,scale=480:-1,split [a][b];[a] palettegen [p];[b][p] paletteuse' -i "${file}" "${temp_file}" 2>&1)
return_code=$?
if [ ${return_code} != 0 ]; then
message "Error ${return_code} processing video file '${file}'. ${result}"
fi
ntfy_data="{ \"topic\": \"motion_detection\", \"message\": \"Event ${event_type} on host ${motion_host} camera ${camera_name}\", \"filename\": \"${temp_file}\" }"
curl -s "${ntfy_server}" -T "${temp_file}" -u "${ntfy_user}:${ntfy_password}" -d "${ntfy_data}"
return_code=$?
rm "${temp_file}"
else
ntfy_data="{ \"topic\": \"motion_detection\", \"message\": \"Event ${event_type} on host ${motion_host} camera ${camera_name}\"}"
curl -s "${ntfy_server}" -u "${ntfy_user}:${ntfy_password}" -d "${ntfy_data}"
return_code=$?
fi
if [ $return_code != 0 ]; then
message "Error ${return_code} publishing in ntfy server '${ntfy}'"
fi
message "Data sent: ${ntfy_data}"
fi
if [ -n "${ha_url}" ]; then
message "Sending event to Home Assistant ..."
result=$(curl -s -X POST -H "Authorization: Bearer ${ha_token}" -H "Content-Type: application/json" "${ha_url}/api/events/motion_event" -d "${json_data}")
return_code=$?
if [ $return_code != 0 ]; then
message "Error ${return_code} sending to Home Assistant '${ha_url}'"
fi
fi
if ${remove_picture}; then
file_dirname=$(dirname "${file}")
file_dirname_parent=$(dirname "${file_dirname}")
find "${file_dirname_parent}" -type f -name \*.jpg -delete
fi