54. rhouse-app
module MyRhouse
class HouseRules < Rhouse::Worker
# Fetch the yaml configuration file
def configuration_file
@config_file_name ||= MyRhouse.confpath( quot;#{config_file_name}.ymlquot; )
end
# Handle event on queue. Parse event and create an associated event object
# for the rules engine to consume.
def handle_event( event )
evt = Rhouse::EventParser.parse( event )
event_type = MyRhouse::Events::EventTypeFactory.create_event( evt )
# fire up rule engine
engine.evaluate( event_type )
end
end
end
Friday, May 8, 2009
55. house-rules
module MyRhouse::Rules
class HouseRuleBook < Ruleby::Rulebook
def rules
# Deal with music
rule [MyRhouse::Events::Generic, :event, m.description =~ /.*XinesPlayer.*/] do |context|
cmd = case context[:event].command_id
when 12 : 'b' # Play Back Completed
when 58 : 'g' # Start playing
when 22 : 'r' # Stop playing
end
MyRhouse::Plugins::Arduino.new.blink_light( cmd )
logger.info quot;n>>> [MATCH] Found Xine Event #{context[:event].device.Description}nquot;
end
end
...
Friday, May 8, 2009
56. rhouse-ws
# Initializes Rhouse
configure do
Rhouse.initialize(
:environment => ENV['RH_ENV'] || 'development',
:requires_db => true,
:log_level => :info,
end
# -----------------------------------------------------------------------------
# send a command to the dce router via a post command
post '/cmd' do
Rhouse::Command.send_raw( keys_to_symbols( params ) )
end
Friday, May 8, 2009
57. ws-client
def send_cmd( command_hash )
params = command_hash.keys.map{ |k| quot;#{k}=#{command_hash[k]}quot; }.join(quot;")
result = rhouse_service.post( '/cmd',
:head => { quot;Content-typequot; => quot;application/x-www-form-urlencodedquot; },
:body => params )
unless result.http_status.index(quot;2quot;) == 0
text = result.http_reason
raise quot;Error querying Rhouse web service: #{text}quot;
end
result.http_body
rescue Errno::ECONNREFUSED
raise quot;Could not contact the Rhouse web servicequot;
end
Friday, May 8, 2009