Skip to content

Instantly share code, notes, and snippets.

@eggie5
Created May 24, 2012 23:41
Show Gist options
  • Select an option

  • Save eggie5/2784901 to your computer and use it in GitHub Desktop.

Select an option

Save eggie5/2784901 to your computer and use it in GitHub Desktop.
event machine loop to listen for insteon messages
require 'eventmachine'
module InsteonNetwork
class Client
attr_accessor :connection
def connect
@connection = EM.connect "192.168.1.102", 9761, Listener, self
end
def do_callback(data)
on_message_received_proc = self.on_message_received
on_message_received_proc.call(data)
end
def on_message_received(&block)
if block_given?
@on_message_received = block
self
else
@on_message_received
end
end
end
class Listener < EM::Connection
attr_accessor :client
def initialize(client_ref)
@client=client_ref
end
def post_init
@client.do_callback "somebody connected"
end
def receive_data(data)
@client.do_callback data
end
def unbind
@client.do_callback "somebody disconnected"
end
end
end
client=InsteonNetwork::Client.new
#event handler for when insteon device broadcasts
client.on_message_received do |message|
puts message
end
EventMachine::run do
client.connect
end
p "listening..."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment