92 lines
3.3 KiB
Bash
Executable File
92 lines
3.3 KiB
Bash
Executable File
#!/bin/ash
|
|
|
|
###############################################################
|
|
# @scriptname: BtPinger #
|
|
# @description: #
|
|
# Checks if device is nearby using Bluetooth #
|
|
# and reports status to Home automation system #
|
|
# #
|
|
# @project: SmartPinger #
|
|
# @author: Bram Prieshof #
|
|
# @author: Branco van de Waal #
|
|
###############################################################
|
|
|
|
. $( dirname "$0" )/../config/BtPing_"$1".cfg
|
|
. $( dirname "$0" )/../config/Pinger.cfg
|
|
|
|
#Default disable log
|
|
: ${LogFile:="/dev/null"}
|
|
|
|
sendDomoticz() {
|
|
#Set messages to send to Domoticz
|
|
if [ "$DeviceStatus" = Online ]; then
|
|
DomoSendMSG=On
|
|
elif [ "$DeviceStatus" = Offline ]; then
|
|
DomoSendMSG=On
|
|
else
|
|
echo "WARN: device status is unset" | tee -a $LogFile
|
|
fi
|
|
|
|
# Check Online / Offline state of Domoticz device
|
|
if [ "$DomoSendMSG" != "$(curl -s "http://"$DomoticzAddr"/json.htm?type=devices&rid="$DomoticzIDX"" | grep '"Data" :' | awk '{ print $3 }' | sed 's/[!@#\$%",^&*()]//g')" ] ; then
|
|
echo "Updating Domoticz device" | tee -a $LogFile
|
|
curl -s "http://"$DomoticzAddr"/json.htm?type=command¶m=switchlight&idx="$DomoticzIDX"&switchcmd="$DomoSendMSG"" 2>&1 1>/dev/null
|
|
fi
|
|
|
|
#Clean up
|
|
unset DomoSendMSG
|
|
}
|
|
|
|
sendMqtt() {
|
|
#Send MQTT device status message
|
|
mosquitto_pub -h $MqttIP -u $MqttUser -p $MqttPassword -r -q 1 -t $MqttBaseTopic/$DeviceName/Status -m "$(eval echo \$Mqtt"$DeviceStatus"Msg)"
|
|
mosquitto_pub -h $MqttIP -u $MqttUser -p $MqttPassword -r -q 1 -t $MqttBaseTopic/$DeviceName/Technology -m "$Technology"
|
|
}
|
|
|
|
while [ 1 ]
|
|
do
|
|
#Prepair for run
|
|
DeviceStatus=Offline
|
|
|
|
# First bluetooth ping attempt
|
|
if [ $DeviceStatus = Offline ]; then
|
|
if [[ "$(l2ping -c5 -s32 -t1000 "$DeviceBTMac" > /dev/null 2>&1 && echo "On" || echo "Off")" == 'On' ]]; then
|
|
DeviceStatus="Online"
|
|
Technology="Bluetooth"
|
|
Attempt="1"
|
|
fi
|
|
fi
|
|
|
|
# Second bluetooth ping attempt
|
|
if [ $DeviceStatus = Offline ]; then
|
|
if [[ "$(l2ping -c5 -s32 -t1000 "$DeviceBTMac" > /dev/null 2>&1 && echo "On" || echo "Off")" == 'On' ]]; then
|
|
DeviceStatus="Online"
|
|
Technology="Bluetooth"
|
|
Attempt="2"
|
|
fi
|
|
fi
|
|
|
|
# If the device is still offline, set Technology and Attempt to None
|
|
if [ $DeviceStatus = Offline ]; then
|
|
Technology="N/A"
|
|
Attempt="N/A"
|
|
fi
|
|
|
|
#Report Current status
|
|
echo "$(date '+%d/%m/%Y %H:%M') | Device: $DeviceName | Status: $DeviceStatus | Technology: $Technology | Attempt: $Attempt |" | tee -a $LogFile
|
|
#SendMsg to automation system
|
|
if [[ "$DeviceStatus" != "$PreviousDeviceStatus" ]]; then
|
|
send$MsgProtocol
|
|
fi
|
|
|
|
#Wait for next run if device is online
|
|
if [ "$DeviceStatus" = Online ]; then
|
|
echo "Online, Waiting"
|
|
sleep 20
|
|
fi
|
|
|
|
#Save current status and clean up for next run
|
|
PreviousDeviceStatus=$DeviceStatus
|
|
unset DeviceStatus Technology Attempt
|
|
done
|