Share the sunrise/sunset data across rules
Also, rewrite the darkness checking logic a bit.
This commit is contained in:
parent
005235fa03
commit
48004beb92
|
@ -22,7 +22,7 @@ module Hued
|
||||||
@priority = entry["priority"] || 0
|
@priority = entry["priority"] || 0
|
||||||
@events = []
|
@events = []
|
||||||
|
|
||||||
@sun_data = nil
|
@@sun_data = nil
|
||||||
|
|
||||||
if entry["events"]
|
if entry["events"]
|
||||||
entry["events"].each do |ev_name|
|
entry["events"].each do |ev_name|
|
||||||
|
@ -128,9 +128,8 @@ module Hued
|
||||||
weekdays = cond_value.split(/,\s*/).map(&:downcase)
|
weekdays = cond_value.split(/,\s*/).map(&:downcase)
|
||||||
weekdays.include? Time.now.strftime("%a").downcase
|
weekdays.include? Time.now.strftime("%a").downcase
|
||||||
when "dark_at"
|
when "dark_at"
|
||||||
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 @sun_data[:day] != now.to_date
|
if @@sun_data.nil? or @@sun_data[:day] != Date.today
|
||||||
lat, lon = cond_value
|
lat, lon = cond_value
|
||||||
url = "http://api.sunrise-sunset.org/json?lat=%s&lng=%s&formatted=0" %
|
url = "http://api.sunrise-sunset.org/json?lat=%s&lng=%s&formatted=0" %
|
||||||
[lat, lon]
|
[lat, lon]
|
||||||
|
@ -140,19 +139,19 @@ module Hued
|
||||||
json_data = JSON(data)
|
json_data = JSON(data)
|
||||||
sunrise = Time.parse(json_data["results"]["sunrise"])
|
sunrise = Time.parse(json_data["results"]["sunrise"])
|
||||||
sunset = Time.parse(json_data["results"]["sunset"])
|
sunset = Time.parse(json_data["results"]["sunset"])
|
||||||
@sun_data = { day: Date.today,
|
@@sun_data = { day: Date.today,
|
||||||
sunrise: sunrise,
|
sunrise: sunrise,
|
||||||
sunset: sunset }
|
sunset: sunset }
|
||||||
rescue => e
|
rescue => e
|
||||||
@log.warn "Could retrieve sunset data: #{e}, will retry"
|
@log.warn "Could retrieve sunset data: #{e}, will retry"
|
||||||
@sun_data = nil
|
@@sun_data = nil
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
# Include twilight
|
# Include twilight
|
||||||
if @sun_data
|
if @@sun_data
|
||||||
sunrise = @sun_data[:sunrise]
|
sunrise = @@sun_data[:sunrise]
|
||||||
sunset = @sun_data[:sunset]
|
sunset = @@sun_data[:sunset]
|
||||||
now < (sunrise - 10*60) or now > (sunset + 10*60)
|
!Time.now.between?(sunrise - 10*60, sunset + 10*60)
|
||||||
else
|
else
|
||||||
false
|
false
|
||||||
end
|
end
|
||||||
|
|
Reference in New Issue