Exemplo n.º 1
0
        /// <summary>
        /// Master worker method to process a Fan automation request.
        /// </summary>
        /// <param name="fan">USB Fan to automate.</param>
        /// <param name="comboBox">ComboBox with sensor serial number.</param>
        /// <param name="textBox">TextBox with user-entered celsius threshold.</param>
        /// <param name="button">Enable/Disable button.</param>
        private void ProcessButtonClick(Fan fan,
                                        ComboBox comboBox,
                                        TextBox textBox,
                                        Button button)
        {
            Debug.Assert(fan != null);
            Debug.Assert(comboBox != null);
            Debug.Assert(textBox != null);
            Debug.Assert(button != null);

            //If it's already enabled, then disable it - allow the user to make edits.
            if (button.Text == DisableText)
            {
                //Unregister the fan from automation.
                AsyncHelper.RunSync(() => { return(m_fanAutomationRef.Unregister(fan)); });

                //Automatically un-power it.
                AsyncHelper.RunSync(() => { return(fan.SetPower(false)); });

                //Now prompt the user that we unpowered the fan.
                MessageBox.Show(string.Format("USB Fan #{0} has been unpowered. To change power without automation go to Fan Manual Override dialog.",
                                              fan.Number),
                                "Fan Unpowered",
                                MessageBoxButtons.OK,
                                MessageBoxIcon.Information);
            }
            else
            {
                Debug.Assert(button.Text == EnableText);

                //Ensure they selected a temperature sensor.
                if (comboBox.SelectedItem as string != TemperatureSensor._59000002A218F928.SerialNumber &&
                    comboBox.SelectedItem as string != TemperatureSensor._F7000002A215B828.SerialNumber)
                {
                    MessageBox.Show("Please select a Temperature Sensor to register the fan against.",
                                    "Warning",
                                    MessageBoxButtons.OK);

                    return;
                }

                //Validate the Celsius Range.
                bool   isValid          = false;
                Single celsiusThreshold = Single.NaN;

                try
                {
                    celsiusThreshold = ConvertUnits.Temperature.FahrenheitToCelsius(Convert.ToSingle(textBox.Text));

                    isValid = (celsiusThreshold >= Limits.MinTemperatureCelsius) && (celsiusThreshold <= Limits.MaxTemperatureCelsius);
                }
                catch { isValid = false; }

                if (isValid == false)
                {
                    MessageBox.Show(string.Format("Temperature Threshold must be between {0}F and {1}F. Please re-enter and try again.",
                                                  ConvertUnits.Temperature.CelsiusToFahrenheit(Limits.MinTemperatureCelsius),
                                                  ConvertUnits.Temperature.CelsiusToFahrenheit(Limits.MaxTemperatureCelsius)),
                                    "Bad Temperature Threshold",
                                    MessageBoxButtons.OK,
                                    MessageBoxIcon.Error);
                }

                //Register the fan for automation.
                TemperatureSensor temperatureSensor = null;

                if (comboBox.SelectedItem as string == TemperatureSensor._59000002A218F928.SerialNumber)
                {
                    temperatureSensor = TemperatureSensor._59000002A218F928;
                }
                else
                {
                    temperatureSensor = TemperatureSensor._F7000002A215B828;
                }

                AsyncHelper.RunSync(() => { return(m_fanAutomationRef.Register(fan, temperatureSensor, celsiusThreshold)); });
            }

            //Call update
            Update();
        }