Skip to content

Commit

Permalink
improvements to flux setup for hue lights
Browse files Browse the repository at this point in the history
  • Loading branch information
samsonjs committed Feb 6, 2015
1 parent 6ee96f9 commit 06a5d80
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 27 deletions.
83 changes: 58 additions & 25 deletions flux
Original file line number Diff line number Diff line change
Expand Up @@ -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',
}

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"
else
raise "Unsure how to change lights for \"#{name}\""
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']}"

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
else
raise "Cannot find today's date (#{date}) in times: #{times.inspect}"
end

main if $0 == __FILE__
24 changes: 24 additions & 0 deletions flux-clouds
Original file line number Diff line number Diff line change
@@ -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 changes: 1 addition & 1 deletion lights
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ TEMPS = {
temp: 4200,
},
'cloudy' => {
brightness: 200,
brightness: 220,
temp: 3500,
},
'sunrise' => {
Expand Down
Loading

0 comments on commit 06a5d80

Please sign in to comment.