Created
May 24, 2012 23:41
-
-
Save eggie5/2784901 to your computer and use it in GitHub Desktop.
event machine loop to listen for insteon messages
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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