Class: EventMachine::MeshRuby
- Inherits:
-
Object
- Object
- EventMachine::MeshRuby
- Defined in:
- lib/meshruby.rb
Overview
Communicates with MeshBlu(tm) via websocket connection.
Instance Attribute Summary collapse
-
#queue ⇒ Object
Returns the value of attribute queue.
-
#socket ⇒ Object
Returns the value of attribute socket.
-
#token ⇒ Object
Returns the value of attribute token.
-
#url ⇒ Object
Returns the value of attribute url.
-
#uuid ⇒ Object
Returns the value of attribute uuid.
Instance Method Summary collapse
- #connect ⇒ Object
-
#create_socket_events ⇒ Object
Bootstraps all the events for MeshBlu in the correct order.
- #credentials ⇒ Object
- #data(telemetry) ⇒ Object
-
#emit(devices, message_hash) ⇒ Object
TODO: Rename to ‘message’, since this is not the only thing we’re emitting.
-
#initialize(uuid, token, url = 'wss://meshblu.octoblu.com:443') ⇒ MeshRuby
constructor
A new instance of MeshRuby.
- #onmessage(&blk) ⇒ Object
- #pop ⇒ Object
-
#push(message) ⇒ Object
Sanitize messages, making a best effort attempt to hashify them, otherwise returns message as-is.
- #toggle_debug! ⇒ Object
Constructor Details
#initialize(uuid, token, url = 'wss://meshblu.octoblu.com:443') ⇒ MeshRuby
Returns a new instance of MeshRuby.
10 11 12 13 |
# File 'lib/meshruby.rb', line 10 def initialize(uuid, token, url = 'wss://meshblu.octoblu.com:443') @uuid, @token, @url, @queue = uuid, token, url, EventMachine::Queue.new @socket = SocketIO::Client::Simple::Client.new url end |
Instance Attribute Details
#queue ⇒ Object
Returns the value of attribute queue.
8 9 10 |
# File 'lib/meshruby.rb', line 8 def queue @queue end |
#socket ⇒ Object
Returns the value of attribute socket.
8 9 10 |
# File 'lib/meshruby.rb', line 8 def socket @socket end |
#token ⇒ Object
Returns the value of attribute token.
8 9 10 |
# File 'lib/meshruby.rb', line 8 def token @token end |
#url ⇒ Object
Returns the value of attribute url.
8 9 10 |
# File 'lib/meshruby.rb', line 8 def url @url end |
#uuid ⇒ Object
Returns the value of attribute uuid.
8 9 10 |
# File 'lib/meshruby.rb', line 8 def uuid @uuid end |
Instance Method Details
#connect ⇒ Object
15 16 17 18 19 |
# File 'lib/meshruby.rb', line 15 def connect socket.connect create_socket_events self end |
#create_socket_events ⇒ Object
Bootstraps all the events for MeshBlu in the correct order.
22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/meshruby.rb', line 22 def create_socket_events #OTHER EVENTS: :identify, :identity, :ready, :disconnect, :message this = self; socket = this.socket # socket.io-client bugs? socket.on :connect do socket.on :identify do |data| auth = {uuid: this.uuid, token: this.token} emit :identity, auth end socket.on(:message) { |msg| this.push(msg); this.pop } end end |
#credentials ⇒ Object
51 52 53 |
# File 'lib/meshruby.rb', line 51 def credentials @credentials ||= {uuid: @uuid, token: @token} end |
#data(telemetry) ⇒ Object
55 56 57 58 59 60 61 |
# File 'lib/meshruby.rb', line 55 def data(telemetry) case telemetry when Hash then nil # do stuff else telemetry = {data: telemetry} end socket.emit("data", telemetry.merge!(credentials)) end |
#emit(devices, message_hash) ⇒ Object
TODO: Rename to ‘message’, since this is not the only thing we’re emitting
47 48 49 |
# File 'lib/meshruby.rb', line 47 def emit(devices, ) socket.emit("message", .merge(devices: devices)) end |
#onmessage(&blk) ⇒ Object
63 64 65 |
# File 'lib/meshruby.rb', line 63 def (&blk) @onmessage = blk end |
#pop ⇒ Object
42 43 44 |
# File 'lib/meshruby.rb', line 42 def pop @queue.pop(&@onmessage) if @onmessage end |
#push(message) ⇒ Object
Sanitize messages, making a best effort attempt to hashify them, otherwise returns message as-is.
36 37 38 39 40 |
# File 'lib/meshruby.rb', line 36 def push() @queue.push .is_a?(String) ? JSON.parse() : rescue JSON::ParserError @queue.push end |
#toggle_debug! ⇒ Object
67 68 69 70 71 72 73 |
# File 'lib/meshruby.rb', line 67 def toggle_debug! if @debugger socket.remove_listener(@debugger) else @debugger = socket.on(:*) { |a, b| puts "#{a} #{b}" } end end |