• No results found

Table 7.5 The final shell script for presence detection in MS Windows PowerShell

7.12 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 of which we want to display via the GUI of our smartphone app.

For this purpose we write another brief Powershell script, which uses the arguments $args[0]

as log entry and $args[0] as log filename.

We further use the Test-Path Cmdlt to verify whether the logfile already exists. It returns True if the file exists, and returns False if the file does not exist:

Test-Path c:\scripts\test.txt

Now we can create our presence logging script preslog.ps1 which either creates the log file if it does not exist yet and writes the first log entry or, if it does exist, appends the log entry at the end of the file. As a presence logging file we again use an 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 validate whether the correct number of arguments are provided, when the script is called:

if (($args.count -lt 2) -or ($args.count -gt 2)){

echo “Argument error: preslog.ps1 [log file] [log message]”

exit }

The first argument $args[0] shall be the name of the log file, the second argument $args[1] the log message. If the log file exists, we append (>>) date and time (which we store in the variable $now) along with the log message to the existing file content. If it does not exist, we create the file (>) and write date, time and message to it:

if (($args.count -lt 2) -or ($args.count -gt 2)){

echo “Argument error: preslog.ps1 [log file] [log message]”

exit }

$path=“C:\Users\smarthome\shProject\ORC\webapps\controller\” + $args[0]

if (Test-Path $path){

$now = [DateTime]::Now echo $args[1] $now >> $path }

else {

$now = [DateTime]::Now echo $args[1] $now > $path echo $args[1] not found

}

We can now test our script typing:

.\preslog.ps1 presencelog.html “return Chris”

All what is left is to add the code line to presence.ps1, which calls the above logging script, right after the code line, which writes the return event to the file presence.html:

echo “return $args[1]” > C:\Users\smarthome\shProject\ORC\webapps\controller\presence.html .\preslog.ps1 presencelog.html “return Chris”

Keep in mind that if a shell argument (as in our case “return Chris”) contains spaces, it needs to be surrounded by quotation marks. Or you can 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.

In order to control our Powershell scripts from OpenRemote, we will start them from the command line without opening Powershell, which can be done by typing powershell.exe followed by the full path to the script and (if required) associated script parameters, for example:

powershell.exe C:\Users\smarthome\shProject\turnPresenceOn.ps1

7.13 Testing the Script

In order to test our work we open the terminal window (Start — CMD) and start the presence function by typing:

powershell.exe C:\Users\smarthome\shProject\turnPresenceOn.ps1

(In order to validate that windump.exe has started you can open a second terminal window and type the command tasklist. Among the processes listed you should see a new one called

windump.exe).

We now open in our web browser the presence status file presencefunction.html and the presence log file presencelog.html:

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 the Wi-Fi function of our smartphone on. Refreshing the two tabs in our web browser should now display on in presencefunction.html, and the return message in

presencelog.html.

We open a new terminal window (Start — CMD) and stop our presence function by typing:

powershell.exe C:\Users\smarthome\shProject\turnPresenceOff.ps1

Refreshing the tab for presencefunction.html in our web browser should now display 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.ps1

if (($args.count -lt 2) -or ($args.count -gt 2)){

echo “Argument error: presence.ps1 [dead time in seconds] [MAC address of target]”

exit }

while ($(cat C:\Users\smarthome\shProject\ORC\webapps\controller\presencefunction.html) -eq “on” ) { $timeout_in_seconds=$args[0]

$current_file_time_in_seconds = ([DateTime]::Now.ToFileTime())/10000000 $trigger_time_in_seconds = $current_file_time_in_seconds + $timeout_in_seconds

while (!( C:\Users\smarthome\shProject\windump.exe -i 1 -c 1 -v ether host $args[1] and dst port 67 2>&1 | select-string -pattern $args[1] -quiet )) {

}

if ((([DateTime]::Now.ToFileTime())/10000000) -le $trigger_time_in_seconds) { echo “return detection blocked”

} else {

echo “return $args[1]” > C:\Users\smarthome\shProject\ORC\webapps\controller\presence.html .\preslog.ps1 presencelog.html “return Chris”

cat C:\Users\smarthome\shProject\ORC\webapps\controller\presence.html Start-Sleep -Second 60

echo “idle” > C:\Users\smarthome\shProject\ORC\webapps\controller\presence.html }

}

echo “presence detection stopped”

preslog.ps1

if (($args.count -lt 2) -or ($args.count -gt 2)){

echo “Argument error: preslog.ps1 [log file] [log message]”

exit }

$path=“C:\Users\smarthome\shProject\ORC\webapps\controller\” + $args[0]

if (Test-Path $path){

$now = [DateTime]::Now echo $args[1] $now >> $path }

else {

$now = [DateTime]::Now echo $args[1] $now > $path echo $args[1] not found }

turnPresenceOn.ps1 (dead time: 10 seconds)

echo “on” > C:\Users\smarthome\shProject\ORC\webapps\controller\presencefunction.html

C:\Users\smarthome\shPRoject\presence.ps1 10 68:a8:6d:84:7d:42

turnPresenceOff.ps1

echo “off” > C:\Users\smarthome\shProject\ORC\webapps\controller\presencefunction.html kill -Name Windump

Table 7.6 Summary of Powershell presence detection scripts