Class: StartDebugSessionInteractor

Inherits:
Object
  • Object
show all
Includes:
ADB, Log
Defined in:
lib/stf/interactor/start_debug_session_interactor.rb

Instance Method Summary collapse

Methods included from Log

#logger, verbose

Constructor Details

#initialize(stf) ⇒ StartDebugSessionInteractor

Returns a new instance of StartDebugSessionInteractor.



12
13
14
# File 'lib/stf/interactor/start_debug_session_interactor.rb', line 12

def initialize(stf)
  @stf = stf
end

Instance Method Details

#_execute_adb_with(timeout, cmd) ⇒ Object



105
106
107
# File 'lib/stf/interactor/start_debug_session_interactor.rb', line 105

def _execute_adb_with(timeout, cmd)
  execute_adb_with timeout, cmd
end

#connect(wanted, all_flag, filter, auto_adb_connect) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/stf/interactor/start_debug_session_interactor.rb', line 28

def connect(wanted, all_flag, filter, auto_adb_connect)
  devices = @stf.get_devices
  if devices.nil? || (devices.is_a?(Array) && devices.empty?)
    logger.info 'No devices connected to STF'
    return 0
  end

  usable_devices = devices
                   .map { |d| Device.new(d) }
                   .select do |d|
    d.ready == true && d.present == true && d.using == false
  end

  if usable_devices.empty?
    logger.error 'All devices are being used'
    return 0
  end

  unless filter.nil?
    key, value = filter.split(':', 2)

    usable_devices = usable_devices.select do |d|
      d.getValue(key) == value
    end
  end

  if usable_devices.empty?
    logger.error 'There is no device with criteria ' + filter
    return 0
  end

  n = 0
  usable_devices.shuffle.each do |d|
    n += 1 if connect_device(d, auto_adb_connect)
    break if !all_flag && n >= wanted
  end

  n
end

#connect_device(device, auto_adb_connect) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/stf/interactor/start_debug_session_interactor.rb', line 68

def connect_device(device, auto_adb_connect)
  return false if device.nil?

  serial = device.serial
  success = @stf.add_device serial
  if success
    logger.info "Device #{serial} added"
  elsif logger.error "Can't add device #{serial}"
    return false
  end

  result = @stf.start_debug serial
  unless result.success
    logger.error "Can't start debugging session for device #{serial}"
    @stf.remove_device serial
    return false
  end

  logger.info "remoteConnectUrl: #{result.remoteConnectUrl}"

  if auto_adb_connect
    _execute_adb_with 30, "connect #{result.remoteConnectUrl}"
  end

  result = @stf.start_debug serial
  unless result.success
    logger.error "Can't start debugging session for device #{serial}"
    @stf.remove_device serial
  end

  return result.success

rescue Net::HTTPFatalError
  logger.error 'Failed to start debug session'
  return false
end

#execute(wanted, all_flag, filter, auto_adb_connect) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
# File 'lib/stf/interactor/start_debug_session_interactor.rb', line 16

def execute(wanted, all_flag, filter, auto_adb_connect)
  wanted = 1 if wanted.nil?
  wanted = wanted.to_i

  1..10.times do
    wanted -= connect(wanted, all_flag, filter, auto_adb_connect)
    return if all_flag || wanted <= 0
    logger.info 'We are still waiting for ' + wanted.to_s + ' device(s). Retrying'
    sleep 5
  end
end