initial commit
This commit is contained in:
23
isphonehere.config.example.sh
Normal file
23
isphonehere.config.example.sh
Normal file
@@ -0,0 +1,23 @@
|
|||||||
|
#mac address of your phone
|
||||||
|
mac="xx:xx:xx:xx:xx:xx"
|
||||||
|
|
||||||
|
#seconds to wait between checks if the phone is in the house
|
||||||
|
#when the zm state is set to "here"
|
||||||
|
#you may want to set this higher (fewer checks) when the phone
|
||||||
|
#is in the house
|
||||||
|
seconds_between_retries_when_state_is_here=20
|
||||||
|
|
||||||
|
#seconds to wait between checks if the phone is in the house
|
||||||
|
#when the zm state is set to "away"
|
||||||
|
seconds_between_retries_when_state_is_away=3
|
||||||
|
|
||||||
|
#if the phone is not found, retry this many times before changing
|
||||||
|
#state to "away"
|
||||||
|
retries_before_setting_away=5
|
||||||
|
|
||||||
|
zm_username='admin'
|
||||||
|
zm_password='urlencodedpassword'
|
||||||
|
zm_baseurl='https://myzoneminderurl'
|
||||||
|
|
||||||
|
zm_away_state='away'
|
||||||
|
zm_here_state='athome'
|
111
isphonehere.sh
Executable file
111
isphonehere.sh
Executable file
@@ -0,0 +1,111 @@
|
|||||||
|
#!/bin/bash
|
||||||
|
|
||||||
|
# script to set away state in zm when the phone is not present
|
||||||
|
# requires jq to be installed
|
||||||
|
curdir="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
|
||||||
|
. $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
|
||||||
|
}
|
||||||
|
|
||||||
|
#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;
|
Reference in New Issue
Block a user