Skip to content

Instantly share code, notes, and snippets.

@mathieuthollet
Last active October 8, 2021 08:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mathieuthollet/b2f0e80211fe267035626bd4b8cf562c to your computer and use it in GitHub Desktop.
Save mathieuthollet/b2f0e80211fe267035626bd4b8cf562c to your computer and use it in GitHub Desktop.
Active / désactive la détection motioneye selon si un ou plusieurs IPs sont présentes sur le réseau (pour désactiver automatiquement la détection quand les téléphones portables sont connectés au wifi de la maison)
#!/bin/bash
# Time mobile offline before active detection (seconds)
IDLE_TIME_TRIGGER_DETECTION=3600
# IPs to check for availability
IP="192.168.1.89 192.168.1.92"
# motion URL
MOTION_URL="http://127.0.0.1:7999/1/detection"
# displays date for logs
date
# get previous detection status
if echo "`wget -qO- http://127.0.0.1:7999/1/detection/status`" | grep -q "ACTIVE"; then
DETECTION_STATUS="active"
else
DETECTION_STATUS="pause"
fi
echo "Detection status : $DETECTION_STATUS"
# send control command to motion
function motion_control () {
wget -q -O - "$MOTION_URL/$1" | grep Camera | sed -e "s/<[^>]*>//g" -e "s/.*Camera/Camera/"
}
# check mobile presence on network
function mobile_presence () {
# ping mobile to wake/fill arp cache
ping -c 1 "$1" &> /dev/null
# check if arp cache contains MAC or not (incomplete)
arp -an | grep "$1" | grep -v "incomplete" &> /dev/null
return $?
}
# check each mobile presence and stop motion detection if online
function presence () {
for i in $IP; do
sudo arp -d $i
if mobile_presence "$i"; then
echo "$i is online"
date +%s > /tmp/last_mobile_presence.txt
if [ ! "$DETECTION_STATUS" = "pause" ]; then
logger "device $i is online, pausing motion detection"
motion_control pause
fi
return 0
else
echo "$i is offline"
fi
done
return 1
}
# main script : if no mobile online, start motion detection
if ! presence; then
echo "$IP appear to be offline... retrying..."
if presence; then
exit 0
fi
echo "$IP is/are offline"
if [ ! "$DETECTION_STATUS" = "active" ]; then
echo "No device is online"
typeset -i LAST_MOBILE_PRESENCE=$(cat /tmp/last_mobile_presence.txt)
IDLE_TIME=$(($(date +%s)-$LAST_MOBILE_PRESENCE))
echo "Idle time : $IDLE_TIME (min idle time to trigger detection : $IDLE_TIME_TRIGGER_DETECTION)"
if (($IDLE_TIME>=$IDLE_TIME_TRIGGER_DETECTION)); then
echo "Starting motion detection"
motion_control start
fi
fi
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment