public void StartProgram(OvenProgram op) { if (_cts != null) { _cts.Dispose(); } _cts = new System.Threading.CancellationTokenSource(); Task.Run(() => ProgramLoop(op, _cts.Token)); }
public void Start(int temperature) { var p = new OvenProgram() { Items = new List <OvenProgramItem>() { new OvenProgramItem() { Time = new DateTime(), Temperature = temperature, Relays = _ovenSettings.OvenSettings.RelayPins.Length } }, Name = "Temperature" }; StartProgram(p); }
private async void SecondLoop(System.Threading.CancellationToken ct, int seconds) { int d = seconds * 1000; CurrentOvenProgram = new OvenProgram() { Items = new List <OvenProgramItem>() { new OvenProgramItem() { Time = new DateTime(), Temperature = 0, Relays = _ovenSettings.OvenSettings.RelayPins.Length } }, Name = "Loop" }; _dtStart = DateTime.Now; IsWorked = true; while (ct.IsCancellationRequested == false) { foreach (var item in _ovenSettings.OvenSettings.RelayPins) { _gpioController.Write(item, PinValue.Low); } await Task.Delay(d); foreach (var item in _ovenSettings.OvenSettings.RelayPins) { _gpioController.Write(item, PinValue.High); } await Task.Delay(d); } IsWorked = false; }
private async void ProgramLoop(OvenProgram op, System.Threading.CancellationToken ct) { _dtStart = DateTime.Now; var stopwatch = new System.Diagnostics.Stopwatch(); CurrentOvenProgram = op; IsWorked = true; foreach (var item in op.Items) { if (ct.IsCancellationRequested || item.Temperature == -1) { break; } stopwatch.Reset(); bool startStopwatch = true; op.CurrentItemId = item.Id; while (stopwatch.Elapsed.TotalSeconds <= item.Time.TimeOfDay.TotalSeconds || item.Time.TimeOfDay == TimeSpan.Zero) { if (ct.IsCancellationRequested) { break; } if (CurrentTemperature < item.Temperature) { //Phase 1 if (CurrentTemperature < item.Temperature - _ovenSettings.OvenSettings.HeatPhase2) { for (int i = 0; i < item.Relays; i++) { _gpioController.Write(_ovenSettings.OvenSettings.RelayPins[i], PinValue.Low); } while (CurrentTemperature < item.Temperature) { if (ct.IsCancellationRequested || CurrentTemperature > item.Temperature - _ovenSettings.OvenSettings.HeatPhase2) { break; } await Task.Delay(_tempLoopDelay); } for (int i = 0; i < item.Relays; i++) { _gpioController.Write(_ovenSettings.OvenSettings.RelayPins[i], PinValue.High); } } else //Phase 2 { while (CurrentTemperature < item.Temperature) { if (ct.IsCancellationRequested) { break; } for (int i = 0; i < item.Relays; i++) { _gpioController.Write(_ovenSettings.OvenSettings.RelayPins[i], PinValue.Low); } await Task.Delay(_ovenSettings.OvenSettings.HeatDuration * 1000); for (int i = 0; i < item.Relays; i++) { _gpioController.Write(_ovenSettings.OvenSettings.RelayPins[i], PinValue.High); } if (ct.IsCancellationRequested) { break; } await Task.Delay(_ovenSettings.OvenSettings.HeatWait * 1000); } } } else { if (startStopwatch) { startStopwatch = false; stopwatch.Start(); } await Task.Delay(_tempLoopDelay); } } } stopwatch.Stop(); IsWorked = false; }