public void EquationOfTimeOnFebruary3()
        {
            var B      = CalculationAbbreviations.DayOnCircle(localTime.DayOfYear);
            var result = SolarTime.EquationOfTime(B);

            Assert.AreEqual(-13.5, result, 0.1);
        }
        public DateTime OriginalTimeToSolarTime(DateTime originalTime)
        {
            var localTime = originalTime + this.timeZone.BaseUtcOffset;

            return(SolarTime.GetSolarTime(localTime,
                                          this.longitudeDegrees,
                                          this.timeZone.BaseUtcOffset));
        }
        public DateTime SolarTimeToOriginalTime(DateTime solarTime)
        {
            var localTime = SolarTime.GetLocalTime(solarTime,
                                                   this.longitudeDegrees,
                                                   this.timeZone.BaseUtcOffset);
            var utcTime = localTime - this.timeZone.BaseUtcOffset;

            return(utcTime);
        }
Beispiel #4
0
 public DateTime OriginalTimeToSolarTime(DateTime originalTime)
 {
     if (timeZone.IsDaylightSavingTime(originalTime))
     {
         originalTime -= TimeSpan.FromHours(1);
     }
     return(SolarTime.GetSolarTime(originalTime,
                                   this.longitudeDegrees,
                                   this.timeZone.BaseUtcOffset));
 }
        public void LocalTimeCalculationOnFebruary3()
        {
            var result = SolarTime.GetSolarTime(localTime, longitude, timeDifferenceToGMT);

            Assert.AreEqual(localTime.Year, result.Year);
            Assert.AreEqual(localTime.Month, result.Month);
            Assert.AreEqual(localTime.Day, result.Day);
            Assert.AreEqual(10, result.Hour);
            var remainderMinutesAndSeconds = result.Minute + result.Second / 60.0;

            Assert.AreEqual(19, remainderMinutesAndSeconds, 0.5);
        }
Beispiel #6
0
        public DateTime SolarTimeToOriginalTime(DateTime solarTime)
        {
            var localTime = SolarTime.GetLocalTime(solarTime,
                                                   this.longitudeDegrees,
                                                   this.timeZone.BaseUtcOffset);

            if (timeZone.IsDaylightSavingTime(localTime))
            {
                localTime += TimeSpan.FromHours(1);
            }
            return(localTime);
        }
Beispiel #7
0
        public void CalendricalDate()
        {
            // generated from http://aa.usno.navy.mil/data/docs/RS_OneYear.php for KUKUIHAELE, HAWAII
            Coordinates coordinates = new Coordinates(
                /* latitude */ 20 + 7.0 / 60.0, /* longitude */ -155.0 - 34.0 / 60.0);
            SolarTime day1solar = new SolarTime(TestUtils.MakeDate(2015, 4, /* day */ 2), coordinates);
            SolarTime day2solar = new SolarTime(TestUtils.MakeDate(2015, 4, 3), coordinates);

            double day1 = day1solar.Sunrise;
            double day2 = day2solar.Sunrise;

            Assert.IsTrue(TimeString(day1) == "16:15");
            Assert.IsTrue(TimeString(day2) == "16:14");
        }
Beispiel #8
0
        public void RightAscensionEdgeCase()
        {
            SolarTime   previousTime = null;
            Coordinates coordinates  = new Coordinates(35 + 47.0 / 60.0, -78 - 39.0 / 60.0);

            for (int i = 0; i < 365; i++)
            {
                SolarTime time = new SolarTime(
                    TestUtils.MakeDateWithOffset(2016, 1, 1, i), coordinates);
                if (i > 0)
                {
                    // transit from one day to another should not differ more than one minute
                    Assert.IsTrue(Math.Abs(time.Transit - previousTime.Transit) < (1.0 / 60.0));

                    // sunrise and sunset from one day to another should not differ more than two minutes
                    Assert.IsTrue(Math.Abs(time.Sunrise - previousTime.Sunrise) < (2.0 / 60.0));
                    Assert.IsTrue(Math.Abs(time.Sunset - previousTime.Sunset) < (2.0 / 60.0));
                }
                previousTime = time;
            }
        }
Beispiel #9
0
        public void SolarTime()
        {
            /*
             * Comparison values generated from
             * http://aa.usno.navy.mil/rstt/onedaytable?form=1&ID=AA&year=2015&month=7&day=12&state=NC&place=raleigh
             */

            Coordinates coordinates = new Coordinates(35 + 47.0 / 60.0, -78 - 39.0 / 60.0);
            SolarTime   solar       = new SolarTime(TestUtils.MakeDate(2015, 7, 12), coordinates);

            double transit       = solar.Transit;
            double sunrise       = solar.Sunrise;
            double sunset        = solar.Sunset;
            double twilightStart = solar.HourAngle(-6, /* afterTransit */ false);
            double twilightEnd   = solar.HourAngle(-6, /* afterTransit */ true);
            double invalid       = solar.HourAngle(-36, /* afterTransit */ true);

            Assert.IsTrue(TimeString(twilightStart) == "9:38");
            Assert.IsTrue(TimeString(sunrise) == "10:08");
            Assert.IsTrue(TimeString(transit) == "17:20");
            Assert.IsTrue(TimeString(sunset) == "24:32");
            Assert.IsTrue(TimeString(twilightEnd) == "25:02");
            Assert.IsTrue(TimeString(invalid) == "");
        }
 public DateTime SolarTimeToOriginalTime(DateTime solarTime)
 => SolarTime.GetLocalTime(solarTime,
                           this.longitudeDegrees,
                           this.timeZone.BaseUtcOffset);
 public DateTime OriginalTimeToSolarTime(DateTime originalTime)
 => SolarTime.GetSolarTime(originalTime,
                           this.longitudeDegrees,
                           this.timeZone.BaseUtcOffset);
        public void LocalStandardMeridianForMadisonWisconsin()
        {
            var result = SolarTime.GetLocalStandardMeridian(timeDifferenceToGMT);

            Assert.AreEqual(-90, result, 0.001);
        }
Beispiel #13
0
 ///////////////////////////////////////////////////////////////////////
 public SunSpec(SolarTime type)
 {
     _type = type;
 }
        private void ProcessDeviceEvents()
        {
            try
            {
                Device coachLights = _handler.AllDevices.Values.FirstOrDefault(d => d.Name == "coachLights");

                SolarTime solarTime = new SolarTime();
                TimeZoneInfo timezone = TimeZoneInfo.Local;
                double sunrise = solarTime.CalculateSunriseOrSunset(true, _latitude, _longitude, timezone.BaseUtcOffset.Hours, timezone.IsDaylightSavingTime(DateTime.Now));

                int sunriseHour = (int)Math.Floor(sunrise / 60);
                int sunriseMinute = (int)Math.Floor(sunrise - (60 * sunriseHour));
                int sunriseSeconds = (int)(60.0 * ((double)sunrise - Math.Floor(sunrise)));

                double sunset = solarTime.CalculateSunriseOrSunset(false, _latitude, _longitude, timezone.BaseUtcOffset.Hours, timezone.IsDaylightSavingTime(DateTime.Now));
                int sunsetHour = (int)Math.Floor(sunset / 60);
                int sunsetMinute = (int)Math.Floor(sunset - (60 * sunsetHour));
                int sunsetSecond = (int)(60.0 * ((double)sunset - Math.Floor(sunset)));

                TimeSpan sunriseTimeSpan = new TimeSpan(sunriseHour, sunriseMinute, sunriseSeconds);
                TimeSpan sunsetTimeSpan = new TimeSpan(sunsetHour, sunsetMinute, sunsetSecond);
                bool processSunset = !(_lastSunset.HasValue && _lastSunset.Value.AddHours(20) > DateTime.Now);
                bool processSunrise = !(_lastSunrise.HasValue && _lastSunrise.Value.AddHours(20) > DateTime.Now);

                if (processSunset && (DateTime.Now.TimeOfDay >= sunsetTimeSpan || DateTime.Now.TimeOfDay < sunriseTimeSpan))
                {
                    _lastSunset = DateTime.Now;

                    foreach (string key in _handler.AllDevices.Keys)
                    {
                        Device dev = _handler.AllDevices[key];

                        if (dev.IsOnAtSunset)
                        {
                            _handler.SendStandardCommand(dev.Address, Constants.STD_COMMAND_FAST_ON, 0x00, 0x0F);
                            _handler.ProcessSendingRelatedEvents(Constants.STD_COMMAND_FAST_ON, dev);
                            dev.Status = 1;
                            dev.LastOn = DateTime.Now;
                            SlapsteonEventLog.AddLogEntry(new SlapsteonEventLogEntry(dev.Name,
                                string.Format("Turned on at sunset")));
                            Thread.Sleep(500);
                        }
                    }
                }

                if (processSunrise && (DateTime.Now.TimeOfDay >= sunriseTimeSpan && DateTime.Now.TimeOfDay < sunsetTimeSpan))
                {
                    _lastSunrise = DateTime.Now;
                    foreach (string key in _handler.AllDevices.Keys)
                    {
                        Device dev = _handler.AllDevices[key];

                        if (dev.IsOffAtSunrise)
                        {
                            _handler.SendStandardCommand(dev.Address, Constants.STD_COMMAND_FAST_OFF, 0x00, 0x0F);
                            _handler.ProcessSendingRelatedEvents(Constants.STD_COMMAND_FAST_OFF, dev);
                            dev.Status = 0;
                            dev.LastOff = DateTime.Now;
                            SlapsteonEventLog.AddLogEntry(new SlapsteonEventLogEntry(dev.Name,
                                string.Format("Turned off at sunrise")));
                            Thread.Sleep(500);
                        }
                    }
                }

                // find all devices that are supporting random

                List<Device> randomDevices = _handler.AllDevices.Values.Where(d => d.IsRandomOn == true).ToList();

                foreach (Device randomDevice in randomDevices)
                {
                    // determine whether we are in the random window
                    if (!(DateTime.Now.Hour >= (randomDevice.RandomOnStart ?? 0) && (DateTime.Now.Hour < ((randomDevice.RandomOnStart ?? 0) + (randomDevice.RandomRunDuration ?? 0) % 24))))
                    {
                        // outside the window, just make sure we have ended the random by turning off
                        if (randomDevice.Status > 0 && randomDevice.LastOnWasRandom)
                        {
                            randomDevice.LastOnWasRandom = false;
                            randomDevice.LastRandomOnTime = null;
                            randomDevice.LastRandomOnLength = null;
                            randomDevice.LastOff = DateTime.Now;
                            randomDevice.Status = 0;
                            SlapsteonEventLog.AddLogEntry(new SlapsteonEventLogEntry(randomDevice.Name, "Turning off after random schedule ended."));

                            _handler.SendStandardCommand(randomDevice.Address, Constants.STD_COMMAND_FAST_OFF, 0x00, 0x0F);
                        }

                        continue;
                    }

                    // below here we must be inside the random window for this device
                    if (randomDevice.Status > 0)
                    {
                        // did we turn it on?  if so we may need to turn it off now
                        if (randomDevice.LastOnWasRandom)
                        {
                            if (randomDevice.LastRandomOnTime.HasValue && randomDevice.LastRandomOnTime.Value.AddMinutes(randomDevice.LastRandomOnLength ?? 0) < DateTime.Now)
                            {
                                // turn off device, wipe out randomization settings
                                randomDevice.LastOnWasRandom = false;
                                randomDevice.LastRandomOnTime = null;
                                randomDevice.LastRandomOnLength = null;
                                randomDevice.LastOff = DateTime.Now;
                                randomDevice.Status = 0;
                                SlapsteonEventLog.AddLogEntry(new SlapsteonEventLogEntry(randomDevice.Name, "Turning off from random schedule."));
                                _handler.SendStandardCommand(randomDevice.Address, Constants.STD_COMMAND_FAST_OFF, 0x00, 0x0F);
                            }
                        }
                        else // do nothing, we did not turn the device on
                        {
                            continue;
                        }
                    }
                    else
                    {
                        // device is off, determine whether to turn it on
                        if (_random.Next(1, 100) <= (randomDevice.RandomOnChance ?? 0))
                        {
                            int randomOnDuration = _random.Next(randomDevice.RandomDurationMin ?? 5, randomDevice.RandomDurationMax ?? 15);
                            randomDevice.LastRandomOnLength = randomOnDuration;
                            randomDevice.LastRandomOnTime = DateTime.Now;
                            randomDevice.LastOnWasRandom = true;
                            randomDevice.LastOn = DateTime.Now;
                            randomDevice.Status = 100;

                            SlapsteonEventLog.AddLogEntry(new SlapsteonEventLogEntry(randomDevice.Name, string.Format("Turned on random for {0} minutes.", randomOnDuration)));
                            _handler.SendStandardCommand(randomDevice.Address, Constants.STD_COMMAND_FAST_ON, 0xFF, 0x0F);
                        }
                    }
                }

                //Device frontDoorHigh = _handler.AllDevices.Values.FirstOrDefault(d => d.Name == "frontDoorHigh");
                //if (null != frontDoorHigh)
                //{
                //    if (frontDoorHigh.Status == 0)
                //    {
                //        if (DateTime.Now.Hour >= 21 || DateTime.Now.Hour < 1)
                //        {
                //            _insteonWebService.FastOn("frontdoorHigh");
                //            frontDoorHigh.Status = 1;
                //            frontDoorHigh.LastOn = DateTime.Now;
                //            log.Info(string.Format("Turned front door high light on at {0}", DateTime.Now));
                //            Thread.Sleep(500);
                //        }
                //    }
                //    else
                //    {
                //        if (DateTime.Now.Hour >= 1 && DateTime.Now.Hour < 21)
                //        {
                //            _insteonWebService.Off("frontdoorHigh");
                //            frontDoorHigh.Status = 0;
                //            frontDoorHigh.LastOff = DateTime.Now;
                //            log.Info(string.Format("Turned front door high light off at {0}", DateTime.Now));
                //            Thread.Sleep(500);
                //        }
                //    }
                //}

                //Device backyard = _handler.AllDevices.Values.FirstOrDefault(d => d.Name == "backyard");
                //if (null != backyard)
                //{
                //    if (backyard.Status == 0)
                //    {
                //        if (DateTime.Now.Hour >= 22 || DateTime.Now.Hour < 6)
                //        {
                //            _insteonWebService.FastOn("backyard");
                //            backyard.Status = 1;
                //            backyard.LastOn = DateTime.Now;
                //            log.Info(string.Format("Turned backyard light on at {0}", DateTime.Now));
                //            Thread.Sleep(500);
                //        }
                //    }
                //    else
                //    {
                //        if (DateTime.Now.Hour >= 6 && DateTime.Now.Hour < 22)
                //        {
                //            _insteonWebService.Off("backyard");
                //            backyard.Status = 0;
                //            backyard.LastOff = DateTime.Now;
                //            log.Info(string.Format("Turned backyard light off at {0}", DateTime.Now));
                //            Thread.Sleep(500);
                //        }
                //    }
                //}

                // handle random events
                //foreach (Device randomDevice in _randomDevices)
                //{
                //    if (DateTime.Now < new DateTime(2013, 8, 9, 7, 0, 0) || DateTime.Now > new DateTime(2013, 8, 22, 6, 0, 0))
                //        continue;

                //    if (randomDevice.Status == 1 && randomDevice.NextOff < DateTime.Now)
                //    {
                //        _insteonWebService.Off(randomDevice.Name);
                //        randomDevice.Status = 0;
                //        randomDevice.LastOff = DateTime.Now;
                //        log.Info(string.Format("Turned off random device {0} at {1}", randomDevice.Name, DateTime.Now));
                //        Thread.Sleep(500);

                //        // turn off partner switch
                //        if (randomDevice.Name == "kitchenMulti")
                //        {
                //            Device kitchenMultiSolo = _handler.AllDevices["192B89"]; // kitchenMultiSolo
                //            if (null != kitchenMultiSolo)
                //            {
                //                _insteonWebService.Off(kitchenMultiSolo.Name);
                //                kitchenMultiSolo.Status = 0;
                //                kitchenMultiSolo.LastOff = DateTime.Now;
                //                Thread.Sleep(500);
                //            }
                //        }
                //    }

                //    // only turn lights on at random during the night
                //    if (DateTime.Now.Hour >= 21 || DateTime.Now.Hour < 5)
                //    {
                //        if (randomDevice.Status != 1)
                //        {
                //            // 2% chance to turn something on
                //            if (_random.Next(1, 100) > 98)
                //            {
                //                int randomDuration = _random.Next(7, 36);
                //                log.Info(string.Format("Decided to turn on device {0} for {1} minutes.", randomDevice.Name, randomDuration));

                //                randomDevice.NextOff = DateTime.Now.AddMinutes(randomDuration);

                //                _insteonWebService.FastOn(randomDevice.Name);
                //                randomDevice.Status = 1;
                //                randomDevice.LastOn = DateTime.Now;
                //                Thread.Sleep(500);

                //                if ("kitchenMulti" == randomDevice.Name)
                //                {
                //                    Device kitchenMultiSolo = _handler.AllDevices["192B89"]; // kitchen multi solo
                //                    if (null != kitchenMultiSolo)
                //                    {
                //                        _insteonWebService.FastOn(kitchenMultiSolo.Name);
                //                        kitchenMultiSolo.Status = 1;
                //                        kitchenMultiSolo.LastOn = DateTime.Now;
                //                        Thread.Sleep(500);
                //                    }
                //                }
                //            }
                //        }
                //    }
                //}
            }
            catch (Exception ex)
            {
                log.Error("Error during device event loop");
                log.Error(ex.Message);
                log.Error(ex.StackTrace);
            }
        }
Beispiel #15
0
        private PrayerTimes(Coordinates coordinates, DateTime date, CalculationParameters parameters)
        {
            DateTime?tempFajr    = null;
            DateTime?tempSunrise = null;
            DateTime?tempDhuhr   = null;
            DateTime?tempAsr     = null;
            DateTime?tempMaghrib = null;
            DateTime?tempIsha    = null;

            //DateTime calendar = date.ToUniversalTime();
            int year      = date.Year;
            int dayOfYear = date.DayOfYear;

            SolarTime solarTime = new SolarTime(date, coordinates);

            TimeComponents timeComponents = TimeComponents.FromDouble(solarTime.Transit);
            DateTime?      transit        = timeComponents?.DateComponents(date);

            timeComponents = TimeComponents.FromDouble(solarTime.Sunrise);
            DateTime?sunriseComponents = timeComponents?.DateComponents(date);

            timeComponents = TimeComponents.FromDouble(solarTime.Sunset);
            DateTime?sunsetComponents = timeComponents?.DateComponents(date);

            bool error = transit == null || sunriseComponents == null || sunsetComponents == null;

            if (!error)
            {
                tempDhuhr   = transit;
                tempSunrise = sunriseComponents;
                tempMaghrib = sunsetComponents;

                timeComponents = TimeComponents.FromDouble(
                    solarTime.Afternoon(parameters.Madhab.GetShadowLength()));

                if (timeComponents != null)
                {
                    tempAsr = timeComponents.DateComponents(date);
                }

                // get night length
                DateTime tomorrowSunrise = sunriseComponents.Value.AddDays(1);
                double   night           = tomorrowSunrise.GetTime() - sunsetComponents.Value.GetTime();

                timeComponents = TimeComponents
                                 .FromDouble(solarTime.HourAngle(-parameters.FajrAngle, false));

                if (timeComponents != null)
                {
                    tempFajr = timeComponents.DateComponents(date);
                }

                if (parameters.Method == CalculationMethod.MOON_SIGHTING_COMMITTEE &&
                    coordinates.Latitude >= 55)
                {
                    tempFajr = sunriseComponents.Value.AddSeconds(-1 * (int)(night / 7000));
                }

                NightPortions nightPortions = parameters.NightPortions();

                DateTime safeFajr;
                if (parameters.Method == CalculationMethod.MOON_SIGHTING_COMMITTEE)
                {
                    safeFajr = SeasonAdjustedMorningTwilight(coordinates.Latitude, dayOfYear, year, sunriseComponents.Value);
                }
                else
                {
                    double portion       = nightPortions.Fajr;
                    long   nightFraction = (long)(portion * night / 1000);
                    safeFajr = sunriseComponents.Value.AddSeconds(-1 * (int)nightFraction);
                }

                if (tempFajr == null || tempFajr.Value.Before(safeFajr))
                {
                    tempFajr = safeFajr;
                }

                // Isha calculation with check against safe value
                if (parameters.IshaInterval > 0)
                {
                    tempIsha = tempMaghrib.Value.AddSeconds(parameters.IshaInterval * 60);
                }
                else
                {
                    timeComponents = TimeComponents.FromDouble(
                        solarTime.HourAngle(-parameters.IshaAngle, true));

                    if (timeComponents != null)
                    {
                        tempIsha = timeComponents.DateComponents(date);
                    }

                    if (parameters.Method == CalculationMethod.MOON_SIGHTING_COMMITTEE &&
                        coordinates.Latitude >= 55)
                    {
                        long nightFraction = (long)night / 7000;
                        tempIsha = sunsetComponents.Value.AddSeconds(nightFraction);
                    }

                    DateTime safeIsha;
                    if (parameters.Method == CalculationMethod.MOON_SIGHTING_COMMITTEE)
                    {
                        safeIsha = PrayerTimes.SeasonAdjustedEveningTwilight(
                            coordinates.Latitude, dayOfYear, year, sunsetComponents.Value);
                    }
                    else
                    {
                        double portion       = nightPortions.Isha;
                        long   nightFraction = (long)(portion * night / 1000);
                        safeIsha = sunsetComponents.Value.AddSeconds(nightFraction);
                    }

                    if (tempIsha == null || (tempIsha.Value.After(safeIsha)))
                    {
                        tempIsha = safeIsha;
                    }
                }
            }

            if (error || tempAsr == null)
            {
                // if we don't have all prayer times then initialization failed
                this.Fajr    = DateTime.MinValue;
                this.Sunrise = DateTime.MinValue;
                this.Dhuhr   = DateTime.MinValue;
                this.Asr     = DateTime.MinValue;
                this.Maghrib = DateTime.MinValue;
                this.Isha    = DateTime.MinValue;
            }
            else
            {
                // Assign final times to public struct members with all offsets
                this.Fajr    = CalendarUtil.RoundedMinute(tempFajr.Value.AddMinutes(parameters.Adjustments.Fajr + parameters.MethodAdjustments.Fajr));
                this.Sunrise = CalendarUtil.RoundedMinute(tempSunrise.Value.AddMinutes(parameters.Adjustments.Sunrise + parameters.MethodAdjustments.Sunrise));
                this.Dhuhr   = CalendarUtil.RoundedMinute(tempDhuhr.Value.AddMinutes(parameters.Adjustments.Dhuhr + parameters.MethodAdjustments.Dhuhr));
                this.Asr     = CalendarUtil.RoundedMinute(tempAsr.Value.AddMinutes(parameters.Adjustments.Asr + parameters.MethodAdjustments.Asr));
                this.Maghrib = CalendarUtil.RoundedMinute(tempMaghrib.Value.AddMinutes(parameters.Adjustments.Maghrib + parameters.MethodAdjustments.Maghrib));
                this.Isha    = CalendarUtil.RoundedMinute(tempIsha.Value.AddMinutes(parameters.Adjustments.Isha + parameters.MethodAdjustments.Isha));
            }
        }