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
# File 'lib/meshruby.rb', line 22

def create_socket_events
  #OTHER EVENTS: :identify, :identity, :ready, :disconnect, :message
  this = self
  socket.on :identify do |data|
    auth = {uuid: this.uuid, token: this.token, socketid: data['socketid']}
    emit :identity, auth
  end
  socket.on(:message) { |msg| this.push(msg); this.pop }
end

#credentialsObject



49
50
51
# File 'lib/meshruby.rb', line 49

def credentials
  @credentials ||= {uuid: @uuid, token: @token}
end

#data(telemetry) ⇒ Object



53
54
55
56
57
58
59
# File 'lib/meshruby.rb', line 53

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



45
46
47
# File 'lib/meshruby.rb', line 45

def emit(devices, message_hash)
  socket.emit("message", devices: devices, message: message_hash)
end

#onmessage(&blk) ⇒ Object



61
62
63
# File 'lib/meshruby.rb', line 61

def onmessage(&blk)
  @onmessage = blk
end

#popObject



40
41
42
# File 'lib/meshruby.rb', line 40

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.



34
35
36
37
38
# File 'lib/meshruby.rb', line 34

def push(message)
  @queue.push message.is_a?(String) ? JSON.parse(message) : message
rescue JSON::ParserError
  @queue.push message
end

#toggle_debug!Object



65
66
67
68
69
70
71
# File 'lib/meshruby.rb', line 65

def toggle_debug!
  if @debugger
    socket.remove_listener(@debugger)
  else
    @debugger = socket.on(:*) { |a, b| puts "#{a} #{b}" }
  end
end