forked from LIFX/lifx-gem
-
Notifications
You must be signed in to change notification settings - Fork 0
/
auto-off.rb
35 lines (28 loc) · 807 Bytes
/
auto-off.rb
1
2
3
4
5
6
7
8
9
10
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
# Auto-off script
#
# This script will turn a light off after 10 seconds when it has detected it has turned on.
# To run: bundle; bundle ruby auto-off.rb [light label]
require 'bundler'
Bundler.require
AUTO_OFF_DELAY = 10
label = ARGV.first
lifx = LIFX::Client.lan
lifx.discover! do
label ? lifx.lights.with_label(label) : lifx.lights.first
end
light = label ? lifx.lights.with_label(label) : lifx.lights.first
puts "#{light} will be automatically turned off after #{AUTO_OFF_DELAY} seconds"
thr = Thread.new do
loop do
if light.on? && !(@off_thr && @off_thr.alive?)
puts "Light detected on. Turning off in #{AUTO_OFF_DELAY}"
@off_thr = Thread.new do
sleep AUTO_OFF_DELAY
light.turn_off
puts "Turning off"
end
end
sleep 1
end
end
thr.join