Add the dark_at condition
Use the EarthTools Webservices to determine when the sun sets and when it rises. Use these moment to determine when it's dark. (See also: http://www.earthtools.org/webservices.htm#usage)
This commit is contained in:
parent
1bc924a0a4
commit
bcb221cc51
|
@ -1,5 +1,8 @@
|
|||
# encoding: utf-8
|
||||
|
||||
require "nokogiri"
|
||||
require "net/http"
|
||||
|
||||
module Hued
|
||||
|
||||
# The rule class
|
||||
|
@ -17,6 +20,8 @@ module Hued
|
|||
@priority = entry["priority"] || 0
|
||||
@events = []
|
||||
|
||||
@sun_data = nil
|
||||
|
||||
if entry["events"]
|
||||
entry["events"].each do |ev_name|
|
||||
event = Huey::Event.find(ev_name)
|
||||
|
@ -117,6 +122,24 @@ module Hued
|
|||
when "weekday", "weekdays"
|
||||
weekdays = cond_value.split(/,\s*/).map(&:downcase)
|
||||
weekdays.include? Time.now.strftime("%a").downcase
|
||||
when "dark_at"
|
||||
now = Time.now
|
||||
# Retrieve new sunrise/sunset data if cache is too old
|
||||
if @sun_data.nil? or
|
||||
@sun_data.at("/sun/date/day").text.to_i != now.day
|
||||
lat, lon = cond_value
|
||||
url = "http://www.earthtools.org/sun/%s/%s/%s/%s/99/0" %
|
||||
[lat, lon, now.day, now.month]
|
||||
@log.debug "Retreiving sunrise/sunset data from #{url}..."
|
||||
data = Net::HTTP.get(URI(url))
|
||||
@sun_data = Nokogiri::XML(data)
|
||||
end
|
||||
sunrise = Chronic.parse("today " +
|
||||
@sun_data.at("/sun/morning/sunrise").text)
|
||||
sunset = Chronic.parse("today " +
|
||||
@sun_data.at("/sun/evening/sunset").text)
|
||||
|
||||
now < sunrise or now > sunset
|
||||
end
|
||||
else
|
||||
@log.warn "Unknown condition type/form #{cond.inspect}"
|
||||
|
|
Reference in New Issue