197 lines
4.8 KiB
Bash
197 lines
4.8 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
|
|
|
|
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
|
|
;;
|
|
*)
|
|
;;
|
|
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}\""
|
|
fi
|
|
|
|
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}"
|
|
fi
|
|
|
|
if [ "${mail_alert}" == "true" ]; then
|
|
message "Sending email to '${email_destination}'..."
|
|
echo "${data}" | jq '.' | mail -s "Motion event '${event_type}' on host '${motion_host}' camera '${camera_name}'" "${email_destination}"
|
|
fi
|
|
|
|
message "Sending Home Assistant event ..."
|
|
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=$?
|
|
message "Return code: ${return_code}. Result: ${result}"
|
|
|
|
if ${remove_picture}; then
|
|
file_dirname=$(dirname "${file}")
|
|
file_dirname_parent=$(dirname "${file_dirname}")
|
|
find "${file_dirname_parent}" -type f -name \*.jpg -delete
|
|
fi
|