improvements to flux setup for hue lights
This commit is contained in:
parent
6ee96f9e77
commit
06a5d8059a
4 changed files with 85 additions and 28 deletions
85
flux
85
flux
|
|
@ -2,32 +2,65 @@
|
||||||
|
|
||||||
require 'date'
|
require 'date'
|
||||||
require 'json'
|
require 'json'
|
||||||
|
require 'optparse'
|
||||||
|
|
||||||
TIMES = JSON.parse(File.read(File.expand_path('../sun-yyj.json', __FILE__)))['sunsets']
|
TIMES = JSON.parse(File.read(File.expand_path('../sun-yyj.json', __FILE__)))['times']
|
||||||
SETTINGS = {
|
USAGE_TEXT = "Usage: flux [options]"
|
||||||
'twilight' => 'sunrise',
|
|
||||||
'sunrise' => 'morning',
|
|
||||||
'sunset' => 'sunset',
|
|
||||||
'night' => 'night',
|
|
||||||
}
|
|
||||||
|
|
||||||
date = Date.today
|
def main
|
||||||
month = date.month.to_s
|
options = parse_options
|
||||||
day = date.day.to_s
|
flux(options)
|
||||||
if times = TIMES[month][day]
|
|
||||||
time = Time.now
|
|
||||||
hour, min = time.hour, time.min
|
|
||||||
padded_min = min < 10 ? "0#{min}" : "#{min}"
|
|
||||||
now = "#{hour}:#{padded_min}"
|
|
||||||
if found = times.detect { |k, v| now == v }
|
|
||||||
name = found[0]
|
|
||||||
if setting = SETTINGS[name]
|
|
||||||
puts "lights #{setting} - 3000"
|
|
||||||
exec "lights #{setting} - 3000"
|
|
||||||
else
|
|
||||||
raise "Unsure how to change lights for \"#{name}\""
|
|
||||||
end
|
|
||||||
end
|
|
||||||
else
|
|
||||||
raise "Cannot find today's date (#{date}) in times: #{times.inspect}"
|
|
||||||
end
|
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]
|
||||||
|
puts "sunrise: #{times['sunrise']}"
|
||||||
|
puts "morning: #{times['morning']}"
|
||||||
|
puts "sunset: #{times['sunset']}"
|
||||||
|
puts "night: #{times['night']}"
|
||||||
|
|
||||||
|
time = Time.now
|
||||||
|
hour, min = time.hour, time.min
|
||||||
|
padded_min = min < 10 ? "0#{min}" : "#{min}"
|
||||||
|
now = "#{hour}:#{padded_min}"
|
||||||
|
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__
|
||||||
|
|
|
||||||
24
flux-clouds
Executable file
24
flux-clouds
Executable file
|
|
@ -0,0 +1,24 @@
|
||||||
|
#!/usr/bin/env ruby
|
||||||
|
|
||||||
|
require 'forecast_io'
|
||||||
|
require 'json'
|
||||||
|
|
||||||
|
ForecastIO.api_key = JSON.parse(File.read(File.expand_path('~/Dropbox/Personal/forecastio.json', __FILE__)))['apikey']
|
||||||
|
|
||||||
|
LATITUDE = 48.456642
|
||||||
|
LONGITUDE = -123.370325
|
||||||
|
|
||||||
|
def main
|
||||||
|
if forecast = ForecastIO.forecast(LATITUDE, LONGITUDE)
|
||||||
|
cloud_cover = forecast.currently.cloudCover
|
||||||
|
puts "Cloud cover: #{cloud_cover}"
|
||||||
|
setting = cloud_cover > 0.6 ? 'cloudy' : 'sunny'
|
||||||
|
# File.open('/Users/sjs/flux-clouds.log', 'a') { |f| f.puts "Cloud cover: #{cloud_cover}"; f.puts "> lights #{setting} - 100" }
|
||||||
|
puts "> lights #{setting} - 100"
|
||||||
|
exec "lights #{setting} - 100"
|
||||||
|
else
|
||||||
|
raise "Unable to check forecast"
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
main if $0 == __FILE__
|
||||||
2
lights
2
lights
|
|
@ -9,7 +9,7 @@ TEMPS = {
|
||||||
temp: 4200,
|
temp: 4200,
|
||||||
},
|
},
|
||||||
'cloudy' => {
|
'cloudy' => {
|
||||||
brightness: 200,
|
brightness: 220,
|
||||||
temp: 3500,
|
temp: 3500,
|
||||||
},
|
},
|
||||||
'sunrise' => {
|
'sunrise' => {
|
||||||
|
|
|
||||||
File diff suppressed because one or more lines are too long
Loading…
Reference in a new issue