#!/bin/bash
# iBeacon Scan by Radius Networks
# written by Micha
³ Dobrzyñski# modified by iNode.pl 08.03.2016
declare
-i cntdeclare -i dcnt
declare -i bcnt
declare -i tl
declare -i bl
declare -i bc
declare -i bt
declare -i DTYPE
if
[[ $1 == "parse" ]]; thenpacket
=""capturing=""
count=0
while read
linedo
count
=$[count + 1]if
[ "$capturing" ]; thenif
[[ $line =~ ^[0-9a-fA-F]{2}\ [0-9a-fA-F] ]]; thenpacket
="$packet $line"else
#echo RAW: $packet
cnt=0
dcnt
=0bl=0
bt=0
bc
=0np=""
mp=""
DTYPE
=0for
i in $packet; doif
tl
=`echo "ibase=16; $i"|bc`#echo TL $tl
fi
if
[[ "$cnt" -gt "13" ]]; thennp+
=$iif
[[ "$bl" -eq "0" ]]; thenif
[[ "$DTYPE" -eq "130" ]]; then # iNode Energy Meter#echo DTYPE
#echo $mp
#CONST
HEX
=`echo $mp | awk '{ print $12$11 }'`DEC
=`echo "ibase=16; $HEX"|bc`CONST
=$DEC#echo CONST $CONST
#WATY
HEX
=`echo $mp | awk '{ print $6$5 }'`DEC
=`echo "ibase=16; $HEX"|bc`#echo MINUTE POWER $DEC
CALC
=`echo $DEC $CONST | awk '{ kw=($1/$2*60); printf"%0.0f\n", kw } '`#IMPULSY
HEXIM
=`echo $mp | awk '{ print $10$9$8$7 }'`#echo TOTAL POWER $HEXIM
DECIM
=`echo "ibase=16; $HEXIM"|bc`#echo TOTAL POWER $DECIM
CALCIM
=`echo $DECIM $CONST | awk '{ kWh=($1/$2); printf"%0.3f\n", kWh }'`#echo CALCIM $CALCIM
TODOMOTICZ
=`echo $CALCIM | sed -r 's/\.//g'`#domoticz
/
usr/bin/curl -s -o /dev/null "http://ADRES_IP_DOMOTICZ:8080/json.htm?type=command¶m=udevice&idx=23&n value=0&svalue=$CALC;$TODOMOTICZ"#emoncms
#/usr/bin/curl -s -o /dev/null
"http://emoncms.org/input/post.json?json={meterpower:$CALC}&apikey=XYZ"#/usr/bin/curl -s -o /dev/null
"http://emoncms.org/input/post.json?json={meterkwh:$CALCIM}&apikey=XYZ"#logowanie do lokalnego sysloga
logger POWER
: $CALC W $CALCIM kWh -p local2.info#print na ekranie
#echo MAC: $MAC POWER: $CALC kW TOTAL: $CALCIM kWh | awk '{ print strftime("%Y-%m-%d %H:%M:%S"), $0; fflush(); }'
echo
MAC: $MAC POWER: $CALC kW TOTAL: $CALCIM kWh | awk -v data="$(date+"%Y-%m-%d %H:%M:%S")" '{print data, $0; fflush();}' DTYPE=0fi
if [[ "$dcnt" -lt "$tl" ]]; then
bl
=`echo "ibase=16; $i"|bc`bcnt=0
#echo BL $bl
mp=$i" "
fi
else
if [[ "$bcnt" -eq "0" ]]; then
bc
=`echo "ibase=16; $i"|bc`#echo BC $bc
fi
if
[[ "$bc" -eq "255" ]]; thenif
DTYPE
=`echo "ibase=16; $i"|bc`#echo DTYPE $DTYPE
fi fi
bcnt
=$bcnt+1 bl=$bl-1 mp+=$i" "fi
dcnt
=$dcnt+1fi
cnt
=$cnt+1done
capturing
="" packet=""fi fi
if
[ ! "$capturing" ]; thenif
[[ $line =~ ^\> ]]; thenpacket
=`echo $line | sed 's/^>.\(.*$\)/\1/'`capturing
=1fi fi
done else
hcitool lescan
--duplicates --passive 1>/dev/null &if
[ "$(pidof hcitool)" ]; thenhcidump
--raw | ./$0 parse $1 fifi