116 lines
2.5 KiB
Bash
Executable File
116 lines
2.5 KiB
Bash
Executable File
#!/bin/bash
|
|
|
|
# script to set away state in ZoneMinder when the phone is not present
|
|
# requires jq to be installed
|
|
|
|
|
|
curdir="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
|
|
|
|
#load configuration
|
|
. $curdir/isphonehere.config.sh
|
|
|
|
currentstate=
|
|
found_mac=
|
|
|
|
login() {
|
|
curl -c /tmp/zm_cookies.txt \
|
|
-sS \
|
|
-H 'Content-Type: application/x-www-form-urlencoded' \
|
|
--data "action=login&view=postlogin&postLoginQuery=&username=$zm_username&password=$zm_password" \
|
|
"$zm_baseurl/index.php" \
|
|
> /dev/null
|
|
}
|
|
|
|
logout() {
|
|
rm /tmp/zm_cookies.txt
|
|
}
|
|
|
|
setcurrentstate() {
|
|
login
|
|
currentstate=$( \
|
|
curl -sS -b /tmp/zm_cookies.txt $zm_baseurl/api/states.json | \
|
|
jq '.states | map(select(.State.IsActive == "1")) | .[0].State.Name' -r \
|
|
)
|
|
logout
|
|
}
|
|
|
|
sethere() {
|
|
login
|
|
curl -sS -b /tmp/zm_cookies.txt "$zm_baseurl/api/states/change/$zm_here_state.json" > /dev/null
|
|
logout
|
|
currentstate=$zm_here_state
|
|
}
|
|
|
|
setaway() {
|
|
login
|
|
curl -sS -b /tmp/zm_cookies.txt "$zm_baseurl/api/states/change/$zm_away_state.json" > /dev/null
|
|
logout
|
|
currentstate=$zm_away_state
|
|
}
|
|
|
|
setphonemac() {
|
|
found_mac=`sudo arp-scan --localnet | grep $mac | awk '{print $2}'`
|
|
}
|
|
|
|
waitbeforeretry() {
|
|
if [ "$currentstate" = "$zm_away_state" ]; then
|
|
sleep $seconds_between_retries_when_state_is_away
|
|
else
|
|
sleep $seconds_between_retries_when_state_is_here
|
|
fi
|
|
}
|
|
|
|
#read current state on startup, so we know what state we are in
|
|
setcurrentstate
|
|
|
|
#look for phone
|
|
while true; do
|
|
date
|
|
waitbeforeretry
|
|
setphonemac
|
|
|
|
if [ -n "$found_mac" ]; then
|
|
echo "phone is in da house"
|
|
|
|
if [ $currentstate != $zm_here_state ]; then
|
|
echo "setting state to $zm_here_state"
|
|
sethere
|
|
fi;
|
|
fi
|
|
|
|
if [ -z "$found_mac" ]; then
|
|
echo "phone is somewhere in the outside world"
|
|
|
|
#check if we should set the away state
|
|
i=0
|
|
phone_is_not_here_for_real="false"
|
|
while [ $i -lt $retries_before_setting_away ]; do
|
|
waitbeforeretry
|
|
let "i+=1"
|
|
echo "trying again: $i/$retries_before_setting_away"
|
|
|
|
setphonemac
|
|
|
|
if [ -n "$found_mac" ]; then
|
|
echo "nononono, phone is in da house!"
|
|
if [ $currentstate != $zm_here_state ]; then
|
|
echo "setting state to $zm_here_state"
|
|
sethere
|
|
fi;
|
|
break
|
|
fi
|
|
|
|
if [ $i == $retries_before_setting_away ]; then
|
|
phone_is_not_here_for_real="true"
|
|
fi
|
|
done
|
|
|
|
if [ $phone_is_not_here_for_real == "true" ]; then
|
|
if [ $currentstate != $zm_away_state ]; then
|
|
echo "setting state to $zm_away_state"
|
|
setaway
|
|
fi;
|
|
fi;
|
|
fi
|
|
done;
|