-
-
Save Epictetus/624bd154fb99b088598bfdbef4cc36a5 to your computer and use it in GitHub Desktop.
Ruby sunrise daemon
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--- | |
latitude: 40.725925 | |
longitude: -74.009399 | |
timezone: "America/New_York" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
source 'https://rubygems.org' | |
gem 'daemons' | |
gem 'RubySunrise' | |
gem 'rufus-scheduler' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'solareventcalculator' | |
require 'rufus/scheduler' | |
require 'optparse' | |
require 'yaml' | |
scheduler = Rufus::Scheduler.new | |
coordinates = YAML.load_file('coordinates.yml') | |
latitude_parameter = coordinates["latitude"].to_s | |
longitude_parameter = coordinates["longitude"].to_s | |
time_zone_parameter = coordinates["timezone"].to_s | |
def calculate(date,lat,long,timezone,type) | |
calucated = SolarEventCalculator.new(date, BigDecimal.new(lat), BigDecimal.new(long)) | |
localOfficialSunrise = calucated.compute_official_sunrise("#{timezone}") | |
localOfficialSunset = calucated.compute_official_sunset("#{timezone}") | |
if type == "sunrise" | |
return localOfficialSunrise | |
elsif type == "sunset" | |
return localOfficialSunset | |
end | |
end | |
def schedule_sunrise(sunrisedate,schedulername) | |
rightnow = DateTime.now | |
if sunrisedate < rightnow | |
puts "Sunrise already happened!" | |
else | |
puts "Scheduling sunrise for hour #{sunrisedate.hour} #{sunrisedate.min} #{sunrisedate.sec}" | |
schedulername.at sunrisedate, :tags => 'sunrise' do | |
puts 'The sunrise begins!' | |
end | |
end | |
end | |
def schedule_sunset(sunsetdate,schedulername) | |
rightnow = DateTime.now | |
if sunsetdate < rightnow | |
puts "Sunset already happened!" | |
else | |
puts "Scheduling sunset for hour #{sunsetdate.hour} #{sunsetdate.min} #{sunsetdate.sec}" | |
schedulername.at sunsetdate, :tags => 'sunset' do | |
puts 'The sunset has finished!' | |
end | |
end | |
end | |
loop do | |
puts "Now: #{DateTime.now}" | |
current_jobs_rise = scheduler.jobs(:tags => 'sunrise') | |
current_jobs_set = scheduler.jobs(:tags =>'sunset') | |
if current_jobs_rise.empty? | |
sunrise_calculated = calculate(Date.parse(Date.today.to_s),latitude_parameter,longitude_parameter,time_zone_parameter,"sunrise") | |
schedule_sunrise(sunrise_calculated,scheduler) | |
else | |
puts "Sunrise already scheduled!" | |
end | |
if current_jobs_set.empty? | |
sunsect_calcuated = calculate(Date.parse(Date.today.to_s),latitude_parameter,longitude_parameter,time_zone_parameter,"sunset") | |
schedule_sunset(sunsect_calcuated,scheduler) | |
else | |
puts "Sunset already scheduled!" | |
end | |
sleep(5) | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'daemons' | |
Daemons.run('sunrise.rb') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment