Class: GembirdBackend::Scanner

Inherits:
Object
  • Object
show all
Defined in:
lib/gembird-backend/scanner.rb

Instance Method Summary collapse

Instance Method Details

#scan(result) ⇒ Array

Returns tokens.

Parameters:

  • result (String)

Returns:

  • (Array)

    tokens



5
6
7
8
9
10
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
36
37
38
39
40
41
42
# File 'lib/gembird-backend/scanner.rb', line 5

def scan result
  tokens = []
  result.split("\n").each do |line|
    
    ### socket
    if matched = line.match(/^Accessing Gembird #(.+) USB device (.+)$/)
      device_number, usb_device = *matched.captures
      tokens << [:accessing_device, device_number, usb_device]
    elsif matched = line.match(/outlet.+?(\d+).+?(on|off)/)
      socket_number, status = *matched.captures
      tokens << [:socket_status, socket_number, status]
    
    ### device
    elsif matched = line.match(/^Gembird #(.+)$/)
      device_number = *matched.captures
      tokens << [:begin_device, device_number]
    elsif matched = line.match(/device type:\s+(.+)/)
      device_type = *matched.captures
      tokens << [:device_type, device_type]
    elsif matched = line.match(/serial number:\s+(.+)/)
      device_serial = *matched.captures
      tokens << [:device_serial, device_serial]
    elsif matched = line.match(/USB information:\s+bus (.+), device (.+)/)
      usb_bus, usb_device = *matched.captures
      tokens << [:usb_info, usb_device, usb_bus]
    
    ### error
    elsif line =~ /error/i || line =~ /Check USB connections/
      tokens << [:error, result]
    elsif line =~ /Invalid number or given device not found/ ||
          line =~ /No device with serial number/
      tokens << [:device_not_found]
    else
      # noop
    end
  end
  tokens
end