private void ViewNextSunriseSunset_Load(object sender, EventArgs e) { DahuaSunriseSunsetConfig cfg = new DahuaSunriseSunsetConfig(); cfg.Load(); DateTime rise, set; TimeSpan utcOffset = TimeZone.CurrentTimeZone.GetUtcOffset(DateTime.Now); bool timeZoneAndLongitudeAreCompatible; SunHelper.Calc(cfg.latitude, cfg.longitude, out rise, out set, out timeZoneAndLongitudeAreCompatible, cfg.sunriseOffsetHours, cfg.sunsetOffsetHours); label1.Text = "Lat " + cfg.latitude + Environment.NewLine + "Lon " + cfg.longitude + Environment.NewLine + "UTC Offset: " + utcOffset.TotalSeconds + " seconds (" + utcOffset.TotalHours + " hours)" + Environment.NewLine + Environment.NewLine + (timeZoneAndLongitudeAreCompatible ? "" : "Your machine's time zone needs to be on the same side " + Environment.NewLine + "of the prime meridian as the longitude you have entered." + Environment.NewLine + Environment.NewLine) + (rise > set ? ("Sunset at " + set + OffsetStr(cfg.sunsetOffsetHours) + Environment.NewLine + "Sunrise at " + rise + OffsetStr(cfg.sunriseOffsetHours)) : ("Sunrise at " + rise + OffsetStr(cfg.sunriseOffsetHours) + Environment.NewLine + "Sunset at " + set + OffsetStr(cfg.sunsetOffsetHours))); }
private static void scheduler() { try { while (true) { try { // Calculate the next SunEvent SunEvent nextEvent = null; DahuaSunriseSunsetConfig cfg = new DahuaSunriseSunsetConfig(); cfg.Load(); DateTime rise, set; bool timeZoneAndLongitudeAreCompatible; SunHelper.Calc(cfg.latitude, cfg.longitude, out rise, out set, out timeZoneAndLongitudeAreCompatible, cfg.sunriseOffsetHours, cfg.sunsetOffsetHours); if (!timeZoneAndLongitudeAreCompatible) { Logger.Debug("Pausing scheduler for 1 day due to incompatible time zone and longitude. Please fix the problem and restart the service."); Thread.Sleep(TimeSpan.FromDays(1)); continue; } if (rise < set) { nextEvent = new SunEvent(rise, true); } else if (set < rise) { nextEvent = new SunEvent(set, false); } else { nextEvent = new SunEvent(rise, false); // Rise and set are at the same time ... lets just call it a sunset. } // Now we know when the next event is and what type it is, so we know what profile the cameras should be now. if (nextEvent.rise) { // Next event is a sunrise, which means it is currently Night. TriggerSunsetActions(nextEvent.time); } else { // Next event is a sunset, which means it is currently Day. TriggerSunriseActions(nextEvent.time); } while (DateTime.Now <= nextEvent.time) { Thread.Sleep(1000); } } catch (ThreadAbortException) { throw; } catch (Exception ex) { Logger.Debug(ex); Thread.Sleep(1000); } } } catch (ThreadAbortException) { } catch (Exception ex) { Logger.Debug(ex); } }