improvements to flux setup for hue lights

This commit is contained in:
Sami Samhuri 2015-02-06 15:55:09 -08:00
parent 6ee96f9e77
commit 06a5d8059a
4 changed files with 85 additions and 28 deletions

71
flux
View file

@ -2,32 +2,65 @@
require 'date'
require 'json'
require 'optparse'
TIMES = JSON.parse(File.read(File.expand_path('../sun-yyj.json', __FILE__)))['sunsets']
SETTINGS = {
'twilight' => 'sunrise',
'sunrise' => 'morning',
'sunset' => 'sunset',
'night' => 'night',
}
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]
puts "sunrise: #{times['sunrise']}"
puts "morning: #{times['morning']}"
puts "sunset: #{times['sunset']}"
puts "night: #{times['night']}"
date = Date.today
month = date.month.to_s
day = date.day.to_s
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"
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
raise "Unsure how to change lights for \"#{name}\""
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
else
raise "Cannot find today's date (#{date}) in times: #{times.inspect}"
end
main if $0 == __FILE__

24
flux-clouds Executable file
View 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
View file

@ -9,7 +9,7 @@ TEMPS = {
temp: 4200,
},
'cloudy' => {
brightness: 200,
brightness: 220,
temp: 3500,
},
'sunrise' => {

File diff suppressed because one or more lines are too long