Class: Hive::Controller::Android

Inherits:
Hive::Controller show all
Defined in:
lib/hive/controller/android.rb

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Android

Returns a new instance of Android.



9
10
11
12
13
# File 'lib/hive/controller/android.rb', line 9

def initialize(options)
  @device_type ||= 'Mobile'
  @remote = false if @remote.nil?
  super(options)
end

Instance Method Details

#detectObject

Register and poll connected devices



16
17
18
19
20
21
22
# File 'lib/hive/controller/android.rb', line 16

def detect
  if Hive.hive_mind.device_details.has_key? :error
    detect_without_hivemind
  else
    detect_with_hivemind
  end
end

#detect_with_hivemindObject



24
25
26
27
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
67
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
104
105
106
107
108
# File 'lib/hive/controller/android.rb', line 24

def detect_with_hivemind
  connected_devices = get_connected_devices
  Hive.logger.debug('No devices attached') if connected_devices.empty?

  # Selecting only android mobiles
  hivemind_devices = get_hivemind_devices

  to_poll = []
  attached_devices = []
  hivemind_devices.each do |device|
    Hive.logger.debug("Device details: #{device.inspect}")
    begin
      registered_device = connected_devices.select { |a| a.serial == device['serial'] }
    rescue => e
      registered_device = []
    end
    if registered_device.empty?
      # A previously registered device isn't attached
      Hive.logger.debug("A previously registered device has disappeared: #{device}")
    else
      # A previously registered device is attached, poll it

      Hive.logger.debug("Setting #{device['name']} to be polled")
      Hive.logger.debug("Device: #{registered_device.inspect}")
      begin
        Hive.logger.debug("#{device['name']} OS version: #{registered_device[0].version}")
        # Check OS version and update if necessary
        if device['operating_system_version'] != registered_device[0].version
          Hive.logger.info("Updating OS version of #{device['name']} from #{device['operating_system_version']} to #{registered_device[0].version}")
          Hive.hive_mind.register(
            id: device['id'],
            operating_system_name: 'android',
            operating_system_version: registered_device[0].version
          )
        end

        attached_devices << self.create_device(device.merge('os_version' => registered_device[0].version))
        to_poll << device['id']
      rescue DeviceAPI::DeviceNotFound => e
        Hive.logger.warn("Device disconnected before registration (serial: #{device['serial']})")
      rescue => e
        Hive.logger.warn("Error with connected device: #{e.message}")
      end

      connected_devices = connected_devices - registered_device
    end
  end

  # Poll already registered devices
  Hive.logger.debug("Polling: #{to_poll}")
  Hive.hive_mind.poll(*to_poll)

  # Register new devices
  if !connected_devices.empty?
    begin
     connected_devices.select{|a| a.status != :unauthorized && a.status != :no_permissions && a.status != :unknown && a.status != :offline}.each do |device|
      begin
       dev = Hive.hive_mind.register(
           hostname: device.model,
           serial: device.serial,
           macs: [device.wifi_mac_address],
           ips: [device.ip_address],
           brand: device.manufacturer.capitalize,
           model: device.model,
           device_type: @device_type,
           imei: device.imei,
           operating_system_name: 'android',
           operating_system_version: device.version
       )
       Hive.hive_mind.connect(dev['id'])
       Hive.logger.info("Device registered: #{dev}")
      rescue DeviceAPI::DeviceNotFound => e
       Hive.logger.warn("Device disconnected before registration #{e.message}")
      rescue => e
       Hive.logger.warn("Error with connected device: #{e.message}")
      end
     end
    rescue => e
     Hive.logger.debug("Connected Devices: #{connected_devices}")
     Hive.logger.warn(e)
    end 
  end
  Hive.logger.info(attached_devices)
  attached_devices
end

#detect_without_hivemindObject



110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/hive/controller/android.rb', line 110

def detect_without_hivemind
  connected_devices = get_connected_devices
  Hive.logger.debug('No devices attached') if connected_devices.empty?

  Hive.logger.info('No Hive Mind connection')
  Hive.logger.debug("Error: #{Hive.hive_mind.device_details[:error]}")
  # Hive Mind isn't available, use DeviceAPI instead
  begin
    device_info = connected_devices.map do |device|
      {
        'id' =>  device.serial,
        'serial' => device.serial,
        'status' => 'idle',
        'model' => device.model,
        'brand' => device.manufacturer,
        'os_version' => device.version
      }
    end

    attached_devices = device_info.collect do |physical_device|
      self.create_device(physical_device)
    end
  rescue DeviceAPI::DeviceNotFound => e
    Hive.logger.warn("Device disconnected while fetching device_info #{e.message}")
  rescue => e
    Hive.logger.warn(e)
  end

  Hive.logger.info(attached_devices)
  attached_devices
end

#display_devices(hive_details) ⇒ Object



167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
# File 'lib/hive/controller/android.rb', line 167

def display_devices(hive_details)
  rows = []
  if hive_details.key?('devices')
    unless hive_details['devices'].empty?
      rows = hive_details['devices'].map do |device|
        [
            "#{device['device_brand']} #{device['device_model']}",
            device['serial'],
            (device['device_queues'].map { |queue| queue['name']}).join("\n"),
            device['status']
        ]
      end
    end
  end
  table = Terminal::Table.new :headings => ['Device', 'Serial', 'Queue Name', 'Status'], :rows => rows

  Hive.logger.info(table)
end

#get_connected_devicesObject



142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
# File 'lib/hive/controller/android.rb', line 142

def get_connected_devices
  begin
   DeviceAPI::Android.devices.select do |a|
     a.status != :unauthorized &&
     a.status != :no_permissions &&
     a.status != :offline &&
     a.status != :unknown &&
     a.is_remote? == @remote
   end
  rescue DeviceAPI::DeviceNotFound => e
     Hive.logger.info("Device disconnected while getting list of devices")
  rescue => e
     Hive.logger.warn("Device has got some issue. Exception => #{e}. Debug and connect device manually")
  end
end

#get_hivemind_devicesObject



158
159
160
161
162
163
164
165
# File 'lib/hive/controller/android.rb', line 158

def get_hivemind_devices
  begin
    Hive.hive_mind.device_details['connected_devices'].select{ |d| d['device_type'] == @device_type && d['operating_system_name'] == 'android' }
  rescue NoMethodError
    # Failed to find connected devices
    raise Hive::Controller::DeviceDetectionFailed
  end
end