Global Constant
Steve Nay's ramblings

The local HTTP proxy

This is the third in a series about the home automation prototype system. The first post is here.

If you remember from this post, there is an HTTP proxy that sits between the local home devices and the Internet. It can talk the protocols the devices understand as well as HTTP that KNS understands. Here’s the system diagram again:

Right now, we don’t have any physical devices (this is still a prototype), so rather than communicating with the devices, the server simply prints out the effect that the directives would have on them.

If you remember the functions I described in the previous post, you’ll get a feel for what the server protocol looks like. Here they are again:

Sinatra makes it very easy to write the server (although you could use something like Bottle or Flask if you prefer). We use global variables (in the settings object) to keep track of “device” state. The server looks like this:

Once we get further in this project, we plan to get some real hardware and make it talk to the server. For now, though, we just have to read the output from Sinatra’s logs to see how the directives are taking effect.

Here’s an example interaction when the phone:incoming_call event was raised. The media_state and light_level are checked, and then they are both set. (See the rule that handles this event here.)

eridanus:home-emulator snay2$ ruby -rubygems server.rb 
==Sinatra/1.2.6 has taken the stage on 4567 for development with backup from Mongrel - - [20/Jul/2011 12:24:15] "GET /media_state HTTP/1.0" 200 23 0.0003 - - [20/Jul/2011 12:24:16] "GET /light_level HTTP/1.0" 200 19 0.0003
Media state is now stop. Was stop. - - [20/Jul/2011 12:24:16] "GET /media_state/stop HTTP/1.0" 200 23 0.0004
Light level now 10. Was 10. - - [20/Jul/2011 12:24:16] "GET /light_level/10 HTTP/1.0" 200 19 0.0005