-
Notifications
You must be signed in to change notification settings - Fork 0
/
flux
executable file
·69 lines (61 loc) · 1.7 KB
/
flux
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#!/usr/bin/env ruby
require 'date'
require 'json'
require 'optparse'
TIMES = JSON.parse(File.read(File.expand_path('../sun-yyj.json', __FILE__)))['times']
USAGE_TEXT = "Usage: flux [options]"
def main
options = parse_options
flux(options)
end
def parse_options
options = {
dry_run: false,
set_current: false,
}
OptionParser.new do |opts|
opts.banner = USAGE_TEXT
opts.on("-n", "--dry-run", "Don't actually change the lights") do |dry_run|
options[:dry_run] = dry_run
end
opts.on("-c", "--set-current", "Change the lights to the setting that should currently be active") do |current|
options[:set_current] = current
end
end.parse!
options
end
def flux(options)
date = Date.today
month = date.month.to_s
day = date.day.to_s
if times = TIMES[month][day]
times['midnight'] = '22:30'
puts "sunrise: #{times['sunrise']}"
puts "morning: #{times['morning']}"
puts "sunset: #{times['sunset']}"
puts "night: #{times['night']}"
puts "midnight: #{times['midnight']}"
time = Time.now
hour, min = time.hour, time.min
padded_min = min < 10 ? "0#{min}" : "#{min}"
now = "#{hour}:#{padded_min}"
puts "current time: #{now}"
found =
if options[:set_current]
now = now.sub(':', '').to_i
if k = times.keys.select { |k| now >= times[k].sub(':', '').to_i }.last
[k, times[k]]
end
else
times.detect { |k, v| now == v }
end
if found
setting = found[0]
puts "> exec lights #{setting} - 300"
exec "lights #{setting} - 300" unless options[:dry_run]
end
else
raise "Cannot find today's date (#{date}) in times: #{TIMES.inspect}"
end
end
main if $0 == __FILE__