#! /bin/bash # If allowed_telnetPort is protected by a password, add the password here TELNETPASSWORD="" # Attribute for batterylevel in FHEM ATTRIBUT="batterylevel" # Use this, if you dont want the script to determine the tags on its own LETAGS="" # Ensure that we find gatttool in our PATH PATH=$PATH:/bin:/usr/bin:/usr/local/bin if [ "$1" = "-v" ]; then VERBOSE=y shift fi if [ ! -z "$*" -a -z "$LETAGS" ]; then LETAGS="$*" fi [ ! -z "$TELNETPASSWORD" ] && TELNETPASSWORD="$TELNETPASSWORD\n" if [ -z "$LETAGS" ]; then TAGS=$(echo -e "${TELNETPASSWORD}list MODE=lan-bluetooth" | socat -t60 - TCP:127.0.0.1:7072 | grep -v "Password" | tr "[:cntrl:]" "\n" | sed 's/ //g') echo "$TAGS" | grep "Internals" >/dev/null 2>&1 if [ $? -eq 0 ]; then TAGS=$(echo "$TAGS" | grep "NAME" | sed 's/NAME//') fi else TAGS="$LETAGS" fi for tag in $TAGS do [ ! -z "$VERBOSE" ] && echo "Determining address for $tag ..." TCONFIG=$(echo -e "${TELNETPASSWORD}list $tag" | socat -t60 - TCP:127.0.0.1:7072 | tr "[:cntrl:]" "\n") STATE=$(echo "$TCONFIG" | gawk '{if($1=="STATE")print $2}') if [ "$STATE" != "present" ]; then [ ! -z "$VERBOSE" ] && echo "$tag is in state ${STATE}, no further action required" else [ ! -z "$VERBOSE" ] && printf "Determining type for $tag ... " GTYPE=$(echo "$TCONFIG" | gawk '{if($3=="device_name")print $4}') [ ! -z "$VERBOSE" ] && echo "$GTYPE" TYPE="public" [ "$GTYPE" = "nut" ] && TYPE="random" ADDR=$(echo "$TCONFIG" | gawk '{if($1=="ADDRESS") print $2}' | tr "[:cntrl:]" "\n" | sed 's/ //g') [ ! -z "$VERBOSE" ] && echo "Fetching $ATTRIBUT for $tag ($ADDR) ..." DATA=$(gatttool -t $TYPE -b $ADDR --char-read --uuid 0x2a19 2>&1) LEVEL=$(echo "$DATA" | gawk '{if($1 == "handle:") {printf("%d\n",strtonum("0x"$4))}}') if [ ! -z "$LEVEL" ] && [ $LEVEL -gt 0 -a $LEVEL -lt 101 ]; then echo -e "${TELNETPASSWORD}setreading $tag $ATTRIBUT $LEVEL" | socat -t60 - TCP:127.0.0.1:7072 | grep -v "Password" [ ! -z "$VERBOSE" ] && echo "Setting $ATTRIBUT for $tag to ${LEVEL}%" else if [ ! -z "$VERBOSE" ]; then echo "No valid data found for $tag ($ADDR), data is:" echo ">$DATA<" fi fi [ ! -z "$VERBOSE" ] && echo "" fi done