Replace the dark_at condition implementation
The previous implementation used the EarthTools Webservices, but this website has been down for a few months now. This replacement uses the Sunset-Sunrise API. (See also: http://sunrise-sunset.org/api.)
This commit is contained in:
parent
c68b56714d
commit
a894d55f8c
|
@ -18,9 +18,9 @@ Support conditions:
|
||||||
* Start time (per day or globally)
|
* Start time (per day or globally)
|
||||||
* End time (per day or globally)
|
* End time (per day or globally)
|
||||||
* Specific weekdays
|
* Specific weekdays
|
||||||
* When it is dark (using sunset/sunrise time using EarthTools Websevices [1])
|
* When it is dark (using sunset/sunrise time using the Sunrise-Sunset API [1])
|
||||||
|
|
||||||
[1]: http://www.earthtools.org/webservices.htm
|
[1]: http://sunrise-sunset.org/api
|
||||||
|
|
||||||
## Requirements
|
## Requirements
|
||||||
|
|
||||||
|
|
|
@ -1,7 +1,9 @@
|
||||||
# encoding: utf-8
|
# encoding: utf-8
|
||||||
|
|
||||||
require "nokogiri"
|
require "date"
|
||||||
|
require "json"
|
||||||
require "net/http"
|
require "net/http"
|
||||||
|
require "time"
|
||||||
|
|
||||||
module Hued
|
module Hued
|
||||||
|
|
||||||
|
@ -128,21 +130,32 @@ module Hued
|
||||||
when "dark_at"
|
when "dark_at"
|
||||||
now = Time.now
|
now = Time.now
|
||||||
# Retrieve new sunrise/sunset data if cache is too old
|
# Retrieve new sunrise/sunset data if cache is too old
|
||||||
if @sun_data.nil? or
|
if @sun_data.nil? or @sun_data[:day] != now.to_date
|
||||||
@sun_data.at("/sun/date/day").text.to_i != now.day
|
|
||||||
lat, lon = cond_value
|
lat, lon = cond_value
|
||||||
url = "http://www.earthtools.org/sun/%s/%s/%s/%s/99/0" %
|
url = "http://api.sunrise-sunset.org/json?lat=%s&lng=%s&formatted=0" %
|
||||||
[lat, lon, now.day, now.month]
|
[lat, lon]
|
||||||
@log.debug "Retreiving sunrise/sunset data from #{url}..."
|
@log.debug "Retreiving sunrise/sunset data from #{url}..."
|
||||||
|
begin
|
||||||
data = Net::HTTP.get(URI(url))
|
data = Net::HTTP.get(URI(url))
|
||||||
@sun_data = Nokogiri::XML(data)
|
json_data = JSON(data)
|
||||||
|
sunrise = Time.parse(json_data["results"]["sunrise"])
|
||||||
|
sunset = Time.parse(json_data["results"]["sunset"])
|
||||||
|
@sun_data = { day: Date.today,
|
||||||
|
sunrise: sunrise,
|
||||||
|
sunset: sunset }
|
||||||
|
rescue => e
|
||||||
|
@log.warn "Could retrieve sunset data: #{e}, will retry"
|
||||||
|
@sun_data = nil
|
||||||
|
end
|
||||||
|
end
|
||||||
|
# Include twilight
|
||||||
|
if @sun_data
|
||||||
|
sunrise = @sun_data[:sunrise]
|
||||||
|
sunset = @sun_data[:sunset]
|
||||||
|
now < (sunrise - 10*60) or now > (sunset + 10*60)
|
||||||
|
else
|
||||||
|
false
|
||||||
end
|
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
|
end
|
||||||
else
|
else
|
||||||
@log.warn "Unknown condition type/form #{cond.inspect}"
|
@log.warn "Unknown condition type/form #{cond.inspect}"
|
||||||
|
|
Reference in New Issue