public void StartMashCycle()
 {
     _currentStep = null;
     _stepStartTime = DateTime.MinValue;
     _stepsComplete = false;
     _reachedStepTemperature = false;
 }
        public static bool UpdateMashProfile(RequestReceivedEventArgs e, JsonArray h)
        {
            try
            {
                string[] steps = null;
                string[] stepData = null;
                float tempValue = 0.0F;
                MashStep newMashStep = null;

                PinManagement.mashSteps = new MashSteps();
                PinManagement.mashSteps.Steps = new ArrayList();

                // mashProfile=0:122:15,1:135:15,2:148:60,3:170:15,4:225:90,

                if (e.Request.GetArguments.Contains("mashProfile"))
                {
                    string temp = e.Request.GetArguments["mashProfile"].ToString();
                    temp = temp.Replace("%3A", ":");
                    temp = temp.Replace("%2C", ",");

                    steps = temp.Split(',');

                    if (steps != null)
                    {
                        foreach (string step in steps)
                        {
                            stepData = step.Split(':');
                            if ((stepData != null) && (stepData.Length > 2))
                            {
                                newMashStep = new MashStep();
                                Settings.TryParseFloat(stepData[1], out tempValue);
                                newMashStep.StepNumber = Convert.ToInt32(stepData[0]); ;
                                newMashStep.Temperature = tempValue;
                                newMashStep.Time = Convert.ToInt32(stepData[2]);
                                PinManagement.mashSteps.Steps.Add(newMashStep);
                            }
                        }

                        PinManagement.mashSteps.CurrentStep = null;
                    }
                }

                // send back an ok response
                h.Add("OK");
            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.WriteLine(ex.Message);
                h.Add("ERROR");
                h.Add(ex.Message);
            }

            return true;
        }
        public MashStep First()
        {
            _index = 0;
            MashStep result = null;

            if ((_steps != null) && (_steps.Count > 0))
            {
                result = (MashStep)_steps[_index];
                _currentStep = result;
            }
            else
            {
                _currentStep = null; ;
            }

            return result;
        }
        protected override void Run()
        {
            while (true)
            {
                try
                {
                    if (PinManagement.heaterEngaged)
                    {
                        if ((PinManagement.mashSteps != null) && (PinManagement.mashSteps.Steps.Count > 0))
                        {
                            _currentStep = PinManagement.mashSteps.CurrentStep;

                            if ((_currentStep == null) && (!_stepsComplete))
                            {
                                _currentStep = PinManagement.mashSteps.First();
                                PinManagement.setTemperature = _currentStep.Temperature;
                                _stepStartTime = DateTime.Now;
                            }

                            DateTime _currentTime = DateTime.Now;
                            // calculate the difference between the set temperature and current temperature
                            float temperatureDifference = (float)System.Math.Abs(PinManagement.setTemperature - PinManagement.currentTemperatureSensor);
                            // are we at the step temperature
                            if (temperatureDifference < SystemSettings.TemperatureHeaterOffset)
                            {
                                if (_reachedStepTemperature)
                                {
                                    // check how long at the step temperature
                                    TimeSpan timeInterval = _currentTime.Subtract(_stepStartTime);

                                    _outputHelper.DisplayText("Step: " + _currentStep.StepNumber.ToString() + "Temp: " + _currentStep.Temperature.ToString("f2") + "|Time: " + timeInterval.Minutes.ToString());

                                    if (timeInterval.Minutes >= _currentStep.Time)
                                    {
                                        // pulse pezio for five seconds to indicate profile step complete
                                        PinManagement.buzzerSolidPort.Write(true);
                                        Thread.Sleep(5000);
                                        PinManagement.buzzerSolidPort.Write(false);
                                        // get the next step
                                        _currentStep = PinManagement.mashSteps.Next();
                                        // set the temp
                                        PinManagement.setTemperature = _currentStep.Temperature;
                                        // set the start time
                                        _stepStartTime = DateTime.Now;
                                        // update the flag
                                        _reachedStepTemperature = false;
                                        // no more steps turn off the heater
                                        if (_currentStep == null)
                                        {
                                            PinManagement.setTemperature = 0.0F;
                                            PinManagement.heaterEngaged = false;
                                            _stepsComplete = true;
                                        }
                                    }
                                }
                                else
                                {
                                    // we have reached the step temperature
                                    // update the start time
                                    _stepStartTime = DateTime.Now;
                                    PinManagement.currentMashStepStartTime = _stepStartTime;
                                    // update the flag
                                    _reachedStepTemperature = true;
                                }
                            }
                            else
                            {
                                if (!_reachedStepTemperature)
                                {
                                    // reset the minutes remaining time
                                    PinManagement.currentMashStepStartTime = _currentTime;
                                }
                                else
                                {
                                    // check how long at the step temperature
                                    TimeSpan timeInterval = _currentTime.Subtract(_stepStartTime);
                                    _outputHelper.DisplayText("Step: " + _currentStep.StepNumber.ToString() + "Temp: " + _currentStep.Temperature.ToString("f2") + "|Time: " + timeInterval.Minutes.ToString());
                                }
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    System.Diagnostics.Debug.WriteLine(ex.Message);
                }

                // wait till next reading cycle
                Thread.Sleep(1000);
            }
        }
        public MashStep Next()
        {
            _index++;
            MashStep result = null;

            if ((_steps != null) && (_index < _steps.Count))
            {
                result = (MashStep)_steps[_index];
                _currentStep = result;
            }
            else
            {
                _currentStep = null; ;
            }

            return result;
        }