Hirdetés

Új hozzászólás Aktív témák

  • ojb

    tag

    válasz koala69 #7911 üzenetére

    ARPDetect Linux -> Domoticz
    Az alábbi bash file-t Crontab-bal 1 percenként futtatom. A példában 3 telefonra:
    A telefonoknak fix IP cím van beállítva a routerben!!!
    Az eredmény 3 kapcsolón 880; 949; 950 jelenik meg. 10 percig tartja fenn az állapotot, miután a készülék nem elérhető.

    # m h dom mon dow command
    */1 * * * * /home/ojb/domoticz/scripts/bash/arpdetect.sh 192.168.1.*** 880 10
    */1 * * * * /home/ojb/domoticz/scripts/bash/arpdetect.sh 192.168.1.*** 950 10
    */1 * * * * /home/ojb/domoticz/scripts/bash/arpdetect.sh 192.168.1.*** 949 10

    arpdetect.sh

    #!/bin/bash
    # experiment for presence detection: update dummy switch in Domoticz based on ARP result.
    # will run every minute if setup in crontab
    # Date: 2016-12-01

    # parameters for calling script:
    # 1: name of device
    # 2: numeric ID of switchdevice in Domoticz
    # 3: number of minutes after which away is detected (on is always set directly)
    # example: ./[B]arpdetect phoneIP 72 10[/B]

    # Variables
    dir="[B]/home/ojb***/domoticz/scripts/bash[/B]"
    username=""
    password=""
    passcode=""

    # do not change below line
    #-----------------------------------

    # check correct number of arguments
    if [ "$#" -ne 3 ] ; then
    echo "Usage: $0 devicename #domoticzdevice #minutes" >&2
    exit 1
    fi
    period=$(expr $3 \* 60)
    now=$(date +%s)
    limit=$(expr $now - $period)
    result=$(/usr/sbin/arp -a $1)
    readdate=$(date)
    # first do ping to make sure ARP is updated
    /bin/ping -q -c 1 $1

    # check if devicename exists in ARP table
    if [[ ! "$result" =~ "$1" ]] ; then
    echo "devicename $1 does not exist" >&2
    exit 1
    fi
    # determine last detection:
    # if devicename is in ARP table then last detection is now,
    # otherwise read from file
    if [[ $result =~ "ether" ]] ; then
    lastdetect=$now
    echo $now $result $readdate > $dir/lastdetect_$1.log
    else
    lastdetect=$(/usr/bin/awk '{print $1}' $dir/lastdetect_$1.log)
    fi

    # read current switch status
    /usr/bin/curl -s "[B]http://192.168.1.***:8080[/B]/json.htm?type=devices&rid="$2"" | /bin/grep '"Status" : "Off",'
    switchon=$?

    # if device has not been present in the requested timeframe and switch is on then switch it off.
    if [[ $limit -ge $lastdetect && $switchon -eq 1 ]] ; then
    /usr/bin/curl "http://192.168.1.***:8080/json.htm?type=command¶m=switchlight&idx="$2"&switchcmd=Off";
    fi

    # if lastdetect in the requested timeframe and switch is off then switch it on.
    if [[ $lastdetect -gt $limit && $switchon -eq 0 ]] ; then
    /usr/bin/curl "[B]http://192.168.1.***:8080[/B]/json.htm?type=command¶m=switchlight&idx="$2"&switchcmd=On";
    fi

    A szükséges editálásokat *** jelöltem!

    [ Szerkesztve ]

Új hozzászólás Aktív témák