Skip to content

Commit 2f5a0c7

Browse files
committed
* 0.14 - 2015/04/13 - owagner
- API: added cache for sunrise/sunset calculations
1 parent b917249 commit 2f5a0c7

File tree

3 files changed

+70
-36
lines changed

3 files changed

+70
-36
lines changed

README.md

+3
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,9 @@ prior to use.
124124

125125
Changelog
126126
---------
127+
* 0.14 - 2015/04/13 - owagner
128+
- API: added cache for sunrise/sunset calculations
129+
127130
* 0.13 - 2015/04/12 - owagner
128131
- API: onChange logic was completely broken and would retrigger
129132
on every update, or not at all

build.gradle

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
version = '0.12'
1+
version = '0.14'
22

33
apply plugin: 'java'
44
apply plugin: 'eclipse'

src/main/java/com/tellerulam/logic4mqtt/api/Time.java

+66-35
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
/*
22
* This object contains utility functions to deal with Sunset/Sunrise calculations
33
*
4-
* It is effectivly a very thin wrapper around Mike Reedell's https://github.com/mikereedell/sunrisesunsetlib-java/
5-
* library, which in turn implements http://williams.best.vwh.net/sunrise_sunset_algorithm.htm
6-
*
74
*/
85

96
package com.tellerulam.logic4mqtt.api;
@@ -43,7 +40,7 @@ public synchronized void setLocation(String latitude,String longitude)
4340
L.config("Set location for sunset calculations to "+location);
4441
}
4542

46-
private double getZenith(String name)
43+
private double getHorizon(String name)
4744
{
4845
if(name==null || "OFFICIAL".equalsIgnoreCase(name))
4946
return Solar.LN_SOLAR_STANDART_HORIZON;
@@ -53,14 +50,14 @@ else if("NAUTICAL".equalsIgnoreCase(name))
5350
return -12;
5451
else if("CIVIL".equalsIgnoreCase(name))
5552
return -6;
56-
throw new IllegalArgumentException("Unknown zenith "+name);
53+
throw new IllegalArgumentException("Unknown horizon "+name);
5754
}
5855

59-
private LnRstTime calcSSTimes(String zenith)
56+
private LnRstTime calcSSTimes(String horizon)
6057
{
6158
double jd=JulianDay.ln_get_julian_from_sys();
6259
LnRstTime result=new LnRstTime();
63-
Solar.ln_get_solar_rst_horizon(jd, location, getZenith(zenith), result);
60+
Solar.ln_get_solar_rst_horizon(jd, location, getHorizon(horizon), result);
6461
return result;
6562
}
6663

@@ -80,28 +77,28 @@ private static String jdToTimeString(double jd)
8077
/**
8178
* Get the Sunrise time (format hh:mm) for the given Zentih
8279
*
83-
* @param zenith OFFICIAL (or null) / ASTRONOMICAL / NAUTICAL / CIVIL
80+
* @param horizon OFFICIAL (or null) / ASTRONOMICAL / NAUTICAL / CIVIL
8481
* @return the sunrise time
8582
*/
86-
public String getSunrise(String zenith)
83+
public String getSunrise(String horizon)
8784
{
88-
LnRstTime res=calcSSTimes(zenith);
89-
return jdToTimeString(res.rise);
85+
SunCacheEntry sce=getSunriseSunsetTime(horizon);
86+
return sce.sunrise;
9087
}
9188
/**
9289
* Get the Sunset time (format hh:mm) for the given Zentih
9390
*
94-
* @param zenith OFFICIAL (or null) / ASTRONOMICAL / NAUTICAL / CIVIL
91+
* @param horizon OFFICIAL (or null) / ASTRONOMICAL / NAUTICAL / CIVIL
9592
* @return the sunset time
9693
*/
97-
public String getSunset(String zenith)
94+
public String getSunset(String horizon)
9895
{
99-
LnRstTime res=calcSSTimes(zenith);
100-
return jdToTimeString(res.set);
96+
SunCacheEntry sce=getSunriseSunsetTime(horizon);
97+
return sce.sunset;
10198
}
10299

103100
/**
104-
* Shortcut for getSunrise() with the given zenith
101+
* Shortcut for getSunrise() with the given horizon
105102
* @return the sunrise time
106103
*
107104
* @see getSunrise
@@ -111,7 +108,7 @@ public String getAstronomicalSunrise()
111108
return getSunrise("ASTRONOMICAL");
112109
}
113110
/**
114-
* Shortcut for getSunset() with the given zenith
111+
* Shortcut for getSunset() with the given horizon
115112
* @return the sunset time
116113
*
117114
* @see getSunset
@@ -122,7 +119,7 @@ public String getAstronomicalSunset()
122119
}
123120

124121
/**
125-
* Shortcut for getSunrise() with the given zenith
122+
* Shortcut for getSunrise() with the given horizon
126123
* @return the sunrise time
127124
*
128125
* @see getSunrise
@@ -132,7 +129,7 @@ public String getNauticalSunrise()
132129
return getSunrise("NAUTICAL");
133130
}
134131
/**
135-
* Shortcut for getSunset() with the given zenith
132+
* Shortcut for getSunset() with the given horizon
136133
* @return the sunset time
137134
*
138135
* @see getSunset
@@ -143,7 +140,7 @@ public String getNauticalSunset()
143140
}
144141

145142
/**
146-
* Shortcut for getSunrise() with the given zenith
143+
* Shortcut for getSunrise() with the given horizon
147144
* @return the sunrise time
148145
*
149146
* @see getSunrise
@@ -153,7 +150,7 @@ public String getCivilSunrise()
153150
return getSunrise("CIVIL");
154151
}
155152
/**
156-
* Shortcut for getSunset() with the given zenith
153+
* Shortcut for getSunset() with the given horizon
157154
* @return the sunset time
158155
*
159156
* @see getSunset
@@ -164,7 +161,7 @@ public String getCivilSunset()
164161
}
165162

166163
/**
167-
* Shortcut for getSunrise() with the given zenith
164+
* Shortcut for getSunrise() with the given horizon
168165
* @return the sunrise time
169166
*
170167
* @see getSunrise
@@ -174,7 +171,7 @@ public String getOfficialSunrise()
174171
return getSunrise(null);
175172
}
176173
/**
177-
* Shortcut for getSunset() with the given zenith
174+
* Shortcut for getSunset() with the given horizon
178175
* @return the sunset time
179176
*
180177
* @see getSunset
@@ -185,23 +182,19 @@ public String getOfficialSunset()
185182
}
186183

187184
/**
188-
* Determine whether we're currently having daylight according to the given zenith
185+
* Determine whether we're currently having daylight according to the given horizon
189186
*
190-
* @param zenith
187+
* @param horizon
191188
* @return whether it is currently daylight
192189
*/
193190

194-
public boolean isDaylight(String zenith)
191+
public boolean isDaylight(String horizon)
195192
{
196-
LnRstTime res=calcSSTimes(zenith);
197-
double jd=JulianDay.ln_get_julian_from_sys();
198-
if(jd<res.rise && jd<=res.set)
199-
jd++;
200-
return jd>=res.rise && jd<=res.set;
193+
return isBetween(getSunrise(horizon),getSunset(horizon));
201194
}
202195

203196
/**
204-
* Shortcut to isDaylight() with the given zenith
197+
* Shortcut to isDaylight() with the given horizon
205198
* @return whether it is currently daylight
206199
*/
207200
public boolean isCivilDaylight()
@@ -210,7 +203,7 @@ public boolean isCivilDaylight()
210203
}
211204

212205
/**
213-
* Shortcut to isDaylight() with the given zenith
206+
* Shortcut to isDaylight() with the given horizon
214207
* @return whether it is currently daylight
215208
*/
216209
public boolean isNauticalDaylight()
@@ -219,7 +212,7 @@ public boolean isNauticalDaylight()
219212
}
220213

221214
/**
222-
* Shortcut to isDaylight() with the given zenith
215+
* Shortcut to isDaylight() with the given horizon
223216
* @return whether it is currently daylight
224217
*/
225218
public boolean isAstronomicalDaylight()
@@ -228,14 +221,52 @@ public boolean isAstronomicalDaylight()
228221
}
229222

230223
/**
231-
* Shortcut to isDaylight() with the given zenith
224+
* Shortcut to isDaylight() with the given horizon
232225
* @return whether it is currently daylight
233226
*/
234227
public boolean isOfficialDaylight()
235228
{
236229
return isDaylight(null);
237230
}
238231

232+
private static class SunCacheEntry
233+
{
234+
final String sunrise;
235+
final String sunset;
236+
SunCacheEntry(String sunrise, String sunset)
237+
{
238+
this.sunrise = sunrise;
239+
this.sunset = sunset;
240+
}
241+
}
242+
private static final Map<String,SunCacheEntry> sunCache=new HashMap<>();
243+
private static long sunCacheValidUntil;
244+
245+
private synchronized SunCacheEntry getSunriseSunsetTime(String horizon)
246+
{
247+
long now=System.currentTimeMillis();
248+
if(now>=sunCacheValidUntil)
249+
{
250+
sunCache.clear();
251+
// Newly calculated values will be valid until midnight tomorrow
252+
Calendar cnow=Calendar.getInstance();
253+
cnow.add(Calendar.DAY_OF_YEAR, 1);
254+
cnow.set(Calendar.HOUR_OF_DAY, 0);
255+
cnow.set(Calendar.MINUTE, 0);
256+
cnow.set(Calendar.SECOND, 0);
257+
cnow.set(Calendar.MILLISECOND, 0);
258+
sunCacheValidUntil=cnow.getTimeInMillis();
259+
}
260+
SunCacheEntry sc=sunCache.get(horizon);
261+
if(sc==null)
262+
{
263+
LnRstTime res=calcSSTimes(horizon);
264+
sc=new SunCacheEntry(jdToTimeString(res.rise),jdToTimeString(res.set));
265+
sunCache.put(horizon, sc);
266+
}
267+
return sc;
268+
}
269+
239270
private final Pattern timeSpecPattern=Pattern.compile("([0-9]{1,2}):([0-9]{1,2})(?:\\:([0-9]{1,2}))?");
240271
private Calendar parseTimeSpec(String timespec)
241272
{

0 commit comments

Comments
 (0)