Class: LegoEv3::BaseConnection

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

Direct Known Subclasses

LocalConnection, SSHConnection, TCPConnection

Instance Method Summary collapse

Constructor Details

#initializeBaseConnection

Returns a new instance of BaseConnection.



3
4
5
# File 'lib/connection/base.rb', line 3

def initialize
  @to_send = []
end

Instance Method Details

#flush(summary = nil) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/connection/base.rb', line 11

def flush(summary = nil)
  @connection ||= create_connection

  commands = @to_send.map{ |(c, _)| c }
  callbacks = @to_send.map{ |(_, c)| c }

  responses, time = LegoEv3::with_timer do
    call_connection(commands)
  end

  summary ||= commands
    .map{ |c| "[#{c.join(' ')}]" }
    .join(' ')

  puts "#{summary}. #{time} ms."

  callbacks.each_with_index.each do |c, i|
    c.call(responses[i]) if c
  end

  @to_send.clear
end

#send(verb, path, value = nil, handle = 'r+', &callback) ⇒ Object



7
8
9
# File 'lib/connection/base.rb', line 7

def send(verb, path, value = nil, handle = 'r+', &callback)
  @to_send << [[verb, path, value, handle], callback]
end