Inital commit
This commit is contained in:
111
bin/SmartPing.sh
Normal file
111
bin/SmartPing.sh
Normal file
@@ -0,0 +1,111 @@
|
||||
#!/bin/ash
|
||||
|
||||
###############################################################
|
||||
# @scriptname: SmartPinger #
|
||||
# @description: #
|
||||
# Checks if device is nearby first using WiFi, #
|
||||
# if not it will check using Bluetooth #
|
||||
# and reports status to Home automation system #
|
||||
# #
|
||||
# @project: SmartPinger #
|
||||
# @author: Bram Prieshof #
|
||||
# @author: Branco van de Waal #
|
||||
###############################################################
|
||||
|
||||
. $( dirname "$0" )/../config/SmartPing_"$1".cfg
|
||||
. $( dirname "$0" )/../config/Pinger.cfg
|
||||
unset $1
|
||||
|
||||
#Default disable log
|
||||
: ${LogFile:="/dev/null"}
|
||||
|
||||
sendDomoticz() {
|
||||
#Set messages to send to Domoticz
|
||||
if [ "$DeviceStatus" = Online ]; then
|
||||
DomoSendMSG=On
|
||||
elif [ "$DeviceStatus" = Offline ]; then
|
||||
DomoSendMSG=Off
|
||||
else
|
||||
echo "WARN: device status is unset" | tee -a $LogFile
|
||||
fi
|
||||
|
||||
# Check Online / Offline state of Domoticz device
|
||||
if [ "$DomoSendMSG" != "$(curl -s "http://"$DomoticzIP"/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://"$DomoticzIP"/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"$Status"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 network ping attempt
|
||||
fping -c5 -b 32 -t1000 $DeviceIP > /dev/null 2>&1
|
||||
if [ "$?" = 0 ] ; then
|
||||
DeviceStatus="Online"
|
||||
Technology="Wifi"
|
||||
Attempt="1"
|
||||
fi
|
||||
|
||||
# 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 network ping attempt
|
||||
if [ $DeviceStatus = Offline ]; then
|
||||
fping -c5 -b 32 -t1000 $DeviceIP > /dev/null 2>&1
|
||||
if [ "$?" = 0 ] ; then
|
||||
DeviceStatus="Online"
|
||||
Technology="Wifi"
|
||||
Attempt="2"
|
||||
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 60
|
||||
fi
|
||||
|
||||
#Save current status and clean up for next run
|
||||
PreviousDeviceStatus=$DeviceStatus
|
||||
unset DeviceStatus Technology Attempt
|
||||
done
|
||||
Reference in New Issue
Block a user