public void Pause()
 {
     _trapState = TrapStateEnum.Closed;
     _fanState = FanStateEnum.Stopped;
     SetOutputState();
 }
        public void OnUpdateVentilationData(FanStateEnum fanState, TrapStateEnum trapState, VentilationState ventilationState,
                                            String ventilationDuration, int ventilationIntervalTarget, int ventilationDurationTarget,
                                            bool ventilationStandby)
        {
            try
              {
            String ventilationTxt = "Ventilation: ??";

            if (ventilationState == VentilationState.Stopped)
            {
                ventilationTxt = "Ventilation: OFF";

                if (ventilationDuration == "00:00:00")
                {
                    ventilationOnOff.Foreground = Brushes.Black;
                }
                else
                {
                    ventilationOnOff.Foreground = Brushes.OrangeRed;
                }
            }
            else if (ventilationState == VentilationState.Started)
            {
                if (ventilationStandby)
                {
                    ventilationTxt = "Ventilation: OFF";
                    ventilationOnOff.Foreground = Brushes.OrangeRed;
                }
                else
                {
                    ventilationTxt = "Ventilation: ON";
                    ventilationOnOff.Foreground = Brushes.Green;
                }
            }

            ventilationOnOff.Content = ventilationTxt;

            if (ventilationIntervalTarget != double.MaxValue)
            {
                if (ventilationIntervalTxtBox.Text == "????")
                {
                    ventilationIntervalTxtBox.Text = ventilationIntervalTarget.ToString();
                }
            }

            if (ventilationDurationTarget != double.MaxValue)
            {
                if (ventilationDurationTxtBox.Text == "????")
                {
                    ventilationDurationTxtBox.Text = ventilationDurationTarget.ToString();
                }
            }

            ventilationOnOff.Content += " [ " + ventilationDuration + " ] ";
              }
              catch (Exception ex)
              {
            MessageBox.Show(ex.ToString());
              }
        }
        public void ManageState()
        {
            if (_duration > TimeSpan.Zero)
            {
                _duration = _duration.Subtract(new TimeSpan(0, 0, 1));
            }

            if (ProcessControl.Instance.CO2 > 0)
            {
                if (_ventilationState == VentilationState.Stopped)
                {
                    if (ProcessControl.Instance.CO2 > (ProcessControl.Instance.TargetCO2 + CO2_TOLERANCE))
                    {
                        if (ProcessControl.Instance.TargetCO2 > 0)
                        {
                            _ventilationState = VentilationState.Started;
                            _trapState = TrapStateEnum.Opened;
                            _fanState = FanStateEnum.Running;
                        }
                    }
                }
                else if (_ventilationState == VentilationState.Started)
                {
                    if (ProcessControl.Instance.CO2 <= ProcessControl.Instance.TargetCO2)
                    {
                        _ventilationState = VentilationState.Stopped;
                        _duration = TimeSpan.Zero;
                        _toggleVentilationState = false;
                        _ventilationStandby = false;
                    }
                }
            }
            else
            {
                _ventilationState = VentilationState.Stopped;
                _duration = TimeSpan.Zero;
                _toggleVentilationState = false;
                _ventilationStandby = false;
            }

            //////////////////////////////////////////////////////////////////

            if (_ventilationState == VentilationState.Started)
            {
                if (_duration == TimeSpan.Zero)
                {
                    if (_toggleVentilationState)
                    {
                        _duration = new TimeSpan(0, 0, _intervalTargetMinutes);
                        _ventilationStandby = true;

                        _trapState = TrapStateEnum.Closed;
                        _fanState = FanStateEnum.Stopped;
                    }
                    else
                    {
                        _duration = new TimeSpan(0, 0, _durationTargetSeconds);
                        _ventilationStandby = false;
                    }

                    _toggleVentilationState = !_toggleVentilationState;
                }
            }

            //////////////////////////////////////////////////////////////////

            if (ProcessControl.Instance.RelativeHumidity > 0)
            {
                if (_trapState == TrapStateEnum.Closed)
                {
                    if (ProcessControl.Instance.RelativeHumidity >= (ProcessControl.Instance.TargetRelativeHumidity + RELATIVE_HUMIDITY_TRAP_DELTA))
                    {
                        if (ProcessControl.Instance.TargetRelativeHumidity > 0 || (_ventilationState == VentilationState.Started && _ventilationStandby == false))
                        {
                            _trapState = TrapStateEnum.Opened;
                        }
                    }
                    else if (_ventilationState == VentilationState.Started && _ventilationStandby == false)
                    {
                        _trapState = TrapStateEnum.Opened;
                    }
                }
                else if (_trapState == TrapStateEnum.Opened)
                {
                    if (_ventilationState != VentilationState.Started)
                    {
                        if ((ProcessControl.Instance.RelativeHumidity < (ProcessControl.Instance.TargetRelativeHumidity + RELATIVE_HUMIDITY_TRAP_DELTA))
                            || ProcessControl.Instance.TargetRelativeHumidity == 0)
                        {
                            _trapState = TrapStateEnum.Closed;
                        }
                    }
                }

                if (_fanState == FanStateEnum.Stopped)
                {
                    if (ProcessControl.Instance.RelativeHumidity >= (ProcessControl.Instance.TargetRelativeHumidity + RELATIVE_HUMIDITY_FAN_DELTA))
                    {
                        if (ProcessControl.Instance.TargetRelativeHumidity > 0 || (_ventilationState == VentilationState.Started && _ventilationStandby == false))
                        {
                            _fanState = FanStateEnum.Running;
                        }
                    }
                    else if (_ventilationState == VentilationState.Started && _ventilationStandby == false)
                    {
                        _fanState = FanStateEnum.Running;
                    }
                }
                else if (_fanState == FanStateEnum.Running)
                {
                    if (_ventilationState != VentilationState.Started)
                    {
                        if ((ProcessControl.Instance.RelativeHumidity < (ProcessControl.Instance.TargetRelativeHumidity + RELATIVE_HUMIDITY_TRAP_DELTA + 1))
                            || ProcessControl.Instance.TargetRelativeHumidity == 0)
                        {
                            _fanState = FanStateEnum.Stopped;
                        }
                    }
                }
            }
            else
            {
                _trapState = TrapStateEnum.Closed;
                _fanState = FanStateEnum.Stopped;
            }

            if (ProcessControl.Instance.TargetCO2 == 0)
            {
                _ventilationState = VentilationState.Stopped;
                _duration = TimeSpan.Zero;
                _toggleVentilationState = false;
                _ventilationStandby = false;
            }

            if (ProcessControl.Instance.TargetRelativeHumidity == 0 && ProcessControl.Instance.TargetCO2 == 0)
            {
                _trapState = TrapStateEnum.Closed;
                _fanState = FanStateEnum.Stopped;
            }

            if (ProcessControl.Instance.Temperature > ProcessControl.Instance.TemperatureMax)
            {
                _trapState = TrapStateEnum.Opened;
            }

            if (_overHeatProtection == false)
            {
                if (ProcessControl.Instance.Temperature > ProcessControl.Instance.TargetTemperature + 0.2)
                {
                    _trapState = TrapStateEnum.Opened;
                    _fanState = FanStateEnum.Running;
                    _overHeatProtection = true;
                }
            }
            if (_overHeatProtection)
            {
                if (ProcessControl.Instance.Temperature > ProcessControl.Instance.TargetTemperature + 0.1)
                {
                    _trapState = TrapStateEnum.Opened;
                    _fanState = FanStateEnum.Running;
                }
                else
                {
                    _overHeatProtection = false;
                }
            }

            SetOutputState();
        }
        public void OnUpdateRelativeHumidityData(double relativeHumidity, double targetRelativeHumidity, FanStateEnum fanState,
                                                 TrapStateEnum trapState, PumpStateEnum pumpState, String pumpDuration, 
                                                 int pumpIntervalTarget, int pumpDurationTarget)
        {
            try
              {
            if (relativeHumidity != double.MaxValue)
            {
                relativeHumidityValue.Content = relativeHumidity.ToString("F2") + " %";
            }

            if (targetRelativeHumidity != double.MaxValue)
            {
                TargetRelativeHumidity = targetRelativeHumidity;

                if (targetRelativeHumidityValue.Text == "???.??")
                {
                    targetRelativeHumidityValue.Text = targetRelativeHumidity.ToString("F2");
                }
            }

            if (pumpIntervalTarget != double.MaxValue)
            {
              if (pumpIntervalTxtBox.Text == "????")
              {
                pumpIntervalTxtBox.Text = pumpIntervalTarget.ToString();
              }
            }

            if (pumpDurationTarget != double.MaxValue)
            {
              if (pumpDurationTxtBox.Text == "????")
              {
                pumpDurationTxtBox.Text = pumpDurationTarget.ToString();
              }
            }

            if (trapState == TrapStateEnum.Closed)
            {
                trapOnOff.Content = "Cheminée: Fermé";
                trapOnOff.Foreground = Brushes.Black;
            }
            else if (trapState == TrapStateEnum.Opened)
            {
                trapOnOff.Content = "Cheminée: Ouverte";
                trapOnOff.Foreground = Brushes.Green;
            }

            if (fanState == FanStateEnum.Stopped)
            {
                fanOnOff.Content = "Fan: OFF";
                fanOnOff.Foreground = Brushes.Black;
            }
            else if (fanState == FanStateEnum.Running)
            {
                fanOnOff.Content = "Fan: ON";
                fanOnOff.Foreground = Brushes.Green;
            }

            if (pumpState == PumpStateEnum.Stopped)
            {
                pumpOnOff.Content = "Pompe: OFF";

                if (pumpDuration == "00:00:00")
                {
                    pumpOnOff.Foreground = Brushes.Black;
                }
                else
                {
                    pumpOnOff.Foreground = Brushes.OrangeRed;
                }
            }
            else if (pumpState == PumpStateEnum.Running)
            {
                pumpOnOff.Content = "Pompe: ON";
                pumpOnOff.Foreground = Brushes.Green;
            }

            pumpOnOff.Content += " [ " + pumpDuration + " ] ";
              }
              catch (Exception ex)
              {
            MessageBox.Show(ex.ToString());
              }
        }