Class: EventMachine::MeshRuby

Inherits:
Object
  • Object
show all
Defined in:
lib/meshruby.rb

Overview

Communicates with MeshBlu(tm) via websocket connection.

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#queueObject

Returns the value of attribute queue.



8
9
10
# File 'lib/meshruby.rb', line 8

def queue
  @queue
end

#socketObject

Returns the value of attribute socket.



8
9
10
# File 'lib/meshruby.rb', line 8

def socket
  @socket
end

#tokenObject

Returns the value of attribute token.



8
9
10
# File 'lib/meshruby.rb', line 8

def token
  @token
end

#urlObject

Returns the value of attribute url.



8
9
10
# File 'lib/meshruby.rb', line 8

def url
  @url
end

#uuidObject

Returns the value of attribute uuid.



8
9
10
# File 'lib/meshruby.rb', line 8

def uuid
  @uuid
end

Instance Method Details

#connectObject



15
16
17
18
19
# File 'lib/meshruby.rb', line 15

def connect
  socket.connect
  create_socket_events
  self
end

#create_socket_eventsObject

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

#credentialsObject



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, message_hash)
  socket.emit("message", message_hash.merge(devices: devices))
end

#onmessage(&blk) ⇒ Object



63
64
65
# File 'lib/meshruby.rb', line 63

def onmessage(&blk)
  @onmessage = blk
end

#popObject



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(message)
  @queue.push message.is_a?(String) ? JSON.parse(message) : message
rescue JSON::ParserError
  @queue.push message
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