async Task RunSingleTimeLapse(TimeSpan duration, CancellationToken token) { _log.Debug("Awaiting timelapse"); while (duration.TotalSeconds > 0) { totalMinutes = duration.TotalMinutes; tlCount = duration.TotalMinutes.ToString() + " minute(s)"; TimeLapseStatus.Raise(this, new EventArgs()); if (!cycle.runningCycle) { if (!peripheral.IsNightTime() && !growLightsOn) { peripheral.SetLight(Peripheral.GrowLight, true, true); growLightsOn = true; } else if (peripheral.IsNightTime() && growLightsOn) { peripheral.SetLight(Peripheral.GrowLight, false, false); growLightsOn = false; } } //_log.Debug("Waiting 1 Minute"); await Task.Delay(60 * 1000, token); //_log.Debug("1 minute elapsed"); duration = duration.Subtract(TimeSpan.FromMinutes(1)); } }
public void Start() { growLightsOn = false; _log.Info("Timelapse Starting"); runningTimeLapse = true; TimeSpan timeLapseInterval = TimeSpan.FromMilliseconds(Properties.Settings.Default.tlInterval * Properties.Settings.Default.tlIntervalType); _log.Debug("Calculated Milliseconds: " + Properties.Settings.Default.tlInterval * Properties.Settings.Default.tlIntervalType); _log.Debug("Actual Milliseconds: " + timeLapseInterval.TotalSeconds); if (Properties.Settings.Default.StartNow) { Properties.Settings.Default.tlStartDate = DateTime.Now; } double endTime = Properties.Settings.Default.tlEndIntervalType * Properties.Settings.Default.tlEndInterval; DateTime endDate = Properties.Settings.Default.tlStartDate.AddMilliseconds(endTime); tlEnd = endDate.ToString(); tlCount = Properties.Settings.Default.tlStartDate.ToString(); TimeLapseStatus.Raise(this, new EventArgs()); HandleTimelapseCalculations(timeLapseInterval, endTime); //timeLapseCount.Text = "Not Running"; }
public void Stop() { cycle.Stop(); if (tokenSource != null) { tokenSource.Cancel(); } growLightsOn = true; runningTimeLapse = false; TimeLapseStatus.Raise(this, new EventArgs()); }
public async void HandleTimelapseCalculations(TimeSpan timeLapseInterval, Double endDuration) { if (((Properties.Settings.Default.StartNow || Properties.Settings.Default.tlStartDate <= DateTime.Now)) && endDuration > 0) { _log.Info("Running single timelapse cycle"); tokenSource = new CancellationTokenSource(); tempExperiment = new Experiment(); tempExperiment.LoadExperiment(); timeLapseExperiment = Experiment.LoadExperimentAndSave(Properties.Settings.Default.tlExperimentPath); ExperimentStatus.Raise(this, new EventArgs()); runningSingleCycle = true; _log.Debug("TimeLapse Single Cycle Executed at: " + DateTime.Now); cycle.Start(); try { await RunSingleTimeLapse(timeLapseInterval, tokenSource.Token); } catch (TaskCanceledException e) { _log.Error("TimeLapse Cancelled: " + e); Stop(); TimeLapseStatus.Raise(this, new EventArgs()); return; } catch (Exception e) { _log.Error("Unknown timelapse error: " + e); } HandleTimelapseCalculations(timeLapseInterval, endDuration - timeLapseInterval.TotalMilliseconds); } else if (Properties.Settings.Default.tlStartDate > DateTime.Now) { await WaitForStartNow(); HandleTimelapseCalculations(timeLapseInterval, endDuration); } else { _log.Info("TimeLapse Finished"); runningTimeLapse = false; TimeLapseStatus.Raise(this, new EventArgs()); return; } }