7.4 Writing the Code for the Web Client
For the Web-enabled light switch, we will create a simple Ruby on Rails project to manage the user interface interaction first via a web browser. We won’t spend a lot of time on the user interface, though, since that will ultimately be the job of the custom Android application we will create after the web interface is functionally tested.
Rails runs optimally on Mac or Linux computers, and it is already installed by default on Mac OS X 10.6. However, it is not the latest version. Because this project requires Rails 3.0 or higher, the instructions are not applicable to older versions of the framework. Follow the instructions on the Ruby on Rails website to get the latest Rails release running on your computer.
With the Rails web framework installed, create a new directory and switch to that directory before creating the new Rails project:
>
mkdir ~/projects/ruby/rails/homeprojects/ |
|
> cd
~/projects/ruby/rails/homeprojects |
|
>
rails new x10switch |
|
|
|
create |
|
create
README |
|
create
Rakefile |
|
create
config.ru |
|
create
.gitignore |
|
create
Gemfile |
|
create
app |
|
create
app/controllers/application_controller.rb |
|
create
app/helpers/application_helper.rb |
|
create
app/mailers |
|
create
app/models |
|
... |
|
create
vendor/plugins |
|
create
vendor/plugins/.gitkeep |
Next, change into the new x10switch directory and create a new
controller called command with an
action called cmd to manage the
interaction between the web interface and the Heyu terminal
application.
> cd
x10switch |
|
>
rails generate controller Command cmd |
|
|
|
create
app/controllers/command_controller.rb |
|
route
get "command/cmd" |
|
invoke
erb |
|
create
app/views/command |
|
create
app/views/command/cmd.html.erb |
|
invoke
test_unit |
|
create
test/functional/command_controller_test.rb |
|
invoke
helper |
|
create
app/helpers/command_helper.rb |
|
invoke
test_unit |
|
create
test/unit/helpers/command_helper_test.rb |
Then, locate the app/controllers/command_controller.rb file and
check for the on and
off parameters and execute
the appropriate action:
class
CommandController < ApplicationController |
|
def cmd |
|
@result
= params[:cmd] |
|
|
|
if @result == "on" |
|
%x[/usr/local/bin/heyu on h3] |
|
end |
|
|
|
if @result == "off" |
|
%x[/usr/local/bin/heyu off h3] |
|
end |
|
end |
|
end |
The %x is a Ruby construct to execute an application
with command-line arguments. Hence, %x[/usr/local/bin/heyu on h3] tells Heyu to send an
on command code to the
H3 house code X10 switch.
Likewise, the %x[/usr/local/bin/heyu off
h3] tells that same switch to turn off.
Next, edit the app/views/command/cmd.html.erb document and
replace its placeholder contents with the following single line of
embedded Ruby code to display the results of the On and Off request:
The
light should now be <%= @result
%>. |
While we could go much further with this Rails
application, dressing it up with a nice user-friendly interface
accessed from the public/index.html file as well as providing
more verbose output of the result of the action, I will leave that
exercise for the aspiring reader. Since we will ultimately be
controlling the switch from a native mobile client application,
there’s little incentive to invest time in whipping up a sparkly
web UI when it will hardly ever be seen.
Finally, edit the config/routes.rb file and replace the
get "command/cmd" with the
following:
match
"/command/:cmd", :to => 'command#cmd' |
This instructs the Rails application on how to route incoming command requests to execute the on/off actions. Save your work and get ready to rumble!
If you’re setting up a newer version of Rails (such
as Rails 3.1) on a Linux system, you may also need to install a few
package dependencies (or gems as they’re known in Ruby parlance) in
order for Rails to run. Just edit the Gemfile file that was generated in the
x10switch directory and add the
following:
gem
'execjs' |
|
gem
'therubyracer' |
Save the changes and then run this command:
>
bundle install |
This will download and install the extra files used by the Rails 3.1 JavaScript processing engine. With these two gems successfully installed, you’re ready to run and test out the X10switch Rails application.