Class: Ban::CLI

Inherits:
Thor
  • Object
show all
Defined in:
lib/ban/cli.rb

Instance Method Summary collapse

Instance Method Details

#serverObject



11
12
13
14
15
16
17
18
19
20
21
22
23
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
# File 'lib/ban/cli.rb', line 11

def server
  device = options[:device]
  interface = options[:interface]
  port = options[:port]
  chroot = options[:chroot] || Dir.mktmpdir

  EM.epoll
  EM.threadpool_size = options[:em_threads]
  EM.run do
    board = Board.new
    server = Server.new(options[:user], options[:group], chroot)

    board.on :event do |event|
      if event.valid?
        Ban::Logger.debug "#{event.class}: #{event}"
        server.broadcast event
      end
    end

    server.on :command do |command|
      Ban::Logger.debug "Received cmd #{command}"
      if options = command['rc-turn-off']
        Ban::Logger.debug "Turn off #{options['address']}"
        board.turn_off(options['address'])
      elsif options = command['rc-turn-on']
        Ban::Logger.debug "Turn on #{options['address']}"
        board.turn_on(options['address'])
      end
    end
    
    trap(:INT) { board.close; EM.stop }
    trap(:TERM) { board.close; EM.stop }

    Ban::Logger.info "Connecting to #{device || 'auto'}"
    board.start(device)
    Ban::Logger.info "Starting WebSocket Server on #{interface}:#{port}"
    server.start(interface, port)
  end
end