/// <summary> /// Sets Index to that of the image whose progress is closest to sun's current /// progress without exceeding it, and changes wallpaper to that image /// /// Note: calling this method will "start" the scheduler if it isn't running /// </summary> private void SyncToSunProgress() { _timer.Enabled = false; // TODO probably don't need this check, since we're checking for IsRunning == true before calling this // from everywhere except DirPath_Change() (which only calls this if _wallpaper was created successfully) if (_wallpaper == null) { Stop(); return; } // Set Index to that of the image whose progress is closest to sun's current progress without exceeding it DateTime now = DateTime.Now; double currentProgress = SunCalcHelper.GetSunProgress(now, _location.Latitude, _location.Longitude); for (int i = 0; i < _wallpaper.Images.Count; i++) { double progress = _wallpaper.Images[i].Progress; if (progress > currentProgress) { break; } Index = i; } // Change wallpaper immediately via _timer _timer.Interval = 1; _timer.Enabled = true; }
private void Phases_Change(IEnumerable <SunCalcNet.Model.SunPhase> phases) { // Update TextBlocks // time DateTime sunrise = phases.First(phase => phase.Name.Value.Equals("Sunrise")).PhaseTime; DateTime sunriseEnd = phases.First(phase => phase.Name.Value.Equals("Sunrise End")).PhaseTime; DateTime goldenHourEnd = phases.First(phase => phase.Name.Value.Equals("Golden Hour End")).PhaseTime; DateTime solarNoon = phases.First(phase => phase.Name.Value.Equals("Solar Noon")).PhaseTime; DateTime goldenHour = phases.First(phase => phase.Name.Value.Equals("Golden Hour")).PhaseTime; DateTime sunsetStart = phases.First(phase => phase.Name.Value.Equals("Sunset Start")).PhaseTime; DateTime sunset = phases.First(phase => phase.Name.Value.Equals("Sunset")).PhaseTime; DateTime dusk = phases.First(phase => phase.Name.Value.Equals("Dusk")).PhaseTime; DateTime nauticalDusk = phases.First(phase => phase.Name.Value.Equals("Nautical Dusk")).PhaseTime; DateTime night = phases.First(phase => phase.Name.Value.Equals("Night")).PhaseTime; DateTime nadir = phases.First(phase => phase.Name.Value.Equals("Nadir")).PhaseTime; DateTime nightEnd = phases.First(phase => phase.Name.Value.Equals("Night End")).PhaseTime; DateTime nauticalDawn = phases.First(phase => phase.Name.Value.Equals("Nautical Dawn")).PhaseTime; DateTime dawn = phases.First(phase => phase.Name.Value.Equals("Dawn")).PhaseTime; string timeFormat = "h:mm:ss tt"; // e.g. "5:30:00 PM" sunriseTimeTextBlock.Text = sunrise.ToString(timeFormat); sunriseEndTimeTextBlock.Text = sunriseEnd.ToString(timeFormat); goldenHourEndTimeTextBlock.Text = goldenHourEnd.ToString(timeFormat); solarNoonTimeTextBlock.Text = solarNoon.ToString(timeFormat); goldenHourTimeTextBlock.Text = goldenHour.ToString(timeFormat); sunsetStartTimeTextBlock.Text = sunsetStart.ToString(timeFormat); sunsetTimeTextBlock.Text = sunset.ToString(timeFormat); duskTimeTextBlock.Text = dusk.ToString(timeFormat); nauticalDuskTimeTextBlock.Text = nauticalDusk.ToString(timeFormat); nightTimeTextBlock.Text = night.ToString(timeFormat); nadirTimeTextBlock.Text = nadir.ToString(timeFormat); nightEndTimeTextBlock.Text = nightEnd.ToString(timeFormat); nauticalDawnTimeTextBlock.Text = nauticalDawn.ToString(timeFormat); dawnTimeTextBlock.Text = dawn.ToString(timeFormat); // progress double lat = Properties.Settings.Default.Latitude; double lng = Properties.Settings.Default.Longitude; sunriseProgressTextBlock.Text = Math.Round(SunCalcHelper.GetSunProgress(sunrise, lat, lng), 2).ToString(); sunriseEndProgressTextBlock.Text = Math.Round(SunCalcHelper.GetSunProgress(sunriseEnd, lat, lng), 2).ToString(); goldenHourEndProgressTextBlock.Text = Math.Round(SunCalcHelper.GetSunProgress(goldenHourEnd, lat, lng), 2).ToString(); solarNoonProgressTextBlock.Text = Math.Round(SunCalcHelper.GetSunProgress(solarNoon, lat, lng), 2).ToString(); goldenHourProgressTextBlock.Text = Math.Round(SunCalcHelper.GetSunProgress(goldenHour, lat, lng), 2).ToString(); sunsetStartProgressTextBlock.Text = Math.Round(SunCalcHelper.GetSunProgress(sunsetStart, lat, lng), 2).ToString(); sunsetProgressTextBlock.Text = Math.Round(SunCalcHelper.GetSunProgress(sunset, lat, lng), 2).ToString(); duskProgressTextBlock.Text = Math.Round(SunCalcHelper.GetSunProgress(dusk, lat, lng), 2).ToString(); nauticalDuskProgressTextBlock.Text = Math.Round(SunCalcHelper.GetSunProgress(nauticalDusk, lat, lng), 2).ToString(); nightProgressTextBlock.Text = Math.Round(SunCalcHelper.GetSunProgress(night, lat, lng), 2).ToString(); nadirProgressTextBlock.Text = Math.Round(SunCalcHelper.GetSunProgress(nadir, lat, lng), 2).ToString(); nightEndProgressTextBlock.Text = Math.Round(SunCalcHelper.GetSunProgress(nightEnd, lat, lng), 2).ToString(); nauticalDawnProgressTextBlock.Text = Math.Round(SunCalcHelper.GetSunProgress(nauticalDawn, lat, lng), 2).ToString(); dawnProgressTextBlock.Text = Math.Round(SunCalcHelper.GetSunProgress(dawn, lat, lng), 2).ToString(); }
// Private methods /// <summary> /// Returns a Dictionary containing the average sun progress values for each SunPhase in the SunCalc library for the next year /// </summary> /// <returns></returns> private Dictionary <string, double> CalculateAverageProgresses() { // Build a Dictionary that will contain progress totals for each SunPhase Dictionary <string, double> progressTotals = new Dictionary <string, double>(); progressTotals.Add(SunCalcHelper.PHASE_NAME_SUNRISE, 0); progressTotals.Add(SunCalcHelper.PHASE_NAME_SUNRISE_END, 0); progressTotals.Add(SunCalcHelper.PHASE_NAME_GOLDEN_HOUR_END, 0); progressTotals.Add(SunCalcHelper.PHASE_NAME_SOLAR_NOON, 0); progressTotals.Add(SunCalcHelper.PHASE_NAME_GOLDEN_HOUR, 0); progressTotals.Add(SunCalcHelper.PHASE_NAME_SUNSET_START, 0); progressTotals.Add(SunCalcHelper.PHASE_NAME_SUNSET, 0); progressTotals.Add(SunCalcHelper.PHASE_NAME_DUSK, 0); progressTotals.Add(SunCalcHelper.PHASE_NAME_NAUTICAL_DUSK, 0); progressTotals.Add(SunCalcHelper.PHASE_NAME_NIGHT, 0); progressTotals.Add(SunCalcHelper.PHASE_NAME_NADIR, 0); progressTotals.Add(SunCalcHelper.PHASE_NAME_NIGHT_END, 0); progressTotals.Add(SunCalcHelper.PHASE_NAME_NAUTICAL_DAWN, 0); progressTotals.Add(SunCalcHelper.PHASE_NAME_DAWN, 0); // Build a List of DateTimes containing one DateTime per week for the next 52 weeks List <DateTime> days = new List <DateTime>(); for (int week = 0; week < 52; week++) { days.Add(Date.AddDays(7 * week)); } // Sum progresses for each Sunphase across all days foreach (var day in days) { var phases = SunCalcHelper.GetSunPhases(day, _lat, _lng); foreach (var phase in phases) { progressTotals[phase.Name.Value] += SunCalcHelper.GetSunProgress(phase.PhaseTime, _lat, _lng); } } // Build a Dictionary of average progresses by dividing each entry in progressTotals by the number of elements in days Dictionary <string, double> result = new Dictionary <string, double>(); foreach (KeyValuePair <string, double> entry in progressTotals) { double avg = progressTotals[entry.Key] / days.Count; result.Add(entry.Key, avg); } return(result); }