Class: UsbDetectionSensor::CommandLineHandler
- Inherits:
-
Object
- Object
- UsbDetectionSensor::CommandLineHandler
- Includes:
- OAttr
- Defined in:
- lib/usb-detection-sensor/command-line-handler.rb
Instance Method Summary collapse
-
#add ⇒ Object
‘add’ gets called as udev rules action.
- #dump_defaults ⇒ Object
- #dump_udev_rules ⇒ Object
- #ginfo ⇒ Object
-
#gom ⇒ Object
lazy load the GOM connection.
- #id_serial ⇒ Object
- #info ⇒ Object
-
#initialize(options = {}) ⇒ CommandLineHandler
constructor
configuration precedence: Defaults -> –config=file -> commandline.
- #process(env) ⇒ Object
-
#push_device_path ⇒ Object
def device_path @device_path ||= dev_config end write GOM specific device path for mobile: /areas/mobile/Generic-MD-I18.
-
#remove ⇒ Object
‘remove’ gets called as udev rules action.
- #remove_device_path_attr ⇒ Object
- #start_droid_exe ⇒ Object
- #stop_droid_exe ⇒ Object
- #switch_horizon_overlay(mode) ⇒ Object
- #wait_for_device ⇒ Object
Constructor Details
#initialize(options = {}) ⇒ CommandLineHandler
configuration precedence: Defaults -> –config=file -> commandline
9 10 11 12 13 |
# File 'lib/usb-detection-sensor/command-line-handler.rb', line 9 def initialize = {} defaults = (YAML.load_file defaults_path) = (YAML.load_file [:config]) rescue {} = defaults.merge(.merge ) end |
Instance Method Details
#add ⇒ Object
‘add’ gets called as udev rules action
31 32 33 34 35 36 37 38 |
# File 'lib/usb-detection-sensor/command-line-handler.rb', line 31 def add puts " -- connect: #{id_serial}" wait_for_device push_device_path # device_path start_droid_exe :grabber end |
#dump_defaults ⇒ Object
70 71 72 |
# File 'lib/usb-detection-sensor/command-line-handler.rb', line 70 def dump_defaults puts(File.read defaults_path) end |
#dump_udev_rules ⇒ Object
66 67 68 |
# File 'lib/usb-detection-sensor/command-line-handler.rb', line 66 def dump_udev_rules puts(File.read rules_path) end |
#ginfo ⇒ Object
26 27 28 |
# File 'lib/usb-detection-sensor/command-line-handler.rb', line 26 def ginfo puts(gom.read "#{@gom_path}.json") end |
#gom ⇒ Object
lazy load the GOM connection
75 76 77 78 79 |
# File 'lib/usb-detection-sensor/command-line-handler.rb', line 75 def gom return @gom unless @gom.nil? @gom, @gom_path = Gom::Remote::Connection.init gom_url @gom end |
#id_serial ⇒ Object
62 63 64 |
# File 'lib/usb-detection-sensor/command-line-handler.rb', line 62 def id_serial @id_serial ||= @env["ID_SERIAL"] end |
#info ⇒ Object
22 23 24 |
# File 'lib/usb-detection-sensor/command-line-handler.rb', line 22 def info puts " -- runtime env:\n#{@options.inspect}" end |
#process(env) ⇒ Object
15 16 17 18 19 20 |
# File 'lib/usb-detection-sensor/command-line-handler.rb', line 15 def process env @env = env #puts " -- ENV: #{@env.inspect}" op = @env["ACTION"].downcase self.send op end |
#push_device_path ⇒ Object
def device_path
@device_path ||= dev_config["device-path"]
end write GOM specific device path for mobile: /areas/mobile/Generic-MD-I18
89 90 91 92 93 94 95 96 |
# File 'lib/usb-detection-sensor/command-line-handler.rb', line 89 def push_device_path # device_path device_path ||= dev_config["device-path"] puts " .. push device-path: #{device_path}" # http://gom/sensors/usb-detect/home:device_path &>/dev/null (gom.write "#{@gom_path}:device_path", device_path) rescue => e puts " !! failed: #{e}" end |
#remove ⇒ Object
‘remove’ gets called as udev rules action
41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/usb-detection-sensor/command-line-handler.rb', line 41 def remove :off dev_type = @env["DEVTYPE"] unless dev_type == "usb_device" puts " .. ignoring remove for dev_type: #{dev_type}" return end puts " -- disconnect: #{id_serial}" stop_droid_exe remove_device_path_attr end |
#remove_device_path_attr ⇒ Object
123 124 125 126 |
# File 'lib/usb-detection-sensor/command-line-handler.rb', line 123 def remove_device_path_attr puts " .. remove device_path attribute" gom.destroy "#{@gom_path}:device_path" end |
#start_droid_exe ⇒ Object
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 |
# File 'lib/usb-detection-sensor/command-line-handler.rb', line 98 def start_droid_exe puts " .. start_droid_exe" # java -cp DroidEx.jar:ddmlib.jar com.commonsware.droidex.DroidEx clazz = "com.commonsware.droidex.DroidEx" cmd = "#{java_exe} #{classpath} #{clazz}" cmd = "DISPLAY=:0.0 #{cmd} -s #{scale} #{droid_log_redirect}" puts " executing: #{cmd}" pid = fork do sid = Process.setsid puts " session id: #{sid}" system cmd end puts " droid pid: #{pid}" File.open(droid_pid_file, "w") {|fd| fd.write pid; fd.flush} Process.detach pid end |
#stop_droid_exe ⇒ Object
115 116 117 118 119 120 121 |
# File 'lib/usb-detection-sensor/command-line-handler.rb', line 115 def stop_droid_exe puts " .. kill droid" pid = Integer(File.read droid_pid_file) Process.kill -15, pid rescue => e puts " !! stopping droid: #{e}" end |
#switch_horizon_overlay(mode) ⇒ Object
54 55 56 57 58 59 60 |
# File 'lib/usb-detection-sensor/command-line-handler.rb', line 54 def mode if .nil? puts " !! :overlay_path not defined" return end gom.write , mode.to_s end |
#wait_for_device ⇒ Object
81 82 83 |
# File 'lib/usb-detection-sensor/command-line-handler.rb', line 81 def wait_for_device puts = open("|#{adb} wait-for-device 2>&1").read end |