Permalink
Browse files

Making it possible to send on and off commands to devices.

  • Loading branch information...
1 parent b2b76b2 commit 8fb5fb9a85cc30759720f836d5139ccc53027c2a @JEG2 committed Dec 11, 2010
Showing with 70 additions and 0 deletions.
  1. +45 −0 lib/haunted_house.rb
  2. +25 −0 lib/haunted_house/device.rb
View
@@ -0,0 +1,45 @@
+require "openssl"
+require "open-uri"
+require "rexml/document"
+
+require "haunted_house/device"
+
+# FIXME: this is dangerous
+OpenSSL::SSL.send(:remove_const, :VERIFY_PEER)
+OpenSSL::SSL::VERIFY_PEER = OpenSSL::SSL::VERIFY_NONE
+
+class HauntedHouse
+ def initialize(address, user, password)
+ @address = address
+ @user = user
+ @password = password
+ @devices = [ ]
+
+ lookup_devices
+ end
+
+ def request(url)
+ open( "#{@address}/rest/#{url}",
+ :http_basic_authentication => [@user, @password] ) do |data|
+ yield data if block_given?
+ end
+ end
+
+ def device(name)
+ @devices.find { |device| device.name == name }
+ end
+
+ private
+
+ def lookup_devices
+ request("nodes") do |data|
+ xml = REXML::Document.new(data)
+ xml.elements.each("nodes/node") do |node|
+ if (name = node.elements["name"].text rescue nil) and
+ (address = node.elements["address"].text rescue nil)
+ @devices << Device.new(self, name, address)
+ end
+ end
+ end
+ end
+end
@@ -0,0 +1,25 @@
+require "erb"
+
+class Device
+ def initialize(service, name, address)
+ @service = service
+ @name = name
+ @address = address
+ end
+
+ attr_reader :name
+
+ def fast_on
+ send_command("DFON")
+ end
+
+ def fast_off
+ send_command("DFOF")
+ end
+
+ private
+
+ def send_command(command)
+ @service.request("nodes/#{ERB::Util.u @address}/cmd/#{command}")
+ end
+end

0 comments on commit 8fb5fb9

Please sign in to comment.