Class: UsbDetectionSensor::CommandLineHandler

Inherits:
Object
  • Object
show all
Includes:
OAttr
Defined in:
lib/usb-detection-sensor/command-line-handler.rb

Instance Method Summary collapse

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 options = {}
  defaults = (YAML.load_file defaults_path)
  @options = (YAML.load_file options[:config]) rescue {}
  @options = defaults.merge(@options.merge options)
end

Instance Method Details

#addObject

‘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
  switch_horizon_overlay :grabber
end

#dump_defaultsObject



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_rulesObject



66
67
68
# File 'lib/usb-detection-sensor/command-line-handler.rb', line 66

def dump_udev_rules
  puts(File.read rules_path)
end

#ginfoObject



26
27
28
# File 'lib/usb-detection-sensor/command-line-handler.rb', line 26

def ginfo 
  puts(gom.read "#{@gom_path}.json")
end

#gomObject

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_serialObject



62
63
64
# File 'lib/usb-detection-sensor/command-line-handler.rb', line 62

def id_serial
  @id_serial ||= @env["ID_SERIAL"] 
end

#infoObject



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_pathObject

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

#removeObject

‘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
  switch_horizon_overlay :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_attrObject



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_exeObject



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_exeObject



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 switch_horizon_overlay mode
  if overlay_path.nil?
    puts " !! :overlay_path not defined"
    return
  end
  gom.write overlay_path, mode.to_s
end

#wait_for_deviceObject



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