/// <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;
        }
        public void Timer_Elapsed(Object source, ElapsedEventArgs e)
        {
            // Set wallpaper to the image at the current Index
            string path = Path.Combine(_dirPath, _wallpaper.Images[Index].Name);

            if (!File.Exists(path))
            {
                Console.Error.WriteLine($"File doesn't exist: {path}");
            }
            DesktopManager.SetDesktopWallpaper(path);

            // Persist current wallpaper's path via Settings
            Properties.Settings.Default.LastSetWallpaperPath = path;
            Properties.Settings.Default.Save(); // persist settings across application sessions

            // Schedule _timer to run when the sun reaches the next image's progress
            Index = (Index + 1) % _wallpaper.Images.Count; // set Index to next image's index
            DateTime changeTime = SunCalcHelper.GetNextTime(_wallpaper.Images[Index].Progress, DateTime.Now, _location.Latitude, _location.Longitude);
            double   interval   = (changeTime.Ticks - DateTime.Now.Ticks) / TimeSpan.TicksPerMillisecond;

            if (interval < 1)
            {
                interval = 1; // fire timer immediately if it should have fired in the past
            }
            _timer.Interval = interval;
            IsRunning       = true;
            NextChangeTime  = changeTime;
        }
        public void Location_Click()
        {
            // Show window to change location
            ChangeLocationWindow w = new ChangeLocationWindow(Location.Latitude, Location.Longitude);

            w.Owner = Application.Current.MainWindow;
            w.ShowDialog();

            // If user clicked Ok button (and input was validated)...
            if (w.OkClicked)
            {
                // Verify user input won't crash app by checking if given location has all SunPhase entries
                var       phases = SunCalcHelper.GetSunPhases(DateTime.Now, w.Latitude, w.Longitude);
                const int EXPECTED_NUM_SUNPHASES = 14;
                if (phases.Count() != EXPECTED_NUM_SUNPHASES)
                {
                    // User input an invalid location. Notify user
                    MessageBox.Show($"There's a problem with\n{w.Latitude}\u00B0N, {w.Longitude}\u00B0E");
                    return;
                }

                // No problem with user input
                // Persist location values in settings
                Properties.Settings.Default.Latitude  = w.Latitude;
                Properties.Settings.Default.Longitude = w.Longitude;
                Properties.Settings.Default.Save();
                // Set Location property (which updates _scheduler)
                Location = new Location(w.Latitude, w.Longitude);
            }
        }
Esempio n. 4
0
 // Constructor
 public SunInfoWindowViewModel()
 {
     // Initialize properties
     Date              = DateTime.Now;
     _lat              = Properties.Settings.Default.Latitude;
     _lng              = Properties.Settings.Default.Longitude;
     Phases            = SunCalcHelper.GetSunPhases(Date, _lat, _lng);
     AverageProgresses = CalculateAverageProgresses();
 }
        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();
        }
Esempio n. 6
0
        // 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);
        }