private void _CalculateSunriseSunset(double latitude, double longitude, out DateTime sunrise, out DateTime sunset) { //equation from https://en.wikipedia.org/wiki/Sunrise_equation#Complete_calculation_on_Earth //calculate current julian day. don't want extra hours so we chop off any decimals from the current julian date double n = (double)((int)DateTimeToJulianDate(DateTime.Now.ToUniversalTime())) - 2451545.0 + 0.0008; //calculate mean solar noon double msn = n - (longitude / 360.0); //calculate solar mean anomaly double sma = (357.5291 + 0.98560028 * msn) % 360; //equation of the center double center = (1.9148 * SinDegrees(sma)) + (0.02 * SinDegrees(2 * sma)) + (0.0003 * SinDegrees(3 * sma)); //ecliptic longitude double elong = (sma + center + 180 + 102.9372) % 360; //solar transit double strans = 2451545.0 + msn + (0.0053 * SinDegrees(sma)) - (0.0069 * SinDegrees(2 * elong)); //declination of the sun and hour-angle double declination = MathHelpers.RadiansToDegrees(Math.Asin(SinDegrees(elong) * SinDegrees(23.55))); //hour angle double hAngle = MathHelpers.RadiansToDegrees(Math.Acos((SinDegrees(-0.83) - SinDegrees(latitude) * SinDegrees(declination)) / (Math.Cos(MathHelpers.DegreesToRadians(latitude)) * Math.Cos(MathHelpers.DegreesToRadians(declination))))); //julian date of sunrise double rise = strans - (hAngle / 360); double set = strans + (hAngle / 360); sunrise = JulianDateToUTC(rise).ToLocalTime(); sunset = JulianDateToUTC(set).ToLocalTime(); }
private double SinDegrees(double degrees) { return(Math.Sin(MathHelpers.DegreesToRadians(degrees))); }
private void _InitializeApp(Window window) { DisplayController dispController = DisplayController.Instance; _LoadAppData(); if (m_startMinimized) { window.Hide(); window.ShowInTaskbar = false; } /*wait for the display controller to finish initializing before proceeding. * if something went wrong this call will throw a win32exception*/ dispController.Initialization.Wait(); WindowTitle = "Unblind " + m_versionMajor.ToString() + "." + m_versionMinor.ToString() + "." + m_versionIteration.ToString(); if (dispController.AttachedDisplays.Count > 0) { bool supportsBrightness = true; foreach (Display display in dispController.AttachedDisplays) { if (m_firstRun && supportsBrightness && display.SupportsBrightness() == false) { supportsBrightness = false; MessageBox.Show("It appears that one or more of your displays do not support the DDC/CI protocol required to change the brightness via software. Unblind may or may not be able to control these displays.", "Hmm...", MessageBoxButton.OK); } //determine the highest minimum brightness and lowest maximum brightness among all displays and use these as the brightness limits if (display.MinBrightness > m_minDisplayBrightness) { MinDisplayBrightness = MathHelpers.Greater(display.MinBrightness, (uint)1); } if (display.MaxBrightness > m_maxDisplayBrightness) { MaxDisplayBrightness = MathHelpers.Greater(display.MaxBrightness, (uint)1); } if (display.SupportsBrightness() == false) { NoSupportDetected = true; } } } else if (dispController.IntegratedDisplaySupported == true) { MinDisplayBrightness = 0; MaxDisplayBrightness = 100; } if (dispController.AttachedDisplays.Count > 1 || (dispController.AttachedDisplays.Count >= 1 && dispController.IntegratedDisplaySupported == true)) { MultipleDisplaysConnected = true; } dispController.OnDisplayRefesh += _OnDisplayRefresh; CurrentBrightnessTransitionStatus = DimmerStatus.Idle; _UpdateTimeToNextPeriod(); m_brightnessDimmer.SetDisplayBrightness((m_brightnessScheduler.CurrentTimePeriod == TimePeriod.Day) ? (uint)m_brightnessScheduler.DayBrightness : (uint)m_brightnessScheduler.NightBrightness); m_systemClockTimer.Start(); }