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.



37
38
39
40
# File 'lib/vision_mate/telnet.rb', line 37

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
# File 'lib/vision_mate/telnet.rb', line 14

def self.connect(host, port, telnet_class = Net::Telnet)
  @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



23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/vision_mate/telnet.rb', line 23

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

#initiate_scanObject



49
50
51
52
# File 'lib/vision_mate/telnet.rb', line 49

def initiate_scan
  telnet_command("S")
  wait_for_data
end

#scanObject

Raises:



42
43
44
45
46
47
# File 'lib/vision_mate/telnet.rb', line 42

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