1
1
/*
2
2
* This object contains utility functions to deal with Sunset/Sunrise calculations
3
3
*
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
- *
7
4
*/
8
5
9
6
package com .tellerulam .logic4mqtt .api ;
@@ -43,7 +40,7 @@ public synchronized void setLocation(String latitude,String longitude)
43
40
L .config ("Set location for sunset calculations to " +location );
44
41
}
45
42
46
- private double getZenith (String name )
43
+ private double getHorizon (String name )
47
44
{
48
45
if (name ==null || "OFFICIAL" .equalsIgnoreCase (name ))
49
46
return Solar .LN_SOLAR_STANDART_HORIZON ;
@@ -53,14 +50,14 @@ else if("NAUTICAL".equalsIgnoreCase(name))
53
50
return -12 ;
54
51
else if ("CIVIL" .equalsIgnoreCase (name ))
55
52
return -6 ;
56
- throw new IllegalArgumentException ("Unknown zenith " +name );
53
+ throw new IllegalArgumentException ("Unknown horizon " +name );
57
54
}
58
55
59
- private LnRstTime calcSSTimes (String zenith )
56
+ private LnRstTime calcSSTimes (String horizon )
60
57
{
61
58
double jd =JulianDay .ln_get_julian_from_sys ();
62
59
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 );
64
61
return result ;
65
62
}
66
63
@@ -80,28 +77,28 @@ private static String jdToTimeString(double jd)
80
77
/**
81
78
* Get the Sunrise time (format hh:mm) for the given Zentih
82
79
*
83
- * @param zenith OFFICIAL (or null) / ASTRONOMICAL / NAUTICAL / CIVIL
80
+ * @param horizon OFFICIAL (or null) / ASTRONOMICAL / NAUTICAL / CIVIL
84
81
* @return the sunrise time
85
82
*/
86
- public String getSunrise (String zenith )
83
+ public String getSunrise (String horizon )
87
84
{
88
- LnRstTime res = calcSSTimes ( zenith );
89
- return jdToTimeString ( res . rise ) ;
85
+ SunCacheEntry sce = getSunriseSunsetTime ( horizon );
86
+ return sce . sunrise ;
90
87
}
91
88
/**
92
89
* Get the Sunset time (format hh:mm) for the given Zentih
93
90
*
94
- * @param zenith OFFICIAL (or null) / ASTRONOMICAL / NAUTICAL / CIVIL
91
+ * @param horizon OFFICIAL (or null) / ASTRONOMICAL / NAUTICAL / CIVIL
95
92
* @return the sunset time
96
93
*/
97
- public String getSunset (String zenith )
94
+ public String getSunset (String horizon )
98
95
{
99
- LnRstTime res = calcSSTimes ( zenith );
100
- return jdToTimeString ( res . set ) ;
96
+ SunCacheEntry sce = getSunriseSunsetTime ( horizon );
97
+ return sce . sunset ;
101
98
}
102
99
103
100
/**
104
- * Shortcut for getSunrise() with the given zenith
101
+ * Shortcut for getSunrise() with the given horizon
105
102
* @return the sunrise time
106
103
*
107
104
* @see getSunrise
@@ -111,7 +108,7 @@ public String getAstronomicalSunrise()
111
108
return getSunrise ("ASTRONOMICAL" );
112
109
}
113
110
/**
114
- * Shortcut for getSunset() with the given zenith
111
+ * Shortcut for getSunset() with the given horizon
115
112
* @return the sunset time
116
113
*
117
114
* @see getSunset
@@ -122,7 +119,7 @@ public String getAstronomicalSunset()
122
119
}
123
120
124
121
/**
125
- * Shortcut for getSunrise() with the given zenith
122
+ * Shortcut for getSunrise() with the given horizon
126
123
* @return the sunrise time
127
124
*
128
125
* @see getSunrise
@@ -132,7 +129,7 @@ public String getNauticalSunrise()
132
129
return getSunrise ("NAUTICAL" );
133
130
}
134
131
/**
135
- * Shortcut for getSunset() with the given zenith
132
+ * Shortcut for getSunset() with the given horizon
136
133
* @return the sunset time
137
134
*
138
135
* @see getSunset
@@ -143,7 +140,7 @@ public String getNauticalSunset()
143
140
}
144
141
145
142
/**
146
- * Shortcut for getSunrise() with the given zenith
143
+ * Shortcut for getSunrise() with the given horizon
147
144
* @return the sunrise time
148
145
*
149
146
* @see getSunrise
@@ -153,7 +150,7 @@ public String getCivilSunrise()
153
150
return getSunrise ("CIVIL" );
154
151
}
155
152
/**
156
- * Shortcut for getSunset() with the given zenith
153
+ * Shortcut for getSunset() with the given horizon
157
154
* @return the sunset time
158
155
*
159
156
* @see getSunset
@@ -164,7 +161,7 @@ public String getCivilSunset()
164
161
}
165
162
166
163
/**
167
- * Shortcut for getSunrise() with the given zenith
164
+ * Shortcut for getSunrise() with the given horizon
168
165
* @return the sunrise time
169
166
*
170
167
* @see getSunrise
@@ -174,7 +171,7 @@ public String getOfficialSunrise()
174
171
return getSunrise (null );
175
172
}
176
173
/**
177
- * Shortcut for getSunset() with the given zenith
174
+ * Shortcut for getSunset() with the given horizon
178
175
* @return the sunset time
179
176
*
180
177
* @see getSunset
@@ -185,23 +182,19 @@ public String getOfficialSunset()
185
182
}
186
183
187
184
/**
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
189
186
*
190
- * @param zenith
187
+ * @param horizon
191
188
* @return whether it is currently daylight
192
189
*/
193
190
194
- public boolean isDaylight (String zenith )
191
+ public boolean isDaylight (String horizon )
195
192
{
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 ));
201
194
}
202
195
203
196
/**
204
- * Shortcut to isDaylight() with the given zenith
197
+ * Shortcut to isDaylight() with the given horizon
205
198
* @return whether it is currently daylight
206
199
*/
207
200
public boolean isCivilDaylight ()
@@ -210,7 +203,7 @@ public boolean isCivilDaylight()
210
203
}
211
204
212
205
/**
213
- * Shortcut to isDaylight() with the given zenith
206
+ * Shortcut to isDaylight() with the given horizon
214
207
* @return whether it is currently daylight
215
208
*/
216
209
public boolean isNauticalDaylight ()
@@ -219,7 +212,7 @@ public boolean isNauticalDaylight()
219
212
}
220
213
221
214
/**
222
- * Shortcut to isDaylight() with the given zenith
215
+ * Shortcut to isDaylight() with the given horizon
223
216
* @return whether it is currently daylight
224
217
*/
225
218
public boolean isAstronomicalDaylight ()
@@ -228,14 +221,52 @@ public boolean isAstronomicalDaylight()
228
221
}
229
222
230
223
/**
231
- * Shortcut to isDaylight() with the given zenith
224
+ * Shortcut to isDaylight() with the given horizon
232
225
* @return whether it is currently daylight
233
226
*/
234
227
public boolean isOfficialDaylight ()
235
228
{
236
229
return isDaylight (null );
237
230
}
238
231
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
+
239
270
private final Pattern timeSpecPattern =Pattern .compile ("([0-9]{1,2}):([0-9]{1,2})(?:\\ :([0-9]{1,2}))?" );
240
271
private Calendar parseTimeSpec (String timespec )
241
272
{
0 commit comments