Skip to content

Instantly share code, notes, and snippets.

@mathieuthollet
Last active October 8, 2021 08:07
Show Gist options
  • Select an option

  • Save mathieuthollet/b2f0e80211fe267035626bd4b8cf562c to your computer and use it in GitHub Desktop.

Select an option

Save mathieuthollet/b2f0e80211fe267035626bd4b8cf562c to your computer and use it in GitHub Desktop.

Revisions

  1. mathieuthollet revised this gist Oct 8, 2021. No changes.
  2. mathieuthollet revised this gist Jul 4, 2021. 1 changed file with 17 additions and 4 deletions.
    21 changes: 17 additions & 4 deletions motiondetection.sh
    Original file line number Diff line number Diff line change
    @@ -1,10 +1,16 @@
    #!/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"
    @@ -33,9 +39,10 @@ function presence () {
    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
    motion_control pause
    fi
    return 0
    else
    @@ -53,7 +60,13 @@ if ! presence; then
    fi
    echo "$IP is/are offline"
    if [ ! "$DETECTION_STATUS" = "active" ]; then
    echo "No device is online, starting motion detection"
    motion_control start
    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
    fi
  3. mathieuthollet revised this gist Jun 12, 2021. 1 changed file with 11 additions and 17 deletions.
    28 changes: 11 additions & 17 deletions motiondetection.sh
    Original file line number Diff line number Diff line change
    @@ -1,32 +1,24 @@
    #!/bin/bash

    if [ ! -f `dirname "$0"`/includes.sh ]; then
    function debug () {
    echo $@
    }
    else
    . `dirname "$0"`/includes.sh
    fi

    # 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"
    #DEBUG=yes

    # 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
    debug "Detection status : $DETECTION_STATUS"
    echo "Detection status : $DETECTION_STATUS"

    # valud arguments: status start pause
    # 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
    @@ -35,31 +27,33 @@ function mobile_presence () {
    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
    debug "$i is online"
    echo "$i is online"
    if [ ! "$DETECTION_STATUS" = "pause" ]; then
    logger "device $i is online, pausing motion detection"
    motion_control pause
    fi
    return 0
    else
    debug "$i is offline"
    echo "$i is offline"
    fi
    done
    return 1
    }

    # main script : if no mobile online, start motion detection
    if ! presence; then
    debug "$IP appear to be offline... retrying..."
    echo "$IP appear to be offline... retrying..."
    if presence; then
    exit 0
    fi
    debug "$IP is/are offline"
    echo "$IP is/are offline"
    if [ ! "$DETECTION_STATUS" = "active" ]; then
    logger "no device is online, starting motion detection"
    echo "No device is online, starting motion detection"
    motion_control start
    fi
    fi
    fi
  4. mathieuthollet created this gist Jun 12, 2021.
    65 changes: 65 additions & 0 deletions motiondetection.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,65 @@
    #!/bin/bash

    if [ ! -f `dirname "$0"`/includes.sh ]; then
    function debug () {
    echo $@
    }
    else
    . `dirname "$0"`/includes.sh
    fi

    # 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"
    #DEBUG=yes

    # 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
    debug "Detection status : $DETECTION_STATUS"

    # valud arguments: status start pause
    function motion_control () {
    wget -q -O - "$MOTION_URL/$1" | grep Camera | sed -e "s/<[^>]*>//g" -e "s/.*Camera/Camera/"
    }

    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 $?
    }

    function presence () {
    for i in $IP; do
    sudo arp -d $i
    if mobile_presence "$i"; then
    debug "$i is online"
    if [ ! "$DETECTION_STATUS" = "pause" ]; then
    logger "device $i is online, pausing motion detection"
    motion_control pause
    fi
    return 0
    else
    debug "$i is offline"
    fi
    done
    return 1
    }

    if ! presence; then
    debug "$IP appear to be offline... retrying..."
    if presence; then
    exit 0
    fi
    debug "$IP is/are offline"
    if [ ! "$DETECTION_STATUS" = "active" ]; then
    logger "no device is online, starting motion detection"
    motion_control start
    fi
    fi