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
50
51
52
53
|
# File 'lib/lifx/console/identify.rb', line 14
def run(lights)
candidates = lights.to_a.shuffle
mapping = {}
if lights.respond_to?(:set_color)
lights.set_color(LIFX::Color.white, duration: 0)
else
lights.each { |l| l.set_color(LIFX::Color.white, duration: 0) }
end
while candidates.count > 1
puts "Searching through #{candidates.count} devices..."
partitions = partition(candidates, COLORS.values.count)
COLORS.keys.each_with_index do |color_name, index|
color = COLORS[color_name]
mapping[color_name] = partitions[index]
next if partitions[index].nil?
partitions[index].each do |l|
l.pulse(color, period: 10, duty_cycle: 1.0)
end
end
puts "What color did the light change to? (#{COLORS.keys.join(', ')}. Press enter to retry)"
resp = gets.strip
abbrev = Abbrev.abbrev(COLORS.keys)
if mapping.has_key?(abbrev[resp])
candidates = mapping[abbrev[resp]]
else
puts "Color not found. Pulsing again."
end
end
if candidates.count == 1
result = candidates.first
puts "Light identified: #{result}"
result.pulse(LIFX::Color.green, cycles: 3, period: 1)
return candidates.first
else
puts "No lights matched."
end
end
|