Sunset/sunrise: rotate ids and reuse the id from two days ago for tomorrow, this way we will have sunrise/sunset for 3 days while sending only sunrise/sunset per day

here
Andreas Shimokawa 2016-05-24 14:46:22 +02:00
parent e3bee37b81
commit 55a40f7b06
1 changed files with 14 additions and 21 deletions

View File

@ -39,21 +39,24 @@ public class AlarmReceiver extends BroadcastReceiver {
public void onReceive(Context context, Intent intent) {
LOG.info("will resend sunrise and sunset events");
GBApplication.deviceService().onDeleteCalendarEvent(CalendarEventSpec.TYPE_SUNRISE, 1);
GBApplication.deviceService().onDeleteCalendarEvent(CalendarEventSpec.TYPE_SUNRISE, 2);
GBApplication.deviceService().onDeleteCalendarEvent(CalendarEventSpec.TYPE_SUNSET, 1);
GBApplication.deviceService().onDeleteCalendarEvent(CalendarEventSpec.TYPE_SUNSET, 2);
final GregorianCalendar dateTimeTomorrow = new GregorianCalendar();
dateTimeTomorrow.add(GregorianCalendar.DAY_OF_MONTH, 1);
/*
* rotate ids ud reuse the id from two days ago for tomorrow, this way we will have
* sunrise /sunset for 3 days while sending only sunrise/sunset per day
*/
byte id_tomorrow = (byte) ((dateTimeTomorrow.getTimeInMillis() / (1000L * 60L * 60L * 24L)) % 3);
GBApplication.deviceService().onDeleteCalendarEvent(CalendarEventSpec.TYPE_SUNRISE, id_tomorrow);
GBApplication.deviceService().onDeleteCalendarEvent(CalendarEventSpec.TYPE_SUNSET, id_tomorrow);
Prefs prefs = GBApplication.getPrefs();
float latitude = prefs.getFloat("location_latitude", 0);
float longitude = prefs.getFloat("location_longitude", 0);
final GregorianCalendar dateTimeToday = new GregorianCalendar();
final GregorianCalendar dateTimeTomorrow = new GregorianCalendar();
dateTimeTomorrow.add(GregorianCalendar.DAY_OF_MONTH, 1);
GregorianCalendar[] sunriseTransitSetToday = SPA.calculateSunriseTransitSet(dateTimeToday, latitude, longitude, DeltaT.estimate(dateTimeToday));
GregorianCalendar[] sunriseTransitSetTomorrow = SPA.calculateSunriseTransitSet(dateTimeTomorrow, latitude, longitude, DeltaT.estimate(dateTimeToday));
GregorianCalendar[] sunriseTransitSetTomorrow = SPA.calculateSunriseTransitSet(dateTimeTomorrow, latitude, longitude, DeltaT.estimate(dateTimeTomorrow));
CalendarEventSpec calendarEventSpec = new CalendarEventSpec();
calendarEventSpec.durationInSeconds = 0;
@ -61,26 +64,16 @@ public class AlarmReceiver extends BroadcastReceiver {
calendarEventSpec.type = CalendarEventSpec.TYPE_SUNRISE;
calendarEventSpec.title = "Sunrise";
if (sunriseTransitSetToday[0] != null) {
calendarEventSpec.id = 1;
calendarEventSpec.timestamp = (int) (sunriseTransitSetToday[0].getTimeInMillis() / 1000);
GBApplication.deviceService().onAddCalendarEvent(calendarEventSpec);
}
if (sunriseTransitSetTomorrow[0] != null) {
calendarEventSpec.id = 2;
calendarEventSpec.id = id_tomorrow;
calendarEventSpec.timestamp = (int) (sunriseTransitSetTomorrow[0].getTimeInMillis() / 1000);
GBApplication.deviceService().onAddCalendarEvent(calendarEventSpec);
}
calendarEventSpec.type = CalendarEventSpec.TYPE_SUNSET;
calendarEventSpec.title = "Sunset";
if (sunriseTransitSetToday[2] != null) {
calendarEventSpec.id = 1;
calendarEventSpec.timestamp = (int) (sunriseTransitSetToday[2].getTimeInMillis() / 1000);
GBApplication.deviceService().onAddCalendarEvent(calendarEventSpec);
}
if (sunriseTransitSetTomorrow[2] != null) {
calendarEventSpec.id = 2;
calendarEventSpec.id = id_tomorrow;
calendarEventSpec.timestamp = (int) (sunriseTransitSetTomorrow[2].getTimeInMillis() / 1000);
GBApplication.deviceService().onAddCalendarEvent(calendarEventSpec);
}