Module: Huebot::CLI::Runner

Defined in:
lib/huebot/cli/runner.rb

Class Method Summary collapse

Class Method Details

.check(sources, lights, groups, opts) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/huebot/cli/runner.rb', line 30

def self.check(sources, lights, groups, opts)
  programs = sources.map { |src|
    Huebot::Compiler.build src
  }

  # Assume all devices and inputs are correct
  if opts.no_device_check
    light_input_names = opts.inputs.select { |i| i.is_a? Light::Input }.map(&:val)
    lights = programs.reduce(light_input_names) { |acc, p| acc + p.light_names }.uniq.each_with_index.map { |name, i| Light.new(nil, i+1, {"name" => name}) }

    group_input_names = opts.inputs.select { |i| i.is_a? Group::Input }.map(&:val)
    groups = programs.reduce(group_input_names) { |acc, p| acc + p.group_names }.uniq.each_with_index.map { |name, i| Group.new(nil, i+1, {"name" => name}) }
  end

  device_mapper = Huebot::DeviceMapper.new(lights: lights, groups: groups, inputs: opts.inputs)
  found_errors, found_warnings, missing_devices = Helpers.check! programs, device_mapper, opts.stderr
  return (found_errors || found_warnings || missing_devices) ? 1 : 0
rescue ::Huebot::Error => e
  opts.stderr.puts "#{e.class.name}: #{e.message}"
  return 1
end

.clear_ip(config, opts) ⇒ Object



69
70
71
72
73
74
75
# File 'lib/huebot/cli/runner.rb', line 69

def self.clear_ip(config, opts)
  config["ip"] = nil
  0
rescue ::Huebot::Error => e
  opts.stderr.puts "#{e.class.name}: #{e.message}"
  return 1
end

.get_state(lights, groups, opts) ⇒ Object



52
53
54
55
56
57
58
59
# File 'lib/huebot/cli/runner.rb', line 52

def self.get_state(lights, groups, opts)
  device_mapper = Huebot::DeviceMapper.new(lights: lights, groups: groups, inputs: opts.inputs)
  device_mapper.each do |device|
    opts.stdout.puts device.name
    opts.stdout.puts "  #{device.get_state}"
  end
  0
end

.ls(lights, groups, opts) ⇒ Object



4
5
6
7
8
9
10
11
# File 'lib/huebot/cli/runner.rb', line 4

def self.ls(lights, groups, opts)
  opts.stdout.puts "Lights\n" + lights.map { |l| "  #{l.id}: #{l.name}" }.join("\n") + \
    "\nGroups\n" + groups.map { |g| "  #{g.id}: #{g.name}" }.join("\n")
  return 0
rescue ::Huebot::Error => e
  opts.stderr.puts "#{e.class.name}: #{e.message}"
  return 1
end

.run(sources, lights, groups, opts) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/huebot/cli/runner.rb', line 13

def self.run(sources, lights, groups, opts)
  programs = sources.map { |src|
    Huebot::Compiler.build src
  }
  device_mapper = Huebot::DeviceMapper.new(lights: lights, groups: groups, inputs: opts.inputs)
  found_errors, _found_warnings, missing_devices = Helpers.check! programs, device_mapper, opts.stderr
  return 1 if found_errors || missing_devices

  logger = opts.debug ? Logging::IOLogger.new(opts.stdout) : nil
  bot = Huebot::Bot.new(device_mapper, logger: logger, waiter: opts.bot_waiter)
  programs.each { |prog| bot.execute prog }
  return 0
rescue ::Huebot::Error => e
  opts.stderr.puts "#{e.class.name}: #{e.message}"
  return 1
end

.set_ip(config, ip, opts) ⇒ Object



61
62
63
64
65
66
67
# File 'lib/huebot/cli/runner.rb', line 61

def self.set_ip(config, ip, opts)
  config["ip"] = ip
  0
rescue ::Huebot::Error => e
  opts.stderr.puts "#{e.class.name}: #{e.message}"
  return 1
end

.unregister(config, opts) ⇒ Object



77
78
79
80
81
82
83
# File 'lib/huebot/cli/runner.rb', line 77

def self.unregister(config, opts)
  config.clear
  0
rescue ::Huebot::Error => e
  opts.stderr.puts "#{e.class.name}: #{e.message}"
  return 1
end