Module: CMusBt::App

Defined in:
lib/cmus-bt/main.rb

Class Method Summary collapse

Class Method Details

.availablesObject



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/cmus-bt/main.rb', line 15

def availables
  ret = []
  a   = Device::Bluetooth.list
  b   = Device::Input.list

  a.each { |ai|
    b.each {|bi|
      if bi[:name] == ai[:address]
        ret << {
          :address  => ai[:address],
          :name     => ai[:name],
          :dev_file => bi[:dev_file],
        }
      end
    }
  }

  return ret
end

.list_deviceObject



36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/cmus-bt/main.rb', line 36

def list_device
  list = availables()
 
  if list.empty?
    print("Available device is not exist.\n")

  else
    list.each { |info| 
      print(" %17s    %s\n" % [info[:address], info[:name]])
    }
  end
end

.run_app(targ) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/cmus-bt/main.rb', line 51

def run_app(targ)
  $logger.info("start #{APP_NAME} version #{CMusBt::VERSION}")
  list = availables()
  if list.empty?
    raise("available device is not exist")
  end

  if targ
    info = list.find {|inf| inf[:address] == targ || inf[:name] == targ}
    raise("specified device is not connected") if not info
  else
    info = list.first
  end

  thread = Thread.fork {Player.daemon(info)}
  status = system("cmus")
  raise("execute cmus failed (exit status #{$?}.") if not status

ensure
  thread&.raise
  thread&.join
  $logger.info("exit #{APP_NAME}")
end

.start(info) ⇒ Object



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/cmus-bt/main.rb', line 76

def start(info)
  case $mode
  when :list_device
    list_device

  when :run_app
    run_app(info)

  else
    raise("Not implement yet")
  end

rescue Exception => e
  STDERR.print("#{e.message}\n")
  exit(1)
end