private static DaySegment GetDaySegment(SolarData data, DateTimeTZ time) { if (data.polarPeriod == PolarPeriod.PolarDay) { return(DaySegment.AllDay); } else if (data.polarPeriod == PolarPeriod.PolarNight) { return(DaySegment.AllNight); } else if (data.solarTimes[0].Time <= time.Time && time.Time < data.solarTimes[1].Time) { return(DaySegment.Sunrise); } else if (data.solarTimes[1].Time <= time.Time && time.Time < data.solarTimes[2].Time) { return(DaySegment.Day); } else if (data.solarTimes[2].Time <= time.Time && time.Time < data.solarTimes[3].Time) { return(DaySegment.Sunset); } else { return(DaySegment.Night); } }
private void OnBackgroundTimerElapsed(object sender, EventArgs e) { DateTimeTZ DateTimeNow = DateTimeTZ.Local.Now.ConvertTime(JsonConfig.settings.timezone); if (nextUpdateTime.HasValue && DateTimeNow.Time >= nextUpdateTime.Value) { HandleTimerEvent(true); } }
public static DateTimeTZ SafeParse(string dateTime, TimeZoneInfo tz) { try { return(DateTimeTZ.Parse(dateTime, tz, CultureInfo.InvariantCulture)); } catch (FormatException) { return(DateTimeTZ.Parse(dateTime, tz)); } }
private void StartTimer(DateTime futureTime) { DateTimeTZ DateTimeNow = DateTimeTZ.Local.Now.ConvertTime(JsonConfig.settings.timezone); long intervalTicks = futureTime.Ticks - DateTimeNow.Time.Ticks; if (intervalTicks < timerError) { intervalTicks = 1; } TimeSpan interval = new TimeSpan(intervalTicks); schedulerTimer.Interval = interval.TotalMilliseconds; schedulerTimer.Start(); }
public static SolarData GetSolarData(DateTimeTZ date) { double latitude = double.Parse(JsonConfig.settings.latitude, CultureInfo.InvariantCulture); double longitude = double.Parse(JsonConfig.settings.longitude, CultureInfo.InvariantCulture); var sunPhases = GetSunPhases(date, latitude, longitude); SolarData data = new SolarData(); if (JsonConfig.settings.dontUseLocation) { data.sunriseTime = UpdateHandler.SafeParse(JsonConfig.settings.sunriseTime, TimeZoneInfo.FindSystemTimeZoneById(JsonConfig.settings.timezone)); data.sunsetTime = UpdateHandler.SafeParse(JsonConfig.settings.sunsetTime, TimeZoneInfo.FindSystemTimeZoneById(JsonConfig.settings.timezone)); } try { data.sunriseTime = GetSolarTime(sunPhases, SunPhaseName.Sunrise).ConvertTime(JsonConfig.settings.timezone); data.sunsetTime = GetSolarTime(sunPhases, SunPhaseName.Sunset).ConvertTime(JsonConfig.settings.timezone); data.solarTimes = new DateTimeTZ[4] { GetSolarTime(sunPhases, SunPhaseName.Dawn), GetSolarTime(sunPhases, SunPhaseName.GoldenHourEnd), GetSolarTime(sunPhases, SunPhaseName.GoldenHour), GetSolarTime(sunPhases, SunPhaseName.Dusk) }; } catch (InvalidOperationException) // Handle polar day/night { DateTimeTZ solarNoon = GetSolarTime(sunPhases, SunPhaseName.SolarNoon); double sunAltitude = SunCalcNet.SunCalc.GetSunPosition(solarNoon.ToUniversalTime(), latitude, longitude).Altitude; if (sunAltitude > 0) { data.polarPeriod = PolarPeriod.PolarDay; } else { data.polarPeriod = PolarPeriod.PolarNight; } } return(data); }
public void RunScheduler(bool forceImageUpdate = false) { if (!LaunchSequence.IsLocationReady() || !LaunchSequence.IsThemeReady()) { return; } schedulerTimer.Stop(); DateTimeTZ DateTimeToday = DateTimeTZ.UTC.Today.ConvertTime(JsonConfig.settings.timezone); SolarData data = SunriseSunsetService.GetSolarData(DateTimeToday); DateTimeTZ DateTimeNow = DateTimeTZ.UTC.Now.ConvertTime(JsonConfig.settings.timezone); isSunUp = (data.sunriseTime.Time <= DateTimeNow.Time && DateTimeNow.Time < data.sunsetTime.Time); DateTime?nextImageUpdateTime = null; if (ThemeManager.currentTheme != null) { if (forceImageUpdate) { lastImagePath = null; } WallpaperShuffler.MaybeShuffleWallpaper(); } SchedulerState imageData = GetImageData(data, ThemeManager.currentTheme, DateTimeNow); if (ThemeManager.currentTheme != null) { SetWallpaper(imageData.imageId); nextImageUpdateTime = new DateTimeTZ(JsonConfig.settings.timezone, new DateTime(imageData.nextUpdateTicks)).Time; } ScriptManager.RunScripts(new ScriptArgs { daySegment2 = imageData.daySegment2, daySegment4 = imageData.daySegment4, imagePath = (ThemeManager.currentTheme != null) ? lastImagePath : null }); if (data.polarPeriod != PolarPeriod.None) { nextUpdateTime = DateTimeToday.AddDays(1).Time; } else if (isSunUp) { nextUpdateTime = data.sunsetTime.Time; } else if (DateTimeNow.Time < data.solarTimes[0].Time) { nextUpdateTime = data.sunriseTime.Time; } else { SolarData tomorrowsData = SunriseSunsetService.GetSolarData(DateTimeToday.AddDays(1)); nextUpdateTime = tomorrowsData.sunriseTime.Time; } if (nextImageUpdateTime.HasValue && nextImageUpdateTime.Value < nextUpdateTime.Value) { nextUpdateTime = nextImageUpdateTime; } StartTimer(nextUpdateTime.Value); }
public SchedulerState GetImageData(SolarData data, ThemeConfig theme, DateTimeTZ current) { int[] imageList = null; DateTimeTZ segmentStart; DateTimeTZ segmentEnd; SchedulerState imageData = new SchedulerState() { daySegment2 = isSunUp ? 0 : 1 }; // Use 4-segment mode if sunrise and sunset image lists are defined, and dark mode is not enabled if (theme != null && ThemeManager.IsTheme4Segment(theme) && !JsonConfig.settings.darkMode) { switch (GetDaySegment(data, current)) { case DaySegment.AllDay: imageList = theme?.dayImageList; segmentStart = current; segmentEnd = current.AddDays(1); imageData.daySegment4 = 1; break; case DaySegment.AllNight: imageList = theme?.nightImageList; segmentStart = current; segmentEnd = current.AddDays(1); imageData.daySegment4 = 3; break; case DaySegment.Sunrise: imageList = theme?.sunriseImageList; segmentStart = data.solarTimes[0]; segmentEnd = data.solarTimes[1]; imageData.daySegment4 = 0; break; case DaySegment.Day: imageList = theme?.dayImageList; segmentStart = data.solarTimes[1]; segmentEnd = data.solarTimes[2]; imageData.daySegment4 = 1; break; case DaySegment.Sunset: imageList = theme?.sunsetImageList; segmentStart = data.solarTimes[2]; segmentEnd = data.solarTimes[3]; imageData.daySegment4 = 2; break; default: imageList = theme?.nightImageList; imageData.daySegment4 = 3; if (current.Time < data.solarTimes[0].Time) { SolarData yesterdaysData = SunriseSunsetService.GetSolarData(current.AddDays(-1)); segmentStart = yesterdaysData.solarTimes[3]; segmentEnd = data.solarTimes[0]; } else { segmentStart = data.solarTimes[3]; SolarData tomorrowsData = SunriseSunsetService.GetSolarData(current.AddDays(1)); segmentEnd = tomorrowsData.solarTimes[0]; } break; } } else { imageList = theme?.nightImageList; if (!JsonConfig.settings.darkMode && (isSunUp || data.polarPeriod == PolarPeriod.PolarDay)) { imageList = theme?.dayImageList; } if (data.polarPeriod != PolarPeriod.None) { segmentStart = current; segmentEnd = current.AddDays(1); } else if (isSunUp) { segmentStart = data.sunriseTime; segmentEnd = data.sunsetTime; } else if (current.Time < data.sunriseTime.Time) { SolarData yesterdaysData = SunriseSunsetService.GetSolarData(current.AddDays(-1)); segmentStart = yesterdaysData.sunsetTime; segmentEnd = data.sunriseTime; } else { segmentStart = data.sunsetTime; SolarData tomorrowsData = SunriseSunsetService.GetSolarData(current.AddDays(1)); segmentEnd = tomorrowsData.sunriseTime; } } if (imageList != null) { TimeSpan segmentLength = segmentEnd.Time - segmentStart.Time; TimeSpan timerLength = new TimeSpan(segmentLength.Ticks / imageList.Length); int imageNumber = (int)((current.Time.Ticks - segmentStart.Time.Ticks) / timerLength.Ticks); imageData.imageId = imageList[imageNumber]; imageData.imageNumber = imageNumber; imageData.nextUpdateTicks = segmentStart.Time.Ticks + timerLength.Ticks * (imageNumber + 1); } return(imageData); }
private static List <SunPhase> GetSunPhases(DateTimeTZ date, double latitude, double longitude) { return(SunCalcNet.SunCalc.GetSunPhases(date.AddHours(12).ToUniversalTime(), latitude, longitude).ToList()); }