ansible-role-motion/files/process_motion_event.sh

257 lines
6.2 KiB
Bash
Raw Normal View History

2022-10-11 09:19:01 +02:00
#!/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
2022-10-14 07:46:17 +02:00
remove_picture=false
signal_alert=false
mail_alert=false
2023-01-30 08:45:02 +01:00
move_to=''
2022-10-14 07:46:17 +02:00
2022-10-11 09:19:01 +02:00
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
;;
2022-10-14 07:46:17 +02:00
"--remove-picture"|"-r")
shift
remove_picture=true
;;
"--signal-alert"|"-S")
shift
signal_alert=true
;;
"--mail-alert"|"-m")
shift
mail_alert=true
;;
2023-01-30 08:45:02 +01:00
"--move-to"|"-M")
shift
move_to="${1}"
shift
;;
2023-01-30 08:48:27 +01:00
"--move-to-port")
shift
move_to_port="${1}"
shift
;;
2023-03-20 21:04:27 +01:00
"--ntfy-server")
shift
ntfy_server="${1}"
shift
;;
"--ntfy-user")
shift
ntfy_user="${1}"
shift
;;
"--ntfy-password-file")
shift
ntfy_password_file="${1}"
shift
;;
2022-10-11 09:19:01 +02:00
*)
;;
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}\""
2023-01-30 08:45:02 +01:00
if [ -n "${move_to}" ]; then
2023-02-26 20:41:55 +01:00
message "Moving file '${file}' to '${move_to}'..."
2023-01-30 08:48:27 +01:00
scp -P "${move_to_port}" "${file}" "${move_to}/" && rm -rf "${file}"
2023-03-20 21:22:07 +01:00
return_code=$?
if [ $return_code != 0 ]; then
message "Error ${return_code} moving file."
fi
2023-01-30 08:45:02 +01:00
fi
2022-10-11 09:19:01 +02:00
fi
2023-03-20 21:41:42 +01:00
ntfy_data="{ ${data}, \"topic\": \"motion_detection\" }"
2022-10-11 09:19:01 +02:00
data="{${data}}"
2022-12-01 15:52:11 +01:00
if [ "${signal_alert}" == "true" ]; then
2022-10-11 09:19:01 +02:00
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}"
2023-03-20 21:22:07 +01:00
return_code=$?
if [ $return_code != 0 ]; then
message "Error ${return_code} sending to signal."
fi
fi
2022-10-11 09:19:01 +02:00
2022-12-01 15:52:11 +01:00
if [ "${mail_alert}" == "true" ]; then
2022-10-11 09:19:01 +02:00
message "Sending email to '${email_destination}'..."
2022-10-14 08:04:14 +02:00
echo "${data}" | jq '.' | mail -s "Motion event '${event_type}' on host '${motion_host}' camera '${camera_name}'" "${email_destination}"
2023-03-20 21:22:07 +01:00
return_code=$?
if [ $return_code != 0 ]; then
message "Error ${return_code} sending email"
fi
2022-10-11 09:19:01 +02:00
fi
2023-03-20 21:04:27 +01:00
if [ -n "${ntfy_server}" ]; then
ntfy_password="$(cat "${ntfy_password_file}")"
2023-03-20 21:39:05 +01:00
message "Sending event to ntfy server '${ntfy_server}/motion_detection' ..."
2023-03-20 21:39:55 +01:00
message "Data: ${ntfy_data}"
2023-03-20 21:43:50 +01:00
curl -s "${ntfy_server}" -u "${ntfy_user}:${ntfy_password}" -d "${ntfy_data}"
2023-03-20 21:22:07 +01:00
return_code=$?
if [ $return_code != 0 ]; then
message "Error ${return_code} publishing in ntfy server '${ntfy}'"
fi
2023-03-20 21:04:27 +01:00
fi
2023-03-20 21:31:01 +01:00
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 "${data}")
return_code=$?
if [ $return_code != 0 ]; then
message "Error ${return_code} sending to Home Assistant '${ha_url}'"
fi
2023-03-20 21:22:07 +01:00
fi
2022-10-14 07:46:17 +02:00
if ${remove_picture}; then
file_dirname=$(dirname "${file}")
file_dirname_parent=$(dirname "${file_dirname}")
find "${file_dirname_parent}" -type f -name \*.jpg -delete
fi