Class: VisionMate::Telnet

Inherits:
Object
  • Object
show all
Defined in:
lib/vision_mate/telnet.rb

Defined Under Namespace

Classes: BadHostNameOrPort, CouldNotConnect, TubeReadError

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(telnet_connection) ⇒ Telnet

Returns a new instance of Telnet.



40
41
42
43
# File 'lib/vision_mate/telnet.rb', line 40

def initialize(telnet_connection)
  self.telnet_connection = telnet_connection
  set_scanner_to_manual
end

Instance Attribute Details

#telnet_connectionObject

Returns the value of attribute telnet_connection.



5
6
7
# File 'lib/vision_mate/telnet.rb', line 5

def telnet_connection
  @telnet_connection
end

Class Method Details

.connect(host, port, telnet_class = Net::Telnet) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
# File 'lib/vision_mate/telnet.rb', line 14

def self.connect(host, port, telnet_class = Net::Telnet)
  if host.to_s.empty? || port.to_s.empty?
    raise BadHostNameOrPort, "No host or port specified for Telnet connection"
  end
  telnet_connection = verified_connection host, port, telnet_class
  new(telnet_connection)
rescue Net::OpenTimeout, Timeout::Error
  raise CouldNotConnect, "Failed to connect to #{host}:#{port}"
rescue SocketError
  raise BadHostNameOrPort, "Malformed host name or port"
end

.verified_connection(host, port, telnet_class) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/vision_mate/telnet.rb', line 26

def self.verified_connection(host, port, telnet_class)
  telnet_connection = telnet_class.new(
    "Host" => host, "Port" => port, "Timeout" => Configuration.timeout
  )
  telnet_connection.cmd("String" => "L", "Match" => /OK/)

  telnet_connection
rescue Net::OpenTimeout, Timeout::Error => e
  retries ||= 0
  raise e if retries == 5
  retries += 1
  retry
end

Instance Method Details

#closeObject



57
58
59
# File 'lib/vision_mate/telnet.rb', line 57

def close
  telnet_connection.close
end

#initiate_scanObject



52
53
54
55
# File 'lib/vision_mate/telnet.rb', line 52

def initiate_scan
  telnet_command("S")
  wait_for_data
end

#scanObject

Raises:



45
46
47
48
49
50
# File 'lib/vision_mate/telnet.rb', line 45

def scan
  initiate_scan
  result = retrieve_data
  raise TubeReadError, "One or more tubes not read" if result[/No Read/]
  strip_prefix(result)
end