class StartDebugSessionInteractor

Public Class Methods

new(stf) click to toggle source
# File lib/stf/interactor/start_debug_session_interactor.rb, line 12
def initialize(stf)
  @stf = stf
end

Public Instance Methods

_execute_adb_with(timeout, cmd) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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