-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcamera-watch
executable file
·72 lines (60 loc) · 1.82 KB
/
camera-watch
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#!/bin/bash
## with slash at the end
CHROOT=/home/hass/camera/
SECRETS=/home/hass/.homeassistant/secrets.yaml
MINSECONDS=60
###########################################
PASS=$(egrep "^http_password" ${SECRETS} | awk '{ print $2 }')
inotifywait -q -r -m -e CLOSE_WRITE ~/camera |
while read -r dirname event filename; do
echo -n $event: $dirname $filename " "
CURRENT=$(date +"%s")
EXTENSION="${filename##*.}"
if [ "$EXTENSION" != "jpg" ]; then
echo "SKIP extension $EXTENSION"
continue
fi
CAMERA=$(cut -d'/' -f1 <<<"${dirname/$CHROOT/}")
CAMERA=${CAMERA,,} ## lowercase
#[ "${filename}" = "${CAMERA}.jpg" ] && continue
echo " $CAMERA: $filename"
FLAG=${CHROOT}${CAMERA}.jpg
if [ $(($CURRENT - $(stat -c %Y $FLAG))) -lt $MINSECONDS ]; then
echo "SKIP time"
continue
fi
ln -sf ${dirname}${filename} $FLAG
## notify hass
curl -q -X POST -H "x-ha-access: $PASS" -H "Content-Type: application/json" -d "{\"source\": \"$CAMERA\"}" \
http://localhost:8123/api/events/${CAMERA}_motion | jq .
done
#
## passed by ftpd
#FILENAME="$1"
#EXTENSION="${FILENAME##*.}"
#
#set -x
#
#if [ "$EXTENSION" != "jpg" ]; then
# echo "$EXTENSION skipped"
# exit 0
#fi
#
## name of the first folder level inside CHROOT
#INPUT=$(cut -d'/' -f1 <<<"${FILENAME/$CHROOT/}")
#
## lowercase
#INPUT=${INPUT,,}
#
## create link to FILENAME named INPUT
#[ -f $FILENAME ] && ln -sf $FILENAME ${CHROOT}${INPUT}.jpg
#
## notify hass
#PASS=$(egrep "^http_password" ${SECRETS} | awk '{ print $2 }')
##curl -X POST -H "x-ha-access: $PASS" -H "Content-Type: application/json" -d '{"state": "on"}' \
## http://localhost:8123/api/states/input_boolean.motion_${INPUT}
#
#curl -X POST -H "x-ha-access: $PASS" -H "Content-Type: application/json" -d "{\"source\": \"$INPUT\"}" \
# http://localhost:8123/api/events/motion_detected_${INPUT}
#
#exit 0