Hardware Reference
In-Depth Information
As you can see from the code you will be running an HTTP server on port 80 and binding on all addresses.
You may have also noticed this line:
access_log = [ [ access_log_stream, AccessLog::COMBINED_LOG_FORMAT ] ]
This line sets up the access log to the exact same format of the Apache server access logs. This is very handy for
any Apache log parsing scripts you may already have; WEBrick will just seem like another Apache server log. You also
will notice that everything, including the log files, will be contained in one directory. This is not a really good practice
but it's okay for this code demonstration. A very important line is the following:
pipe = IO.popen("./reader-loop.rb")
This line will spawn the reader-loop.rb script and the best part of using the popen function is that when the
security-server.rb script is terminated it will also terminate the spawned child script.
Listing 5-3. The reader-loop.rb Code
#!/usr/bin/ruby
# Description : Ruby webrick html server and reader loop
# this app will run out of one directory self contained
# Author : Brendan Horan
# Start a loop that should not end
while true do
# Open index.html for writing and the two value files for reading
index = File.new("index.html", "w+")
mat = File.open("/sys/class/gpio/gpio4/value")
pir = File.open("/sys/class/gpio/gpio17/value")
# Set the html page to auto refresh every second
index.puts "<HTML><HEAD> <meta http-equiv='refresh' content='1'></HEAD>"
# get the status of the pressure mat
while line = mat.gets do
index.puts "<p>Mat status :-</p>"
index.puts line
end
mat.close
# get the status of the PIR
while line = pir.gets do
index.puts "<p>PIR status :-</p>"
index.puts line
end
pir.close
# Close index.html and sleep to allow security-server.rb to read
index.close
sleep 2
end
 
Search WWH ::




Custom Search