public static void TriggerSunsetActions(DateTime nextEventTime) { Logger.Info("TriggerSunsetActions"); lock (syncLockCameraControl) { DahuaSunriseSunsetConfig cfg = new DahuaSunriseSunsetConfig(); cfg.Load(); ParallelOptions opt = new ParallelOptions(); opt.MaxDegreeOfParallelism = NumberUtil.Clamp(cfg.DahuaCameras.Count, 1, 8); Parallel.ForEach(cfg.DahuaCameras, opt, (cam) => { try { WebClient wc = new WebClient(); wc.Credentials = cam.GetCredentials(); WebRequestRobust(nextEventTime, wc, cam.GetUrlBase() + "cgi-bin/configManager.cgi?action=setConfig&VideoInMode[0].Config[0]=" + (int)cam.sunsetProfile); HandleZoomAndFocus(nextEventTime, wc, cam, cam.nightZoom, cam.nightFocus); } catch (ThreadAbortException) { throw; } catch (Exception ex) { Logger.Debug(ex); } }); } }
public static void TriggerSunriseActions(DateTime nextEventTime) { Logger.Info("TriggerSunriseActions"); lock (syncLockCameraControl) { DahuaSunriseSunsetConfig cfg = new DahuaSunriseSunsetConfig(); cfg.Load(); ParallelOptions opt = new ParallelOptions(); opt.MaxDegreeOfParallelism = NumberUtil.Clamp(cfg.DahuaCameras.Count, 1, 8); Parallel.ForEach(cfg.DahuaCameras, opt, (cam) => { try { WebClient wc = new WebClient(); wc.Credentials = cam.GetCredentials(); int[] channelIndexes = cam.ChannelIndexes; for (int i = 0; i < channelIndexes.Length; i++) { WebRequestRobust(nextEventTime, wc, cam.GetUrlBase() + "cgi-bin/configManager.cgi?action=setConfig&VideoInMode[" + channelIndexes[i] + "].Config[0]=" + (int)cam.sunriseProfile); if (i + 1 < channelIndexes.Length) { Thread.Sleep(5000); } } HandleZoomAndFocus(nextEventTime, wc, cam, cam.dayZoom, cam.dayFocus); } catch (ThreadAbortException) { throw; } catch (Exception ex) { Logger.Debug(ex); } }); } }
private void ConfigurationForm_Load(object sender, EventArgs e) { DahuaSunriseSunsetConfig cfg = new DahuaSunriseSunsetConfig(); cfg.Load(); cfg.SaveIfNoExist(); txtLat.Text = cfg.latitude.ToString(); txtLon.Text = cfg.longitude.ToString(); txtRiseOffset.Text = cfg.sunriseOffsetHours.ToString(); txtSetOffset.Text = cfg.sunsetOffsetHours.ToString(); foreach (CameraDefinition cam in cfg.DahuaCameras) { lbCameras.Items.Add(cam); } }
private void ConfigurationForm_FormClosing(object sender, FormClosingEventArgs e) { DahuaSunriseSunsetConfig cfg = new DahuaSunriseSunsetConfig(); cfg.Load(); try { cfg.latitude = double.Parse(txtLat.Text); cfg.longitude = double.Parse(txtLon.Text); cfg.sunriseOffsetHours = double.Parse(txtRiseOffset.Text); cfg.sunsetOffsetHours = double.Parse(txtSetOffset.Text); cfg.DahuaCameras = BuildCameraList(); cfg.Save(); } catch (Exception ex) { e.Cancel = true; MessageBox.Show(ex.Message); } }
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); } }