moel@247: /*
moel@247:   
moel@247:   Version: MPL 1.1/GPL 2.0/LGPL 2.1
moel@247: 
moel@247:   The contents of this file are subject to the Mozilla Public License Version
moel@247:   1.1 (the "License"); you may not use this file except in compliance with
moel@247:   the License. You may obtain a copy of the License at
moel@247:  
moel@247:   http://www.mozilla.org/MPL/
moel@247: 
moel@247:   Software distributed under the License is distributed on an "AS IS" basis,
moel@247:   WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
moel@247:   for the specific language governing rights and limitations under the License.
moel@247: 
moel@247:   The Original Code is the Open Hardware Monitor code.
moel@247: 
moel@247:   The Initial Developer of the Original Code is 
moel@247:   Michael Möller <m.moeller@gmx.ch>.
moel@247:   Portions created by the Initial Developer are Copyright (C) 2010
moel@247:   the Initial Developer. All Rights Reserved.
moel@247: 
moel@247:   Contributor(s):
moel@247: 
moel@247:   Alternatively, the contents of this file may be used under the terms of
moel@247:   either the GNU General Public License Version 2 or later (the "GPL"), or
moel@247:   the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
moel@247:   in which case the provisions of the GPL or the LGPL are applicable instead
moel@247:   of those above. If you wish to allow use of your version of this file only
moel@247:   under the terms of either the GPL or the LGPL, and not to allow others to
moel@247:   use your version of this file under the terms of the MPL, indicate your
moel@247:   decision by deleting the provisions above and replace them with the notice
moel@247:   and other provisions required by the GPL or the LGPL. If you do not delete
moel@247:   the provisions above, a recipient may use your version of this file under
moel@247:   the terms of any one of the MPL, the GPL or the LGPL.
moel@247:  
moel@247: */
moel@247: 
moel@247: using System;
moel@247: using System.Globalization;
moel@247: 
moel@247: namespace OpenHardwareMonitor.Hardware {
moel@247: 
moel@247:   internal delegate void ControlEventHandler(Control control);
moel@247: 
moel@247:   internal class Control : IControl {
moel@247: 
moel@247:     private readonly Identifier identifier;
moel@247:     private readonly ISettings settings;
moel@247:     private ControlMode mode;
moel@247:     private float softwareValue;
moel@247:     private float minSoftwareValue;
moel@247:     private float maxSoftwareValue;
moel@247: 
moel@247:     public Control(ISensor sensor, ISettings settings, float minSoftwareValue,
moel@247:       float maxSoftwareValue) 
moel@247:     {
moel@247:       this.identifier = new Identifier(sensor.Identifier, "control");
moel@247:       this.settings = settings;
moel@247:       this.minSoftwareValue = minSoftwareValue;
moel@247:       this.maxSoftwareValue = maxSoftwareValue;
moel@247: 
moel@247:       if (!float.TryParse(settings.GetValue(
moel@247:           new Identifier(identifier, "value").ToString(), "0"),
moel@247:         NumberStyles.Float, CultureInfo.InvariantCulture,
moel@247:         out this.softwareValue)) 
moel@247:       {
moel@247:         this.softwareValue = 0;
moel@247:       }
moel@247:       int mode;
moel@247:       if (!int.TryParse(settings.GetValue(
moel@247:           new Identifier(identifier, "mode").ToString(),
moel@247:           ((int)ControlMode.Default).ToString(CultureInfo.InvariantCulture)),
moel@247:         NumberStyles.Integer, CultureInfo.InvariantCulture,
moel@247:         out mode)) 
moel@247:       {
moel@247:         this.mode = ControlMode.Default;
moel@247:       } else {
moel@247:         this.mode = (ControlMode)mode;
moel@247:       }
moel@247:     }
moel@247: 
moel@247:     public Identifier Identifier {
moel@247:       get {
moel@247:         return identifier;
moel@247:       }
moel@247:     }
moel@247: 
moel@247:     public ControlMode ControlMode {
moel@247:       get {
moel@247:         return mode;
moel@247:       }
moel@247:       private set {
moel@247:         if (mode != value) {
moel@247:           mode = value;
moel@247:           if (ControlModeChanged != null)
moel@247:             ControlModeChanged(this);
moel@247:           this.settings.SetValue(new Identifier(identifier, "mode").ToString(),
moel@247:             ((int)mode).ToString(CultureInfo.InvariantCulture));
moel@247:         }
moel@247:       }
moel@247:     }
moel@247: 
moel@247:     public float SoftwareValue {
moel@247:       get {
moel@247:         return softwareValue;
moel@247:       }
moel@247:       private set {
moel@247:         if (softwareValue != value) {
moel@247:           softwareValue = value;
moel@247:           if (SoftwareControlValueChanged != null)
moel@247:             SoftwareControlValueChanged(this);
moel@247:           this.settings.SetValue(new Identifier(identifier,
moel@247:             "value").ToString(),
moel@247:             value.ToString(CultureInfo.InvariantCulture));
moel@247:         }
moel@247:       }
moel@247:     }
moel@247: 
moel@247:     public void SetDefault() {
moel@247:       ControlMode = ControlMode.Default;
moel@247:     }
moel@247: 
moel@247:     public float MinSoftwareValue {
moel@247:       get {
moel@247:         return minSoftwareValue;
moel@247:       }
moel@247:     }
moel@247: 
moel@247:     public float MaxSoftwareValue {
moel@247:       get {
moel@247:         return maxSoftwareValue;
moel@247:       }
moel@247:     }
moel@247: 
moel@247:     public void SetSoftware(float value) {
moel@247:       ControlMode = ControlMode.Software;
moel@247:       SoftwareValue = value;
moel@247:     }
moel@247: 
moel@247:     internal event ControlEventHandler ControlModeChanged;
moel@247:     internal event ControlEventHandler SoftwareControlValueChanged;
moel@247:   }
moel@247: }