Class: Smartware::CommandBasedDevice

Inherits:
EventMachine::Connection
  • Object
show all
Defined in:
lib/smartware/drivers/common/command_based_device.rb

Direct Known Subclasses

CCNETConnection, SZZTConnection, SankyoConnection

Instance Method Summary collapse

Constructor Details

#initializeCommandBasedDevice

Returns a new instance of CommandBasedDevice.



3
4
5
6
7
8
9
10
11
12
13
# File 'lib/smartware/drivers/common/command_based_device.rb', line 3

def initialize
  @ready_for_commands = true
  @executing_command = false
  @retries = nil
  @command = nil
  @completion_proc = nil
  @buffer = ""

  @command_queue = []
  @command_mutex = Mutex.new
end

Instance Method Details

#command(*args, &block) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/smartware/drivers/common/command_based_device.rb', line 21

def command(*args, &block)
  if !block_given?
    queue = Queue.new
    command(*args) do |resp|
      queue.push resp
    end
    queue.pop
  else
    @command_mutex.synchronize do
      @command_queue.push [ args, block ]
    end

    EventMachine.schedule method(:post_command)
  end
end

#receive_data(data) ⇒ Object



15
16
17
18
19
# File 'lib/smartware/drivers/common/command_based_device.rb', line 15

def receive_data(data)
  @buffer << data

  handle_response
end