hive-runner-android 1.1.3 → 1.1.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 791d7c48d21d1e3627d48e26c5533b9322e84fa3
4
- data.tar.gz: 8d3b477541b6c21792b41bb9842df29826ebe24a
3
+ metadata.gz: 76719b13d1011b70fadf007b22fe7dd936319a47
4
+ data.tar.gz: 46f0e34cc3a6178c5d25ca9c5c63aed074fceb1c
5
5
  SHA512:
6
- metadata.gz: 7efa0377366f1eede0880a902d00a7f7ca0737bfbe5010c7ed723597ee3457584b10749bb0f1916b3a2e8d96c76a79ffc47fefaae012c2e62023816bde90ff97
7
- data.tar.gz: 756c0d99968a708b3e465327595b7dffc5e1ff270c6636061a2630b7eccab56ad4d0d1f6eb457832380c5d0442a8b9bb2d41007048d47863d1bcd5251bb8e33b
6
+ metadata.gz: 292ebf4003518d361c169fdfba25652b37c7a6b04f7bc53a98bd23e0ec317f5499add31c8cd1c1edbd0e6301627b7e97bf415e3121189c92d9538e287cececf4
7
+ data.tar.gz: 5b62ace7cb0c0a3bbb2f930f5db7e400ba56fed1c7cf3b014d3fdf019667dbe6f33ca5713b57ce97bf361ac106e10cc2f2706cd8ec8e7853b4611937461c417c
@@ -6,107 +6,105 @@ module Hive
6
6
  class Controller
7
7
  class Android < Controller
8
8
 
9
- # Uses either DeviceAPI or DeviceDB to generate queue names for a device
10
- def calculate_queue_names(device)
11
- if device.is_a? DeviceAPI::Android::Device
12
- queues = [
13
- device.model,
14
- device.manufacturer,
15
- 'android',
16
- "android-#{device.version}",
17
- "android-#{device.version}-#{device.model}"
18
- ]
19
- else
20
-
21
- queues = [
22
- device['device_model'],
23
- device['device_brand'],
24
- device['os'],
25
- "#{device['os']}-#{device['os_version']}",
26
- "#{device['os']}-#{device['os_version']}-#{device['device_model']}"
27
- ]
28
-
29
- queues << device["features"] unless device["features"].empty?
30
-
31
- queues.flatten
32
- end
33
- queues
9
+ def detect
10
+ self.detect_hive_mind
11
+ self.detect_devicedb
34
12
  end
35
13
 
36
- def populate_queues(device)
37
- queues = calculate_queue_names(device)
38
-
39
- # Add the queue prefix if it has been setup in the config
40
- queues = queues.map { |a| "#{@config['queue_prefix']}-#{a}"} if @config['queue_prefix']
41
-
42
- devicedb_queues = device['device_queues'].map { |d| d['name'] }
43
- # Check to see if the queues have already been registered with this device
44
- missing_queues = (queues - devicedb_queues) + (devicedb_queues - queues)
45
- return if missing_queues.empty?
46
-
47
- queues << missing_queues
48
-
49
- queue_ids = queues.flatten.uniq.map { |queue| find_or_create_queue(queue) }
14
+ # Register with Hive Mind (New)
15
+ def detect_hive_mind
16
+ devices = DeviceAPI::Android.devices
17
+ Hive.logger.debug('HM) No devices attached') if devices.empty?
50
18
 
51
- values = {
52
- name: device['name'],
53
- hive_id: device['hive_id'],
54
- feature_list: device['features'],
55
- device_queue_ids: queue_ids
56
- }
19
+ if not Hive.hive_mind.device_details.has_key? :error
20
+ # Selecting only android mobiles
21
+ connected_devices = Hive.hive_mind.device_details['connected_devices'].select{ |d| d['device_type'] == 'Mobile' && d['operating_system_name'] == 'android' }
57
22
 
58
- Hive.devicedb('Device').edit(device['id'], values)
59
- end
23
+ to_poll = []
24
+ attached_devices = []
25
+ connected_devices.each do |device|
26
+ Hive.logger.debug("HM) Device details: #{device.inspect}")
27
+ registered_device = devices.select { |a| a.serial == device['serial'] && a.status != :unauthorized }
28
+ if registered_device.empty?
29
+ # A previously registered device isn't attached
30
+ Hive.logger.debug("HM) Removing previously registered device - #{device}")
31
+ Hive.hive_mind.disconnect(device['id'])
32
+ else
33
+ # A previously registered device is attached, poll it
34
+ Hive.logger.debug("HM) Setting #{device} to be polled")
35
+ Hive.logger.info("HM) Stuff: #{registered_device.inspect}")
36
+ begin
37
+ attached_devices << self.create_device(device.merge('os_version' => registered_device[0].version))
38
+ to_poll << device['id']
39
+ rescue DeviceAPI::DeviceNotFound => e
40
+ Hive.logger.warn("HM) Device disconnected before registration (serial: #{device.serial})")
41
+ rescue => e
42
+ Hive.logger.warn("HM) Error with connected device: #{e.message}")
43
+ end
60
44
 
61
- def find_or_create_queue(name)
62
- queue = Hive.devicedb('Queue').find_by_name(name)
45
+ devices = devices - registered_device
46
+ end
47
+ end
63
48
 
64
- return queue.first['id'] unless queue.empty? || queue.is_a?(Hash)
49
+ # Poll already registered devices
50
+ Hive.logger.debug("HM) Polling: #{to_poll}")
51
+ Hive.hive_mind.poll(*to_poll)
52
+
53
+ # Register new devices
54
+ devices.select{|a| a.status != :unauthorized}.each do |device|
55
+ begin
56
+ dev = Hive.hive_mind.register(
57
+ hostname: device.model,
58
+ serial: device.serial,
59
+ macs: [device.wifi_mac_address],
60
+ ips: [device.ip_address],
61
+ brand: device.manufacturer.capitalize,
62
+ model: device.model,
63
+ device_type: 'Mobile',
64
+ imei: device.imei,
65
+ operating_system_name: 'android',
66
+ operating_system_version: device.version
67
+ )
68
+ Hive.hive_mind.connect(dev['id'])
69
+ Hive.logger.info("HM) Device registered: #{dev}")
70
+ rescue DeviceAPI::DeviceNotFound => e
71
+ Hive.logger.warn("HM) Device disconnected before registration (serial: #{device.serial})")
72
+ rescue => e
73
+ Hive.logger.warn("HM) Error with connected device: #{e.message}")
74
+ end
75
+ end
65
76
 
66
- queue = create_queue(name, "#{name} queue created by Hive Runner")
67
- queue['id'] unless queue.empty?
68
- end
77
+ else
78
+ Hive.logger.info('HM) No Hive Mind connection')
79
+ # Hive Mind isn't available, use DeviceAPI instead
80
+ device_info = devices.select { |a| a.status != :unauthorized }.map do |device|
81
+ {
82
+ 'id' => device.serial,
83
+ 'serial' => device.serial,
84
+ 'status' => 'idle',
85
+ 'model' => device.model,
86
+ 'brand' => device.manufacturer,
87
+ 'os_version' => device.version
88
+ }
89
+ end
69
90
 
70
- def create_queue(name, description)
71
- queue_attributes = {
72
- name: name,
73
- description: description
74
- }
91
+ attached_devices = device_info.collect do |physical_device|
92
+ self.create_device(physical_device)
93
+ end
94
+ end
75
95
 
76
- Hive.devicedb('Queue').register(device_queue: queue_attributes )
96
+ Hive.logger.info(attached_devices)
97
+ attached_devices
77
98
  end
78
99
 
79
- def detect
100
+ # Register with DeviceDB (Old)
101
+ def detect_devicedb
80
102
  devices = DeviceAPI::Android.devices
81
103
  Hive.logger.debug('No devices attached') if devices.empty?
82
104
  Hive.logger.debug("#{Time.now} Retrieving hive details")
83
105
  hive_details = Hive.devicedb('Hive').find(Hive.id)
84
106
  Hive.logger.debug("#{Time.now} Finished fetching hive details")
85
-
86
- devices.each do |device|
87
- begin
88
- Hive.hive_mind.register(
89
- hostname: device.model,
90
- serial: device.serial,
91
- macs: [device.wifi_mac_address],
92
- ips: [device.ip_address],
93
- brand: device.manufacturer.capitalize,
94
- model: device.model,
95
- device_type: 'Mobile',
96
- imei: device.imei,
97
- hive_id: Hive.id
98
- )
99
- rescue DeviceAPI::DeviceNotFound
100
- Hive.logger.info("Device '#{device.serial}' disconnected during registration")
101
- devices.delete(device)
102
- rescue DeviceAPI::UnauthorizedDevice
103
- Hive.logger.info("Device '#{device.serial}' is unauthorized")
104
- devices.delete(device)
105
- rescue => e
106
- Hive.logger.warn("Error with connected device: #{e.message}")
107
- devices.delete(device)
108
- end
109
- end
107
+ attached_devices = []
110
108
 
111
109
  if hive_details.key?('devices')
112
110
  # Update the 'cached' results from DeviceDB
@@ -130,10 +128,21 @@ module Hive
130
128
  Hive.devicedb('Device').poll(device['id'])
131
129
  Hive.logger.debug("#{Time.now} Finished polling device")
132
130
 
133
- # Make sure that this device has all the queues it should have
134
- populate_queues(device)
135
-
136
131
  devices = devices - registered_device
132
+ begin
133
+ attached_devices <<
134
+ self.create_device(device.merge(
135
+ 'os_version' => registered_device[0].version,
136
+ 'model' => device['device_model'],
137
+ 'brand' => device['device_brand'],
138
+ 'queues' => device['device_queues'].map{ |d| d['name'] },
139
+ 'queue_prefix' => @config['queue_prefix']
140
+ ))
141
+ rescue DeviceAPI::DeviceNotFound
142
+ Hive.logger.info("Device '#{device.serial}' disconnected during registration")
143
+ rescue => e
144
+ Hive.logger.warn("Error with connected device: #{e.message}")
145
+ end
137
146
  end
138
147
  end
139
148
 
@@ -142,20 +151,26 @@ module Hive
142
151
  end
143
152
 
144
153
  display_devices(hive_details)
145
-
146
- hive_details['devices'].select {|a| a['os'] == 'android'}.collect do |hive_device|
147
- self.create_device(hive_device)
148
- end
149
154
  else
150
155
  # DeviceDB isn't available, use DeviceAPI instead
151
- device_info = devices.map do |device|
152
- {'id' => device.serial, 'serial' => device.serial, status: 'idle', devices: [{ device_queues: [ calculate_queue_names(device).map { |q| { name: q } } ]}]}
156
+
157
+ device_info = devices.select { |a| a.status != :unauthorized }.map do |device|
158
+ {
159
+ 'id' => device.serial,
160
+ 'serial' => device.serial,
161
+ 'status' => 'idle',
162
+ 'model' => device.model,
163
+ 'brand' => device.manufacturer,
164
+ 'os_version' => device.version,
165
+ 'queue_prefix' => @config['queue_prefix']
166
+ }
153
167
  end
154
168
 
155
- device_info.collect do |physical_device|
169
+ attached_devices = device_info.collect do |physical_device|
156
170
  self.create_device(physical_device)
157
171
  end
158
172
  end
173
+ attached_devices
159
174
  end
160
175
 
161
176
  def register_new_device(device)
@@ -3,10 +3,56 @@ require 'hive/device'
3
3
  module Hive
4
4
  class Device
5
5
  class Android < Device
6
+ attr_accessor :model, :brand, :os_version
7
+
6
8
  def initialize(config)
7
9
  @identity = config['id']
10
+ @queue_prefix = config['queue_prefix'].to_s == '' ? '' : "#{config['queue_prefix']}-"
11
+ @model = config['model'].downcase.gsub(/\s/, '_')
12
+ @brand = config['brand'].downcase.gsub(/\s/, '_')
13
+ @os_version = config['os_version']
14
+
15
+ Hive.logger.info("Config: #{config.inspect}")
16
+ new_queues = calculate_queue_names
17
+ new_queues = new_queues | config['queues'] if config.has_key?('queues')
18
+
19
+ devicedb_ids = new_queues.map { |queue| find_or_create_queue(queue) }
20
+ Hive.devicedb('Device').edit(@identity, { device_queue_ids: devicedb_ids })
21
+ config['queues'] = new_queues
8
22
  super
9
23
  end
24
+
25
+ # Uses either DeviceAPI or DeviceDB to generate queue names for a device
26
+ def calculate_queue_names
27
+ Hive.logger.info("QUEUE PREFIX; #{@queue_prefix}")
28
+ [
29
+ "#{@queue_prefix}#{self.model}",
30
+ "#{@queue_prefix}#{self.brand}",
31
+ "#{@queue_prefix}android",
32
+ "#{@queue_prefix}android-#{self.os_version}",
33
+ "#{@queue_prefix}android-#{self.os_version}-#{self.model}"
34
+ ]
35
+ end
36
+
37
+ private
38
+
39
+ def find_or_create_queue(name)
40
+ queue = Hive.devicedb('Queue').find_by_name(name)
41
+
42
+ return queue.first['id'] unless queue.empty? || queue.is_a?(Hash)
43
+
44
+ queue = create_queue(name, "#{name} queue created by Hive Runner")
45
+ queue['id'] unless queue.empty?
46
+ end
47
+
48
+ def create_queue(name, description)
49
+ queue_attributes = {
50
+ name: name,
51
+ description: description
52
+ }
53
+
54
+ Hive.devicedb('Queue').register(device_queue: queue_attributes)
55
+ end
10
56
  end
11
57
  end
12
- end
58
+ end
@@ -82,6 +82,7 @@ module Hive
82
82
  end
83
83
 
84
84
  def device_status
85
+ # TODO Get from Hive Mind
85
86
  details = Hive.devicedb('Device').find(@options['id'])
86
87
  if details.key?('status')
87
88
  @state = details['status']
@@ -91,6 +92,7 @@ module Hive
91
92
  end
92
93
 
93
94
  def set_device_status(status)
95
+ # TODO Report to Hive Mind
94
96
  @state = status
95
97
  begin
96
98
  details = Hive.devicedb('Device').poll(@options['id'], status)
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: hive-runner-android
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.3
4
+ version: 1.1.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jon Wilson
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-02-09 00:00:00.000000000 Z
11
+ date: 2016-02-18 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: device_api-android
@@ -30,14 +30,14 @@ dependencies:
30
30
  requirements:
31
31
  - - "~>"
32
32
  - !ruby/object:Gem::Version
33
- version: '2.0'
33
+ version: 2.0.5
34
34
  type: :runtime
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
38
  - - "~>"
39
39
  - !ruby/object:Gem::Version
40
- version: '2.0'
40
+ version: 2.0.5
41
41
  - !ruby/object:Gem::Dependency
42
42
  name: terminal-table
43
43
  requirement: !ruby/object:Gem::Requirement
@@ -87,7 +87,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
87
87
  version: '0'
88
88
  requirements: []
89
89
  rubyforge_project:
90
- rubygems_version: 2.5.0
90
+ rubygems_version: 2.4.8
91
91
  signing_key:
92
92
  specification_version: 4
93
93
  summary: Hive Runner Android