Class: CMUX::ModemChatter

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(io) ⇒ ModemChatter

Returns a new instance of ModemChatter.



5
6
7
8
9
10
11
12
13
14
15
16
17
# File 'lib/cmux/modem_chatter.rb', line 5

def initialize(io)
  @io = io
  @command_queue = []
  @buffer = ""
  @response = []
  @unprocessed_lines = []
  @have_running_command = true # because echo can be disabled before init string
  @unsolicited = Hash.new do |hash, key|
    hash[key] = Set.new
  end

  command "E1V1+CMEE=1", 2
end

Instance Attribute Details

#ioObject (readonly)

Returns the value of attribute io.



3
4
5
# File 'lib/cmux/modem_chatter.rb', line 3

def io
  @io
end

Class Method Details

.poll(devices, timeout_limit = nil) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/cmux/modem_chatter.rb', line 63

def self.poll(devices, timeout_limit = nil)
  timeouts = devices.map(&:desired_timeout)
  timeouts << timeout_limit
  timeouts.reject! &:nil?
  read, write, except = ::IO.select devices.map(&:io), [], [], timeouts.min

  unless read.nil?
    read.each do |io|
      devices.find { |dev| dev.io == io }.poll
    end
  end

  devices.each(&:run_periodic)
end

Instance Method Details

#command(command, timeout = nil, &block) ⇒ Object



19
20
21
22
23
24
25
26
27
# File 'lib/cmux/modem_chatter.rb', line 19

def command(command, timeout = nil, &block)
  submit_now = @command_queue.empty?
  cmd = ModemCommand.new(command, self, timeout, &block)
  @command_queue.push cmd

  submit if submit_now

  cmd
end

#desired_timeoutObject



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/cmux/modem_chatter.rb', line 38

def desired_timeout
  return nil if @command_queue.empty?

  command = @command_queue.first
  return nil if command.timeout.nil?

  now = DateTime.now.to_time

  remaining = command.issued_at + command.timeout - now
  if remaining < 0
    remaining = 0
  end

  remaining
end

#pollObject



78
79
80
81
82
83
84
# File 'lib/cmux/modem_chatter.rb', line 78

def poll
  begin
    @buffer += @io.read_nonblock 4096
    on_read
  rescue Errno::EINTR, ::IO::WaitReadable
  end
end

#run_periodicObject



54
55
56
57
58
59
60
61
# File 'lib/cmux/modem_chatter.rb', line 54

def run_periodic
  remaining = desired_timeout

  if remaining == 0
    @have_running_command = false
    complete_first "timeout"
  end
end

#submitObject



29
30
31
32
33
34
35
36
# File 'lib/cmux/modem_chatter.rb', line 29

def submit
  return if @command_queue.empty?

  cmd = @command_queue.first
  cmd.start

  @io.write "AT#{cmd.command}\r\n"
end

#subscribe(type, receiver) ⇒ Object



86
87
88
# File 'lib/cmux/modem_chatter.rb', line 86

def subscribe(type, receiver)
  @unsolicited[type].add receiver
end

#unsubscribe(type, receiver) ⇒ Object



90
91
92
# File 'lib/cmux/modem_chatter.rb', line 90

def unsubscribe(type, receiver)
  @unsolicited[type].delete receiver
end