As a final improvement of our code we enhance our two presence detection control files
turnPresenceOff.sh and turnPresenceOn.sh. In order to start presence detection in addition to setting
presencefunction.html to on we need to start the batch file presence.sh. This gets us to the following two lines in turnPresenceOn.sh:
echo “on” > /Users/smarthome/ shProject/ORC/webapps/controller/presencefunction.html sh ./presence.sh 10 68:a8:6d:84:7d:42
And for stopping presence detection in addition to setting presencefunction.html to off we will stop the three processes turnPresenceOn.sh, presence.sh and tcpdump, which are initiated by our script turnPresenceOn.sh. Otherwise our presence detection would only stop after one more presence occurrence, which could take a long time. To stop a process we can use the kill command along with the process id. To list the processes which are currently active we use the command ps.
PID TTY TIME CMD 53735 ttys000 0:00.27 -bash 1058 ttys001 0:00.01 -bash
54459 ttys001 0:00.00 sh /Users/smarthome/shProject/turnPresenceOn.sh
54460 ttys001 0:00.00 /bin/sh /Users/smarthome/shProject/presence.sh 10 68:a8:6d:84:7d:42 54463 ttys001 0:00.03 tcpdump -i en1 -c 1 -v ether host 68:a8:6d:84:7d:42 and dst port 67
In order to retrieve the process id (PID) for our presence script we use the filter command
egrep:
ps | egrep ‘resence’ | awk ‘{print $1}’
(We avoid the p in the filter expression to have a match for both variants of presence:
presence.sh and turnPresenceOn.sh). The command ps lists all active processes, the pipe command | routes the output to egrep, which searches for the string ‘resence’, in order to capture turnPresenceOn.sh and presence.sh. Then the output of grep is routed to awk ‘{print $1}’, which gives us the first field of every matching line, which is the process id (PID) of the processes we are looking for. After starting our presence function you can try
ps | egrep ‘resence’ | awk ‘{print $1}’
which will get you something like
54512 54459 54460
We can now construct our kill command for all processes containing the strings ‘resence’
and ‘tcpdump’. The term $(x) means to execute x, then take its output and put it on the
command line. Since our egrep command outputs a process id, the below commands would actually execute something like kill 54512. To match two or more strings with egrep you need to separate the strings with a vertical bar |.With that our command reads:
kill $(ps | egrep ‘resence|tspdump’ | awk ‘{print $1}’)
We now have the following two lines in the turnPresenceOff.sh script:
echo “off” > /Users/smarthome/ shProject/ORC/webapps/controller/presencefunction.html kill $(ps | egrep ‘resence|tcpdump’ | awk ‘{print $1}’)
For the many options of kill, ps, egrep in the terminal window simply type man followed by the according command.
7.7 A Log File for Presence Detection
In addition to writing the return message to the file presence.html we want to log every return event along with date and time to the file presencelog.html. While presence.html serves as the real time sensor output, which reports a return for 60 seconds, and then goes back to idle, waiting for the next return event, presencelog.html shall contain a history of all return events, of which the last entry we want to display via the GUI of our smartphone app. For this purpose we write another brief bash script, which uses the arguments $1 as log entry and $2 as log filename. We further use the command -f filename, which returns true if a particular file is a regular file and exists. Now we can create our presence logging script preslog.sh which either creates the log file, in case it does not exist yet, and writes the first log entry or, in case it does exist, appends the log entry at the end of the file. As a presence logging file we again use a html file located in the root directory of the OpenRemote webserver under
ORC/webapps/controller/
in order to have the option to access its content through a web browser or through the OpenRemote HTTP protocol function. At the beginning of the script we again validate if the correct number of arguments are provided, when the script is called:
#!/bin/sh
#Logging script appends a log message to the end of a log file in case it exists, or creates the log file and writes the log message as the first line in case the log file does not exist.
if [[ $# -lt 2 || $# -gt 2 ]];then
echo “$0: Argument error: preslog.sh [log file] [log message]”
exit 2 fi if [ -f “$1” ] then
date “+$2 %m-%d-%y %Hh%M” >> /Users/smarthome/shProject/ORC/webapps/controller/$1 echo “$1 found.”
else
date “+$2 %m-%d-%y %Hh%M” > /Users/smarthome/shProject/ORC/webapps/controller/$1 echo “$1 not found.”
fi
All what is left is to add the code line to presence.sh, which calls the above logging script, right after the code line which writes the return event to the file presence.html:
echo “return $2” > /Users/smarthome/shProject/ORC/webapps/controller/presence.html sh /Users/smarthome/shProject/preslog.sh presencelog.html “return Chris”
Keep in mind, that in case a shell argument (as in our case “return Chris”) contains spaces, it needs to be surrounded by quotation marks. Or you bridge the space with an underscore:
“return_Chris”. For better readability instead of logging the MAC address of the phone we use the name of the smartphone owner. For each smartphone you want to monitor, you now can easily set up the monitoring and logging function in parallel processes.
7.8 Testing the Script
In order to test our work we open a terminal window (Applications — Utilities — Terminal) and start the presence function typing:
./turnPresenceOn.sh
We open the presence status file presence.html and the presence log file presencelog.html in our web browser window:
file:///Users/smarthome/shProject/ORC/webapps/controller/presencefunction.html file:///Users/smarthome/shProject/ORC/webapps/controller/presencelog.html
After waiting until the dead time since the start of our presence script has passed, we switch on the Wi-Fi function of our smartphone. Refreshing the two tabs in our web
browser should display on in presencefunction.html and the return message along with date and time in presencelog.html.
We now open a new terminal window (Applications — Utilities — Terminal) and stop our presence function by typing:
./turnPresenceOff.sh
After refreshing the tab for presencefunction.html in our web browser, its content should read off .
If the above works as desired, we can move on to the last step, which is to configure our presence function in OpenRemote Designer.
presence.sh
#!/bin/sh
#Presence detection using the smartphone DHCP request when booking ton a Wi-Fi (WLAN) network if [[ $# -lt 2 || $# -gt 2 ]];then
echo “$0: Argument error: presence.sh [dead time in seconds] [target MAC address]”
exit 2 fi
while [ $(cat /Users/smarthome/shProject/ORC/webapps/controller/presencefunction.html) = “on” ] ; do
# set timers for return detection timeout and variable for dhcp packet capture
echo “idle” > /Users/smarthome/shProject/ORC/webapps/controller/presence.html timeout_in_seconds=$1;
current_time_in_seconds=$(date +%s);
((trigger_time_in_seconds = $current_time_in_seconds + $timeout_in_seconds));
# capture until a dhcp request from MAC address 68:a8:6d:84:7d:42 is detected tcpdump -i en1 -c 1 -v ether host $2 and dst port 67 > capture.txt
# if dhcp request is received before the timeout, do nothing, else set presence.html to “return_Chris”
if [ “$(date +%s)” -le “$trigger_time_in_seconds” ] ; then
echo “return detection blocked”
else
echo “return $2” > /Users/smarthome/shProject/ORC/webapps/controller/presence.html sh /Users/smarthome/shProject/preslog.sh presencelog.html “return Chris”
cat /Users/smarthome/shProject/ORC/webapps/controller/presence.html sleep 60
echo “idle” > /Users/smarthome/shProject/ORC/webapps/controller/presence.html cat /Users/smarthome/shProject/ORC/webapps/controller/presence.html
fi done
echo “presence detection off”
preslog.sh
#!/bin/sh
#Logging script appends a log message to the end of a log file in case it exists, or creates the log file and writes the log message as the first line in case the log file does not exist.
if [[ $# -lt 2 || $# -gt 2 ]];then
echo “$0: Argument error: preslog.sh [log file] [log message]”
exit 2 fi
if [ -f “/Users/smarthome/shProject/ORC/webapps/controller/$1” ] then
date “+$2 %m-%d-%y %Hh%M” >> /Users/smarthome/shProject/ORC/webapps/controller/$1 echo “$1 found.”
else
date “+$2 %m-%d-%y %Hh%M” > /Users/smarthome/shProject/ORC/webapps/controller/$1 echo “$1 not found.”
fi
turnPresenceOn.sh (dead time: 10 seconds)
#!/bin/sh
echo “on” > /Users/smarthome/shProject/ORC/webapps/controller/presencefunction.html
sh ./presence.sh 10 68:a8:6d:84:7d:42
turnPresenceOff.sh
#!/bin/sh
echo “off” > /Users/smarthome/shProject/ORC/webapps/controller/presencefunction.html kill $(ps | egrep ‘resence|tspdump’ | awk ‘{print $1}’)