Class: LegoEv3::BaseConnection

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

Direct Known Subclasses

LocalConnection, RemoteConnection

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

#flushObject



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

def flush
  @connection ||= create_connection

  joined_command = @to_send
    .map{ |(c, _)| c }
    .join(';')

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

  joined_response, time = LegoEv3::with_timer do
    call_connection(joined_command)
  end

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

  # We assume that one command output one line of result.
  responses = joined_response.split("\n")

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

  @to_send.clear
end

#send(command, &callback) ⇒ Object



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

def send(command, &callback)
  @to_send << [command, callback]
end